woodsyc
Pine Script Rookie
Pine Script Rookie
Posts: 5
Joined: April 2nd, 2023
Contact: TradingView Profile

Alert for multiple tickers on multiple time frames

So I am trying to do a multiple ticker on multiple time frames for crossover/under of EMA's.

I am getting an Undeclared identifier 'alertMsg' but it is identified. I have other variations of this and do not get the error. Can anyone see why I am getting an error? Error line is alertMsg += "5 min Buy Long opportunity for \n" + syminfo.tickerid

Thank you

//@version=5
indicator('trigger for multiple symbols on multiple time frames')


checkForAlert() =>
alertMsg = ""
// Get indicator data

time_5min_close_6 = request.security(syminfo.tickerid, "5" , ta.ema(close,6))
time_5min_high_32 = request.security(syminfo.tickerid, "5" , ta.ema(high,32))
time_5min_low_32 = request.security(syminfo.tickerid, "5" , ta.ema(low,32))
time_15min_close_6 = request.security(syminfo.tickerid, "15" , ta.ema(close,6))
time_15min_high_32 = request.security(syminfo.tickerid, "15" , ta.ema(high,32))
time_15min_low_32 = request.security(syminfo.tickerid, "15" , ta.ema(low,32))
time_60min_close_6 = request.security(syminfo.tickerid, "60" , ta.ema(close,6))
time_60min_high_32 = request.security(syminfo.tickerid, "60" , ta.ema(high,32))
time_60min_low_32 = request.security(syminfo.tickerid, "60" , ta.ema(low,32))
time_240min_close_6 = request.security(syminfo.tickerid, "240" , ta.ema(close,6))
time_240min_high_32 = request.security(syminfo.tickerid, "240" , ta.ema(high,32))
time_240min_low_32 = request.security(syminfo.tickerid, "240" , ta.ema(low,32))

// Detect buy or sell
buy_5min = ta.crossover(time_5min_close_6, time_5min_high_32)
sell_5min = ta.crossunder(time_5min_close_6, time_5min_low_32)
buy_15min = ta.crossover(time_15min_close_6, time_15min_high_32)
sell_15min = ta.crossunder(time_15min_close_6, time_15min_low_32)
buy_60min = ta.crossover(time_60min_close_6, time_60min_high_32)
sell_60min = ta.crossunder(time_60min_close_6, time_60min_low_32)
buy_240min = ta.crossover(time_240min_close_6, time_240min_high_32)
sell_240min = ta.crossunder(time_240min_close_6, time_240min_low_32)

//
if buy_5min
alertMsg += "5 min Buy Long opportunity for \n" + syminfo.tickerid
if sell_5min
alertMsg += "5 min Buy Short opportunity for \n" + syminfo.tickerid
if buy_15min
alertMsg += "15 min Buy Long opportunity for \n" + syminfo.tickerid
if sell_15min
alertMsg += "15 min Buy Short opportunity for \n" + syminfo.tickerid
if buy_60min
alertMsg += "60 min Buy Long opportunity for \n" + syminfo.tickerid
if sell_60min
alertMsg += "60 min Buy Short opportunity for \n" + syminfo.tickerid
if buy_240min
alertMsg += "240 min Buy Long opportunity for \n" + syminfo.tickerid
if sell_240min
alertMsg += "240 min Buy Short opportunity for \n" + syminfo.tickerid
alertMsg

fireAlert(ticker, freq = alert.freq_once_per_bar)=>
Msg = request.security(ticker, timeframe.period, checkForAlert())
if str.length(Msg) > 0
alert(Msg, freq)

fireAlert("USDCAD")
fireAlert("EURUSD")
fireAlert("USDJPY")
fireAlert("GBPUSD")
fireAlert("AUDUSD")
fireAlert("NZDUSD")
fireAlert("USDCHF")
fireAlert("EURJPY")
fireAlert("GBPJPY")

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: Alert for multiple tickers on multiple time frames

Woodsyc, have you managed to resolve this or do you still need help?

woodsyc
Pine Script Rookie
Pine Script Rookie
Posts: 5
Joined: April 2nd, 2023
Contact: TradingView Profile

Re: Alert for multiple tickers on multiple time frames

I was never able to figure it out unfortunately. It stumped me as my variable is declared.

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: Alert for multiple tickers on multiple time frames

I resolved this by adding a variable declaration as the 1st line after Indicator(). Basically it was saying that alertMsg was undefined

var alertMsg = ""

woodsyc
Pine Script Rookie
Pine Script Rookie
Posts: 5
Joined: April 2nd, 2023
Contact: TradingView Profile

Re: Alert for multiple tickers on multiple time frames

Thank you. I didn't realize I had to use 'var'.

Return to “Pine Script Q&A”