K-algo Trail Strategy

ATR supertrend GANN HEIKIN-ASHI EMA
Created on: 2025-08-26 11:23:33 Modified on: 2025-08-26 11:23:33
Copy: 0 Number of hits: 288
avatar of ianzeng123 ianzeng123
2
Follow
319
Followers

K-algo Trail Strategy K-algo Trail Strategy

This Isn’t Your Average SuperTrend - It’s a Multi-Dimensional Trend Hunter

Don’t be fooled by the name. K-algo trail is far from a simple ATR tracking strategy. This system brilliantly fuses SuperTrend, Gann Square of 9, and smoothed Heikin Ashi into a three-dimensional trend identification framework. The 10-period ATR with 3x multiplier design ensures trend sensitivity while effectively filtering market noise.

Double EMA-Smoothed Heikin Ashi: The Real Signal Filter

The strategy’s core innovation lies in its double 11-period EMA smoothed Heikin Ashi candles. Traditional Heikin Ashi generates false signals, but after two rounds of EMA smoothing, signal quality improves dramatically. Long signals confirm when smoothed open < close AND SuperTrend shows uptrend; shorts trigger on the opposite. This dual confirmation mechanism significantly reduces false trades.

1.7:2.5:3.0 Risk-Reward Ratio Shows Professional Design

Stop losses use SuperTrend lines directly - the most logical dynamic stop approach. The real brilliance is the three-tier profit taking: 1.7x, 2.5x, 3.0x risk distance. This progressive profit-taking secures base returns while leaving room for trending moves. Historical backtests show this ratio configuration achieves positive expectancy across most market environments.

Gann Square of 9 Isn’t Decoration - It’s Key Support/Resistance

The Gann Square of 9 calculation appears simple but serves a crucial purpose. By calculating support/resistance levels through current price square roots, it provides additional price anchors. While the main strategy logic doesn’t directly use these levels, they offer vital references for manual adjustments and risk assessment.

Perfect for Medium-Term Trends, Mediocre in Choppy Markets

This strategy excels in trending markets, especially high-volatility instruments like cryptocurrencies and stock index futures. But let’s be clear: in sideways choppy conditions, frequent false breakouts lead to consecutive small losses. Recommended for high-volatility, trending periods. Avoid during uncertain times around major economic releases.

Risk Warning: Historical Backtests Don’t Guarantee Future Returns

Every quantitative strategy carries loss risks, and this one is no exception. While backtests show solid risk-adjusted returns, actual trading may still face consecutive losses. Strictly limit single positions to 2% of total capital and pause trading after 3 consecutive stop-outs to reassess market conditions. Strategy effectiveness heavily depends on market trending behavior - use cautiously in directionless markets.

Strategy source code
/*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))