Chiến lược dừng lỗ theo sau dựa trên khoảng cách giá

Tác giả:ChaoZhang, Ngày: 2023-11-28 13:53:16
Tags:

img

Tổng quan

Chiến lược này áp dụng nguyên tắc chênh lệch giá để đi dài khi giá phá vỡ mức thấp gần đây, với lệnh dừng lỗ và lấy lợi nhuận để theo dõi giá thấp nhất để lấy lợi nhuận.

Chiến lược logic

Nó xác định khoảng cách khi giá phá vỡ dưới mức giá thấp nhất trong N giờ gần đây, đi dài dựa trên tỷ lệ phần trăm được cấu hình, với lệnh dừng lỗ và lấy lợi nhuận.

  1. Tính toán giá thấp nhất trong N giờ gần đây như giá ràng buộc
  2. Đi dài khi giá thời gian thực thấp hơn giá ràng buộc * mua phần trăm
  3. Đặt lợi nhuận dựa trên giá nhập * bán phần trăm
  4. Đặt lệnh dừng lỗ dựa trên giá nhập - giá nhập * tỷ lệ dừng lỗ
  5. Kích thước vị trí là tỷ lệ phần trăm vốn chủ chiến lược
  6. Đường dừng mất mát với giá thấp nhất
  7. Khóa vị trí khi bắt đầu lấy lợi nhuận hoặc dừng lỗ

Phân tích lợi thế

Những lợi thế của chiến lược này:

  1. Sử dụng khái niệm chênh lệch giá, cải thiện tỷ lệ thắng
  2. Tự động dừng lỗ để khóa hầu hết lợi nhuận
  3. Phân phần dừng lỗ và lấy lợi nhuận tùy chỉnh cho các thị trường khác nhau
  4. Hoạt động tốt cho các nhạc cụ với rebounds rõ ràng
  5. Logic đơn giản và dễ thực hiện

Phân tích rủi ro

Ngoài ra còn có một số rủi ro:

  1. Việc phá vỡ khoảng trống có thể thất bại với mức thấp hơn
  2. Cài đặt stop loss hoặc take profit không đúng có thể gây ra việc thoát sớm
  3. Yêu cầu điều chỉnh thông số định kỳ cho những thay đổi thị trường
  4. Các công cụ hạn chế có thể không hiệu quả cho một số
  5. Sự can thiệp bằng tay cần thiết thỉnh thoảng

Hướng dẫn tối ưu hóa

Chiến lược có thể được cải thiện trong các khía cạnh sau:

  1. Thêm các mô hình học máy để điều chỉnh tham số tự động
  2. Thêm nhiều loại lệnh dừng lỗ/lấy lợi nhuận, ví dụ như lệnh dừng lỗ, lệnh khung.
  3. Tối ưu hóa logic dừng lỗ / lấy lợi nhuận để thoát thông minh hơn
  4. Tích hợp nhiều chỉ số hơn để lọc các tín hiệu sai
  5. Mở rộng thêm các công cụ để cải thiện tính phổ quát

Kết luận

Kết luận, đây là một chiến lược dừng lỗ đơn giản và hiệu quả dựa trên khoảng cách giá. Nó làm giảm các mục nhập sai và khóa lợi nhuận hiệu quả. Vẫn còn nhiều chỗ để cải thiện trong điều chỉnh tham số và lọc tín hiệu. Nó đáng nghiên cứu và tinh chỉnh thêm.


/*backtest
start: 2022-11-21 00:00:00
end: 2023-11-27 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4

strategy(title="Squeeze Backtest by Shaqi v1.0", overlay=true, pyramiding=0, currency="USD", process_orders_on_close=true, commission_type=strategy.commission.percent, commission_value=0.075, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=100, backtest_fill_limits_assumption=0)
strategy.risk.allow_entry_in(strategy.direction.long)

R0 = "6 Hours"
R1 = "12 Hours"
R2 = "24 Hours"
R3 = "48 Hours"
R4 = "1 Week"
R5 = "2 Weeks"
R6 = "1 Month"
R7 = "Maximum"


buyPercent = input( title="Buy, %",         type=input.float,   defval=3,       minval=0.01,                        step=0.01,  inline="Percents",  group="Squeeze Settings") * 0.01
sellPercent = input(title="Sell, %",        type=input.float,   defval=1,       minval=0.01,                        step=0.01,  inline="Percents",  group="Squeeze Settings") * 0.01
stopPercent = input(title="Stop Loss, %",   type=input.float,   defval=1,       minval=0.01,        maxval=100,     step=0.01,  inline="Percents",  group="Squeeze Settings") * 0.01
isMaxBars = input(  title="Max Bars To Sell",               type=input.bool,    defval=true ,                                   inline="MaxBars",   group="Squeeze Settings")
maxBars = input(    title="",       type=input.integer, defval=2,     minval=0,           maxval=1000, step=1,                  inline="MaxBars",   group="Squeeze Settings")
bind = input(       title="Bind",           type=input.source,  defval=close,                                                                       group="Squeeze Settings")
isRange = input(    title="Fixed Range",               type=input.bool,    defval=true,                                         inline="Range",     group="Backtesting Period")
rangeStart = input( title="",                       defval=R4,      options=[R0, R1, R2, R3, R4, R5, R6, R7],                   inline="Range",     group="Backtesting Period")
periodStart = input(title="Backtesting Start", type=input.time,    defval=timestamp("01 Aug 2021 00:00 +0000"),                                     group="Backtesting Period")
periodEnd = input(  title="Backtesting End",   type=input.time,    defval=timestamp("01 Aug 2022 00:00 +0000"),                                     group="Backtesting Period")

int startDate = na
int endDate = na
if isRange
    if rangeStart == R0
        startDate := timenow - 21600000
        endDate := timenow
    else if rangeStart == R1
        startDate := timenow - 43200000
        endDate := timenow
    else if rangeStart == R2
        startDate := timenow - 86400000
        endDate := timenow
    else if rangeStart == R3
        startDate := timenow - 172800000
        endDate := timenow
    else if rangeStart == R4
        startDate := timenow - 604800000
        endDate := timenow
    else if rangeStart == R5
        startDate := timenow - 1209600000
        endDate := timenow
    else if rangeStart == R6
        startDate := timenow - 2592000000
        endDate := timenow
    else if rangeStart == R7
        startDate := time
        endDate := timenow
else 
    startDate := periodStart
    endDate := periodEnd

afterStartDate = (time >= startDate)
beforeEndDate = (time <= endDate)
notInTrade = strategy.position_size == 0
inTrade = strategy.position_size > 0

barsFromEntry = barssince(strategy.position_size[0] > strategy.position_size[1])
entry = strategy.position_size[0] > strategy.position_size[1]
entryBar = barsFromEntry == 0
notEntryBar = barsFromEntry != 0
buyLimitPrice = bind - bind * buyPercent
buyLimitFilled = low <= buyLimitPrice
sellLimitPriceEntry = buyLimitPrice * (1 + sellPercent)
sellLimitPrice = strategy.position_avg_price * (1 + sellPercent)

stopLimitPriceEntry = buyLimitPrice - buyLimitPrice * stopPercent
stopLimitPrice = strategy.position_avg_price - strategy.position_avg_price * stopPercent

if afterStartDate and beforeEndDate and notInTrade
    strategy.entry("BUY", true, limit = buyLimitPrice)
    strategy.exit("INSTANT", limit = sellLimitPriceEntry, stop = stopLimitPriceEntry)
strategy.cancel("INSTANT", when = inTrade)
if isMaxBars
    strategy.close("BUY", when = barsFromEntry >= maxBars, comment = "Don't Sell")
strategy.exit("SELL", limit = sellLimitPrice, stop = stopLimitPrice)

showStop = stopPercent <= 0.03

plot(showStop ? stopLimitPrice : na, title="Stop Loss Limit Order", style=plot.style_linebr, color=color.red, linewidth=1)
plot(sellLimitPrice, title="Take Profit Limit Order", style=plot.style_linebr, color=color.purple, linewidth=1)
plot(strategy.position_avg_price, title="Buy Order Filled Price", style=plot.style_linebr, color=color.blue, linewidth=1)
plot(buyLimitPrice, title="Trailing Buy Limit Order", style=plot.style_stepline, color=color.new(color.blue, 30), offset=1)



Thêm nữa