pine-scriptalerts

What is the correct way to add alerts to a pine script indicator


How do I add alerts to this pine script (v5) eg. buy/sell conditions, with alert being for buy OR sell

buy = s > the 2 ma's sell = s < the 2 ma's

alertcondition(buy or sell, "text")

The plots for the 2 ma's are not straight forward, so I have failed to incorporate them into the alert condition.

Any ideas will be appreciated - Thanks

indicator(title='TBA Test', shorttitle='TBA')
src = close
rsi_length = input(14, title='RSI Length')
rsi_mom_length = input(30, title='RSI Momentum Length')
rsi_ma_length = input(9, title='RSI MA Length')
ma_length = input(6, title='SMA Length')
fastLength = input(23)
slowLength = input(50)

r = ta.rsi(src, rsi_length)
rsidelta = ta.mom(r, rsi_mom_length)
rsisma = ta.sma(ta.rsi(src, rsi_ma_length), ma_length)
s = rsidelta + rsisma

plot(s, color=color.new(#e7c7e7, 0), linewidth=2)
plot(ta.sma(s, fastLength), linewidth=1, color=color.new(#FFFF00, 0))
plot(ta.sma(s, slowLength), linewidth=1, color=color.new(#FF0000, 0))

// I have tried crossovers and < >, but pine does not like the way I am describing the two sma's.


Solution

  • Use math.max and math.min functions to figure out the highest/lowest SMAs.

    sma_max = math.max(sma1, sma2)
    sma_min = math.min(sma1, sma2)
    
    buy_alert = (s > sma_max)
    sell_alert = (s < sma_max)
    

    Then use buy_alert and sell_alert in your alertcondition call.