moreaubrady
Pine Script Scholar
Pine Script Scholar
Posts: 2
Joined: August 10th, 2021
Contact: TradingView Profile

ALERTS TO MY CMA

HEY guys brady here, first time poster, long time mastery course student. (that was supposed to be a joke lmao, title="Not Funny")
anyways, im trying to get my CMA line (colored moving average) to have alert function, so id like it when the CMA turned red to post a red x and alert its changed red.
likewise for when it turns green a green x and alert that says its turned green,
the thing is i only know how to code alerts to something that has already been defined in the script above. but the only way i know how to code the CMA to color like that is only one line of code so i dont know how i would go about defining the alert in the script.
the script code is literally the same code as Matts on his zen and art of trading website. i just would love someone who could help me get this CMA Coded with alerts, i will be forever grateful. i hope to hear back soon and happy coding everybody! :zen: :zen: :heart: :heart: :zen:

moreaubrady
Pine Script Scholar
Pine Script Scholar
Posts: 2
Joined: August 10th, 2021
Contact: TradingView Profile

Re: ALERTS TO MY CMA

EVERYONE!! : Matt has helped me with my current post above, ill provide script below

// @version=4
study("EMA Alert", overlay=true)

// Get EMA & plot it
ema = ema(close, 50)
emaColor = close > ema ? color.green : color.red
plot(ema, color=emaColor, linewidth=2)

// Check for bearish cross of price < ema (previous close was above EMA, current close is below)
bearishCross = false
if close[1] > ema and close < ema
alert("The EMA has turned red!", alert.freq_once_per_bar_close)
bearishCross := true

// Check for bullish cross of price > ema (previous close was below EMA, current close is above)
bullishCross = false
if close[1] < ema and close > ema
alert("The EMA has turned green!", alert.freq_once_per_bar_close)
bullishCross := true

// Change chart background color (to see alerts visually - optional)
bgcolor(bearishCross ? color.new(color.red,50) : na)
bgcolor(bullishCross ? color.new(color.green,50) : na)

Return to “Pine Script Q&A”