이 전략은 自适应ATR追踪止损策略 (自适应ATR追踪止损策略) 이라고 불린다. 이 전략은 ATR 지표를 사용하여 스톱포트를 설정하고, 입문 후 스톱포드를 단단한 것에서 느슨하게 전환하여, 트렌드 운행을 추적하면서도 위험을 제어하는 것을 목적으로 한다.
전략의 구체적인 논리는 다음과 같습니다.
일정 주기 동안의 최고 가격과 최저 가격 범위를 계산하여, 가격이 범위를 돌파할 때 출입 신호를 발생시킨다.
입문 후, 처음에는 더 긴밀한 ATR 상실을 적용하고, 상한점은 ATR 값의 1.5배로 고정한다. 이것은 입문 후의 상실을 제한할 수 있다.
포지션 단계에서는 4배로 더 느슨한 ATR 값으로 스톱로스를 전환한다. 스톱로스는 가격운동을 계속 추적하지만, 더 큰 공간이 수요의 지속을 허용한다.
최종 중단 지점은 항상 최저 가격 ((다중요금) 또는 최고 가격 ((무료요금) 을 추적하며, 가격 변동에 따라 조정하여 추적 중지 효과를 달성한다.
가격이 스톱로스 포인트를 넘으면 스톱로스를 치르거나 스톱로스를 넘으면 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르거나 스톱로스를 치르면 스톱로스를 치르거나 스톱로스를 치르거나
이 전략의 장점은, 적응된 손해 제도를 채택함으로써, 위험 통제를 보장하고, 조기 중단을 피한다는 것이다. 그러나 ATR 파라미트와 곱수는 최적화되어야 하며, 트렌드 판단에 부합하는 손해 중지 전략을 사용한다.
전반적으로, 동적 추적 스톱은 전략의 수익률을 높이는 중요한 수단이다. 스톱을 유연하게 사용하면 트렌드 수익률을 더 잘 유지하고 위험을 제어 할 수 있습니다.
/*backtest
start: 2023-08-13 00:00:00
end: 2023-09-12 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=4
//@author=Takazudo
strategy("ATR trailing SL tight to slack [Takazudo]",
overlay=true,
default_qty_type=strategy.fixed,
initial_capital=0,
currency=currency.USD)
posSize = strategy.position_size
hasNoPos = posSize == 0
hasLongPos = posSize > 0
hasShortPos = posSize < 0
//============================================================================
// consts, inputs
//============================================================================
// colors
var COLOR_SL_LINE = color.new(#e0f64d, 20)
var COLOR_SL_LINE_THIN = color.new(#e0f64d, 90)
var COLOR_ENTRY_BAND = color.new(#43A6F5, 30)
var COLOR_TRANSPARENT = color.new(#000000, 100)
// Entry strategy
_g1 = 'Entry strategy'
var config_entryBandBars = input(defval = 100, title = "Entry band bar count", minval=1, group=_g1)
_g2 = 'ATR SL'
var config_slAtr_length = input(24, title = "Trailing stop ATR Length", group=_g2)
var config_slAtr_multi1 = input(1.5, title = "Trailing stop ATR Multiple on tight", type=input.float, step=0.1, group=_g2)
var config_slAtr_multi2 = input(4, title = "Trailing stop ATR Multiple on slack", type=input.float, step=0.1, group=_g2)
_g3 = 'Backtesting range'
var config_fromYear = input(defval = 2016, title = "From Year", minval = 1970, group=_g3)
var config_fromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12, group=_g3)
var config_fromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31, group=_g3)
var config_toYear = input(defval = 2021, title = "To Year", minval = 1970, group=_g3)
var config_toMonth = input(defval = 4, title = "To Month", minval = 1, maxval = 12, group=_g3)
var config_toDay = input(defval = 5, title = "To Day", minval = 1, maxval = 31, group=_g3)
//============================================================================
// Range Edge calculation
//============================================================================
f_calcEntryBand_high() =>
_highest = max(open[3], close[3])
for i = 4 to (config_entryBandBars - 1)
_highest := max(_highest, open[i], close[i])
_highest
f_calcEntryBand_low() =>
_lowest = min(open[3], close[3])
for i = 4 to (config_entryBandBars - 1)
_lowest := min(_lowest, open[i], close[i])
_lowest
entryBand_high = f_calcEntryBand_high()
entryBand_low = f_calcEntryBand_low()
entryBand_height = entryBand_high - entryBand_low
plot(entryBand_high, color=COLOR_ENTRY_BAND, linewidth=1)
plot(entryBand_low, color=COLOR_ENTRY_BAND, linewidth=1)
rangeBreakDetected_long = entryBand_high < close
rangeBreakDetected_short = entryBand_low > close
shouldMakeEntryLong = (strategy.position_size == 0) and rangeBreakDetected_long
shouldMakeEntryShort = (strategy.position_size == 0) and rangeBreakDetected_short
//============================================================================
// ATR based stuff
//============================================================================
sl_atrHeight_tight = atr(config_slAtr_length) * config_slAtr_multi1
sl_atrHeight_slack = atr(config_slAtr_length) * config_slAtr_multi2
sl_tight_bull = min(open, close) - sl_atrHeight_tight
sl_tight_bear = max(open, close) + sl_atrHeight_tight
sl_slack_bull = min(open, close) - sl_atrHeight_slack
sl_slack_bear = max(open, close) + sl_atrHeight_slack
plot(sl_tight_bull, color=COLOR_SL_LINE_THIN, transp=0, linewidth=1)
plot(sl_tight_bear, color=COLOR_SL_LINE_THIN, transp=0, linewidth=1)
plot(sl_slack_bull, color=COLOR_SL_LINE_THIN, transp=0, linewidth=1)
plot(sl_slack_bear, color=COLOR_SL_LINE_THIN, transp=0, linewidth=1)
//============================================================================
// Sl
//============================================================================
var trailingSl_long = hl2
var trailingSl_short = hl2
trailingSl_long := if hasLongPos
max(trailingSl_long, sl_slack_bull)
else
sl_tight_bull
trailingSl_short := if hasShortPos
min(trailingSl_short, sl_slack_bear)
else
sl_tight_bear
color_sl_long = hasLongPos ? COLOR_SL_LINE : COLOR_TRANSPARENT
color_sl_short = hasShortPos ? COLOR_SL_LINE : COLOR_TRANSPARENT
plot(trailingSl_long, color=color_sl_long, transp=0, linewidth=2)
plot(trailingSl_short, color=color_sl_short, transp=0, linewidth=2)
//============================================================================
// make entries
//============================================================================
// Calculate start/end date and time condition
startDate = timestamp(config_fromYear, config_fromMonth, config_fromDay, 00, 00)
finishDate = timestamp(config_toYear, config_toMonth, config_toDay, 00, 00)
if (true)
if shouldMakeEntryLong
strategy.entry(id="Long", long=true, stop=close)
if shouldMakeEntryShort
strategy.entry(id="Short", long=false, stop=close)
strategy.exit('Long-SL/TP', 'Long', stop=trailingSl_long)
strategy.exit('Short-SL/TP', 'Short', stop=trailingSl_short)