Dynamische ATR-basierte Stop-Loss-Strategie

Schriftsteller:ChaoZhang, Datum: 2023-10-10 10:50:21
Tags:

Übersicht

Diese Strategie verwendet den ATR-Indikator, um dynamische Stop-Loss-Punkte zu setzen und Stop-Loss-Positionen basierend auf Kursschwankungen anzupassen, um Risiken zu kontrollieren.

Strategie Logik

Die Strategie beurteilt zuerst, ob 5EMA über 20EMA überschreitet, um lang zu gehen. Nach dem Eintritt berechnet sie die ATR-Multiplikatoren des Einstiegspreises zum aktuellen Preis mithilfe des ATR-Indikators und setzt die Stop-Loss-Position auf 1.5ATR unter dem Einstiegspreis. Wenn der Preis steigt, wird die Stop-Loss-Position allmählich angehoben, um die Gewinne der Position zu erhöhen.

Die Strategie definiert insbesondere folgende Variablen:

  • Eingangspreis: Eingangspreis
  • Stop_price: Stop-Loss-Preis
  • Take_profit_price: Gewinnpreis
  • atr_down: ATR nach unten
  • atr_up: ATR nach oben
  • atr_current: aktuelle ATR-Linie
  • ATR-Wert

Nachdem er eingegeben wurde, berechnet er atr_ref als den aktuellen ATR-Wert und atr_div als das ATR-Multiplikator des Einstiegspreises zum aktuellen Preis. Anschließend werden die Positionen von atr_down, atr_current und atr_up basierend auf atr_div festgelegt. Der Stop-Loss-Preis stop_price wird auf 1,5ATR unter dem Einstiegspreis festgelegt.

Wenn der Preis steigt, indem er den aktuellen Preis avg und atr_up vergleicht, wenn avg über atr_up geht, berechnet er atr_div und ATR-Linienpositionen neu, wodurch die Stop-Loss-Linie allmählich erhöht wird, um den Gewinn zu erhöhen.

Wenn der Preis über 3ATR des Einstiegspreises steigt, schließt er die Position teilweise, um Gewinne zu erzielen, und setzt tookProfit auf true. Danach, wenn der Preis weiter steigt, erhöht er den Stop Loss weiter. Wenn der Stop Loss ausgelöst wird, überprüft er tookProfit - wenn er bereits teilweise Gewinne erzielt hat, schließt er nur die verbleibende Position; andernfalls schließt er die volle Position.

Vorteile

  1. Die Verwendung des ATR-Indikators zur dynamischen Anpassung des Stop-Loss ermöglicht die Festlegung einer angemessenen Stoppdistanz basierend auf der Marktvolatilität.

  2. Der Stop-Loss wird allmählich erhöht, um Gewinne zu sammeln.

  3. Der Mechanismus der teilweisen Gewinnentnahme sichert einen gewissen Gewinn und reduziert das Risiko.

Risiken

  1. Der ATR-Indikator ist nicht empfindlich für starke Umkehrungen und Lücken.

  2. EMAs können keine Trendumkehr feststellen und können bei Trendumkehrungen neue Positionen einlegen.

  3. Hohe Gefahr von Verlusten nach einem Teilgewinn.

  4. Die Parameter müssen weiter optimiert werden, die 1,5ATR-Stopp- und 3ATR-Gewinnspielzeiten sollten für verschiedene Produkte angepasst werden.

Verbesserungen

  1. Überlegen Sie, andere Stop-Loss-Indikatoren wie den Donchian Channel hinzuzufügen, um die ATR-Verzögerung auszugleichen.

  2. Testen Sie verschiedene gleitende Durchschnitte oder fügen Sie MACD usw. hinzu, um die Trendumkehr zu beurteilen.

  3. Optimierung der Teilgewinnquoten und -frequenz für verschiedene Produkte.

  4. Optimierung der Parameter für ATR-Multiplikatoren für Stop und Take-Profit.

  5. Testleistung bei schwachen Trends kann die Strategie bei schwachen Trends deaktivieren.

Zusammenfassung

Die Strategie hat eine klare Logik, ATR für dynamisches Stop-Loss-Management zu verwenden, was ihre größte Stärke ist. Allerdings hat ATR selbst Einschränkungen wie Verzögerung. Das Hinzufügen anderer Stop- und Trendindikatoren wird es verbessern. Auch die partielle Gewinnentnahme benötigt Optimierungen über Produkte hinweg. Insgesamt bietet sie die Idee eines ATR-basierten Stop-Loss-Managements, benötigt jedoch weitere Optimierungen und Verbesserungen.


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

Mehr