The ONCE statement in ProBuilder language is used to define a variable or perform a calculation only once during the entire execution of the script. This is particularly useful for initializing variables that are intended to retain a constant value throughout the script’s lifecycle or for calculations that need to be performed just once to optimize performance.
ONCE variable = expression
Consider a scenario where you want to track the lowest price of a stock over the past 10 days, and then use this value to determine if the current price is setting a new low. Here’s how you can use the ONCE statement:
ONCE myFirstLowest = lowest[10](low)
IF low < myFirstLowest OR low < recentLow THEN
recentLow = low
ELSE
recentLow = recentLow
ENDIF
RETURN recentLow
In this example, myFirstLowest is calculated only once, capturing the lowest price of the last 10 days at the time the script starts. The variable recentLow is then used to track if any new lows are set after this initial period.