Hello,
how can I refer to a past condition NOT by period with square brackets […] BUT by last time, 2nd last, 3rd last time and so on?
Meaning and I dont care when exactly the condition was true, I just want to get values from it.
Please help forum brains
Thx & cheers
You need to use a FOR…NEXT iteration.
You simply set the number of the occurrence you want to retrieve and it will be returned in variable EventBar (0=no occurrences):
N = 3 //3rd occurrence
Avg = close CROSSES OVER average[20,0](close) //event to be searched for
EventBar = 0
Count = 0
FOR i = 0 TO BarIndex - 1
IF Avg[i] THEN
EventBar = BarIndex[i]
Count = Count + 1
IF Count = N THEN
Break
ENDIF
ENDIF
NEXT
Alternatively use an array to store the value and increase the array location by +1 each time you store a value.
Not tested.
if (your event) then
a = a + 1
$myvalue[a] = close //store closing price at time of your event
endif
//retrieve 2nd to last event
if a >=2 then
secondlast = $myvalue[a-1]
endif
//retrieve 5th to last event
if a >=5 then
secondlast = $myvalue[a-4]
endif
Vonasi‘s solution is much better because you don’t waste time to scan bars backwards, which can be very time consuming if you want retrieve, say, the 100th event!
Thanks a lot guys!:) much appreciated…