I would like to find out how to find a high or low within a defined area. To test, I used the example code below but probuilder tells me there’s a syntax error in line 14 – there are only 13 lines of code.
FOR i = 20 TO 45 DO
P1High = high
c1 = CLOSE > P1High
IF C1 THEN
BUY 1 SHARES AT MARKET
ENDIF
It would be great if someone could let me know what I’m missing or point me towards code that would allow me to do this…
A FOR/NEXT loop is always made with 3 keywords: FOR + DO + NEXT
The correct syntax should be:
hh = 0
FOR i = 20 TO 45 DO
hh = max(hh,high[i])
NEXT
c1 = CLOSE > hh
IF C1 THEN
BUY 1 SHARES AT MARKET
ENDIF
But you don’t have to do a loop to find the highest high of 20 bars ago, you could also use this instruction:
hh = highest[25](high)[20]
it will find for you, the value of the highest high 25 periods but 20 bars ago.
@Nicolas I have always used the formula you referenced to but at some point realized that for some unknown reason it doesn’t give the same result as a loop. For example:
PSH = Highest[10](high)[1] //Previous Swing High
PSL = Lowest[10](low)[1] //Previous Swing Low
Should yield the same result as:
PSH = high[1] //Previous Swing High
PSL = low[1] //Previous Swing Low
For i = 2 to 10 Do
If high[i] > PSH Then
PSH = high[i]
ElsIf low[i] < PSL Then
PSL = low[i]
EndIf
Next
And unless I am mistaken it doesn’t?
@juanj
Because you are using “elsif”, you should make 2 different conditional statement IF/ENDIF, one for highest high and one for lowest low.
Thank you all for your excellent advice … problem solved.