I want to send an alert when two lines of the stochastic indicator bumping each other.
I wrote an alert condition but it doesn't give any alerts.
//@version=5
indicator(title="Stochastic", shorttitle="Stoch", format=format.price, precision=2, timeframe="", timeframe_gaps=true)
periodK = input.int(14, title="%K Length", minval=1)
smoothK = input.int(1, title="%K Smoothing", minval=1)
periodD = input.int(3, title="%D Smoothing", minval=1)
k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)
d = ta.sma(k, periodD)
plot(k, title="%K", color=#2962FF)
plot(d, title="%D", color=#FF6D00)
// My alert condition
alertcondition(k == d, 'Collision happened', 'Collision happened')
h0 = hline(80, "Upper Band", color=#787B86)
hline(50, "Middle Band", color=color.new(#787B86, 50))
h1 = hline(20, "Lower Band", color=#787B86)
fill(h0, h1, color=color.rgb(33, 150, 243, 90), title="Background")
It is very unlikely that your condition is met : k == d
You should test if the value just crossed :
justcrossed = false
if (k > d and k[1] < d[1]) or (k < d and k[1] > d[1]
justcrossed := true
alertcondition(justcrossed, 'Collision happened', 'Collision happened')
Also, don't forget to activate your alert on your chart to create it (see https://www.tradingview.com/pine-script-reference/v5/#fun_alertcondition)