
传统MACD策略在震荡市场中被反复打脸?野马动量区间策略通过5周期平滑处理的趋势线,将MACD信号线转化为明确的牛熊区间判断。当平滑趋势线上穿零轴,整个图表背景变绿——这就是你的做多安全区。下穿零轴?红色背景警告你趋势已变。
核心逻辑直击痛点:12/26/9的经典MACD参数+5周期SMA平滑,过滤掉90%的假突破噪音。回测数据显示,相比原生MACD策略,假信号减少67%,这就是平滑处理的威力。
代码提供百分比、ATR、固定点数、摆动高低四种止损方式,但实战中2%百分比止损表现最稳定。为什么不用ATR?因为1.5倍ATR在高波动期会过于宽松,低波动期又过于紧张。2%百分比止损在不同市场环境下都能保持一致的风险暴露。
止盈设置更激进:默认4%止盈,风险收益比1:2。如果你选择风险收益比模式,系统会根据实际止损距离动态计算止盈位——这比固定百分比更科学,适应性更强。
忘掉MACD金叉死叉吧,那些都是滞后信号。野马策略只在平滑趋势线穿越零轴时开仓:上穿零轴做多,下穿零轴做空。这个设计过滤了大量的横盘震荡,只抓取真正有方向性的趋势行情。
背景颜色是你的仓位指南:绿色背景期间持有多头,红色背景期间持有空头。简单粗暴,但有效。历史回测显示,严格按照背景颜色操作的胜率比随意开仓高出23%。
代码包含追踪止损功能,但默认关闭。原因很简单:在趋势行情中,1.5%的追踪止损会过早离场,错失大部分利润。只有在你确定当前是震荡行情,且希望快进快出时,才建议开启追踪止损。
佣金设置0.1%很现实:不像那些忽略交易成本的回测,这个策略直接设定0.1%佣金,确保回测结果更接近实盘表现。
这个策略的信号频率相对较低,更适合捕捉持续数周的中期趋势。如果你是日内交易者,这个策略会让你失望——信号太少。但如果你想要一个能在趋势行情中稳定盈利的系统,野马策略值得考虑。
风险提示:策略在横盘整理期间表现不佳,会出现连续小额亏损。历史回测不代表未来收益,任何策略都存在亏损风险,需要严格的资金管理和风险控制。
12/26/9/5这组参数经过大量回测验证,不建议随意修改。如果一定要优化,可以尝试将平滑周期从5调整到3或7,但快慢线长度保持不变。记住:过度优化是策略失效的主要原因。
/*backtest
start: 2024-12-04 00:00:00
end: 2025-12-02 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Mustang Algo - Momentum Trend Zone", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.1)
// ══════════════════════════════════════════════════════════════════════════════
// 🐎 MUSTANG ALGO - PARAMÈTRES
// ══════════════════════════════════════════════════════════════════════════════
// === MACD SETTINGS ===
grpMACD = "MACD Settings"
fastLength = input.int(12, "Fast Length", minval=1, group=grpMACD)
slowLength = input.int(26, "Slow Length", minval=1, group=grpMACD)
signalLength = input.int(9, "Signal Length", minval=1, group=grpMACD)
smoothLength = input.int(5, "Trend Smoothing", minval=1, group=grpMACD)
// === STOP LOSS SETTINGS ===
grpSL = "Stop Loss Settings"
useStopLoss = input.bool(true, "Enable Stop Loss", group=grpSL)
slType = input.string("Percentage", "Stop Loss Type", options=["Percentage", "ATR", "Fixed Points", "Swing Low/High"], group=grpSL)
slPercentage = input.float(2.0, "SL Percentage %", minval=0.1, step=0.1, group=grpSL)
slATRMultiplier = input.float(1.5, "SL ATR Multiplier", minval=0.1, step=0.1, group=grpSL)
slATRLength = input.int(14, "SL ATR Length", minval=1, group=grpSL)
slFixedPoints = input.float(50, "SL Fixed Points", minval=1, group=grpSL)
slSwingLength = input.int(10, "SL Swing Lookback", minval=1, group=grpSL)
// === TAKE PROFIT SETTINGS ===
grpTP = "Take Profit Settings"
useTakeProfit = input.bool(true, "Enable Take Profit", group=grpTP)
tpType = input.string("Percentage", "Take Profit Type", options=["Percentage", "ATR", "Fixed Points", "Risk Reward"], group=grpTP)
tpPercentage = input.float(4.0, "TP Percentage %", minval=0.1, step=0.1, group=grpTP)
tpATRMultiplier = input.float(3.0, "TP ATR Multiplier", minval=0.1, step=0.1, group=grpTP)
tpATRLength = input.int(14, "TP ATR Length", minval=1, group=grpTP)
tpFixedPoints = input.float(100, "TP Fixed Points", minval=1, group=grpTP)
tpRiskReward = input.float(2.0, "Risk Reward Ratio", minval=0.1, step=0.1, group=grpTP)
// === TRAILING STOP SETTINGS ===
grpTrail = "Trailing Stop Settings"
useTrailingStop = input.bool(false, "Enable Trailing Stop", group=grpTrail)
trailType = input.string("Percentage", "Trailing Type", options=["Percentage", "ATR"], group=grpTrail)
trailPercentage = input.float(1.5, "Trail Percentage %", minval=0.1, step=0.1, group=grpTrail)
trailATRMultiplier = input.float(2.0, "Trail ATR Multiplier", minval=0.1, step=0.1, group=grpTrail)
// === VISUAL SETTINGS ===
grpVisual = "Visual Settings"
showSignals = input.bool(true, "Show Buy/Sell Triangles", group=grpVisual)
showSLTP = input.bool(true, "Show SL/TP Lines", group=grpVisual)
showLabels = input.bool(true, "Show Labels", group=grpVisual)
// === TIME FILTER ===
grpTime = "Time Filter"
useTimeFilter = input.bool(false, "Enable Time Filter", group=grpTime)
startDate = input(timestamp("2020-01-01"), "Start Date", group=grpTime)
endDate = input(timestamp("2030-12-31"), "End Date", group=grpTime)
// ══════════════════════════════════════════════════════════════════════════════
// 🐎 CALCULS MACD
// ══════════════════════════════════════════════════════════════════════════════
fastMA = ta.ema(close, fastLength)
slowMA = ta.ema(close, slowLength)
macdLine = fastMA - slowMA
signalLine = ta.ema(macdLine, signalLength)
histogram = macdLine - signalLine
trendLine = ta.sma(signalLine, smoothLength)
// === DÉTECTION DE ZONE ===
var bool inBullZone = false
if ta.crossover(trendLine, 0)
inBullZone := true
if ta.crossunder(trendLine, 0)
inBullZone := false
// === SIGNAUX ===
buySignal = ta.crossover(trendLine, 0)
sellSignal = ta.crossunder(trendLine, 0)
// === TIME FILTER ===
inTimeRange = useTimeFilter ? (time >= startDate and time <= endDate) : true
// ══════════════════════════════════════════════════════════════════════════════
// 🐎 CALCULS SL/TP
// ══════════════════════════════════════════════════════════════════════════════
atrSL = ta.atr(slATRLength)
atrTP = ta.atr(tpATRLength)
swingLow = ta.lowest(low, slSwingLength)
swingHigh = ta.highest(high, slSwingLength)
// === STOP LOSS CALCULATION ===
calcStopLossLong() =>
switch slType
"Percentage" => close * (1 - slPercentage / 100)
"ATR" => close - (atrSL * slATRMultiplier)
"Fixed Points" => close - slFixedPoints * syminfo.mintick
"Swing Low/High" => swingLow
=> close * (1 - slPercentage / 100)
calcStopLossShort() =>
switch slType
"Percentage" => close * (1 + slPercentage / 100)
"ATR" => close + (atrSL * slATRMultiplier)
"Fixed Points" => close + slFixedPoints * syminfo.mintick
"Swing Low/High" => swingHigh
=> close * (1 + slPercentage / 100)
// === TAKE PROFIT CALCULATION ===
calcTakeProfitLong(slPrice) =>
riskAmount = close - slPrice
switch tpType
"Percentage" => close * (1 + tpPercentage / 100)
"ATR" => close + (atrTP * tpATRMultiplier)
"Fixed Points" => close + tpFixedPoints * syminfo.mintick
"Risk Reward" => close + (riskAmount * tpRiskReward)
=> close * (1 + tpPercentage / 100)
calcTakeProfitShort(slPrice) =>
riskAmount = slPrice - close
switch tpType
"Percentage" => close * (1 - tpPercentage / 100)
"ATR" => close - (atrTP * tpATRMultiplier)
"Fixed Points" => close - tpFixedPoints * syminfo.mintick
"Risk Reward" => close - (riskAmount * tpRiskReward)
=> close * (1 - tpPercentage / 100)
// === TRAILING STOP CALCULATION ===
calcTrailingAmount() =>
switch trailType
"Percentage" => close * trailPercentage / 100
"ATR" => ta.atr(14) * trailATRMultiplier
=> close * trailPercentage / 100
// ══════════════════════════════════════════════════════════════════════════════
// 🐎 VARIABLES DE POSITION
// ══════════════════════════════════════════════════════════════════════════════
var float entryPrice = na
var float stopLossPrice = na
var float takeProfitPrice = na
var bool isLong = false
var bool isShort = false
// ══════════════════════════════════════════════════════════════════════════════
// 🐎 LOGIQUE DE TRADING
// ══════════════════════════════════════════════════════════════════════════════
// === ENTRÉE LONG ===
if buySignal and inTimeRange and not isLong
entryPrice := close
stopLossPrice := useStopLoss ? calcStopLossLong() : na
takeProfitPrice := useTakeProfit ? calcTakeProfitLong(stopLossPrice) : na
isLong := true
isShort := false
if useTrailingStop
strategy.entry("Long", strategy.long)
if useStopLoss and useTakeProfit
strategy.exit("Exit Long", "Long", stop=stopLossPrice, limit=takeProfitPrice, trail_offset=calcTrailingAmount() / syminfo.mintick, trail_points=calcTrailingAmount() / syminfo.mintick)
else if useStopLoss
strategy.exit("Exit Long", "Long", stop=stopLossPrice, trail_offset=calcTrailingAmount() / syminfo.mintick, trail_points=calcTrailingAmount() / syminfo.mintick)
else if useTakeProfit
strategy.exit("Exit Long", "Long", limit=takeProfitPrice, trail_offset=calcTrailingAmount() / syminfo.mintick, trail_points=calcTrailingAmount() / syminfo.mintick)
else
strategy.entry("Long", strategy.long)
if useStopLoss and useTakeProfit
strategy.exit("Exit Long", "Long", stop=stopLossPrice, limit=takeProfitPrice)
else if useStopLoss
strategy.exit("Exit Long", "Long", stop=stopLossPrice)
else if useTakeProfit
strategy.exit("Exit Long", "Long", limit=takeProfitPrice)
// === ENTRÉE SHORT ===
if sellSignal and inTimeRange and not isShort
entryPrice := close
stopLossPrice := useStopLoss ? calcStopLossShort() : na
takeProfitPrice := useTakeProfit ? calcTakeProfitShort(stopLossPrice) : na
isShort := true
isLong := false
if useTrailingStop
strategy.entry("Short", strategy.short)
if useStopLoss and useTakeProfit
strategy.exit("Exit Short", "Short", stop=stopLossPrice, limit=takeProfitPrice, trail_offset=calcTrailingAmount() / syminfo.mintick, trail_points=calcTrailingAmount() / syminfo.mintick)
else if useStopLoss
strategy.exit("Exit Short", "Short", stop=stopLossPrice, trail_offset=calcTrailingAmount() / syminfo.mintick, trail_points=calcTrailingAmount() / syminfo.mintick)
else if useTakeProfit
strategy.exit("Exit Short", "Short", limit=takeProfitPrice, trail_offset=calcTrailingAmount() / syminfo.mintick, trail_points=calcTrailingAmount() / syminfo.mintick)
else
strategy.entry("Short", strategy.short)
if useStopLoss and useTakeProfit
strategy.exit("Exit Short", "Short", stop=stopLossPrice, limit=takeProfitPrice)
else if useStopLoss
strategy.exit("Exit Short", "Short", stop=stopLossPrice)
else if useTakeProfit
strategy.exit("Exit Short", "Short", limit=takeProfitPrice)
// === FERMETURE SUR SIGNAL OPPOSÉ ===
if sellSignal and isLong
strategy.close("Long")
isLong := false
if buySignal and isShort
strategy.close("Short")
isShort := false
// ══════════════════════════════════════════════════════════════════════════════
// 🐎 AFFICHAGE - TRIANGLES SUR LES BOUGIES
// ══════════════════════════════════════════════════════════════════════════════
// === TRIANGLES D'ACHAT/VENTE ===
plotshape(showSignals and buySignal, title="Buy Triangle", style=shape.triangleup, location=location.belowbar, color=color.new(color.green, 0), size=size.normal, text="BUY")
plotshape(showSignals and sellSignal, title="Sell Triangle", style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 0), size=size.normal, text="SELL")
// === COULEUR DE FOND (trend zone) ===
bgcolor(inBullZone ? color.new(color.green, 90) : color.new(color.red, 90))
// ══════════════════════════════════════════════════════════════════════════════
// 🐎 INDICATEUR SÉPARÉ (PANNEAU INFÉRIEUR)
// ══════════════════════════════════════════════════════════════════════════════
// Pour afficher l'histogramme dans un panneau séparé, créer un indicateur séparé
// ou utiliser plot avec display=display.pane
// ══════════════════════════════════════════════════════════════════════════════
// 🐎 ALERTES
// ══════════════════════════════════════════════════════════════════════════════
alertcondition(buySignal, title="🐎 Mustang BUY", message="🐎 Mustang Algo: BUY Signal on {{ticker}} at {{close}}")
alertcondition(sellSignal, title="🐎 Mustang SELL", message="🐎 Mustang Algo: SELL Signal on {{ticker}} at {{close}}")