Chiến lược dừng kéo dài tốt nhất

Tác giả:ChaoZhang, Ngày: 2023-09-21 20:58:22
Tags:

Tổng quan

Chiến lược này sử dụng một cơ chế dừng lỗ kéo dài để di chuyển dừng lỗ một cách năng động dựa trên phạm vi biến động giá, đạt được các điểm dừng năng động.

Chiến lược logic

Chiến lược đi vào dựa trên hai MA chéo đánh giá hướng xu hướng.

Sự đổi mới nằm trong thiết kế dừng lỗ:

  1. Một đường dừng kích hoạt được thiết lập. Đặt dừng bắt đầu sau khi giá phá vỡ đường này.

  2. Đường dừng lỗ theo dõi dựa trên thông số tỷ lệ phần trăm. ví dụ: 3% theo dõi có nghĩa là 3% dưới mức thấp nhất.

  3. Vị trí được đóng khi giá đảo ngược để chạm vào đường dừng lỗ.

Điều này đảm bảo việc dừng lại sẽ theo dõi lợi nhuận tự động, đồng thời giảm khả năng dừng lại khi lợi nhuận vẫn tốt.

Ưu điểm

  • Dừng tự động theo tỷ lệ phần trăm
  • Đường kích hoạt tránh kích hoạt sớm
  • Động lực theo dõi bảo vệ lợi nhuận
  • Tránh dừng lại do rút ngắn
  • Đường kích hoạt và tỷ lệ phần trăm điều chỉnh theo thị trường

Rủi ro

  • MA crossover có thể bị chậm, tạo ra tín hiệu sai
  • Cài đặt đường kích hoạt không chính xác gây ra kích hoạt sớm hoặc muộn
  • Cài đặt tỷ lệ phần trăm không chính xác cho phép dừng quá rộng hoặc quá chật
  • Không thể hoàn toàn tránh được rủi ro
  • Các thông số cần tối ưu hóa cho sự biến động của thị trường

Các rủi ro có thể được giảm bằng cách:

  • Tối ưu hóa thời gian MA cho các mục nhập tốt hơn
  • Kiểm tra các giá trị kích hoạt khác nhau để định vị tốt nhất
  • Kiểm tra lại tỷ lệ phần trăm lý tưởng dựa trên các khoản rút tiền trong lịch sử
  • Xem xét tái nhập cảnh để tránh xu hướng bị bỏ lỡ
  • Thêm các bộ lọc để tránh các sự đột phá sai

Hướng dẫn cải thiện

Chiến lược có thể được cải thiện bằng cách:

  1. Tối ưu hóa thời gian MA đôi

  2. Tối ưu hóa hoặc loại bỏ đường kích hoạt

    Bắt đầu theo dõi trực tiếp hoặc sử dụng các giá trị khác nhau cho các sản phẩm khác nhau

  3. Kiểm tra các giá trị tỷ lệ phần trăm khác nhau

    Tìm giá trị tối ưu cho các sản phẩm khác nhau

  4. Thêm các quy tắc nhập cảnh trở lại

    Đặt các điều kiện tái nhập cảnh sau khi dừng lại

  5. Điều chỉnh độ nghiêm ngặt dừng theo biến động

    Dừng rộng hơn trong môi trường biến động cao

Tóm lại

Chiến lược này sử dụng một điểm dừng tỷ lệ phần trăm sau với một đường kích hoạt trước khi kích hoạt. Cơ chế năng động này cân bằng bảo vệ lợi nhuận và tránh dừng không cần thiết dựa trên sự chuyển động của thị trường. Nhưng các tham số cần tối ưu hóa cho các sản phẩm khác nhau, cộng với các bộ lọc bổ sung trên các mục nhập để cải thiện độ chính xác. Việc nhập lại cũng giúp tránh mất xu hướng sau khi dừng sớm. Cần cải tiến liên tục để thích nghi.


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


Thêm nữa