Find first high not highest high

Forums ProRealTime English forum ProBuilder support Find first high not highest high

Viewing 6 posts - 1 through 6 (of 6 total)
  • #197339


    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.


    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.


    1 user thanked author for this post.

    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


    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.





    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:

    1 user thanked author for this post.

    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.

    1 user thanked author for this post.
Viewing 6 posts - 1 through 6 (of 6 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login