动态跟踪止损的三分之一K线量化交易策略

TRINITY ATR
创建日期: 2025-02-18 13:57:33 最后修改: 2025-02-18 13:57:33
复制: 0 点击次数: 54
1
关注
1196
关注者

动态跟踪止损的三分之一K线量化交易策略

概述

这是一个结合了比尔·威廉姆斯(Bill Williams)三分之一K线分析方法和动态跟踪止损功能的量化交易策略。该策略通过分析当前和前一根K线的结构特征来产生明确的多空信号,并利用可配置的跟踪止损机制来保护持仓,实现了精确的入场/出场和风险管理。

策略原理

策略的核心逻辑基于以下几个关键部分: 1. K线三等分计算: 将每根K线的范围(最高价-最低价)分成三等份,得到上层区域和下层区域的边界值。 2. K线形态分类: 根据开盘价和收盘价在三等分区域的位置,将K线分为多种类型。例如,当开盘价在下层区域而收盘价在上层区域时,被认为是强势上涨形态。 3. 信号生成规则: 通过组合分析当前K线和前一根K线的形态,确定有效的交易信号。例如,当连续两根K线都显示强势特征时,触发做多信号。 4. 动态跟踪止损: 在指定的时间周期内,使用前N根K线的最低价(多头)或最高价(空头)作为移动止损点位。

策略优势

  1. 逻辑清晰性: 策略使用直观的K线结构分析方法,交易规则明确且易于理解。
  2. 风险管理完善: 通过动态跟踪止损机制,能够在保留足够盈利空间的同时,有效控制回撤风险。
  3. 适应性强: 策略可以根据不同的市场环境调整跟踪止损参数,具有良好的适应性。
  4. 自动化程度高: 从信号生成到仓位管理都实现了完全自动化,减少了人为干预。

策略风险

  1. 震荡市场风险: 在横盘震荡行情下,可能会产生频繁的假突破信号,导致过度交易。
  2. 跳空风险: 在出现大幅跳空时,跟踪止损可能无法及时触发,造成超预期损失。
  3. 参数敏感性: 跟踪止损的参数选择对策略表现影响较大,不当的参数设置可能导致过早出场或者保护不足。

策略优化方向

  1. 增加市场环境过滤: 可以引入趋势指标或波动率指标,在不同市场环境下动态调整策略参数。
  2. 优化止损机制: 可以考虑结合ATR指标来设置更灵活的止损距离,提高止损的适应性。
  3. 引入仓位管理: 可以根据信号强度和市场波动性动态调整仓位大小,实现更精细的风险控制。
  4. 增加出场优化: 可以添加利润目标或者技术指标辅助判断,优化出场时机。

总结

这是一个结构完整、逻辑清晰的量化交易策略,通过组合使用经典的技术分析方法和现代风险管理技术,具有较好的实用性。策略的设计充分考虑了实盘交易的需求,包括信号生成、持仓管理和风险控制等关键环节。通过进一步优化和完善,该策略有望在实际交易中取得更好的表现。

策略源码
/*backtest
start: 2024-02-18 00:00:00
end: 2025-02-16 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy("TrinityBar with Trailing Stop", overlay=true, initial_capital=100000, 
     default_qty_type=strategy.percent_of_equity, default_qty_value=250)

//─────────────────────────────────────────────────────────────
// 1. BAR THIRDS CALCULATIONS
//─────────────────────────────────────────────────────────────
cur_range      = high - low
cur_lowerThird = low + cur_range / 3
cur_upperThird = high - cur_range / 3

prev_range      = high[1] - low[1]
prev_lowerThird = low[1] + prev_range / 3
prev_upperThird = high[1] - prev_range / 3

//─────────────────────────────────────────────────────────────
// 2. DEFINE BULLISH & BEARISH BAR TYPES (CURRENT & PREVIOUS)
//─────────────────────────────────────────────────────────────
// Current bar types
is_1_3 = (open <= cur_lowerThird) and (close >= cur_upperThird)
is_3_3 = (open >= cur_upperThird) and (close >= cur_upperThird)
is_2_3 = (open > cur_lowerThird) and (open < cur_upperThird) and (close >= cur_upperThird)

is_3_1 = (open >= cur_upperThird) and (close <= cur_lowerThird)
is_1_1 = (open <= cur_lowerThird) and (close <= cur_lowerThird)
is_2_1 = (open > cur_lowerThird) and (open < cur_upperThird) and (close <= cur_lowerThird)

// Previous bar types
prev_is_1_3 = (open[1] <= prev_lowerThird) and (close[1] >= prev_upperThird)
prev_is_3_3 = (open[1] >= prev_upperThird) and (close[1] >= prev_upperThird)
prev_is_2_3 = (open[1] > prev_lowerThird) and (open[1] < prev_upperThird) and (close[1] >= prev_upperThird)

prev_is_3_1 = (open[1] >= prev_upperThird) and (close[1] <= prev_lowerThird)
prev_is_1_1 = (open[1] <= prev_lowerThird) and (close[1] <= prev_lowerThird)
prev_is_2_1 = (open[1] > prev_lowerThird) and (open[1] < prev_upperThird) and (close[1] <= prev_lowerThird)

//─────────────────────────────────────────────────────────────
// 3. VALID SIGNAL CONDITIONS
//─────────────────────────────────────────────────────────────
validBuy  = (prev_is_2_3 or prev_is_3_3 or prev_is_1_3) and (is_1_3 or is_3_3)
validSell = (prev_is_2_1 or prev_is_1_1 or prev_is_3_1) and (is_1_1 or is_3_1)

//─────────────────────────────────────────────────────────────
// 4. PLOT SIGNAL TRIANGLES
//─────────────────────────────────────────────────────────────
plotshape(validBuy, title="Valid Buy", style=shape.triangleup, location=location.belowbar, 
     color=color.green, size=size.small, text="B")
plotshape(validSell, title="Valid Sell", style=shape.triangledown, location=location.abovebar, 
     color=color.red, size=size.small, text="S")

//─────────────────────────────────────────────────────────────
// 5. MARKET ORDER EXECUTION BASED ON SIGNALS
//─────────────────────────────────────────────────────────────
if validBuy
    // Close any short positions.
    strategy.close("Short", comment="")
    // If not already long, enter a market long.
    if strategy.position_size <= 0
        strategy.entry("Long", strategy.long, comment="")
        
if validSell
    // Close any long positions.
    strategy.close("Long", comment="")
    // If not already short, enter a market short.
    if strategy.position_size >= 0
        strategy.entry("Short", strategy.short, comment="")

//─────────────────────────────────────────────────────────────
// 6. TRAILING STOP LOSS FUNCTION
//─────────────────────────────────────────────────────────────
// Inputs for trailing stop settings:
trailBars = input.int(title="Trailing Stop Bars Back", defval=1, minval=1)
trailTF   = input.timeframe(title="Trailing Stop Timeframe", defval="")  // "" = current timeframe

// For long positions, use the low from 'trailBars' bars back on the specified timeframe.
// For short positions, use the high from 'trailBars' bars back.
trailStopLong  = request.security(syminfo.tickerid, trailTF, low[trailBars])
trailStopShort = request.security(syminfo.tickerid, trailTF, high[trailBars])

// Apply trailing stops if a position is open.
if strategy.position_size > 0
    strategy.exit("Trailing Stop Long", from_entry="Long", stop=trailStopLong)
if strategy.position_size < 0
    strategy.exit("Trailing Stop Short", from_entry="Short", stop=trailStopShort)
相关推荐
更多内容