该策略是一个基于ATR(平均真实波幅)指标的动态跟踪止损策略。它通过ATR值动态调整止损位置,并结合EMA均线进行交易信号的确认。策略支持灵活的仓位管理,可以根据不同的市场环境和交易品种自定义买卖数量。它特别适合在5分钟到2小时等中等时间周期上运行,能够有效捕捉市场趋势。
策略的核心逻辑基于以下几个关键要素: 1. 使用ATR指标计算市场波动率,并通过用户自定义的系数来调整止损距离 2. 建立动态跟踪止损线,该止损线会随着价格的变动而自动调整 3. 使用EMA均线与跟踪止损线的交叉来确认交易信号 4. 在价格突破跟踪止损线且EMA确认时产生交易信号 5. 通过仓位管理系统控制每次交易的数量,并实时跟踪投资组合状态
该策略通过结合ATR指标和EMA均线,构建了一个可靠的动态跟踪止损系统。它的优势在于能够自适应市场波动,具有完善的风险管理机制,同时保持操作的灵活性。虽然存在一些固有风险,但通过持续优化和完善,策略有望在不同市场环境下都能保持稳定的表现。建议交易者在实盘使用前,充分测试参数组合,并根据具体交易品种的特点进行针对性优化。
/*backtest
start: 2019-12-23 08:00:00
end: 2024-12-10 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy(title='ADET GİRMELİ Trend İz Süren Stop Strategy', overlay=true, overlay=true,default_qty_type = strategy.fixed, default_qty_value = 1)
// Inputs
a = input(9, title='Key Value. "This changes the sensitivity"')
c = input(3, title='ATR Period')
h = input(false, title='Signals from Heikin Ashi Candles')
xATR = ta.atr(c)
nLoss = a * xATR
src = h ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close, lookahead=barmerge.lookahead_off) : close
xATRTrailingStop = 0.0
iff_1 = src > nz(xATRTrailingStop[1], 0) ? src - nLoss : src + nLoss
iff_2 = src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0) ? math.min(nz(xATRTrailingStop[1]), src + nLoss) : iff_1
xATRTrailingStop := src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0) ? math.max(nz(xATRTrailingStop[1]), src - nLoss) : iff_2
pos = 0
iff_3 = src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0) ? -1 : nz(pos[1], 0)
pos := src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0) ? 1 : iff_3
xcolor = pos == -1 ? color.red : pos == 1 ? color.green : color.blue
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
barbuy = src > xATRTrailingStop
barsell = src < xATRTrailingStop
// Alım ve Satım Sinyalleri
buySignal = src > xATRTrailingStop and above
sellSignal = src < xATRTrailingStop and below
// Kullanıcı girişi
sell_quantity = input.int(1, title="Sell Quantity", minval=1)
buy_quantity = input.int(1, title="Buy Quantity", minval=1)
// Portföy miktarı (örnek simülasyon verisi)
var portfolio_quantity = 0
// Sinyal üretimi (örnek sinyal, gerçek stratejinizle değiştirin)
indicator_signal = (src > xATRTrailingStop and above) ? "buy" :
(src < xATRTrailingStop and below) ? "sell" : "hold"
// Şartlara göre al/sat
if indicator_signal == "buy" and portfolio_quantity < buy_quantity
strategy.entry("Buy Order", strategy.long, qty=buy_quantity)
portfolio_quantity := portfolio_quantity + buy_quantity
if indicator_signal == "sell" and portfolio_quantity >= sell_quantity
strategy.close("Buy Order", qty=sell_quantity)
portfolio_quantity := portfolio_quantity - sell_quantity
// Plot buy and sell signals
plotshape(buy, title='Buy', text='Buy', style=shape.labelup, location=location.belowbar, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), size=size.tiny)
plotshape(sell, title='Sell', text='Sell', style=shape.labeldown, location=location.abovebar, color=color.new(color.red, 0), textcolor=color.new(color.white, 0), size=size.tiny)
// Bar coloring
barcolor(barbuy ? color.rgb(6, 250, 14) : na)
barcolor(barsell ? color.red : na)
// Alerts
alertcondition(buy, 'UT Long', 'UT Long')
alertcondition(sell, 'UT Short', 'UT Short')
// Strategy Entry and Exit
if buy
strategy.entry('Long', strategy.long)
if sell
strategy.entry('Short', strategy.short)
// Optional Exit Conditions
if sell
strategy.close('Long')
if buy
strategy.close('Short')