OPEN-SOURCE SCRIPT

Two MA Crossover with Labels

174
//version=5
indicator("Two MA Crossover with Labels", overlay=true)

// === Input Parameters ===
shortPeriod = input.int(10, title="Short MA Period")
longPeriod = input.int(100, title="Long MA Period")

// === Moving Averages ===
shortMA = ta.sma(close, shortPeriod)
longMA = ta.sma(close, longPeriod)

// === Plotting MAs ===
plot(shortMA, color=color.orange, title="Short MA")
plot(longMA, color=color.blue, title="Long MA")

// === Crossover Conditions ===
bullishCross = ta.crossover(shortMA, longMA)
bearishCross = ta.crossunder(shortMA, longMA)

// === Labels ===
if bullishCross
label.new(bar_index, low, "Buy", style=label.style_label_up, color=color.green, textcolor=color.white)

if bearishCross
label.new(bar_index, high, "Sell", style=label.style_label_down, color=color.red, textcolor=color.white)

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.