ATR 기반의 동적 스톱 로스 전략

저자:차오장, 날짜: 2023-10-10 10:50:21
태그:

전반적인 설명

이 전략은 동적 스톱 로스 포인트를 설정하고 가격 변동에 따라 스톱 로스 포지션을 조정하기 위해 ATR 인디케이터를 사용합니다. 주로 5EMA가 20EMA를 넘을 때 장면을 입력하고 ATR 인디케이터를 사용하여 스톱 로스 포지션을 설정하고 수익을 취합니다. 스톱 로스 포지션은 더 많은 수익을 확보하기 위해 가격 움직임에 따라 조정됩니다.

전략 논리

이 전략은 먼저 5EMA가 20EMA를 넘어서면 장가가 될 수 있는지 판단합니다. 입력 후 ATR 지표를 사용하여 입시 가격과 현재 가격의 ATR 곱을 계산하고 입시 가격보다 1.5ATR 아래로 스톱 로스 포지션을 설정합니다. 가격이 상승함에 따라 포지션의 수익을 높이기 위해 스톱 로스 포지션은 점차 상승합니다.

특히 전략은 다음과 같은 변수를 정의합니다.

  • 입력값: 입력값
  • stop_price: 손해 중지 가격
  • take_profit_price: 수익을 취하는 가격
  • atr_down: ATR 하향선
  • atr_up: ATR 위 라인
  • atr_current: 현재 ATR 라인
  • atr_ref: ATR 값

입력 후, 현재 ATR 값으로 atr_ref를 계산하고, 현재 가격에 대한 입시 가격의 ATR 곱하기로 atr_div을 계산합니다. 그 다음 atr_div을 기반으로 atr_down, atr_current 및 atr_up의 포지션을 설정합니다. 중지 손실 가격은 입시 가격보다 1.5ATR 아래로 설정됩니다.

가격이 상승함에 따라 현재 가격 AVG와 atr_up을 비교하여 AVG가 atr_up을 넘으면 atr_div 및 ATR 라인 포지션을 재 계산하여 수익을 높이기 위해 점진적으로 스톱 로스 라인을 올립니다.

만약 가격이 입상 가격의 3ATR 이상 상승하면, 그것은 부분적으로 포지션을 닫아 이익을 잠금하고, true로 tookProfit를 설정합니다. 그 후 가격이 계속 상승하면, 그것은 계속 스톱 로스를 올릴 것입니다. 스톱 로스가 트리거되면, 그것은 tookProfit를 확인합니다. 이미 부분적 이윤을 취했다면, 남은 포지션을만 닫을 것입니다. 그렇지 않으면 전체 포지션을 닫습니다.

장점

  1. ATR 지표를 사용하여 동적으로 스톱 로스를 조정하면 시장 변동성에 따라 합리적인 스톱 거리를 설정할 수 있습니다.

  2. 손실을 제한하면서 트렌드를 따르십시오. 수익을 축적하기 위해 Stop Loss는 점차 증가합니다.

  3. 부분적인 수익 메커니즘은 수익을 차단하고 위험을 줄여줍니다. 수익을 실행하도록 중지 손실이 계속 증가합니다.

위험성

  1. ATR 지표는 급격한 반전과 격차에 민감하지 않습니다.

  2. EMA는 트렌드 전환을 결정할 수 없으며, 트렌드 전환 시 새로운 포지션을 입력할 수 있습니다.

  3. 부분적인 수익을 낸 후 손실 가능성이 높습니다.

  4. 매개 변수들은 더 이상 최적화되어야 합니다. 1.5ATR 스톱과 3ATR 취업이익은 다른 제품에 맞게 조정되어야 합니다.

개선

  1. ATR 지연을 보완하기 위해 Donchian 채널과 같은 다른 스톱 손실 지표를 추가하는 것을 고려하십시오.

  2. 다른 이동 평균을 테스트하거나 트렌드 반전을 판단하기 위해 MACD 등을 추가하십시오.

  3. 다른 제품들에 대한 부분 수익률 및 빈도를 최적화합니다.

  4. 스톱 및 리프트를 위한 ATR 곱자에 대한 매개 변수 최적화. 후속 스톱 손실 기능을 추가.

  5. 약한 트렌드에서 테스트 성능은 약한 트렌드에서 전략을 비활성화 할 수 있습니다.

요약

이 전략은 동적 스톱 로스 관리에 ATR을 사용하는 명확한 논리를 가지고 있으며, 이것이 가장 큰 강점입니다. 그러나 ATR 자체는 지연과 같은 한계를 가지고 있습니다. 다른 스톱 및 트렌드 지표를 추가하면 개선 될 것입니다. 또한 부분 수익은 제품 전반에 걸쳐 최적화가 필요합니다. 전반적으로 ATR 기반의 스톱 로스 관리의 아이디어를 제공하지만 추가 최적화와 향상이 필요합니다.


/*backtest
start: 2022-10-03 00:00:00
end: 2023-10-09 00:00:00
period: 1d
basePeriod: 1h
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/
// © ekinbasarkomur

//@version=5
strategy("[EKIN] ATR Exit Strategy", overlay=true, initial_capital = 1000, default_qty_value = 100, default_qty_type = strategy.percent_of_equity, calc_on_every_tick = true)

// Simple EMA tracking
fastEMA = ta.ema(close, 5)
slowEMA = ta.ema (close, 20)
atr = ta.atr(14)

// We define entry price for future reference
var float entry_price = na
// We define stop and take profit for future calculations
var float stop_price = na
var float take_profit_price = na

// We define atr limtim its here
var float atr_down = na
var float atr_up = na
var float atr_current = na
var float atr_ref = na

avg = (low + high) / 2

// Conditions
enterCondition = ta.crossover(fastEMA, slowEMA)
var bool tookProfit = false
timePeriod = time >= timestamp(syminfo.timezone, 2021, 12, 15, 0, 0)
InTrade = strategy.position_size > 0

// Go long if conditions are met
if (enterCondition and timePeriod and not InTrade)
    // Calculate and update variables
    entry_price := avg
    atr_ref := atr
    atr_div = int((avg - entry_price) / atr_ref)
    atr_down := entry_price + (atr_ref * (atr_div - 1.5))
    atr_up := entry_price + (atr_ref * (atr_div + 1))
    atr_current := entry_price + (atr_ref * atr_div) 
    stop_price := (entry_price - (atr_ref * 1.5))
    take_profit_price := (entry_price + (atr_ref * 3))
    strategy.order("buy", strategy.long, qty = 2)

// Enter here if in position
if InTrade or tookProfit
    stopCondition = avg < stop_price
    takeProfitCondition = avg > take_profit_price

    if avg < atr_down
        stopCondition := true

    // Move stop price and exit price if price for each atr price increase
    if avg > atr_up
        if tookProfit
            atr_ref := atr
        atr_div = int((avg - entry_price) / atr_ref)
        atr_down := entry_price + (atr_ref * (atr_div - 1))
        atr_up := entry_price + (atr_ref * (atr_div + 1))
        atr_current := entry_price + (atr_ref * atr_div) 

    // Take half of the investment if current price is 3 atr higher than entry price
    if (takeProfitCondition and timePeriod and InTrade and not tookProfit)
        strategy.order("take_half_profit", strategy.short, qty = 1)
        tookProfit := true

    // Exit position if conditions are met and reset the variables
    if (stopCondition and timePeriod and InTrade)
        if tookProfit
            strategy.order("exit", strategy.short, qty = 1)
        else
            strategy.order("stop_loss", strategy.short, qty = 2)

        tookProfit := false

// Plot EMA's
plot(fastEMA, color = color.blue)
plot(slowEMA, color = color.yellow)

// Plot ATR Limit/Stop positions
profit_plot = plot(series = InTrade?atr_up:na, title = "profit", color = color.green, style=plot.style_linebr)
close_plot = plot(series = InTrade?atr_current:na, title = "close", color = color.white, style=plot.style_linebr)
stop_plot = plot(series = InTrade?atr_down:na, title = "stop_loss", color = color.red, style=plot.style_linebr)
fill(profit_plot, close_plot, color = color.new(color.green, 80))
fill(close_plot, stop_plot, color =color.new(color.red,80))

더 많은