I'm new on TradingView and currently testing pine script. I took one from ChartArt which is a double indicator strategy : RSI and Stochastic.
I was wondering how to add alerts but couldn't figured it out. Here the strategy :
//@version=2
strategy("Stochastic + RSI, Double Strategy (by ChartArt)", shorttitle="CA_-_RSI_Stoch_Strat", overlay=true)
// ChartArt's Stochastic Slow + Relative Strength Index, Double Strategy
//
// Version 1.0
// Idea by ChartArt on October 23, 2015.
//
// This strategy combines the classic RSI
// strategy to sell when the RSI increases
// over 70 (or to buy when it falls below 30),
// with the classic Stochastic Slow strategy
// to sell when the Stochastic oscillator
// exceeds the value of 80 (and to buy when
// this value is below 20).
//
// This simple strategy only triggers when
// both the RSI and the Stochastic are together
// in overbought or oversold conditions.
//
// List of my work:
// https://www.tradingview.com/u/ChartArt/
///////////// Stochastic Slow
Stochlength = input(14, minval=1, title="lookback length of Stochastic")
StochOverBought = input(80, title="Stochastic overbought condition")
StochOverSold = input(20, title="Stochastic oversold condition")
smoothK = input(3, title="smoothing of Stochastic %K ")
smoothD = input(3, title="moving average of Stochastic %K")
k = sma(stoch(close, high, low, Stochlength), smoothK)
d = sma(k, smoothD)
///////////// RSI
RSIlength = input( 14, minval=1 , title="lookback length of RSI")
RSIOverBought = input( 70 , title="RSI overbought condition")
RSIOverSold = input( 30 , title="RSI oversold condition")
RSIprice = close
vrsi = rsi(RSIprice, RSIlength)
///////////// Double strategy: RSI strategy + Stochastic strategy
if (not na(k) and not na(d))
if (crossover(k,d) and k < StochOverSold)
if (not na(vrsi)) and (crossover(vrsi, RSIOverSold))
strategy.entry("LONG", strategy.long, comment="LONG")
if (crossunder(k,d) and k > StochOverBought)
if (crossunder(vrsi, RSIOverBought))
strategy.entry("SHORT", strategy.short, comment="SHORT")
To do so, we can use the function: alertcondition(). I tried to put it directly inside my conditions, but scope isn't correct. If i put it at the of the script and taking back the conditions of my if statements, then i don't receive data when testing the strategy. Also i couldn't get the custom alerts i wanted.
Another thing i tried is by creating two bool parameter which become true when in the good if statement. The condition inside alertcondition() become SHORT == true for instance. By doing so, i got the following error then : "Add to Chart operation failed, reason: The script must have at least one output function call (e.g. plot, barcolor, etc.). Reason: AST is Empty".
if (not na(k) and not na(d))
if (crossover(k,d) and k < StochOverSold)
if (not na(vrsi)) and (crossover(vrsi, RSIOverSold))
strategy.entry("LONG", strategy.long, comment="LONG")
SHORT = false
LONG = true
if (crossunder(k,d) and k > StochOverBought)
if (crossunder(vrsi, RSIOverBought))
strategy.entry("SHORT", strategy.short, comment="SHORT")
SHORT = true
LONG = false
alertcondition(SHORT == true, title='SHORT', message='SHORT message')
alertcondition(LONG == true, title='LONG', message='LONG message')
Any idea on how to implement theses alerts ?