这是一个基于ATR(平均真实范围)指标的趋势跟踪策略,结合了动态止损和均线交叉信号。该策略通过计算ATR来确定市场波动性,并利用这个信息建立动态的跟踪止损线。当价格与EMA(指数移动平均线)突破ATR跟踪止损线时,产生交易信号。策略还提供了使用普通K线或平安K线进行计算的选项,增加了策略的灵活性。
策略的核心逻辑基于以下几个关键计算: 1. 使用ATR指标测量市场波动性,周期可调 2. 基于ATR值计算动态止损距离,通过敏感度参数a进行调整 3. 构建ATR跟踪止损线,该线随着价格移动而动态调整 4. 使用1周期EMA与ATR跟踪止损线的交叉来确定交易信号 5. 当EMA向上突破ATR跟踪止损线时开多,向下突破时开空 6. 可选择使用普通收盘价或平安K线的HLC3价格作为计算基准
这是一个结合了动态跟踪止损和均线系统的完整交易策略。通过ATR指标捕捉市场波动特征,利用均线交叉提供交易信号,形成了一个逻辑严密的交易系统。策略的优势在于其动态适应性和风险控制能力,但也需要注意在横盘市场的表现。通过建议的优化方向,策略还有进一步提升的空间。
/*backtest
start: 2024-05-15 00:00:00
end: 2024-08-08 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Binance","currency":"ETH_USDT"}]
*/
//@version=6
strategy(title="UT Bot Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// Inputs
a = input.float(1, title="Key Value. 'This changes the sensitivity'")
c = input.int(10, title="ATR Period")
h = input.bool(false, title="Signals from Heikin Ashi Candles")
// Calculate ATR
xATR = ta.atr(c)
nLoss = a * xATR
// Source for calculations
src = h ? request.security(syminfo.tickerid, timeframe.period, hlc3) : close
// ATR Trailing Stop logic
var float xATRTrailingStop = na
if (not na(xATRTrailingStop[1]) and src > xATRTrailingStop[1] and src[1] > xATRTrailingStop[1])
xATRTrailingStop := math.max(xATRTrailingStop[1], src - nLoss)
else if (not na(xATRTrailingStop[1]) and src < xATRTrailingStop[1] and src[1] < xATRTrailingStop[1])
xATRTrailingStop := math.min(xATRTrailingStop[1], src + nLoss)
else
xATRTrailingStop := src > xATRTrailingStop[1] ? src - nLoss : src + nLoss
// Position logic
var int pos = 0
if (not na(xATRTrailingStop[1]) and src[1] < xATRTrailingStop[1] and src > xATRTrailingStop[1])
pos := 1
else if (not na(xATRTrailingStop[1]) and src[1] > xATRTrailingStop[1] and src < xATRTrailingStop[1])
pos := -1
else
pos := pos[1]
xcolor = pos == -1 ? color.red : pos == 1 ? color.green : color.blue
// Entry and Exit Signals
ema = ta.ema(src, 1)
above = ta.crossover(ema, xATRTrailingStop)
below = ta.crossover(xATRTrailingStop, ema)
buy = src > xATRTrailingStop and above
sell = src < xATRTrailingStop and below
// Strategy Execution
if (buy)
strategy.entry("UT Long", strategy.long)
if (sell)
strategy.entry("UT Short", strategy.short)
// Plotting and Alerts
plotshape(buy, title="Buy", text='Buy', style=shape.labelup, location=location.belowbar, color=color.green, textcolor=color.white, size=size.tiny)
plotshape(sell, title="Sell", text='Sell', style=shape.labeldown, location=location.abovebar, color=color.red, textcolor=color.white, size=size.tiny)
barcolor(src > xATRTrailingStop ? color.green : src < xATRTrailingStop ? color.red : na)
alertcondition(buy, title="UT Long", message="UT Long")
alertcondition(sell, title="UT Short", message="UT Short")