
别被名字骗了,K-algo trail绝不是简单的ATR跟踪策略。这套系统巧妙融合了SuperTrend、Gann九方图、平滑Heikin Ashi三大技术体系,形成了一个立体化的趋势识别框架。10周期ATR配合3倍数乘子的设计,既保证了对趋势的敏感性,又有效过滤了市场噪音。
策略的核心创新在于双重11周期EMA平滑处理的Heikin Ashi蜡烛图。传统Heikin Ashi容易产生假信号,但经过两轮EMA平滑后,信号质量显著提升。当平滑后的开盘价低于收盘价且SuperTrend显示上升趋势时,多头信号确认;反之则为空头信号。这种双重确认机制大幅降低了错误交易的概率。
止损设置直接采用SuperTrend线位,这是最合理的动态止损方案。更精彩的是三档止盈设计:1.7倍、2.5倍、3.0倍风险距离。这种渐进式止盈既保证了基础收益,又给趋势行情留足了空间。历史回测显示,这个比例配置在大多数市场环境下都能实现正期望收益。
代码中的Gann Square of 9计算看似简单,实际作用巨大。通过当前价格的平方根计算上下支撑阻力位,为策略提供了额外的价格锚点。虽然策略主逻辑没有直接使用这些位置,但它们为手动调整和风险评估提供了重要参考。
这套策略在单边趋势市场中表现优异,特别是加密货币和股指期货等波动性较大的品种。但必须明确:在横盘震荡行情中,频繁的假突破会导致连续小额亏损。建议在市场波动率较高、趋势性较强的时段使用,避免在重要经济数据发布前后的不确定时期操作。
任何量化策略都存在亏损风险,该策略也不例外。虽然回测数据显示风险调整后收益表现良好,但实际交易中仍可能面临连续亏损。建议严格控制单笔仓位不超过总资金的2%,并在连续3次止损后暂停交易,重新评估市场环境。策略的有效性高度依赖市场趋势性,在缺乏明确方向的市场中应谨慎使用。
/*backtest
start: 2025-06-11 00:00:00
end: 2025-08-25 00:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"ETH_USDT"}]
*/
//@version=5
strategy('K-algo trail', overlay=true)
// ===== INPUTS =====
Periods = input(title='ATR Period', defval=10)
src = input(hl2, title='Source')
Multiplier = input.float(title='ATR Multiplier', step=0.1, defval=3)
changeATR = input(title='Change ATR Calculation Method ?', defval=true)
showsignals = input(title='Show Buy/Sell Signals ?', defval=true)
// ===== ATR & SUPER TREND (K-TREND) CALCULATION =====
atr2 = ta.sma(ta.tr, Periods)
atr = changeATR ? ta.atr(Periods) : atr2
up = src - Multiplier * atr
up1 = nz(up[1], up)
up := close[1] > up1 ? math.max(up, up1) : up
dn = src + Multiplier * atr
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? math.min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
// Plot SuperTrend
upPlot = plot(trend == 1 ? up : na, title='Up Trend', style=plot.style_linebr, linewidth=2, color=color.new(color.green, 0))
buySignal = trend == 1 and trend[1] == -1
plotshape(buySignal ? up : na, title='UpTrend Begins', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.green, 0))
dnPlot = plot(trend == 1 ? na : dn, title='Down Trend', style=plot.style_linebr, linewidth=2, color=color.new(color.red, 0))
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal ? dn : na, title='DownTrend Begins', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.red, 0))
// ===== GANN SQUARE OF 9 =====
normalise_squareRootCurrentClose = math.floor(math.sqrt(close))
upperGannLevel_1 = (normalise_squareRootCurrentClose + 1) * (normalise_squareRootCurrentClose + 1)
upperGannLevel_2 = (normalise_squareRootCurrentClose + 2) * (normalise_squareRootCurrentClose + 2)
zeroGannLevel = normalise_squareRootCurrentClose * normalise_squareRootCurrentClose
lowerGannLevel_1 = (normalise_squareRootCurrentClose - 1) * (normalise_squareRootCurrentClose - 1)
lowerGannLevel_2 = (normalise_squareRootCurrentClose - 2) * (normalise_squareRootCurrentClose - 2)
// ===== SMOOTHED HEIKIN ASHI CALCULATION =====
ma1_len = input.int(title='MA1', defval=11, minval=1, maxval=100, step=1)
ma2_len = input.int(title='MA2', defval=11, minval=1, maxval=100, step=1)
// First Smoothing (11,11)
o = ta.ema(open, ma1_len)
c = ta.ema(close, ma1_len)
h = ta.ema(high, ma1_len)
l = ta.ema(low, ma1_len)
ha_t = ticker.heikinashi(syminfo.tickerid)
ha_o = request.security(ha_t, timeframe.period, o)
ha_c = request.security(ha_t, timeframe.period, c)
ha_h = request.security(ha_t, timeframe.period, h)
ha_l = request.security(ha_t, timeframe.period, l)
o2 = ta.ema(ha_o, ma2_len)
c2 = ta.ema(ha_c, ma2_len)
h2 = ta.ema(ha_h, ma2_len)
l2 = ta.ema(ha_l, ma2_len)
ha_col = o2 > c2 ? color.orange : color.blue
plotcandle(o2, h2, l2, c2, title='Heikin Ashi Smoothed', color=ha_col, wickcolor=#00000000)
// ===== STRATEGY LOGIC =====
// Final Combined Long Condition
longCondition = (o2 < c2 and trend == 1) and barstate.isconfirmed
// Final Combined Short Condition
shortCondition = (o2 > c2 and trend == -1) and barstate.isconfirmed
// ===== STRATEGY EXECUTION =====
if longCondition
SL = math.round(up, 2)
range_1 = math.abs(close - SL)
TARGET1 = close + range_1 * 1.7
TARGET2 = close + range_1 * 2.5
TARGET3 = close + range_1 * 3.0
strategy.entry('BUY', strategy.long)
strategy.exit('BUY T1', 'BUY', qty=1, limit=TARGET1)
strategy.exit('BUY T2', 'BUY', qty=1, limit=TARGET2)
strategy.exit('BUY T3', 'BUY', qty=1, limit=TARGET3)
strategy.exit('BUY SL', 'BUY', stop=SL)
if shortCondition
SL = math.round(dn, 2)
range_2 = math.abs(close - SL)
TARGET1 = close - range_2 * 1.7
TARGET2 = close - range_2 * 2.5
TARGET3 = close - range_2 * 3.0
strategy.entry('SELL', strategy.short)
strategy.exit('SELL T1', 'SELL', qty=1, limit=TARGET1)
strategy.exit('SELL T2', 'SELL', qty=1, limit=TARGET2)
strategy.exit('SELL T3', 'SELL', qty=1, limit=TARGET3)
strategy.exit('SELL SL', 'SELL', stop=SL)
// Plot entry signals
plotshape(longCondition ? close : na, title='Buy', text='BUY', location=location.belowbar, style=shape.labelup, size=size.tiny, color=color.new(color.green, 0), textcolor=color.new(color.white, 0))
plotshape(shortCondition ? close : na, title='Sell', text='SELL', location=location.abovebar, style=shape.labeldown, size=size.tiny, color=color.new(color.red, 0), textcolor=color.new(color.white, 0))