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! 🚀
JSParticipant
Senior
You could try this approach:
Once HighestWick=High
If High > HighestWick then
HighestWick=High
HighestWickBar=BarIndex
EndIf
Return HighestWick as "HighestWick", HighestWickBar as "HighestWickBar"
If I understood correctly, you want to detect groups of connected wicks dynamically without scanning all previous candles each time.
// -------------------------------
// 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 then
init = 1
n = n + 1 // Increment the group index
elsif High >= High[1] and init = 1 then
// Continue within the same group of connected wicks
else
init = 0 // The connection is broken, reset the flag
endif
// -------------------------------
// 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 then
drawpoint(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 level
elsif init <> init[1] and init = 0 then
drawpoint(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 group
drawsegment($HighestWickBarStart[n], $HighestWickStart[n],$HighestWickBarEnd[n], $HighestWickEnd[n]) coloured("black")
endif
return
The following code is a simplified version that only calculates the highest wick within each group of connected wicks, without drawing any visual elements.
if High >= High[1] and init = 0 then
init = 1
$HighestWickEnd[n]=High
$HighestWickBarEnd[n]=BarIndex
n = n + 1 // Increment the group index
elsif High >= High[1] and init = 1 then
// Continue within the same group of connected wicks
$HighestWickEnd[n]=High
$HighestWickBarEnd[n]=BarIndex
else
init = 0 // The connection is broken, reset the flag
endif
return
I’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!