Chiến lược dừng trượt


Ngày tạo: 2023-10-07 16:11:45 sửa đổi lần cuối: 2023-10-07 16:11:45
sao chép: 0 Số nhấp chuột: 701
1
tập trung vào
1617
Người theo dõi

Tổng quan

Chiến lược này sử dụng bộ lọc Kalman để theo dõi giá và sử dụng đường dừng để điều chỉnh động điểm dừng để thực hiện dừng trượt.

Nguyên tắc

Chiến lược này sử dụng bộ lọc Kalman để theo dõi giá trong thời gian thực. Bộ lọc Kalman bao gồm hai phương trình:

Phương thức dự đoán:

smooth = kf[1] + dk * sqrt(gain / 10000 * 2)

Cách thức cập nhật:

kf = smooth + velo

Trong đó, dk là sai số dự đoán, gain là lợi nhuận của Kalman, và quyết định theo dõi độ nhạy.

Ngoài ra, chiến lược sử dụng đường dừng trượt để khóa lợi nhuận. Khoảng cách dừng ban đầu là tỷ lệ phần trăm dừng, chẳng hạn như 2%.

Trong quá trình thực hiện, nếu giá tăng, đường dừng cũng di chuyển lên dần dần gần đường Kalman, bước dài là downStep, như 0.5%. Nếu giá giảm, dừng lại, mở lại vị trí và thiết lập khoảng cách dừng ban đầu.

Hãy làm như vậy.

Do đó, chiến lược có thể khóa lợi nhuận theo xu hướng và có quản lý rủi ro tốt hơn.

Ưu điểm

  1. Sử dụng bộ lọc Kalman để theo dõi giá cả trong thời gian thực và phản ứng nhanh chóng.

  2. Sử dụng đường dừng trượt để khóa lợi nhuận, quản lý rủi ro có hiệu quả. Khoảng cách dừng lỗ có thể được tùy chỉnh.

  3. Bạn có thể lựa chọn làm thêm hoặc chỉ làm thêm / làm thêm.

  4. Có thể dừng tích cực hoặc dừng bảo vệ tùy theo xu hướng.

  5. Cài đặt dừng lỗ có thể linh hoạt tùy theo nhu cầu.

Rủi ro

  1. Các tham số của bộ lọc Kalman được thiết lập không chính xác có thể gây ra sự không ổn định theo dõi.

  2. Điểm trượt có thể dẫn đến điểm dừng bị kích hoạt trước.

  3. Thị trường có xu hướng mạnh không nên sử dụng chiến lược dừng lỗ trượt, nên theo dõi xu hướng.

  4. Các điểm dừng của thị trường chấn động có thể được kích hoạt thường xuyên. Bạn có thể nới lỏng khoảng cách dừng một cách thích hợp, hoặc không sử dụng dừng trượt.

Tối ưu hóa

  1. Có thể giới thiệu thêm các chỉ số để xác định xu hướng và tối ưu hóa thời gian mở vị trí.

  2. Bạn có thể điều chỉnh độ dài của đường dừng để phù hợp với sự biến động của thị trường.

  3. Có thể kết hợp với kỹ thuật học máy để đào tạo các tham số dừng tối ưu.

  4. Có thể kết hợp với nhiều chỉ số rủi ro, quản lý vị trí điều chỉnh động.

Tóm tắt

Chiến lược dừng trượt sử dụng bộ lọc Kalman để theo dõi sự thay đổi của giá và sử dụng đường dừng trượt để khóa lợi nhuận, kiểm soát rủi ro trong khi đảm bảo lợi nhuận, là một chiến lược đáng tin cậy và dễ dàng tối ưu hóa. Kết hợp với kỹ thuật phán đoán xu hướng và quản lý vị trí động, có thể có hiệu quả chiến lược tốt hơn.

Mã nguồn chiến lược
/*backtest
start: 2023-09-06 00:00:00
end: 2023-10-06 00:00:00
period: 2h
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/
// © BigCoinHunter

//@version=5
// strategy(title='Loft Strategy V1', overlay=true, 
//      pyramiding=0, default_qty_type=strategy.fixed, 
//      default_qty_value=100, initial_capital=100000, 
//      currency=currency.USD, commission_value=0.05, 
//      commission_type=strategy.commission.percent, 
//      process_orders_on_close=true)

//-------------- fetch user inputs ------------------
gain = input.float(title="Kalman Gain:", defval=1.0, minval=1.0, maxval=5000.0, step=100.0)
src = input(defval=close, title='Source:')

stopPercentMax = input.float(title='Beginning Approach(%):', defval=2.0, minval=0.1, maxval=30.0, step=0.1)
stopPercentMin = input.float(title='Final Approach(%):    ', defval=0.5, minval=0.1, maxval=30.0, step=0.1)
downStep = input.float(title='Approach Decrease Step:', defval=0.005, minval=0.0, maxval = 5, step=0.005)

tp = input.float(title="Take Profit:", defval=1.5, minval=0.0, maxval=100.0, step=0.1) * 0.01
sl = input.float(title="Stop Loss:  ", defval=0.0, minval=0.0, maxval=100.0, step=0.1) * 0.01

longEntry = input.bool(defval=true, title= 'Long Entry', inline="11")
shortEntry = input.bool(defval=true, title='Short Entry', inline="11")

//---------- backtest range setup ------------
fromDay   = input.int(defval = 1, title = "From Day", minval = 1, maxval = 31)
fromMonth = input.int(defval = 1, title = "From Month", minval = 1, maxval = 12)
fromYear  = input.int(defval = 2021, title = "From Year", minval = 2010)
toDay     = input.int(defval = 30, title = "To Day", minval = 1, maxval = 31)
toMonth   = input.int(defval = 12, title = "To Month", minval = 1, maxval = 12)
toYear    = input.int(defval = 2022, title = "To Year", minval = 2010)


//------------ time interval setup -----------
start     = timestamp(fromYear, fromMonth, fromDay, 00, 00)  // backtest start window
finish    = timestamp(toYear, toMonth, toDay, 23, 59)        // backtest finish window
window()  => true // create function "within window of time"

//------- define the global variables ------
enterLongComment = "ENTER LONG"
exitLongComment = "EXIT LONG"

enterShortComment = "ENTER SHORT"
exitShortComment = "EXIT SHORT"

longTPSL = "Long TP/SL"
longTP = "Long TP"
longSL = "Long SL"
shortTPSL = "Short TP/SL"
shortTP = "Short TP"
shortSL = "Short SL"

var bool long = true
var bool stoppedOutLong = false
var bool stoppedOutShort = false
var float kf = 0.0
var float velo = 0.0

//------ kalman filter calculation --------
dk = src - nz(kf[1], src)
smooth = nz(kf[1], src) + dk * math.sqrt(gain / 10000 * 2)
velo := nz(velo[1], 0) + gain / 10000 * dk
kf := smooth + velo

//--------- calculate the loft stopLoss line ---------
var stopPercent = stopPercentMax
var stopLoss = kf - kf * (stopPercent /100)

if long == true
    stopLoss := kf - (kf * (stopPercent / 100))
    
    if long[1] == true and stopLoss <= stopLoss[1]
        stopLoss := stopLoss[1]
    else if (long[1] == true)
        stopPercent := stopPercent - downStep
        if(stopPercent < stopPercentMin)
            stopPercent := stopPercentMin
    
    if(kf < stopLoss)
        long := false
        stopPercent := stopPercentMax
        stopLoss := kf + (kf * (stopPercent / 100))
        
else
    stopLoss := kf + (kf * (stopPercent / 100))
    
    if long[1] == false and stopLoss >= stopLoss[1]
        stopLoss := stopLoss[1]
    else if(long[1] == false)
        stopPercent := stopPercent - downStep
        if(stopPercent < stopPercentMin)
            stopPercent := stopPercentMin
            
    if(kf > stopLoss)
        long := true
        stopPercent := stopPercentMax
        stopLoss := kf - (kf * (stopPercent / 100))
        
//--------- calculate the input/output points -----------
longProfitPrice  = strategy.position_avg_price * (1 + tp)     // tp -> take profit percentage
longStopPrice = strategy.position_avg_price * (1 - sl)        // sl -> stop loss percentage

shortProfitPrice  = strategy.position_avg_price * (1 - tp)
shortStopPrice = strategy.position_avg_price * (1 + sl)

//------------------- determine buy and sell points ---------------------
buySignall = window() and long  and (not stoppedOutLong)
sellSignall = window() and (not long)  and (not stoppedOutShort)

//---------- execute the strategy -----------------
if(longEntry and shortEntry)
    if long 
        strategy.entry("LONG", strategy.long, when = buySignall, comment = enterLongComment)
        stoppedOutLong := true
        stoppedOutShort := false
    else 
        strategy.entry("SHORT", strategy.short, when = sellSignall, comment = enterShortComment)
        stoppedOutLong  := false
        stoppedOutShort := true

else if(longEntry)
    strategy.entry("LONG", strategy.long,  when = buySignall, comment = enterLongComment)
    strategy.close("LONG", when = sellSignall, comment = exitLongComment)
    if long 
        stoppedOutLong := true
    else
        stoppedOutLong  := false

else if(shortEntry)
    strategy.entry("SHORT", strategy.short, when = sellSignall, comment = enterShortComment)
    strategy.close("SHORT", when = buySignall, comment = exitShortComment)
    if not long
        stoppedOutShort := true
    else
        stoppedOutShort := false
    

//----------------- take profit and stop loss -----------------
if(tp>0.0 and sl>0.0)
    if ( strategy.position_size > 0 )
        strategy.exit(id="LONG", limit=longProfitPrice, stop=longStopPrice, comment = longTPSL)

    else if ( strategy.position_size < 0 )
        strategy.exit(id="SHORT", limit=shortProfitPrice, stop=shortStopPrice, comment = shortTPSL)

else if(tp>0.0)
    if ( strategy.position_size > 0 )
        strategy.exit(id="LONG", limit=longProfitPrice, comment = longTP)

    else if ( strategy.position_size < 0 )
        strategy.exit(id="SHORT", limit=shortProfitPrice, comment = shortTP)
        
else if(sl>0.0)
    if ( strategy.position_size > 0 )
        strategy.exit(id="LONG",  stop=longStopPrice, comment = longSL)

    else if ( strategy.position_size < 0 )
        strategy.exit(id="SHORT",  stop=shortStopPrice, comment = shortSL)
        
//------------- plot charts ---------------------
lineColor1 = long ? color.green : color.red
lineColor2 = long ? color.aqua : color.fuchsia

kalmanLine = plot(kf, color=lineColor1, linewidth=3, title = "Kalman Filter")
stopLine = plot(stopLoss, color=lineColor2, linewidth=2, title = "Stop Loss Line")