趋势跟踪网格动态调仓量化策略

TTM EMA GRID DCA ATR SMA
创建日期: 2024-12-12 11:19:17 最后修改: 2024-12-12 11:19:17
复制: 0 点击次数: 283
avatar of ChaoZhang ChaoZhang
1
关注
1259
关注者

趋势跟踪网格动态调仓量化策略

概述

该策略是一个基于TTM指标的动态网格交易系统,通过对高低点的指数移动平均线(EMA)进行计算来判断市场趋势方向,并在动态更新的基准价格周围部署网格交易系统。网格的方向和价格水平会根据趋势动态调整,当价格穿越预定义的网格水平时执行交易,每笔交易风险敞口为账户权益的固定百分比。

策略原理

策略的核心逻辑在于TTM状态计算,它通过以下步骤实现: 1. 基于ttmPeriod参数计算两个EMA:低点EMA(lowMA)和高点EMA(highMA) 2. 在highMA和lowMA之间定义两个阈值水平: - lowThird:底部1/3位置 - highThird:底部2/3位置 3. 根据收盘价相对于这些阈值的位置确定TTM状态: - 当收盘价高于highThird时,返回1(上升趋势) - 当收盘价低于lowThird时,返回0(下降趋势) - 当收盘价在lowThird和highThird之间时,返回-1(中性状态)

网格交易系统会根据TTM状态动态调整: 1. 当TTM状态发生变化时,更新网格基准价格和方向 2. 根据网格方向和间距计算买卖价格水平 3. 在价格突破网格水平时执行相应的买入或卖出操作

策略优势

  1. 动态适应性强:策略能够根据市场趋势动态调整网格方向和价格水平,提高了策略的适应性和盈利能力
  2. 风险控制完善:采用固定百分比进行头寸管理,有效控制每笔交易的风险敞口
  3. 参数可调性好:关键参数如TTM周期、网格级别和间距都可以根据不同市场情况进行优化
  4. 执行机制清晰:交易信号明确,执行逻辑简单直观,便于回测和实盘操作

策略风险

  1. 趋势判断延迟:基于EMA的TTM指标存在一定滞后性,可能导致趋势转折点的信号延迟
  2. 震荡市场风险:在横盘震荡市场中,频繁的网格方向切换可能导致过度交易和手续费损失
  3. 资金管理压力:多个网格同时运行时需要较大资金规模,可能影响策略的实际可行性
  4. 滑点影响:高频网格交易在流动性不足时可能面临较大滑点,影响策略表现

策略优化方向

  1. 趋势判断优化:
    • 引入多时间周期分析,提高趋势判断准确性
    • 结合其他技术指标如RSI、MACD等进行趋势确认
  2. 网格参数优化:
    • 根据波动率动态调整网格间距
    • 引入自适应网格级别调整机制
  3. 资金管理改进:
    • 实现动态头寸分配
    • 增加风险平价机制
  4. 执行机制完善:
    • 增加止损和止盈机制
    • 优化订单执行时机

总结

该策略通过将TTM趋势判断与动态网格交易相结合,实现了一个自适应性强、风险可控的交易系统。通过动态调整网格方向和价格水平,策略能够较好地适应不同市场环境。虽然存在一些固有风险,但通过合理的参数设置和优化措施,策略具有良好的实用价值和发展潜力。

策略源码
/*backtest
start: 2024-12-04 00:00:00
end: 2024-12-11 00:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy("TTM Grid Strategy", overlay=true)

// Input parameters
int ttmPeriod = input.int(6, minval=1, title="TTM Period")
int gridLevels = input.int(5, minval=2, title="Grid Levels")
float gridSpacing = input.float(0.01, minval=0.0001, title="Grid Spacing (%)")

// Calculate TTM State
ttmState() =>
    lowMA = ta.ema(low, ttmPeriod)
    highMA = ta.ema(high, ttmPeriod)
    lowThird = (highMA - lowMA) / 3 + lowMA
    highThird = 2 * (highMA - lowMA) / 3 + lowMA

    if close > highThird
        1
    else if close < lowThird
        0
    else
        -1

// State tracking variables
var float gridBasePrice = 0.0
var int gridDirection = -1

// Determine grid state
updateGridState(float currentClose, int currentState) =>
    float newBasePrice = gridBasePrice
    int newDirection = gridDirection

    if currentState != -1 and currentState != gridDirection
        newBasePrice := currentClose
        newDirection := currentState
    
    [newBasePrice, newDirection]

// Calculate grid levels
calcGridLevels(float basePrice, int direction, int levels) =>
    float[] buyLevels = array.new_float(levels)
    float[] sellLevels = array.new_float(levels)

    for i = 1 to levels
        multiplier = i * gridSpacing
        if direction == 1  // Buy grid
            array.set(buyLevels, i-1, basePrice * (1 - multiplier))
            array.set(sellLevels, i-1, basePrice * (1 + multiplier))
        else  // Sell grid
            array.set(buyLevels, i-1, basePrice * (1 + multiplier))
            array.set(sellLevels, i-1, basePrice * (1 - multiplier))
    
    [buyLevels, sellLevels]

// Execute grid trades
executeGridTrades(float basePrice, int direction, int levels) =>
    [buyLevels, sellLevels] = calcGridLevels(basePrice, direction, levels)

    for i = 0 to levels - 1
        float buyLevel = array.get(buyLevels, i)
        float sellLevel = array.get(sellLevels, i)

        if direction == 1  // Buy grid
            if low <= buyLevel
                strategy.entry("GridBuy" + str.tostring(i), strategy.long, comment="Buy Level " + str.tostring(i))
            if high >= sellLevel
                strategy.entry("GridSell" + str.tostring(i), strategy.short, comment="Sell Level " + str.tostring(i))
        else  // Sell grid
            if high >= buyLevel
                strategy.entry("GridBuy" + str.tostring(i), strategy.long, comment="Buy Level " + str.tostring(i))
            if low <= sellLevel
                strategy.entry("GridSell" + str.tostring(i), strategy.short, comment="Sell Level " + str.tostring(i))

// Main strategy logic
currentState = ttmState()
[newGridBasePrice, newGridDirection] = updateGridState(close, currentState)

// Update global variables
if newGridBasePrice != gridBasePrice
    gridBasePrice := newGridBasePrice
if newGridDirection != gridDirection
    gridDirection := newGridDirection

// Execute grid trades
executeGridTrades(newGridBasePrice, newGridDirection, gridLevels)

// Visualization
plotColor = newGridDirection == 1 ? color.green : color.red
plot(newGridBasePrice, color=plotColor, style=plot.style_cross)
相关推荐