Strategies KB.ZH
Strategies

增强型动态通道突破趋势跟踪交易系统-Enhanced-Dynamic-Channel-Breakout-Trend-Following-Trading-System

ianzeng123 · PineScript · Trend Following · 2025-03-05 09:49:33

All strategies →

No English write-up in the source (no author description).

Read the Chinese version →

Source code (99 lines)
/*backtest
start: 2024-03-05 00:00:00
end: 2025-03-03 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"SOL_USDT"}]
*/

//@version=5
strategy("Enhanced Turtle Trading for BTC 1H", overlay=true)

// --- Adjustable Parameters ---
donchianPeriodEntry = input.int(15, "Donchian Entry Period", minval=1)
donchianPeriodExit = input.int(5, "Donchian Exit Period", minval=1)
atrPeriod = input.int(10, "ATR Period", minval=1)
capitalRisk = input.float(2.0, "Risk per Trade (%)", minval=0.1, step=0.1) / 100
volumeUnits = input.int(4, "Max Units per Position", minval=1)
smaPeriod = input.int(200, "SMA Trend Period", minval=1)
rsiPeriod = input.int(14, "RSI Period", minval=1)
rsiOverbought = input.int(70, "RSI Overbought Level", minval=0, maxval=100)
rsiOversold = input.int(30, "RSI Oversold Level", minval=0, maxval=100)
atrMultiplierStop = input.float(2.0, "ATR Multiplier for Stop", minval=0.1, step=0.1)
atrMultiplierAdd = input.float(1.0, "ATR Multiplier for Adding Units", minval=0.1, step=0.1)

// --- Calculations ---
donchianHiEntry = ta.highest(high, donchianPeriodEntry)
donchianLoEntry = ta.lowest(low, donchianPeriodEntry)
donchianHiExit = ta.highest(high, donchianPeriodExit)
donchianLoExit = ta.lowest(low, donchianPeriodExit)
atr = ta.atr(atrPeriod)
sma = ta.sma(close, smaPeriod)
rsi = ta.rsi(close, rsiPeriod)

// --- Trend Filter ---
uptrend = close > sma
downtrend = close < sma

// --- Entry Conditions with Filters ---
longEntry = high > donchianHiEntry[1] and uptrend and rsi < rsiOverbought
shortEntry = low < donchianLoEntry[1] and downtrend and rsi > rsiOversold

// --- Exit Conditions ---
longExit = low < donchianLoExit[1]
shortExit = high > donchianHiExit[1]

// --- Position Sizing ---
capitalPerUnit = strategy.equity * capitalRisk
unitsSize = math.floor(capitalPerUnit / (atr * close))

// --- Conditions for Adding Units ---
addUnitLong = strategy.position_size > 0 and strategy.position_size / unitsSize < volumeUnits and high > strategy.position_avg_price + atrMultiplierAdd * atr
addUnitShort = strategy.position_size < 0 and math.abs(strategy.position_size) / unitsSize < volumeUnits and low < strategy.position_avg_price - atrMultiplierAdd * atr

// --- Plots ---
plot(donchianHiEntry, "Donchian High Entry", color=color.new(color.green, 0))
plot(donchianLoEntry, "Donchian Low Entry", color=color.new(color.red, 0))
plot(donchianHiExit, "Donchian High Exit", color=color.new(color.lime, 50))
plot(donchianLoExit, "Donchian Low Exit", color=color.new(color.orange, 50))
plot(sma, "SMA Trend", color=color.new(color.blue, 0))

// --- Trade Management ---
// Long Entry
if (longEntry and strategy.position_size <= 0)
    strategy.close_all()
    strategy.entry("Long Entry", strategy.long, qty=unitsSize)

// Short Entry
if (shortEntry and strategy.position_size >= 0)
    strategy.close_all()
    strategy.entry("Short Entry", strategy.short, qty=unitsSize)

// Adding Units
if (addUnitLong)
    strategy.entry("Add Long", strategy.long, qty=unitsSize)
if (addUnitShort)
    strategy.entry("Add Short", strategy.short, qty=unitsSize)

// Exits
if (longExit and strategy.position_size > 0)
    strategy.close_all()
if (shortExit and strategy.position_size < 0)
    strategy.close_all()

// --- Stop Loss and Trailing Stop ---
longStopPrice = strategy.position_avg_price - atrMultiplierStop * atr
shortStopPrice = strategy.position_avg_price + atrMultiplierStop * atr

var float longTrailingStop = na
var float shortTrailingStop = na

if (strategy.position_size > 0)
    longTrailingStop := math.max(longTrailingStop[1], longStopPrice)
    strategy.exit("Long Stop", "Long Entry", stop=longTrailingStop)
    strategy.exit("Long Stop", "Add Long", stop=longTrailingStop)

if (strategy.position_size < 0)
    shortTrailingStop := math.min(shortTrailingStop[1], shortStopPrice)
    strategy.exit("Short Stop", "Short Entry", stop=shortTrailingStop)
    strategy.exit("Short Stop", "Add Short", stop=shortTrailingStop)