r/pinescript • • 5d ago

building a Pine script... logic failure

I'm trying to build a "flip-flop level" indicator, where the script would look for the peak low, retested by a peak high, but...

My knowledge of Pine is below minimal.
Thus, I rely on google assistance, and, its logic is failing me by only looking at 5 bars behind, 5 bars ahead.

Truth is, I find that limiting such a search to just a number of bars defeats the logic.

Any ideas, please?

4 Upvotes

13 comments sorted by

1

u/Prior_Bat5628 5d ago

I’d split this into two steps. Five bars on the right can confirm the old pivot low. Once it’s confirmed, keep that price as a level and wait for price to break below it, then for a later retest from below within a tolerance you define. The retest can happen many bars later. The five bars only describe how you confirm the pivot.

In Bar Replay, put one marker where the level first becomes knowable and another where the retest qualifies. TradingView’s “Plotting in the past” example shows why drawing the first marker back on the pivot candle can make the signal look earlier than it was.

1

u/Alik713 5d ago

The issue is that very often 5 bars isn't enough of a delta to go on on, yet sometimes 5 bars is just what the doctor ordered.

Thus, the hard container of "5 bars" breaks the logic

1

u/Prior_Bat5628 5d ago

Ah, you mean five bars can be too short to establish the swing low itself. In that case try confirming by distance instead of time. Track the lowest low until price closes at least one ATR above it, using the ATR from whenever that candidate low was set. Update the candidate and its threshold if a lower low arrives first. The confirmation belongs on the bar that clears the threshold. Once confirmed, the break and retest can happen later.

1

u/aguus__ 5d ago

The 5 bars are only there to confirm the pivot, they shouldn't limit how long you track the level.

What usually works is saving every confirmed pivot low in an array. Then on each new bar you loop through the saved levels: if price closes below one, mark it as broken, and if it later comes back up to it within your tolerance, that's your retest. Once a level has been retested (or gets too old), remove it from the array so it doesn't keep firing.

That way a retest can happen 10 or 200 bars later and the script still catches it.

1

u/Alik713 5d ago

I'm hoping the LLM will not go all stupid on me when I try this, but I will give it a shot.

Thank you! 

1

u/Tasty-Success-9268 5d ago

Ask ai to build it for you

1

u/TSDevStuff 4d ago

The bars after the low are unavoidable, that's just how long it takes to know it was a low. But that side can be small (2 or 3), and the level doesn't need to expire. Keep it until a new pivot replaces it. Rough starting point that tracks the latest level only:

//@version=6
indicator("Flip levels", overlay = true)
left  = input.int(10, "Bars left")
right = input.int(3, "Bars right (confirm delay)")

var float lvl = na
var bool broken = false

pl = ta.pivotlow(low, left, right)
if not na(pl)
    lvl := pl
    broken := false

if not na(lvl) and not broken and close < lvl
    broken := true

retest = broken[1] and high >= lvl and close < lvl
plot(lvl, "Level", color.orange, style = plot.style_linebr)
plotshape(retest, "Retest", shape.triangledown, location.abovebar, color.red)

It flags a retest when price has already closed below the level, then wicks back up into it and closes under. For several levels at once, push them into an array like aguus__ said.

1

u/NJ_Algo 3d ago

The 5 bars left/right is only how a pivot gets confirmed, it doesn't have to limit how far back you look. Confirm each low with ta.pivotlow, push its price into a var array and keep it there. Then on every new pivot high, loop through the array and check whether it touched one of the stored lows. That way a level from hundreds of bars ago still counts. Use AI to build you such indicator. You can literally just copy paste my comment into ChatGPT.

1

u/DanTheDan9 3d ago

A 5-bar window is part of the pivot definition, not a Pine limitation. Peak needs a rule, for example, higher than a set number of bars on each side, or a move of at least a certain percentage or ATR. You can make the level persist instead of scanning a fixed window: detect and store a confirmed pivot low, keep it active, then wait for price to revisit it and meet your retest condition. Invalidate it if price breaks through. There’s always some confirmation delay for a pivot, though - the script can’t know a low is a peak until price moves away from it.