ATR ベースのダイナミックストップ損失戦略

作者: リン・ハーンチャオチャン開催日:2023年10月10日 (火) 10:50:21
タグ:

概要

この戦略は,ATRインジケーターを使用して動的ストップロスのポイントを設定し,リスクを制御するために価格変動に基づいてストップロスのポジションを調整します.主に5EMAが20EMAを超えるとロングに入ります.その後,ATRインジケーターを使用してストップロスを設定し,利益のポジションを設定します.ストップロスのポジションは,より多くの利益をロックするために価格変動に応じて調整されます.

戦略の論理

この戦略は,最初に5EMAが20EMAを超えてロングに行くかどうかを判断する.入った後,ATR指標を使用してエントリー価格と現在の価格のATR倍数を計算し,エントリー価格より1.5ATR低いストップロスのポジションを設定する.価格が上昇するにつれて,ストップロスのポジションは徐々に引き上げられ,ポジションの利益を増やす.

具体的には,戦略は次の変数を定義します.

  • entry_price: 入場価格
  • 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を超えると,利益をロックするために部分的にポジションを閉じて, tookProfit を true に設定します.その後,価格が上昇し続けると,ストップロスを引き上げ続けます.ストップロスが起動すると, tookProfit をチェックします - すでに部分的な利益を取っていた場合,残りのポジションのみを閉じる.そうでなければ,完全なポジションを閉じる.

利点

  1. ATR インディケーターを使用してストップ・ロスを動的に調整することで,市場の変動に基づいて合理的なストップ距離を設定できます.

  2. 損失を制限しながら 傾向を追跡する ストップロスは徐々に増やされ 利益が蓄積されます

  3. 部分的な利益のメカニズムは 利益の一部を固定し リスクを軽減します 利益が実行できるように ストップ・ロスは上昇し続けます

リスク

  1. ATR インジケーターは急激な逆転やギャップに敏感ではない.

  2. EMAはトレンド逆転を決定できないので,トレンド逆転時に新しいポジションを入れることができる.

  3. 部分的な利益を取った後に損失の可能性が高い.

  4. パラメータはさらに最適化され,1.5ATRのストップと3ATRの利益は異なる製品に調整されるべきです.

改善

  1. ATRの遅延を補うために ドンチアンチャンネルのような他のストップ損失指標を追加することを検討します

  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))

もっと