Find first high not highest high
Forums › ProRealTime English forum › ProBuilder support › Find first high not highest high
- This topic has 5 replies, 3 voices, and was last updated 2 years ago by druby.
-
-
07/16/2022 at 2:39 AM #197339
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.
07/16/2022 at 5:24 AM #197340Hi… 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.
Find first previous highest high within a number of bars12345678910111213141516171819202122232425defparam drawonlastbaronly = trueloopback = 300if islastbarupdate thenfor i = 1 to loopbackif high[i] > high thenx = ipreHigh=high[i]hi = highdiff = preHigh - hidrawarrowdown(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)breakendifnextendifreturn1 user thanked author for this post.
07/16/2022 at 12:24 PM #197357Thanks 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
07/17/2022 at 9:31 PM #197420This 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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253// NOTE: to set// dynamicVariable 'lookback' 'integer' default value '300'defparam drawonlastbaronly = truelbk = max(min(lookback,barindex),1) // entry guard limiterif islastbarupdate then // wait till last barchigh = high // current high//once xibar=0 // zero current bar//once xdiff=0 // zero no difference, since current barfor i = 1 to lbk do // limited loopback scopeif high[i] > chigh then // test for a higher high than current highdiff = high[i] - chighibar = iif i = 1 then // used 1st previous value as defaultxdiff = diffxibar = ibarelsif diff < xdiff then // test futher values for smaller diffencexdiff = diffxibar = iendifxb=xibarxd=xdiffxh = high[xb]hi = chighendifnext // 1 to lookbackendif // islastbarupdatedrawarrowdown(barindex,xh+5) coloured("darkSeaGreen") // arrow over scope start startdrawarrowdown(barindex[xb],xh+5) coloured("tomato") // arrow over target in scopedrawpoint(barindex[xb],xh,2) coloured("tomato") // target point highlighted in scopedrawpoint(barindex,hi,2) coloured("darkSeaGreen") // arrow over scope start startdrawtext("#xb#",barindex[xb],xh+10) // look back value of targetdrawsegment(barindex[lbk],hi,barindex,hi)coloured(0,255,255,85)style(dottedline,1) // lookback scope linedrawsegment(barindex[xb]-1,hi,barindex+2,hi)coloured("mediumorchid")style(dottedline,2) // target higher high line within scopedrawtext("H #xh# lookback for #xb# bars from current H #hi# difference #xd# higher",0,-40)Anchor(top) // details!return07/20/2022 at 4:20 PM #197579Try 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:
1234567891011121314151617181920212223242526272829303132333435363738// NOTE: to set// dynamicVariable 'lookback' 'integer' default value '300'defparam drawonlastbaronly = truelookback = 500lbk = max(min(lookback,barindex),1) // entry guard limiterif islastbarupdate then // wait till last barchigh = high // current high//once xibar=0 // zero current bar//once xdiff=0 // zero no difference, since current barj = 0for i = 1 to lbk do // limited loopback scopeif high[i] > chigh then // test for a higher high than current highdiff = high[i] - chighibar = ij = j + 1if j = 1 then // used 1st previous value as defaultxdiff = diffxibar = ibarelsif diff < xdiff then // test futher values for smaller diffencexdiff = diffxibar = iendifxb=xibarxd=xdiffxh = high[xb]hi = chighendifnext // 1 to lookbackendif // islastbarupdatedrawarrowdown(barindex,xh+5) coloured("darkSeaGreen") // arrow over scope start startdrawarrowdown(barindex[xb],xh+5) coloured("tomato") // arrow over target in scopedrawpoint(barindex[xb],xh,2) coloured("tomato") // target point highlighted in scopedrawpoint(barindex,hi,2) coloured("darkSeaGreen") // arrow over scope start startdrawtext("#xb#",barindex[xb],xh+10) // look back value of targetdrawsegment(barindex[lbk],hi,barindex,hi)coloured(0,255,255,85)style(dottedline,1) // lookback scope linedrawsegment(barindex[xb]-1,hi,barindex+2,hi)coloured("mediumorchid")style(dottedline,2) // target higher high line within scopedrawtext("H #xh# lookback for #xb# bars from current H #hi# difference #xd# higher",0,-40)Anchor(top) // details!return1 user thanked author for this post.
07/20/2022 at 8:07 PM #197599hi… Rob
I added variable J 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.
-
AuthorPosts
Find exclusive trading pro-tools on