I've recently jumped into Pine having coded for the last few years in Thinkscript but I'm having some issues when trying to isolate the most recent occurrence of a condition then find a different condition only after that occurrence.
The yellow star in the screenshot is the most recent condition. Logically speaking, if the bar number is higher than the yellow star (10407), I want to find the very first occurrence when a high > high1.
In the past, I would just reference the bar number of the most recent condition that say if bar number > the bar number of the most recent condition, start counting the number of times the high > high1. Then I would reference the first count which would be the bar number I want.
Can anyone point me in the right direction? I've tried a number of things like counters, bars since, trying to reference bar numbers, etc but can't figure it out. Thanks in advance for any help possible!!!
'''
indicator("BarNum Test", shorttitle = 'BN Test', overlay = true, max_bars_back = 1500)
//User Inputs
atrtimeFrame = input.timeframe("D", title="ATR Time Frame")
//HLOC for multi timeframes
[dOpen,dHigh,dLow,dTime] = request.security(syminfo.tickerid,"D", [open[0],high[0],low[0],time[0]])
[wOpen,wHigh,wLow,wTime] = request.security(syminfo.tickerid,"W", [open[0],high[0],low[0],time[0]])
[mOpen,mHigh,mLow,mTime] = request.security(syminfo.tickerid,"M", [open[0],high[0],low[0],time[0]])
[qOpen,qHigh,qLow,qTime] = request.security(syminfo.tickerid,"3M", [open[0],high[0],low[0],time[0]])
[yOpen,yHigh,yLow,yTime] = request.security(syminfo.tickerid,"12M", [open[0],high[0],low[0],time[0]])
//Strat Signals
dTwoDown = dHigh < dHigh[1] and dLow < dLow[1]
// Mag Hit
var dMagHitBearish = 0
dMagHitBearish := dTwoDown and dLow[2] < dLow[1] and dLow < dLow[2]? 1:0 // low 2 bars ago is less than low of 1 bar ago (validates it wasnt a series of 2 downs and was a tto)
//ATR fo 20 periods
atr20 = request.security(syminfo.tickerid, atrtimeFrame, ta.atr(20))
closeOverQhigh = close > qHigh
closeTwiceATR = (atr20 * 2)
closeThriceATR = (atr20 * 3)
var c1arg1b = 0
c1arg1b := close > qOpen and close > (qOpen + closeTwiceATR) and dMagHitBearish ? 1 : 0
plotchar( c1arg1b,text = "TTO", location = location.belowbar, color=color.yellow)
var sig = 0
if close > qOpen and close > (qOpen + closeTwiceATR) and dMagHitBearish
sig :=bar_index
label.new(sig, high, text = str.tostring(sig))
//label.new(bar_index, high, text = str.tostring(bar_index))
cond = close > qOpen and close > (qOpen + closeTwiceATR) and dMagHitBearish
var int count = na
if cond
count := 0
else
count +=1
since_last = ta.barssince(cond)
cond2 = since_last > 0 and high > high[1]
var int count2 = na
if cond2
count2 := 0
else
count2 +=1
since_last2 = ta.barssince(cond2)
label.new(bar_index, high, text = str.tostring(since_last))
plotshape(since_last2 ==0, style=shape.xcross)
'''