how do I loop back to save price and bar index between two points

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #206400 quote
    blackwing
    Participant
    New

    Could somebody please advise. I am completely stuck. I am trying to store the last high over a specific period(but not including the current bar),once a condition is met. Then save both the bar index of the bar that has the high on it and the high price. So that both may be referenced later.

    I am guessing it is a loop but I don’t know how to create a loop that starts looking for the high only from the previous bar. I think I need to store the value and the bar index in arrays but again I am not sure.  I have attached a picture to show exactly what I mean.

    Please excuse the repetition in the question and diagram I just want to be clear so as not to waste anybodies time.

     

    many thanks in advance

    #206429 quote
    robertogozzi
    Moderator
    Master

    Yes, you need to loop through the past bars:

    ONCE Periods = 20
    ONCE HHprice = 0
    ONCE HHbar   = 0
    IF MyCondition THEN
       HHprice = highest[Periods](high[1])
       FOR i = 1 TO BarIndex - 1
          IF high[i] = HHprice THEN
             HHbar = BarIndex[i]
             break
          ENDIF
       NEXT
    ENDIF
    // HHprice is the prior HIGH value
    // HHbar   is the BarIndex where that HIGH is sitting
    blackwing thanked this post
    #206432 quote
    JS
    Participant
    Senior

    Hi @blackwing

    Maybe this will help you too…

    I have chosen as an (example) condition for a “crosses over” …

    When this condition is true, I have the following results stored in an array:

    The point at which the crossing takes place, the price (high) of the crossing and the distance in bars compared to the previous crossing…

    Regards Jaap

    DefParam CalculateOnLastBars=200
    DefParam DrawOnLastBarOnly=true
    
    N=200 //Total number of bars
    xIndex=0 //Reset the index counter
    
    For i=0 to N-1 //Index i runs from 0 to N-1
    
    $Avg[i]=Average[20](Close[i])//Store signal in array $Avg
    
    If Close[i+1] < $Avg[i+1] and Close[i] > $Avg[i] then //Check "Crosses Over" condition
    $Result[xIndex]=1 //Result of condition (boolean)
    $ResultBarIndex[xIndex]=BarIndex-i //BarIndex of result
    $ResultPrice[xIndex]=High[i] //Price of result
    
    xBarDiffResult=$ResultBarIndex[xIndex]-$ResultBarIndex[xIndex+1] //Difference in bars between first result and second result
    
    //Plot
    DrawSegment(BarIndex-i,$Avg[i],BarIndex-(i+1),$Avg[i+1])coloured(255,0,0) //Plot the average signal
    DrawPoint($ResultBarIndex[xIndex],$ResultPrice[xIndex]+1,2)Coloured(255,0,0) //Plot a point above the result
    DrawText("Distance=#xBarDiffResult#",$ResultBarIndex[xIndex],$ResultPrice[xIndex]+2) //Plot the difference between two results
    EndIf
    xIndex=xIndex+1 //Update the index counter
    Next
    
    Return

    blackwing thanked this post
    #206654 quote
    blackwing
    Participant
    New
    Hi Roberto, Thank you for the loop instruction that has helped me a lot! It has taken me a while to figure this one out. I couldn’t get your elegant code to work correctly, when I run it, it is showing multiple high points and does not include the previous condition bar in the calculation.(image1). However using your loop structure and adding my very not elegant code I got it working (image2). But this feels rather clunky. Do you have a better way? Or am I missing something completely? Also how do you recall the HHPrice from the past. ie. 3rd last HHPrice not HHPrice[3]  value from 3 bars ago. Many thanks again.  
    ONCE Periods = 20
    ONCE HHprice = 0
    ONCE HHbar   = 0
    
    
    C1 = low<low[1] and high<high[1] and high[1]>high[2] and low[1]>Low[2] //Down bar after an Up bar
    
    IF C1 THEN
    BarssinceC= BarsSince(C1[1])+1
    FOR i = 1 TO BarssinceC
    IF high[i] = highest[BarssinceC](high[1]) THEN
    HHbar = BarIndex[i]
    HHPrice= high[i]
    break
    ENDIF
    NEXT
    DRAWCANDLE(open, high, low, close) coloured(200,0,0)
    Drawpoint(HHbar,HHPrice,2)
    ENDIF
    return
    // HHprice is the prior HIGH value
    // HHbar   is the BarIndex where that HIGH is sitting
    #206672 quote
    blackwing
    Participant
    New
    Thank you @JS, That is super helpful. I have one more question I am having trouble recalling the the values stored in the array. How do I recall the last [n] value of the $ResultPrice. In your example, how would I recall the $ResultPrice the last time the cross occurred or the $ResultPrice[n]th last time it occurred, I am sure it is simple but I can’t seem to grasp it. Many thanks in advance
    #206676 quote
    JS
    Participant
    Senior
    Hi @blackwing Just type “$ResultPrice[0]” after the Return… The $ResultPrice is the “High” of the last crossing…
    blackwing thanked this post
    #206692 quote
    blackwing
    Participant
    New
    Thanks again @JS I am still confused. My apologies if I am being slow. Returning “$ResultPrice[0]” after the Return is all good. But when I try “$ResultPrice[1]” it does not give the value prior to the “$ResultPrice[0]” but just moves it along one bar, also when I run your programme I get the attached image. With the plot of the average only going back one bar this seems to be a similar issue. I am trying to recall several past values and plot them. My understanding of an $Array was that each saved value of it is stored, but I can’t seem to recall them later. For example how do I plot the 3rd value ago of the crossover, rather than the value of the last crossover 3 bars ago? In the attached image I am returning “$ResultPrice[0], $ResultPrice[3]” Many thanks
    #206697 quote
    JS
    Participant
    Senior

    Hi @blackwing

    Probably most of the forum agrees with me when I say that understanding programming with arrays is one of the most difficult things, so it’s not your fault…

    In the code, the Index is related to the crossings, Index=1 is crossing 1 and so on, and these crossings have several data such as the bar index and the price, but these only belong to these crossings…

    What I added is the Close of each crossing and to meet your request there is an additional variable called “xPriceBefore” and when you give it a certain values like for example 3, then the code plots the value of the close three bars for the first crossing…

    I can’t make it any more fun… 😉

    DefParam CalculateOnLastBars=200
    DefParam DrawOnLastBarOnly=true
    
    N=200 //Total number of bars
    xIndex=0 //Reset the index counter
    xPriceBefore=0 
    
    For i=0 to N-1 //Index i runs from 0 to N-1
    
    $Avg[i]=Average[20](Close[i])//Store signal in array $Avg
    
    If Close[i+1] < $Avg[i+1] and Close[i] > $Avg[i] then //Check "Crosses Over" condition
    $Result[xIndex]=1 //Result of condition (boolean)
    $ResultBarIndex[xIndex]=BarIndex-i //BarIndex of result
    $ResultPrice[xIndex]=Close[i+xPriceBefore] //Price of result
    xResultPrice=$ResultPrice[xIndex]
    xBarDiffResult=$ResultBarIndex[xIndex]-$ResultBarIndex[xIndex+1] //Difference in bars between first result and second result
    
    //Plot
    DrawSegment(BarIndex-i,$Avg[i],BarIndex-(i+1),$Avg[i+1])coloured(255,0,0) //Plot the average signal
    DrawPoint($ResultBarIndex[xIndex],$ResultPrice[xIndex]+1,3)Coloured(255,0,0)BorderColor(0,255,0)//Plot a point above the result
    DrawText("Distance=#xBarDiffResult#",$ResultBarIndex[xIndex],$ResultPrice[xIndex]+15*pipsize,Serif,Italic,18) //Plot the difference between two results
    DrawText("Close=#xResultPrice#",$ResultBarIndex[xIndex],$ResultPrice[xIndex]+25*pipsize,Serif,Italic,18)
    EndIf
    xIndex=xIndex+1 //Update the index counter
    Next
    
    Return $ResultPrice[0]

    #206745 quote
    blackwing
    Participant
    New
    Thank you @JS Oh my goodness.You have just made my head explode! You have probably answered this question, my apologies but I still don’t understand. I am trying to keep track of the last 3 or 4 values (and barindex) of the saved point (crossing, in your example), plot them and then use them in code later. So if I simply wanted to see if C< C1 and C1<C2 where C are the last instances of Crossing. How do I do that? (diag1.) Many thanks
    #206751 quote
    JS
    Participant
    Senior

    Hi @blackwing

    Here’s the simple version… 😉

    You can retrieve the values of the Close and the BarIndex of the first intersection with:

    $xClose[0] and $xBarIndex[0]

    And of the previous crossing with:

    $xClose[1] and $xBarIndex[1]

    And so on…Hi @blackwing

    N=200
    xIndex=0
    
    For i=0 to N-1
    If Close[i] crosses over Average[20](Close[i]) then
    $xClose[xIndex]=Close[i]
    $xBarIndex[xIndex]=BarIndex[BarIndex-i]
    xIndex=xIndex+1
    EndIf
    Next
    
    Return $xClose[0], $xClose[1], $xClose[2], $xBarIndex[0], $xBarIndex[1], $xBarIndex[2]
    
    #206763 quote
    blackwing
    Participant
    New
    Thank you @JS Looks simple but ? Please could you plot this image. Because when I try with either of the following  it doesn’t work Many thanks in advance
    N=200
    xIndex=0
     
    For i=0 to N-1
    If Close[i] crosses over Average[20](Close[i]) then
    $xClose[xIndex]=Close[i]
    $xBarIndex[xIndex]=BarIndex[BarIndex-i]
    xIndex=xIndex+1
    EndIf
    Next
    DrawText("1",$xBarIndex[0],$Close[0]+10*pipsize,Serif,Italic,18)
    DrawText("2",$xBarIndex[1],$Close[1]+10*pipsize,Serif,Italic,18)
    DrawText("3",$xBarIndex[2],$Close[2]+10*pipsize,Serif,Italic,18)
    Return $xClose[0], $xClose[1], $xClose[2], $xBarIndex[0], $xBarIndex[1], $xBarIndex[2]
    
    
    
    
    N=200
    xIndex=0
     
    For i=0 to N-1
    If Close[i] crosses over Average[20](Close[i]) then
    $xClose[xIndex]=Close[i]
    $xBarIndex[xIndex]=BarIndex[BarIndex-i]
    xIndex=xIndex+1
    EndIf
    Next
    DrawText("1",barindex[$xBarIndex[0]],Close[$Close[0]]+10*pipsize,Serif,Italic,18)
    DrawText("2",barindex[$xBarIndex[1]],Close[$Close[1]]+10*pipsize,Serif,Italic,18)
    DrawText("3",barindex[$xBarIndex[2]],Close[$Close[2]]+10*pipsize,Serif,Italic,18)
    Return $xClose[0], $xClose[1], $xClose[2], $xBarIndex[0], $xBarIndex[1], $xBarIndex[2]
    #206782 quote
    JS
    Participant
    Senior

    Hi @blackwing

    Final touch…

    DefParam DrawOnLastBarOnly=true
    DefParam CALCULATEONLASTBARS=200
    
    N=200
    xIndex=0
     
    For i=0 to N-1
    If Close[i] crosses over Average[20](Close[i]) then
    $xClose[xIndex]=Close[i]
    $xBarIndex[xIndex]=BarIndex-i
    xIndex=xIndex+1
    EndIf
    Next
    
    DrawText("1",$xBarIndex[0],$xClose[0]+15*pipsize,Serif,Italic,30)Coloured("Red")
    DrawText("2",$xBarIndex[1],$xClose[1]+15*pipsize,Serif,Italic,30)Coloured("Red")
    DrawText("3",$xBarIndex[2],$xClose[2]+15*pipsize,Serif,Italic,30)Coloured("Red")
    Return //$xClose[0], $xClose[1], $xClose[2], $xBarIndex[0], $xBarIndex[1], $xBarIndex[2]
    
    blackwing thanked this post
    #207103 quote
    blackwing
    Participant
    New
    Dear Jaap @JS Thank you very much indeed!
    JS thanked this post
    #207143 quote
    PeterSt
    Participant
    Master
    But when I try “$ResultPrice[1]” it does not give the value prior to the “$ResultPrice[0]”
    I don’t think it will help you much, but for understandings : with arrays you can’t ask for the previous value of an element in it (or for the previous value of the whole array).  
    Probably most of the forum agrees with me when I say that understanding programming with arrays is one of the most difficult things, so it’s not your fault…
    Agreed. But it is not the programming with arrays which is dificult – it is the way PRT implemented it (and this is not about not being able to ask for the previous bar value (as in [1])). If only the arrays would be multi dimensional most of the issues *I* have with arrays in PRT would be gone. This is not because I need more dimensions, but because it would allow for making clear what you are doing with them. This in itself is hard to explain, but you can simply know when you are used to a normal environment with arrays. Hats off to those like you, Jaap, who can utilise arrays within PRT. I never came further than using the available means without arrays, accomplishing the same (knowing that everything and all already *are* arrays (the [1], [2], [3] thing) and that you can let loose all functions (e.g. Average) on that too – which is not even possible with arrays (last time I looked)).
Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.

how do I loop back to save price and bar index between two points


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
blackwing @blackwing1 Participant
Summary

This topic contains 13 replies,
has 4 voices, and was last updated by PeterSt
3 years, 1 month ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 12/28/2022
Status: Active
Attachments: 11 files
Logo Logo
Loading...