The problem in the picture is indicated in red
I don't know how to find the length of the previous intersection. Look at the picture above and you will immediately understand everything. Is it possible to find the number of bars through the function ta.barssince?
//@version=5
indicator(title="OHLC", shorttitle="Close",overlay = true, max_bars_back = 100)
a = 44967
plot(a, '1', color.white, 3)
var int r = 0
var x = 0
var int t = 0
var int p = 0
var int p1 = 0
var int p2 = 0
var float f = 0
var l = 0
var l1 = 0
var l2 = 0
isCond11 = ta.crossover(close, a)
isCond21 = ta.crossunder(close, a)
isCond1 = ta.cross(a, close)
x := isCond11 ? 1 : isCond21[1] ? 0 : nz(x[1])
r := isCond21 ? 1 : 0
l := ta.barssince(isCond11) + 1
l1 := ta.barssince(isCond21) + 1
p := int(math.max(1, l))
p1 := int(math.max(1, l1))
//highp = ta.highest(close, int(math.max(1, nz(p))))
p2 := l1[1] - l[1]
if isCond21
label.new(bar_index, close, str.tostring(l[1]), color = color.green)
label.new(bar_index, close-7000, str.tostring(p2), color = color.yellow)
Example:
//@version=5
indicator("My script")
ema = ta.ema(close, 50)
cond = ta.crossover(close,ema)
var int bars = 0
plot(ema)
plot(close)
bgcolor(cond? color.green:na)
if cond
bars := ta.valuewhen(cond, bar_index,0) - ta.valuewhen(cond, bar_index,1)
label.new(bar_index, high, str.tostring(bars))