该策略使用平均真实波幅(ATR)作为跟踪止损(TS)的依据,通过动态调整止损位置来实现追踪趋势的目的。当价格向有利方向移动时,止损位置也会随之调整,从而锁定已获得的利润;当价格向不利方向移动时,止损位置保持不变,一旦价格触及止损价格,则平仓止损。该策略的关键在于止损位置的动态调整,既可以保护已获利润,又能让利润随趋势延续而不断扩大。
ATR跟踪止损策略能够根据价格波动幅度动态调整止损位置,在趋势行情中可以取得不错的效果。但是,该策略也存在无法应对震荡市、止损过于频繁以及难以避免跳空缺口等风险。针对上述缺陷,可以从趋势判断、止盈策略、最大止损限制等方面对策略进行优化和改进。通过这些调整,有望增强策略的适应性和盈利能力。
/*backtest start: 2024-04-01 00:00:00 end: 2024-04-30 23:59:59 period: 1h basePeriod: 15m exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=5 strategy("Long TAP", overlay=true) // Constants keyValueDefault = 3.0 keyValueStep = 0.5 atrPeriodDefault = 10 // Inputs keyValue = input.float(keyValueDefault, title="Key Value") atrPeriod = input.int(atrPeriodDefault, title="ATR Period") // Calculations xATR = ta.atr(atrPeriod) nLoss = keyValue * xATR // Trailing Stop Calculation var float xATRTrailingStop = 0.0 xATRTrailingStop := ta.highest(math.max(nz(xATRTrailingStop[1], 0), close - nLoss), 1) xATRTrailingStop := ta.lowest(math.min(nz(xATRTrailingStop, 0), close + nLoss), 1) // Position Calculation var int pos = 0 pos := nz(pos[1], 0) if (close[1] < nz(xATRTrailingStop, 0) and close > nz(xATRTrailingStop, 0)) pos := 1 else if (close[1] > nz(xATRTrailingStop, 0) and close < nz(xATRTrailingStop, 0)) pos := -1 // Plotting Trailing Stop var color xcolor = na if (pos == -1) xcolor := color.red else if (pos == 1) xcolor := color.green plot(xATRTrailingStop, color=xcolor, title="Trailing Stop") // Buy/Sell Signals buySignal = ta.crossover(close, xATRTrailingStop) sellSignal = ta.crossunder(close, xATRTrailingStop) // Strategy if (buySignal) strategy.entry("Long", strategy.long) label.new(bar_index, xATRTrailingStop, text="Buy Signal", color=color.green, style=label.style_label_up, yloc=yloc.belowbar) if (sellSignal) strategy.entry("Short", strategy.short) label.new(bar_index, xATRTrailingStop, text="Sell Signal", color=color.red, style=label.style_label_down, yloc=yloc.abovebar) // Alerts alertcondition(buySignal, title='UT BOT Buy', message='UT BOT Buy') alertcondition(sellSignal, title='UT BOT Sell', message='UT BOT Sell')