pine
//@version=5
indicator("Supertrend 与 MA 融合指标", overlay=true)
// 输入参数
st_atr_period = input.int(10, "Supertrend ATR 周期")
st_factor = input.float(3.0, "Supertrend Factor")
ma_length = input.int(50, "MA 周期")
// 计算移动平均线
ma = ta.sma(close, ma_length)
// 计算 ATR
atr = ta.atr(st_atr_period)
// 计算基础上下轨
hl2 = (high + low) / 2
upperband = hl2 - st_factor * atr
lowerband = hl2 + st_factor * atr
// 初始化趋势方向
var float supertrend = na
var int trend = 1 // 1 表示上升趋势,-1 表示下降趋势
// 计算 Supertrend
if close > supertrend[1] or na(supertrend[1])
trend := 1
else if close < supertrend[1]
trend := -1
if trend == 1
supertrend := max(lowerband, supertrend[1])
else
supertrend := min(upperband, supertrend[1])
// 绘制指标
plot(supertrend, color=trend == 1 ? color.green : color.red, linewidth=2, title="Supertrend")
plot(ma, color=color.blue, linewidth=2, title="MA")
// 创建买卖信号
buySignal = ta.crossover(close, ma) and trend == 1
sellSignal = ta.crossunder(close, ma) and trend == -1
// 绘制买卖标记
plotshape(buySignal, title="买入信号", location=location.belowbar, shape=shape.triangleup, color=color.green, size=size.small)
plotshape(sellSignal, title="卖出信号", location=location.abovebar, shape=shape.triangledown, color=color.red, size=size.small)