Understanding the intricacies of declaration modes in Pine Script is vital for effective scripting in trading strategies and technical analysis. Pine Script’s execution model is unique, and grasping how different declaration modes influence variable behavior is essential for robust script development.
On Each Bar
Default Behavior
In Pine Script, when no explicit declaration mode is specified (no var
or varip
keyword), variables are declared and initialized on each bar. This is the default behavior and is considered as “On each bar” mode. For example:
//@version=5 indicator("", "", true) BULL_COLOR = color.lime i = 1 len = input(20, "Length") float f = 10.5 closeRoundedToTick = math.round_to_mintick(close) [supertrend, direction] = ta.supertrend(3, 10) [macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9) plotColor = close > open ? color.green : color.red
Each of these variables is re-initialized at the start of each new bar.
var
Initialization on First Bar
The var
keyword changes this behavior. When used, the variable is initialized only once: on the first bar in the global scope, or the first time a local block is executed. After its initial assignment, it preserves its last value across successive bars. This is especially useful for variables that need to maintain their state over the course of the script’s execution.
For instance, to count the number of green bars on a chart:
//@version=5 indicator("Green Bars Count") var count = 0 isGreen = close >= open if isGreen then count := count + 1 plot(count)
Without var
, the count
variable would reset to zero on each bar, losing its accumulated value.
Efficient Management of Drawings
Using var
is also efficient for managing drawings. Consider extending the last bar’s close line to the right of the chart:
//@version=5 indicator("Efficient version", "", true) var closeLine = line.new(bar_index - 1, close, bar_index, close, extend = extend.right, width = 3) if barstate.islast then line.set_xy1(closeLine, bar_index - 1, close) line.set_xy2(closeLine, bar_index, close)
Here, closeLine
is only initialized on the first bar, reducing overhead compared to re-creating and deleting the line on each bar.
varip
Persisting Values in Realtime
The varip
keyword is crucial for situations where a variable must retain its value between different executions in a realtime bar, bypassing the usual rollback process. This is particularly relevant for scripts that need to track changes within a single bar in realtime.
Consider this example without varip
:
//@version=5 indicator("") int updateNo = na if barstate.isnew then updateNo := 1 else updateNo := updateNo + 1 plot(updateNo, style = plot.style_circles)
In this scenario, updateNo
resets with each execution, leading to incorrect plotting in realtime bars.
Now, with varip
:
//@version=5 indicator("") varip int updateNo = na if barstate.isnew then updateNo := 1 else updateNo := updateNo + 1 plot(updateNo, style = plot.style_circles)
updateNo
now accurately tracks the number of updates in each realtime bar.
Key Takeaways
- Default Behavior: Without
var
orvarip
, variables are re-initialized on each new bar. var
Usage: Preserves a variable’s value across bars, initializing only once.varip
for Realtime Tracking: Allows variables to retain values between executions within a single realtime bar.
Conclusion
Mastering declaration modes in Pine Script enhances the efficiency and accuracy of trading scripts. Understanding when and how to use var
and varip
can significantly impact the performance and behavior of scripts, especially in dynamic, realtime trading environments.