Built-in Variables 📊
Price Data Variables
These variables provide access to basic price information:
//@version=5
indicator("Price Variables")
// Basic price data
plot(open, "Open", color=color.blue)
plot(high, "High", color=color.green)
plot(low, "Low", color=color.red)
plot(close, "Close", color=color.purple)
// Historical values
plot(close[1], "Previous Close", color=color.gray)
Time Variables
Access various time-related information:
//@version=5
indicator("Time Variables")
// Basic time variables
t = time // Current bar time
tf = timeframe.period // Current timeframe
s = session.ismarket // Is current session market hours?
// Time components
year = year(t)
month = month(t)
weekday = dayofweek(t)
hour = hour(t)
minute = minute(t)
// Example: Only plot during market hours
plot(s ? close : na, "Market Hours Only")
Bar Variables
Variables related to bar information:
//@version=5
indicator("Bar Variables")
// Bar indexing
plot(bar_index, "Bar Index")
// Bar state
isNewBar = barstate.isnew
isConfirmed = barstate.isconfirmed
isLast = barstate.islast
// Example: Plot label on last bar
if isLast
label.new(bar_index, high, "Last Bar")
Volume and Trading Variables
//@version=5
indicator("Volume Variables", format=format.volume)
// Volume data
plot(volume, "Volume", style=plot.style_columns)
// Trading info
plot(hl2, "HL2 Average")
plot(hlc3, "HLC3 Average")
plot(ohlc4, "OHLC4 Average")
// Example: Volume weighted average price
plot(vwap, "VWAP", color=color.blue)
Symbol Information
//@version=5
indicator("Symbol Info")
// Symbol information
sym = syminfo.ticker // Current symbol
exc = syminfo.exchange // Exchange
desc = syminfo.description // Description
// Example: Display symbol info
if barstate.islast
label.new(bar_index, high,
text=sym + "\n" + exc + "\n" + desc,
style=label.style_label_down)
Strategy Variables
Available in strategy scripts:
//@version=5
strategy("Strategy Variables", overlay=true)
// Position information
isLong = strategy.position_size > 0
isShort = strategy.position_size < 0
// Entry price
entryPrice = strategy.position_avg_price
// Profit/Loss
pl = strategy.openprofit
// Example: Display position info
if barstate.islast
label.new(bar_index, high,
text="Position: " + str.tostring(strategy.position_size) +
"\nEntry: " + str.tostring(entryPrice) +
"\nP/L: " + str.tostring(pl),
style=label.style_label_down)
Series vs. Simple Types
Understanding the difference between series and simple types:
//@version=5
indicator("Series vs Simple")
// Simple (single value)
simpleValue = 100 // Constant
// Series (value for each bar)
seriesValue = close // Changes each bar
// Historical reference
prevClose = close[1] // Previous bar's close
prevClose2 = close[2] // Two bars ago
// Example: Comparing types
diff = seriesValue - simpleValue
plot(diff)
Practice Examples
1. Time-Based Trading Hours
//@version=5
indicator("Trading Hours")
// Define trading hours (e.g., 9:30 - 16:00 EST)
isTrading = hour >= 9 and hour < 16 and
(hour != 9 or minute >= 30)
// Color bars based on trading hours
barcolor(isTrading ? color.blue : color.gray)
2. Volume Profile
//@version=5
indicator("Volume Profile")
// Calculate relative volume
avgVol = ta.sma(volume, 20)
relativeVol = volume / avgVol
// Plot volume profile
plot(volume, "Volume",
style=plot.style_columns,
color = switch
relativeVol >= 2.0 => color.green
relativeVol <= 0.5 => color.red
=> color.gray)
3. Multi-Timeframe Analysis
//@version=5
indicator("MTF Analysis")
// Get data from higher timeframe
htf = request.security(syminfo.tickerid, "D", close)
// Compare current timeframe to daily
diff = close - htf
plot(diff, "Difference from Daily Close")
Pro Tips
- Use historical references wisely
- Remember that series calculations are bar-by-bar
- Check timeframe compatibility
- Consider session hours in calculations
Common Pitfalls
Watch Out For
❌ Mixing series and simple types incorrectly ❌ Accessing future bars (not possible) ❌ Ignoring timeframe limitations ❌ Not checking for valid data
Next Steps
Now that you understand Pine Script's built-in variables, let's move on to functions! 🚀