The DO instruction in ProBuilder language is used within looping constructs such as FOR/NEXT and WHILE loops. Its primary purpose is to clearly delineate the start of the actions that should be executed within each iteration of the loop. This helps in organizing the code and making the loop’s structure more readable and maintainable.
The syntax for using the DO instruction is straightforward. It is placed after the loop declaration and before the actions that are to be executed in each iteration:
FOR variable = start TO end DO
// Actions to execute
NEXT
Similarly, it can be used in a WHILE loop as follows:
WHILE condition DO
// Actions to execute
WEND
Here is a simple example using the DO instruction within a FOR/NEXT loop. This example counts how many times the closing price of a stock is less than its opening price over the last 20 bars:
Value = 0
FOR i = 1 TO 20 DO
IF (Close[i] < Open[i]) THEN
Value = Value + 1
ELSE
BREAK
ENDIF
NEXT
RETURN Value
In this example, the loop iterates from 1 to 20. Within each iteration, it checks if the closing price is less than the opening price. If true, it increments the Value by 1. If not, it exits the loop using the BREAK instruction.
Understanding and using the DO instruction effectively can help in writing clearer and more efficient ProBuilder scripts, especially when dealing with repetitive tasks or conditions that need to be checked multiple times.