PROTECTED SOURCE SCRIPT

HMA1

39
//version=5
strategy("黄金 HMA + SuperTrend 趋势增强策略", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10)

// --- 1. 输入参数 ---
// HMA 参数
hmaLen = input.int(55, "HMA 长度", minval=1, group="HMA 设置")

// SuperTrend 参数
stFactor = input.float(3.0, "SuperTrend 乘数", step=0.1, group="SuperTrend 设置")
stPeriod = input.int(10, "SuperTrend ATR 周期", group="SuperTrend 设置")

// 离场设置
useAtrSl = input.bool(true, "启用 ATR 动态止损", group="风险管理")
atrSlMult = input.float(2.0, "止损 ATR 倍数", step=0.1, group="风险管理")

// --- 2. 指标计算 ---
// 计算 HMA
hmaValue = ta.hma(close, hmaLen)

// 计算 SuperTrend
[stValue, stDirection] = ta.supertrend(stFactor, stPeriod)

// 计算 ATR(用于止损)
atr = ta.atr(14)

// --- 3. 绘图 ---
plot(hmaValue, "HMA 趋势线", color=hmaValue > hmaValue[1] ? color.green : color.red, linewidth=2)
plot(stValue, "SuperTrend 线", color=stDirection < 0 ? color.new(color.teal, 0) : color.new(color.maroon, 0), linewidth=2)

// --- 4. 交易逻辑 ---
// 做多条件:
// 1. 价格在 HMA 之上 且 HMA 正在向上拐头
// 2. SuperTrend 变为看涨方向 (stDirection < 0)
longCondition = close > hmaValue and hmaValue > hmaValue[1] and stDirection < 0

// 做空条件:
// 1. 价格在 HMA 之下 且 HMA 正在向下拐头
// 2. SuperTrend 变为看跌方向 (stDirection > 0)
shortCondition = close < hmaValue and hmaValue < hmaValue[1] and stDirection > 0

// --- 5. 执行与止损逻辑 ---
var float longStop = na
var float shortStop = na

// 入场逻辑
if (longCondition)
longStop := close - (atr * atrSlMult)
strategy.entry("Long", strategy.long, comment="HMA+ST 多")

if (shortCondition)
shortStop := close + (atr * atrSlMult)
strategy.entry("Short", strategy.short, comment="HMA+ST 空")

// 离场逻辑:当 SuperTrend 反转或触及 ATR 止损时离场
if (strategy.position_size > 0)
strategy.exit("Exit Long", "Long", stop=longStop, limit=na, when=stDirection > 0, comment="多单离场")

if (strategy.position_size < 0)
strategy.exit("Exit Short", "Short", stop=shortStop, limit=na, when=stDirection < 0, comment="空单离场")

// 填充背景色以示趋势
fill(plot(stValue), plot(open > close ? open : close), color = stDirection < 0 ? color.new(color.green, 90) : color.new(color.red, 90))

면책사항

해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.