동적 트레일링 스톱 로스 전략

저자:차오장, 날짜: 2023-12-29 10:42:27
태그:

img

전반적인 설명

동적 트레일링 스톱 로스 전략은 트레일링 스톱 로스 메커니즘을 활용한 양적 거래 전략이다. 트렌드 추적 이론을 기반으로 트레일링 스톱 로스 라인을 설정하여 스톱 로스를 확인하고 트레일링 스톱 로스를 조정합니다. 주로 단일 거래의 스톱 로스를 제어하고 수익 잠금을 극대화하고 거래 위험을 줄이기 위해 사용됩니다.

전략 논리

역동적인 트레일링 스톱 로스 전략의 핵심은 세 가지 주요 매개 변수를 설정하는 데 있습니다. 초기 스톱 로스 거리, 트레일링 스톱 로스 거리 및 트레일링 스톱 로스 트리거 거리. 구매 신호가 트리거 된 후, 초기 스톱 로스 가격은 엔트리 가격과 설정 된 초기 스톱 로스 거리를 기반으로 계산됩니다. 그 다음 각 바는 트레일링 스톱 로스 트리거 조건이 충족되었는지 여부를 판단합니다. 만약 그렇습니다. 새로운 트레일링 스톱 로스 가격이 설정됩니다. 새로운 트레일링 스톱 로스 가격은 현재 폐쇄 가격과 트레일링 스톱 로스 거리를 기반으로 계산됩니다. 따라서 가격이 유리한 방향으로 진행되는 한, 트레일링 스톱 로스 라인은 이익을 잠금하기 위해 계속 올라갈 것입니다. 가격 반전이 트레일링 스톱 로스 라인을 트리거 할 때 판매 신호가 생성됩니다.

이 전략은 또한 낮은 스톱 손실을 가지고 있습니다. 후속 스톱 손실이 활성화 되든 아니든, 가격이 낮은 스톱 손실 아래로 넘어지면 스톱 손실이 직접적으로 유발됩니다. 낮은 스톱 손실은 갑작스러운 사건으로 인한 가격 격차로부터 보호하는 역할을합니다. 따라서, 이중 라인 스톱 손실의 형태로 동적 스톱 손실 메커니즘을 통해 스톱 손실 라인은 과도한 손실을 방지하면서 자동으로 유리한 추세를 추적 할 수 있습니다.

장점

  1. 계속적으로 수익을 차단하여 후속 스톱 손실을 방지하고, 너무 많은 리트레이싱 공간을 피합니다.

  2. 두 줄의 스톱 손실 구조를 채택하여 과도한 손실을 방지하면서 스톱 손실 라인이 신속하게 추적 할 수 있도록 보장합니다.

  3. 스톱 로스 조정을 위한 연속 판단 메커니즘을 사용하며, 간단하게 작동하고 쉽게 구현할 수 있습니다.

  4. 매개 변수는 시장 및 주식 특성에 따라 최적화되어 스톱 로스 효과를 향상시킬 수 있습니다.

  5. 시장의 흐름을 예측할 필요도 없고, 흐름을 따라가기만 하면 됩니다.

위험성

  1. 부적절한 매개 변수 설정은 너무 느슨하거나 너무 긴 스톱 손실로 이어질 수 있습니다. 너무 느슨하면 스톱 손실이 무효가 될 반면 너무 긴 것은 정상적인 가격 변동에 의해 중단되는 경향이 있습니다.

  2. 갑작스러운 사건으로 인한 가격 격차의 경우 손실을 멈추지 못할 수 있습니다. 다른 보호 조치가 적절히 취해야합니다.

  3. 거래 비용과 미끄러짐은 스톱 로스 라인이 활성화 된 후 실제 판매 가격에 영향을 줄 수 있습니다.

  4. 적응력이 강하지 않습니다. 범위 제한 운동과 같은 특정 단계에서 잘 작동하지 않습니다.

대책:

  1. 백테스트 및 실시간 결과에 따라 매개 변수를 지속적으로 최적화하는 것이 좋습니다.
  2. 가격 격차를 방지하기 위해 더 넓은 낮은 스톱 로스를 설정합니다.
  3. 스톱 로스 가격을 계산할 때 거래 비용과 미끄러짐을 고려해야 합니다.
  4. 트렌드 및 변동성 지표와 함께 사용하십시오.

최적화 위한 지침

  1. 마감 손실 라인을 비율 변화로 조정하여 다른 가격 수준에서 가격 움직임을 더 잘 추적 할 수 있습니다.

  2. 높은 변동성을 직면했을 때 후속 스톱 손실을 중지하기 위해 변동성 메트릭을 추가하여 정상적인 변동이 스톱 손실을 유발하는 것을 피합니다.

  3. 기계 학습을 통해 매개 변수를 자동으로 최적화합니다. 훈련 샘플로 최근 기간에 다른 매개 변수 조합의 반환을 선택합니다.

  4. 트렌드, 지원 및 저항과 같은 지표를 고려하여 오픈 포지션 기준을 추가하여 다양한 시장에서 포지션을 열지 않도록하십시오.

결론

동적 트레일링 스톱 로스 전략은 스톱 로스를 확인하고 가격 변화에 따라 트레일링 스톱 로스를 조정하기 위해 이중 라인 스톱 로스 메커니즘을 통해 트레일링 스톱 로스 라인을 설정합니다. 수익을 잠금하고 인기를 줄이고 손실을 제어하기 위해 자동으로 스톱 로스 거리를 조정할 수 있습니다. 간단한 운영과 쉬운 구현으로이 전략은 시장 조건에 따라 더 최적화되어 더 나은 성능을 위해 다른 전략과 함께 사용할 수 있습니다. 그러나 또한 몇 가지 제한이 있습니다. 라이브 거래에 적용하기 전에 충분히 개선하고 테스트하는 것이 좋습니다.


/*backtest
start: 2023-11-28 00:00:00
end: 2023-12-17 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/
// © Thumpyr
//@version=5

/////////////////////////////////////////////////////////////////////////////////////////////
// Comment out Strategy Line and remove // from Indicator line to turn into Indicator  //////
// Do same for alertConidction at bottom                                               //////
/////////////////////////////////////////////////////////////////////////////////////////////
strategy("PCT Trailing Stoploss-Strategy", shorttitle="PCT Trailing Stoploss- Strategy", overlay=true)
//indicator(title="PCT Trailing Stoploss- Indicator", shorttitle="PCT Trailing Stoploss - Indicator", timeframe="", timeframe_gaps=true, overlay=true)//

sellLow=input.float(.035, minval=0, title="Stop Loss Loss: 1% = .01", group="Sell Settings")
trailStopArm=input.float(.0065, minval=0, title="Trailing Stop Arm: 1%=.01", group="Sell Settings")
trailStopPct=input.float(.003, minval=0, title="Trailing Stop Trigger: 1%=.01 ", group="Sell Settings")

/////////////////////////////////////////////////
//               Indicators                    //
/////////////////////////////////////////////////
ema1Len = input.int(14, minval=1, title=" ema 1 Length", group="Trend Line Settings")
ema1Src = input(close, title="ema 1 Source", group="Trend Line Settings")
ema1 = ta.ema(ema1Src, ema1Len)
plot(ema1, title="EMA", color=color.blue)

ema2Len = input.int(22, minval=1, title=" ema 2 Length", group="Trend Line Settings")
ema2Src = input(close, title="ema 2 Source", group="Trend Line Settings")
ema2 = ta.ema(ema2Src, ema2Len)
plot(ema2, title="EMA", color=color.orange)

ema3Len = input.int(200, minval=1, title=" ema 3 Length", group="Trend Line Settings")
ema3Src = input(close, title="ema 2 Source", group="Trend Line Settings")
ema3 = ta.ema(ema3Src, ema3Len)
plot(ema3, title="EMA", color=color.gray)


/////////////////////////////
////   Buy Conditions    ////
/////////////////////////////

alertBuy = ta.crossover(ema1,ema2) and close>ema3

////////////////////////////////////////////////////////////////////
////   Filter redundant Buy Signals if Sell has not happened    ////
////////////////////////////////////////////////////////////////////
var lastsignal = 0
showAlertBuy = 0
if(alertBuy and lastsignal !=1)
    showAlertBuy  := 1
    lastsignal      := 1
buyAlert= showAlertBuy > 0


//////////////////////////////////////////////////////////////////
////          Track Conditions at buy Signal                  ////
//////////////////////////////////////////////////////////////////

alertBuyValue = ta.valuewhen(buyAlert, close,0)
alertSellValueLow = alertBuyValue - (alertBuyValue*sellLow)

////////////////////////////////////////////////////////////
/////            Trailing Stop                         /////
////////////////////////////////////////////////////////////
var TSLActive=0         //Check to see if TSL has been activated
var TSLTriggerValue=0.0 //Initial and climbing value of TSL
var TSLStop = 0.0       //Sell Trigger
var TSLRunning =0       //Continuously check each bar to raise TSL or not

//  Check if a Buy has been triggered and set initial value for TSL //
if buyAlert
    TSLTriggerValue := alertBuyValue+(alertBuyValue*trailStopArm)
    TSLActive := 0
    TSLRunning :=1
    TSLStop := TSLTriggerValue - (TSLTriggerValue*trailStopPct)
    

//  Check that Buy has triggered and if Close has reached initial TSL//  
//  Keeps from setting Sell Signal before TSL has been armed w/TSLActive//
beginTrail=TSLRunning==1 and TSLActive==0 and close>alertBuyValue+(alertBuyValue*trailStopArm) and ta.crossover(close,TSLTriggerValue)
if beginTrail
    TSLTriggerValue :=close
    TSLActive :=1
    TSLStop :=TSLTriggerValue - (TSLTriggerValue*trailStopPct)
    
//  Continuously check if TSL needs to increase and set new value //    
runTrail= TSLActive==1 and (ta.crossover(close,TSLTriggerValue) or close>=TSLTriggerValue)
if runTrail
    TSLTriggerValue :=close
    TSLStop :=TSLTriggerValue - (TSLTriggerValue*trailStopPct)
    
//  Verify that TSL is active and trigger when close cross below TSL Stop//
TSL=TSLActive==1 and (ta.crossunder(close,TSLStop) or (close[1]>TSLStop and close<TSLStop)) 

// Plot point of inital arming of TSL//
TSLTrigger=TSLActive==1 and TSLActive[1]==0
plotshape(TSLTrigger, title='TSL Armed', location=location.abovebar, color=color.new(color.blue, 0), size=size.small, style=shape.cross, text='TSL Armed')


////////////////////////////////////////////////////////////
// Plots used for troubleshooting and verification of TSL //
////////////////////////////////////////////////////////////
//plot(TSLActive,"Trailing Stop", color=#f48fb1)
//plot(TSLRunning,"Trailing Stop", color=#f48fb1)
//plot(TSLTriggerValue,"Trailing Stop Trigger", color.new(color=#ec407a, transp = TSLRunning==1 ? 0 : 100))
//plot(TSLStop,"Trailing Stop", color.new(color=#f48fb1, transp = TSLRunning==1 ? 0 : 100))//


////////////////////////////////////////////////////////////
/////             Sell Conditions                    ///////
////////////////////////////////////////////////////////////
Sell1 = TSL
Sell2 = ta.crossunder(close,alertSellValueLow)

alertSell= Sell1 or Sell2
////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////
////        Remove Redundant Signals                    ////
////////////////////////////////////////////////////////////
showAlertSell = 0
if(alertSell and lastsignal != -1)
    showAlertSell := 1
    lastsignal      := -1
sellAlert= showAlertSell > 0

if sellAlert
    TSLActive :=0
    TSLRunning :=0

/////////////////////////////////////////
//  Plot Buy and Sell Shapes on Chart  //
/////////////////////////////////////////
plotshape(buyAlert, title='Buy', location=location.belowbar, color=color.new(color.green, 0), size=size.small, style=shape.triangleup, text='Buy')
plotshape(sellAlert, title='Sell', location=location.abovebar, color=color.new(color.red, 0), size=size.small, style=shape.triangledown, text='Sell')

/////////////////////////////////////////////////////////////////////////////////////////////
//                        Remove // to setup for Indicator                                 //
/////////////////////////////////////////////////////////////////////////////////////////////
//Alerts
//alertcondition(title='Buy Alert', condition=buyAlert, message='Buy Conditions are Met')
//alertcondition(title='Sell Alert', condition=sellAlert, message='Sell Conditions are Met')
/////////////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////
////  Comment out this section if setup as Indicator    ////
////////////////////////////////////////////////////////////
longCondition = buyAlert
if (longCondition)
    strategy.entry("Buy", strategy.long)
    alert(message='Buy', freq=alert.freq_once_per_bar_close)
    
shortCondition = sellAlert
if (shortCondition)
    strategy.close_all(sellAlert,"Sell")
    alert(message='Sell', freq=alert.freq_once_per_bar_close)
/////////////////////////////////////////////////////////////




더 많은