MA Crossover Signals
MA Crossover Signals
// 输入参数
shortLength = input.int(9, title="短期移动平均周期", minval=1)
longLength = input.int(21, title="长期移动平均周期", minval=1)
useEMA = input.bool(true, title="使用 EMA 代替 SMA")
// 计算短期和长期移动平均
shortMA = useEMA ? ta.ema(close, shortLength) : ta.sma(close, shortLength)
longMA = useEMA ? ta.ema(close, longLength) : ta.sma(close, longLength)
// 生成买入和卖出信号
buySignal = ta.crossover(shortMA, longMA) // 短期 MA 突破长期 MA 时买入信号
sellSignal = ta.crossunder(shortMA, longMA) // 短期 MA 跌破长期 MA 时卖出信号
// 绘制买入和卖出信号
plotshape(series=buySignal, title="买入信号", location=location.belowbar,
color=color.green, style=shape.labelup, text="BUY", textcolor=color.white,
size=size.small)
plotshape(series=sellSignal, title="卖出信号", location=location.abovebar,
color=color.red, style=shape.labeldown, text="SELL", textcolor=color.white,
size=size.small)
// 绘制移动平均线
plot(shortMA, color=color.blue, title="短期 MA")
plot(longMA, color=color.orange, title="长期 MA")
// 背景颜色:在买入信号时背景变绿色,卖出信号时背景变红色
bgcolor(buySignal ? color.new(color.green, 90) : na)
bgcolor(sellSignal ? color.new(color.red, 90) : na)