ONCE

Category: Instructions

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.

Syntax:

ONCE variable = expression

Example:

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.

Additional Information:

  • The ONCE statement is executed only the first time the script is processed. Any subsequent calls or loops will use the value initially calculated or assigned.
  • This feature is particularly useful in trading algorithms where certain parameters (like historical lows or highs) need to be referenced multiple times without recalculating them each time.
  • It's important to note that the variable initialized with ONCE will not reset unless the script is completely restarted, making it ideal for use cases involving static reference values or initial setup parameters.
Logo Logo
Loading...