Find first high not highest high

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #197339 quote
    IG_CFD_Trader
    Participant
    Average

    Hello,

    How do I lookback (say 300 bars lookback) and find and store the first High encountered that is higher than the current High? I am not looking for highest high but first high that is higher than current high.

    #197340 quote
    druby
    Participant
    New

    Hi… Looked at a few things like ‘barsSince’  looked promising but could get to work and ‘highest’ finds the highest high.

    Had to go back to nuts and bolts, this seems to work.

    ‘For’ loop loops  from the previous bar ‘1’ to a ‘Loopback’ variable which sets the max iterations of the loop to look back over.

    ‘If’ statement test for a higher high and continues looping back till either ‘i’ increment to ‘loopback’ or ‘if’ statement becomes true.

    On true ‘x’ is assigned with the current ‘i’ value which represents how many bars have been looked back.

    Then some drawing stuff were ‘x’ is used for the look back index.

    And since that all what was needed the ‘for’ loop is terminated with ‘break’, no point carrying on.

    ‘drawonlastbaronly’ and ‘islastbarupdate are in there for drawing purposes and to make sure when looking back it doesn’t try to go beyond bar ‘0’. You may have to guard against that depending on how you use it, lookback < barindex condition.

     

    defparam drawonlastbaronly = true
    
    loopback = 300
    
    if islastbarupdate then
    for i = 1 to loopback
    
    if high[i] > high  then
    x = i
    
    preHigh=high[i]
    hi = high
    diff = preHigh - hi
    drawarrowdown(barindex[x],high[x]+3) coloured("tomato")
    drawtriangle(barindex,high+2,barindex[6],high+6,barindex,high+10)coloured("darkOliveGreen")bordercolor(0,0,0,0)
    drawtext("H #preHigh# lookback for #x# bars from current H #hi# difference #diff# higher",0,-40)Anchor(top)
    drawsegment(barindex[x]-5,high,barindex+5,high)coloured("mediumOrchid")style(dottedline,1)
    
    break
    
    endif
    next
    endif 
    
    return
    robertogozzi thanked this post
    #197357 quote
    IG_CFD_Trader
    Participant
    Average

    Thanks Druby.

    How do I improve it to find the first high that is nearest to the current high? by nearest I mean by value and not by occurrence. for example that high minus the current high is the smallest than all other higher highs irrespective of when they occurred.

    Example assume current high is 100 and first higher high occurred day before at 130 and next higher high occurred a week earlier at 120. I want x to = 120 because 120-100 =20 which is less than 30

    #197420 quote
    druby
    Participant
    New

    This appears to work about 90% of time, not sure what affecting other 10%, needs some thinking time.

    I tried this on 10-30sec to see it tracking, seems to work fine but then deviates till next bar starts, or doesn’t track for a few bars.

    Not sure whats happening, maybe fresh eyes with see/think of something.

    Give it a try and let me know what you think, and btw what timeframe (s) are you working to.

    regards

    // NOTE:  to set 
    // dynamicVariable  'lookback'  'integer' default value  '300'
    
    defparam drawonlastbaronly = true
     
    lbk = max(min(lookback,barindex),1)     // entry guard limiter
    
    if islastbarupdate then           // wait till last bar
     
    chigh = high // current high
     
    //once xibar=0 // zero current bar
    //once xdiff=0  // zero no difference, since current bar
    
    for i = 1 to lbk do               // limited loopback scope
     
    if high[i] > chigh then      // test for a higher high than current high
    
    diff = high[i] - chigh
    ibar = i
    
    if i = 1 then            // used 1st previous value as default
    xdiff = diff
    xibar = ibar
    
     
    elsif diff < xdiff then  // test futher values for smaller diffence
    xdiff = diff
    xibar = i
    endif
    
    xb=xibar
    xd=xdiff
    xh = high[xb]
    hi = chigh
     endif 
    next            // 1 to lookback
    
    endif              // islastbarupdate
     
    drawarrowdown(barindex,xh+5) coloured("darkSeaGreen") // arrow over scope start start
    drawarrowdown(barindex[xb],xh+5) coloured("tomato") //  arrow over target in scope
    drawpoint(barindex[xb],xh,2) coloured("tomato") //  target point highlighted in scope
    drawpoint(barindex,hi,2) coloured("darkSeaGreen") // arrow over scope start start
    
    drawtext("#xb#",barindex[xb],xh+10) // look back value of target
    
    drawsegment(barindex[lbk],hi,barindex,hi)coloured(0,255,255,85)style(dottedline,1) // lookback scope line
    drawsegment(barindex[xb]-1,hi,barindex+2,hi)coloured("mediumorchid")style(dottedline,2) // target higher high line within  scope
    
    drawtext("H #xh# lookback for #xb# bars from current H #hi# difference #xd# higher",0,-40)Anchor(top) // details!
    
    return
    #197579 quote
    robertogozzi
    Moderator
    Master

    Try this one (not tested). I added variable J to count the very first occurrence, as it can’t be assumed it’s when I=1:

    // NOTE:  to set
    // dynamicVariable  'lookback'  'integer' default value  '300'
    defparam drawonlastbaronly = true
    lookback = 500
    lbk = max(min(lookback,barindex),1)     // entry guard limiter
    if islastbarupdate then           // wait till last bar
       chigh = high // current high
       //once xibar=0 // zero current bar
       //once xdiff=0  // zero no difference, since current bar
       j = 0
       for i = 1 to lbk do               // limited loopback scope
          if high[i] > chigh then      // test for a higher high than current high
             diff = high[i] - chigh
             ibar = i
             j = j + 1
             if j = 1 then            // used 1st previous value as default
                xdiff = diff
                xibar = ibar
             elsif diff < xdiff then  // test futher values for smaller diffence
                xdiff = diff
                xibar = i
             endif
             xb=xibar
             xd=xdiff
             xh = high[xb]
             hi = chigh
          endif
       next            // 1 to lookback
    endif              // islastbarupdate
    drawarrowdown(barindex,xh+5) coloured("darkSeaGreen") // arrow over scope start start
    drawarrowdown(barindex[xb],xh+5) coloured("tomato") //  arrow over target in scope
    drawpoint(barindex[xb],xh,2) coloured("tomato") //  target point highlighted in scope
    drawpoint(barindex,hi,2) coloured("darkSeaGreen") // arrow over scope start start
    drawtext("#xb#",barindex[xb],xh+10) // look back value of target
    drawsegment(barindex[lbk],hi,barindex,hi)coloured(0,255,255,85)style(dottedline,1) // lookback scope line
    drawsegment(barindex[xb]-1,hi,barindex+2,hi)coloured("mediumorchid")style(dottedline,2) // target higher high line within  scope
    drawtext("H #xh# lookback for #xb# bars from current H #hi# difference #xd# higher",0,-40)Anchor(top) // details!
    return
    druby thanked this post
    #197599 quote
    druby
    Participant
    New

    hi… Rob

     I added variable to count the very first occurrence, as it can’t be assumed it’s when I=1:

    It took me a while to grasp what you comments meant. Just for other’s following at home!

    When the loop starts, it doesn’t process anything until it looks back apon a high higher than the current high.

    Now if that just happens to be the 1st previous bar , when i = 1, then the calculated ‘diff’ value along with it index location would be stored in the ‘If’ statements ‘xdiff’ and ‘xibar’ as the default/first found value which is used later for comparisons.

    These values, but specifically, ‘xdiff’ is used to determine if following found ‘diff’ values are smaller than it. If it is, it replaces it, if its not , it lives through other  iterations of  the loop till it is, or the loop reaches the end and terminates.

    However, when i=1 and not a higher value, then ‘no’ code in the loop is executed, that correct. But when a later, the first, higher high is found and i > 1 then this new set of values are not stored in the ‘if’ statements ‘xdiff’ and ‘xibar’ lines because the condition is false so not updating ‘xdiff’ for ‘this’ first time.

    This creates a knock on affect because when it compares this first diff value, it’s comparing it to an invalid/previous, but wrong ‘xdiff’ value and also not being the first ‘xdiff’. This means that not only is it being compared to a wrong value, also any other finds are being compared to a wrong value and its not until a value registers as smaller higher high to this wrong value, that normal service is resumed, If this doesn’t correct itself before it draws, it ripples through to drawing elements, randomly not moving or making wild jumps. Just as i saw.

    Thanks for your time Roberto, that’s great, learned a lot from trying to understand my own code from you comments.

    Best Regards.

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

Find first high not highest high


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
IG_CFD_Trader @wnakhoul Participant
Summary

This topic contains 5 replies,
has 3 voices, and was last updated by druby
3 years, 6 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 07/16/2022
Status: Active
Attachments: 2 files
Logo Logo
Loading...