
이 전략은 두 개의 다른 주기의 ATR (Average True Range) 지표를 사용하여 두 개의 동적 트래킹 스톱 라인을 구축하고, 가격이 스톱 라인을 돌파 할 때 거래 신호를 발생시킨다. 동적 스톱 스톱을 실현하기 위해 스톱 라인 엔티티의 길이를 동적으로 사용하여 스톱 가격을 설정한다. 전략은 또한 EMA 지표를 결합하여 추세를 판단하는 것을 보조한다.
이 전략은 ATR 지표의 특성을 활용하여 이중 동적 스톱을 구축하여 다른 시장의 변동률에 더 잘 적응할 수 있으며 시장 변동에도 빠르게 대응할 수 있다. 동적 스톱의 설정은 전략이 트렌드 상황에서 더 많은 이익을 얻을 수 있게 한다. 종합적으로 볼 때, 이 전략은 트렌드 시장에서 더 잘 작동하지만, 흔들리는 시장에서 더 많은 손실이 발생할 수 있다.
이 전략은 이중 동적 스톱 라인 및 동적 스톱을 설계하여 다양한 시장 환경에 더 잘 적응할 수 있으며 추세 상황에서 우수한 성능을 발휘합니다. 그러나 불안정한 시장에서는 빈번한 거래 및 손해배상 문제가 발생할 수 있습니다. 따라서 이 전략은 추세 시장에서 사용하기에 더 적합하며 제품 특성과 시장 환경을 결합하여 매개 변수를 최적화하고 조정해야합니다. 또한, 전략의 안정성과 수익성을 높이기 위해 더 많은 필터링 조건, 위치 관리 및 위험과 같은 제어 모듈을 도입하는 것과 같은 추가적인 최적화의 여지가 있습니다.
/*backtest
start: 2024-02-01 00:00:00
end: 2024-02-29 23:59:59
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
strategy(title="UT Bot Strategy", overlay=true)
// Inputs
a1 = input(1, title="Key Value 1 ('This changes the sensitivity')")
c1 = input(10, title="ATR Period 1")
a2 = input(2, title="Key Value 2 ('This changes the sensitivity')")
c2 = input(20, title="ATR Period 2")
h = input(false, title="Signals from Heikin Ashi Candles")
////////////////////////////////////////////////////////////////////////////////
// BACKTESTING RANGE
// From Date Inputs
fromDay = input(defval=1, title="From Day", minval=1, maxval=31)
fromMonth = input(defval=1, title="From Month", minval=1, maxval=12)
fromYear = input(defval=2019, title="From Year", minval=1970)
// To Date Inputs
toDay = input(defval=1, title="To Day", minval=1, maxval=31)
toMonth = input(defval=1, title="To Month", minval=1, maxval=12)
toYear = input(defval=2100, title="To Year", minval=1970)
// Calculate start/end date and time condition
startDate = timestamp(fromYear, fromMonth, fromDay, 00, 00)
finishDate = timestamp(toYear, toMonth, toDay, 00, 00)
time_cond = time >= startDate and time <= finishDate
////////////////////////////////////////////////////////////////////////////////
xATR1 = atr(c1)
nLoss1 = a1 * xATR1
xATR2 = atr(c2)
nLoss2 = a2 * xATR2
src = h ? security(heikinashi(syminfo.tickerid), timeframe.period, close, lookahead=false) : close
xATRTrailingStop1 = 0.0
xATRTrailingStop1 := iff(src > nz(xATRTrailingStop1[1], 0) and src[1] > nz(xATRTrailingStop1[1], 0), max(nz(xATRTrailingStop1[1]), src - nLoss1),
iff(src < nz(xATRTrailingStop1[1], 0) and src[1] < nz(xATRTrailingStop1[1], 0), min(nz(xATRTrailingStop1[1]), src + nLoss1),
iff(src > nz(xATRTrailingStop1[1], 0), src - nLoss1, src + nLoss1)))
xATRTrailingStop2 = 0.0
xATRTrailingStop2 := iff(src > nz(xATRTrailingStop2[1], 0) and src[1] > nz(xATRTrailingStop2[1], 0), max(nz(xATRTrailingStop2[1]), src - nLoss2),
iff(src < nz(xATRTrailingStop2[1], 0) and src[1] < nz(xATRTrailingStop2[1], 0), min(nz(xATRTrailingStop2[1]), src + nLoss2),
iff(src > nz(xATRTrailingStop2[1], 0), src - nLoss2, src + nLoss2)))
pos = 0
pos := iff(src[1] < nz(xATRTrailingStop1[1], 0) and src > nz(xATRTrailingStop1[1], 0), 1,
iff(src[1] > nz(xATRTrailingStop1[1], 0) and src < nz(xATRTrailingStop1[1], 0), -1, nz(pos[1], 0)))
xcolor = pos == -1 ? color.red: pos == 1 ? color.green : color.blue
ema1 = ema(src, 1)
above1 = crossover(ema1, xATRTrailingStop1)
below1 = crossover(xATRTrailingStop1, ema1)
buy1 = src > xATRTrailingStop1 and above1
sell1 = src < xATRTrailingStop1 and below1
barbuy1 = src > xATRTrailingStop1
barsell1 = src < xATRTrailingStop1
ema2 = ema(src, 1)
above2 = crossover(ema2, xATRTrailingStop2)
below2 = crossover(xATRTrailingStop2, ema2)
buy2 = src > xATRTrailingStop2 and above2
sell2 = src < xATRTrailingStop2 and below2
barbuy2 = src > xATRTrailingStop2
barsell2 = src < xATRTrailingStop2
plotshape(buy1, title="Buy 1", text='Buy 1', style=shape.labelup, location=location.belowbar, color=color.green, textcolor=color.white, transp=0, size=size.tiny)
plotshape(sell1, title="Sell 1", text='Sell 1', style=shape.labeldown, location=location.abovebar, color=color.red, textcolor=color.white, transp=0, size=size.tiny)
plotshape(buy2, title="Buy 2", text='Buy 2', style=shape.labelup, location=location.belowbar, color=color.green, textcolor=color.white, transp=0, size=size.tiny)
plotshape(sell2, title="Sell 2", text='Sell 2', style=shape.labeldown, location=location.abovebar, color=color.red, textcolor=color.white, transp=0, size=size.tiny)
barcolor(barbuy1 ? color.green : na)
barcolor(barsell1 ? color.red : na)
barcolor(barbuy2 ? color.green : na)
barcolor(barsell2 ? color.red : na)
// Calculate SL and TP levels
candle_size = abs(open - close)
tp_level = close + candle_size *65
// Close long positions if TP is hit
strategy.exit("TP Long", "long", limit=tp_level)
// Close short positions if TP is hit
strategy.exit("TP Short", "short", limit=tp_level)
// Enter long position
strategy.entry("long", strategy.long, when=(buy1 or buy2) and time_cond)
// Enter short position
strategy.entry("short", strategy.short, when=(sell1 or sell2) and time_cond)
//adding ema with width
// Calculate EMA and SMA
ema5 = ema(close, 5)
ema200 = ema(close, 200)
ema21 = ema(close, 21)
ema50 = ema(close, 50)
sma50 = sma(close, 50)
// Plot EMA and SMA with width
plot(ema5, color=color.rgb(130, 235, 139), title="EMA 5", linewidth=1)
plot(ema200, color=color.rgb(243, 246, 249), title="EMA 200", linewidth=2)
plot(ema21, color=color.blue, title="21", linewidth=1)
plot(ema50, color=color.rgb(255, 64, 0), title="EMA 50", linewidth=2)
//plot(sma50, color=color.purple, title="SMA 20", linewidth=2)