Please help: How can I refer to past conditions NOT by period

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #170312 quote
    fiboman3000
    Participant
    Junior

    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

    #170323 quote
    robertogozzi
    Moderator
    Master

    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
    #170383 quote
    Vonasi
    Moderator
    Master

    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
    robertogozzi thanked this post
    #170387 quote
    robertogozzi
    Moderator
    Master

    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!

    Vonasi thanked this post
    #170442 quote
    fiboman3000
    Participant
    Junior

    Thanks a lot guys!:) much appreciated…

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.

Please help: How can I refer to past conditions NOT by period


ProBuilder: Indicators & Custom Tools

New Reply
Author
Summary

This topic contains 4 replies,
has 3 voices, and was last updated by fiboman3000
4 years, 8 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 05/25/2021
Status: Active
Attachments: No files
Logo Logo
Loading...