최선 후속 중지 전략

저자:차오장, 날짜: 2023-09-21 20:58:22
태그:

전반적인 설명

이 전략은 트레일링 스톱 로스 메커니즘을 사용하여 가격 변동 범위에 따라 동적으로 스톱 로스를 이동하여 동적 스톱을 달성합니다. 트레일링 스톱은 가격이 수익 목표에 도달 한 후 활성화되며, 조기 스톱 아웃을 피하면서 수익을 보호하는 것을 목표로합니다. 일반적인 스톱 로스 전략을 개선합니다.

전략 논리

전략은 트렌드 방향을 판단하는 이중 MA 크로스오버에 기초합니다.

혁신은 스톱 로스 디자인에 있습니다.

  1. 스톱 트리거 라인이 설정됩니다. 가격이 이 라인을 넘으면 트레일 스톱이 시작됩니다.

  2. 스톱 로스 라인은 % 매개 변수를 기준으로 트레일합니다. 예를 들어, 3% 트레일하면 마지막 최저보다 3% 낮습니다.

  3. 포지션은 가격이 트레일링 스톱 로스 라인을 만질 때 닫습니다.

이것은 스톱이 수익을 자동으로 추적할 수 있도록 보장하며, 수익이 여전히 좋은 상태에서 스톱을 중단할 확률을 줄입니다.

장점

  • %에 기초한 자동 후속 중지
  • 트리거 라인은 조기 활성화를 방지합니다.
  • 역동적 인 후속은 이익을 보호합니다.
  • 짧은 회귀로 인해 정지하는 것을 피합니다
  • 트리거 라인 및 시장에 따라 조정 가능한 비율

위험성

  • MA 크로스오버가 지연되어 잘못된 신호를 생성할 수 있습니다.
  • 잘못된 트리거 라인 설정으로 인해 조기 또는 늦은 활성화
  • 부적절한 비율 설정은 너무 넓거나 좁은 중지
  • 윙사 위험은 완전히 피할 수 없습니다.
  • 시장 변동성에 대한 매개 변수 최적화가 필요합니다

위험은 다음과 같이 감소 할 수 있습니다.

  • 더 나은 입력을 위해 MA 기간을 최적화
  • 최적의 위치를 위해 다른 트리거 값을 테스트합니다.
  • 과거 사용량에 기초한 이상적인 비율의 백테스팅
  • 실종 추세를 피하기 위해 재입입입을 고려
  • 가짜 브레이크를 피하기 위해 필터를 추가합니다.

개선 방향

이 전략은 다음과 같이 개선될 수 있습니다.

  1. 이중 MA 기간 최적화

  2. 트리거 라인을 최적화하거나 제거

    직접 추적을 시작하거나 다른 제품에 대한 다른 값을 사용

  3. 다른 후속 비율 값을 테스트

    다양한 제품에 대한 최적 값을 찾아

  4. 재입국규칙 추가

    정지된 후 재입구 조건을 설정합니다.

  5. 변동성에 따라 정지 엄격성을 조정합니다.

    변동성이 높은 환경에서 더 넓은 중지

요약

이 전략은 활성화하기 전에 트리거 라인을 가진 후속 비율 정지를 사용합니다. 이 역동적 메커니즘은 수익을 보호하고 시장 움직임에 따라 불필요한 정지를 피하는 균형을 맞추고 있습니다. 그러나 매개 변수는 정확성을 향상시키기 위해 다른 제품에 대한 최적화와 추가 필터가 필요합니다. 재입구는 조기 중단 후 트렌드를 놓치는 것을 피하는 데 도움이됩니다. 적응성이 위해 지속적인 개선이 필요합니다.


/*backtest
start: 2022-09-14 00:00:00
end: 2023-09-20 00:00:00
period: 2d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
//@author=Daveatt

SystemName = "BEST Trailing Stop Strategy"
TradeId = "BEST"

InitCapital = 100000
InitPosition = 100
InitCommission = 0.075
InitPyramidMax = 1
CalcOnorderFills = true
CalcOnTick = true
DefaultQtyType = strategy.fixed
DefaultQtyValue = strategy.fixed
Precision = 2
Overlay=true


// strategy(title=SystemName, shorttitle=SystemName, overlay=Overlay, 
//  pyramiding=InitPyramidMax, initial_capital=InitCapital, default_qty_type=DefaultQtyType, default_qty_value=InitPosition, commission_type=strategy.commission.percent, 
//  commission_value=InitCommission, calc_on_order_fills=CalcOnorderFills, calc_on_every_tick=CalcOnTick, precision=2)


src = close
// Calculate moving averages
fastSMA = sma(close, 15)
slowSMA = sma(close, 45)

// Calculate trading conditions
enterLong  = crossover(fastSMA, slowSMA)
enterShort = crossunder(fastSMA, slowSMA)

// trend states
since_buy  = barssince(enterLong)
since_sell = barssince(enterShort)
buy_trend  = since_sell > since_buy
sell_trend = since_sell < since_buy 

change_trend = (buy_trend and sell_trend[1]) or (sell_trend and buy_trend[1])

//plot(buy_trend ? 1 : 0, title='buy_trend', transp=100)
//plot(sell_trend ? 1 : 0, title='sell_trend', transp=100)

// get the entry price
entry_price = valuewhen(enterLong or enterShort, close, 0)

// Plot moving averages
plot(series=fastSMA, color=color.teal)
plot(series=slowSMA, color=color.orange)

// Plot the entries
plotshape(enterLong, style=shape.circle, location=location.belowbar, color=color.green, size=size.small)
plotshape(enterShort, style=shape.circle, location=location.abovebar, color=color.red, size=size.small)



///////////////////////////////
//======[ Trailing STOP ]======//
///////////////////////////////

// use SL?
useSL = input(true, "Use stop Loss")
// Configure trail stop level with input
StopTrailPerc = input(title="Trail Loss (%)", type=input.float, minval=0.0, step=0.1, defval=3) * 0.01
// Will trigger the take profit trailing once reached
use_SL_Trigger = input(true, "Use stop Loss Trigger")
StopTrailTrigger   = input(2.0, "SL Trigger (%)",minval=0,step=0.5,type=input.float) * 0.01


StopLossPriceTrigger = 0.0
StopLossPriceTrigger := if (use_SL_Trigger)
    if buy_trend
        entry_price * (1 + StopTrailTrigger) 
    else
        entry_price * (1 - StopTrailTrigger)
else
    -1


var SL_Trigger_Long_HIT = false
SL_Trigger_Long_HIT := useSL and use_SL_Trigger and buy_trend and high >= StopLossPriceTrigger
 ? true : SL_Trigger_Long_HIT[1]


var SL_Trigger_Short_HIT = false
SL_Trigger_Short_HIT := useSL and use_SL_Trigger and sell_trend and low <= StopLossPriceTrigger
 ? true : SL_Trigger_Short_HIT[1]


display_long_SL_trigger     = useSL and buy_trend  and use_SL_Trigger 
 and SL_Trigger_Long_HIT == false and StopLossPriceTrigger != -1
display_short_SL_trigger    = useSL and sell_trend and use_SL_Trigger 
 and SL_Trigger_Short_HIT == false and StopLossPriceTrigger != -1
display_SL_trigger          = display_long_SL_trigger or display_short_SL_trigger

plot(display_SL_trigger ? StopLossPriceTrigger : na, title='SLPriceTrigger', transp=0, 
 color=color.maroon, style=plot.style_circles, linewidth=3)


// Determine trail stop loss prices
longStopPrice = 0.0, shortStopPrice = 0.0

longStopPrice := if useSL and buy_trend
    stopValue = low * (1 - StopTrailPerc)
    max(stopValue, longStopPrice[1])
else
    0

shortStopPrice := if useSL and sell_trend
    stopValue = high * (1 + StopTrailPerc)
    min(stopValue, shortStopPrice[1])
else
    999999

//////////////////////////////////////////////////////////////////////////////////////////
//*** STOP LOSS HIT CONDITIONS TO BE USED IN ALERTS  ***//
//////////////////////////////////////////////////////////////////////////////////////////

cond_long_stop_loss_hit  = useSL and buy_trend and crossunder(low, longStopPrice[1]) 
 and (SL_Trigger_Long_HIT or use_SL_Trigger == false)
cond_short_stop_loss_hit = useSL and sell_trend and crossover(high, shortStopPrice[1]) 
 and (SL_Trigger_Short_HIT or use_SL_Trigger == false)


// Plot stop loss values for confirmation
plot(series=useSL and buy_trend and low >= longStopPrice 
 and (SL_Trigger_Long_HIT or use_SL_Trigger == false)
 ? longStopPrice : na,
 color=color.fuchsia, style=plot.style_cross,
 linewidth=2, title="Long Trail Stop")

plot(series=useSL and sell_trend and high <= shortStopPrice 
 and (SL_Trigger_Short_HIT or use_SL_Trigger == false)
 ? shortStopPrice : na,
 color=color.fuchsia, style=plot.style_cross,
 linewidth=2, title="Short Trail Stop")

close_long  = cond_long_stop_loss_hit
close_short = cond_short_stop_loss_hit

// Submit entry orders
strategy.entry(TradeId + " L", long=true, when=enterLong)
strategy.close(TradeId + " L", when=close_long)

//if (enterShort)
strategy.entry(TradeId + " S", long=false, when=enterShort)
strategy.close(TradeId + " S", when=close_short)


if change_trend
    SL_Trigger_Long_HIT := false
    SL_Trigger_Short_HIT := false


더 많은