Page 1 of 1

Alert for multiple tickers on multiple time frames

Posted: Thu Apr 06, 2023 3:36 pm
by woodsyc
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")

Re: Alert for multiple tickers on multiple time frames

Posted: Mon Apr 17, 2023 10:13 pm
by Steve Burman
Woodsyc, have you managed to resolve this or do you still need help?

Re: Alert for multiple tickers on multiple time frames

Posted: Tue Apr 18, 2023 5:57 pm
by woodsyc
I was never able to figure it out unfortunately. It stumped me as my variable is declared.

Re: Alert for multiple tickers on multiple time frames

Posted: Thu Apr 20, 2023 2:25 am
by Steve Burman
I resolved this by adding a variable declaration as the 1st line after Indicator(). Basically it was saying that alertMsg was undefined

var alertMsg = ""

Re: Alert for multiple tickers on multiple time frames

Posted: Mon Apr 24, 2023 7:55 pm
by woodsyc
Thank you. I didn't realize I had to use 'var'.