Optimized Way to Find Last Connected Overlapping Wick (Without Full Loopback Tra
Forums › ProRealTime English forum › ProRealTime platform support › Optimized Way to Find Last Connected Overlapping Wick (Without Full Loopback Tra
- This topic has 3 replies, 3 voices, and was last updated 1 month ago by
shiv55.
-
-
02/26/2025 at 12:45 AM #244365
I’m working on an indicator that needs to find the highest overlapping wick of any candle and dynamically update a variable (
highestConnectedWick
). The problem is that my current approach requires looping back through all previous candles to check for wick connections, which makes my indicator slow or even freeze when applied to many bars.Currently, I’m using a standard for/while loop to traverse back and check if:
- The high of a previous candle overlaps with the highest wick found so far.
- The body of any previous candle overlaps with this highest wick (which stops the search).
However, this method is inefficient as it runs O(n) loops for every bullish/bearish engulfing pattern, leading to performance issues.
What I Need:
I am looking for an optimized approach that does not rely on a full backward traversal using a for/while loop every time a new candle is processed. Instead, I would prefer:
- Memoization or caching: Store the highest connected wick values to avoid redundant calculations.
- Binary search or another fast lookup method: To quickly find the relevant wick instead of checking all previous candles.
- Any other ProRealTime-specific function that can efficiently track the last connected overlapping wick.
I must be able to look back as far as needed if no body overlap is found, but I don’t want to traverse linearly through all past candles every time.
Any suggestions or alternative solutions in ProRealTime code? Thanks in advance! 🚀
02/26/2025 at 8:35 AM #244367You could try this approach:
Highest Wick12345678Once HighestWick=HighIf High > HighestWick thenHighestWick=HighHighestWickBar=BarIndexEndIfReturn HighestWick as "HighestWick", HighestWickBar as "HighestWickBar"2 users thanked author for this post.
02/26/2025 at 10:51 AM #244371If I understood correctly, you want to detect groups of connected wicks dynamically without scanning all previous candles each time.
123456789101112131415161718192021222324252627282930313233343536// -------------------------------// DETECTION OF CONNECTED WICKS// -------------------------------// This section identifies groups of candles where the wicks are connected,// meaning each new high must be greater than or equal to the previous one.if High >= High[1] and init = 0 theninit = 1n = n + 1 // Increment the group indexelsif High >= High[1] and init = 1 then// Continue within the same group of connected wickselseinit = 0 // The connection is broken, reset the flagendif// -------------------------------// VISUALIZATION OF DETECTED GROUPS// -------------------------------// This section marks the beginning and end of each connected wick group// using blue and red points, and draws a segment between them for visualization.if init <> init[1] and init = 1 thendrawpoint(barindex[1], high[1], 3) coloured("blue") // Start of the group$HighestWickBarStart[n] = barindex[1] // Store start bar index$HighestWickStart[n] = high[1] // Store start wick levelelsif init <> init[1] and init = 0 thendrawpoint(barindex[1], high[1], 3) coloured("red") // End of the group$HighestWickBarEnd[n] = barindex[1] // Store end bar index$HighestWickEnd[n] = high[1] // Store end wick level// Draw a segment connecting the start and end of the wick groupdrawsegment($HighestWickBarStart[n], $HighestWickStart[n],$HighestWickBarEnd[n], $HighestWickEnd[n]) coloured("black")endifreturnThe following code is a simplified version that only calculates the highest wick within each group of connected wicks, without drawing any visual elements.
1234567891011121314if High >= High[1] and init = 0 theninit = 1$HighestWickEnd[n]=High$HighestWickBarEnd[n]=BarIndexn = n + 1 // Increment the group indexelsif High >= High[1] and init = 1 then// Continue within the same group of connected wicks$HighestWickEnd[n]=High$HighestWickBarEnd[n]=BarIndexelseinit = 0 // The connection is broken, reset the flagendifreturn1 user thanked author for this post.
02/26/2025 at 10:52 PM #244392I’m realy sorry JS wrote: Iván wrote: for not being able to explain my question here i have attached screenshot of given you two example assume that marked number are their respective barindex
- Highest connected wick candle’s barindex for candle ‘0’ is ‘0’ because candle ‘0’ upper wick is overalpping with body of candle ‘1’ so barindex ‘0’ is my answer.
- Highest connected wick candle’s barindex for candle ‘1’ is ‘9’ because candle ‘1’ upper wick is overlapping with upperwick of ‘2’ and ‘2’ upper wick overlapping with ‘3’ and so on we wiil keep looking ‘up’ and ‘left’ as long as candles uppper wicks are loverlapping and stop when there is body overlap with wick(which happens at ’10’) and return barindex ‘9’ as my answer.
You can see that for candle ‘1’, ‘2’,’3’…. till ‘9’ all of them has answer ‘9’. You might notice that there are unmarked candle in between ‘3’ to ‘4’ and ‘8’ to ‘9’ because we need to look straight left from high of the candle.
P.S- My general for/while loop run through all the previous candles checking is making my whole indicator slow.So i need efficient way which is <= O(n) because i’m doing this query so many time in my indicator.
Thank you!
-
AuthorPosts