Chiến lược giao dịch ngắn hạn đột phá đường K ngược


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

Chiến lược giao dịch ngắn hạn đột phá đường K ngược

Tổng quan

Chiến lược này được sử dụng để nắm bắt các cơ hội giao dịch đảo ngược đường ngắn. Nó sẽ mở trương vị sau khi đường N gốc K liên tiếp tăng, và trương vị sau khi đường M gốc K liên tiếp giảm. Đồng thời, chiến lược này đã thêm vào các chức năng hạn chế thời gian và dừng lỗ.

Nguyên tắc

  1. Các tham số đầu vào: số dòng K tăng liên tục N, số dòng K giảm liên tục M
  2. Định nghĩa logic:
    • ups số liệu thống kê tăng K, giá> giá[1] là +1, nếu không thì reset là 0
    • dns thống kê giảm số dòng K, price
  3. Bước vào: khi ups≥N, tháo lỗ; khi dns≥M, trơn
  4. Ra sân: Đặt điểm dừng lỗ hoặc kết thúc khoảng thời gian

Ưu điểm

  1. Bắt được cơ hội giao dịch ngược, phù hợp với hoạt động ngắn hạn
  2. Có thể thiết lập thời gian giao dịch linh hoạt để phù hợp với các chương trình giao dịch khác nhau
  3. Chức năng dừng thiệt hại tích hợp giúp kiểm soát rủi ro

Rủi ro

  1. Việc đảo ngược đường ngắn không nhất thiết phải thành công, có thể dẫn đến tổn thất khi quay lại
  2. Cần thiết lập các tham số N, M hợp lý, quá lớn hoặc quá nhỏ là không tốt
  3. Không đúng thời gian dừng có thể không dừng đúng thời gian

Hướng tối ưu hóa

  1. Kết hợp các chỉ số xu hướng để tránh hoạt động ngược
  2. Động thái điều chỉnh tham số
  3. Tối ưu hóa hệ thống ngăn chặn thiệt hại

Tóm tắt

Chiến lược này nắm bắt cơ hội giao dịch ngắn hạn bằng cách tính toán K-line. Thiết lập các tham số hợp lý và các biện pháp kiểm soát gió là rất quan trọng để có được lợi nhuận ổn định. Hiệu quả tốt hơn có thể đạt được bằng cách kết hợp thêm các tham số định hướng và điều chỉnh động.

Mã nguồn chiến lược
/*backtest
start: 2023-11-13 00:00:00
end: 2023-11-20 00:00:00
period: 3h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4

// Strategy
strategy("Up/Down Short Strategy", overlay=true, initial_capital = 10000, default_qty_value = 10000, default_qty_type = strategy.cash)

// There will be no short entries, only exits from long.
strategy.risk.allow_entry_in(strategy.direction.short)

consecutiveBarsUp = input(1, title='Consecutive Bars Up')
consecutiveBarsDown = input(1, title='Consecutive Bars Down')

price = close

ups = 0.0
ups := price > price[1] ? nz(ups[1]) + 1 : 0

dns = 0.0
dns := price < price[1] ? nz(dns[1]) + 1 : 0

// Strategy Backtesting
startDate  = input(timestamp("2021-01-01T00:00:00"), type = input.time, title='Backtesting Start Date')
finishDate = input(timestamp("2021-12-31T00:00:00"), type = input.time, title='Backtesting End Date')

time_cond  = true

//Time Restriction Settings
startendtime = input("", title='Time Frame To Enter Trades')
enableclose = input(false, title='Enable Close Trade At End Of Time Frame')
timetobuy = (time(timeframe.period, startendtime))
timetoclose = na(time(timeframe.period, startendtime))

// Stop Loss & Take Profit Tick Based
enablesltp = input(false, title='Enable Take Profit & Stop Loss')
stopTick = input(5.0, title='Stop Loss Ticks', type=input.float) / 100
takeTick = input(10.0, title='Take Profit Ticks', type=input.float) / 100

longStop = strategy.position_avg_price - stopTick
shortStop = strategy.position_avg_price + stopTick
shortTake = strategy.position_avg_price - takeTick
longTake = strategy.position_avg_price + takeTick

plot(strategy.position_size > 0 and enablesltp ? longStop : na, style=plot.style_linebr, color=color.red, linewidth=1, title="Long Fixed SL")
plot(strategy.position_size < 0 and enablesltp ? shortStop : na, style=plot.style_linebr, color=color.red, linewidth=1, title="Short Fixed SL")
plot(strategy.position_size > 0 and enablesltp ? longTake : na, style=plot.style_linebr, color=color.green, linewidth=1, title="Long Take Profit")
plot(strategy.position_size < 0 and enablesltp ? shortTake : na, style=plot.style_linebr, color=color.green, linewidth=1, title="Short Take Profit")

// Alert messages
message_enterlong  = input("", title="Long Entry message")
message_entershort = input("", title="Short Entry message")
message_closelong = input("", title="Close Long message")
message_closeshort = input("", title="Close Short message")
message_takeprofit = input("", title="Take Profit message")
message_stoploss = input("", title="Stop Loss message")

// Strategy Execution
if (ups >= consecutiveBarsUp) and time_cond and timetobuy
    strategy.entry("Long", strategy.long, stop = high + syminfo.mintick, alert_message = message_enterlong)
    
if (dns >= consecutiveBarsDown) and time_cond and timetobuy
    strategy.entry("Short", strategy.short, stop = low + syminfo.mintick, alert_message = message_entershort)
    
if strategy.position_size > 0 and timetoclose and enableclose
    strategy.close_all(alert_message = message_closelong)
if strategy.position_size < 0 and timetoclose and enableclose
    strategy.close_all(alert_message = message_closeshort)
    
if strategy.position_size > 0 and enablesltp and time_cond
    strategy.exit(id="Close Long", stop=longStop, limit=longTake, alert_message = message_takeprofit)
if strategy.position_size < 0 and enablesltp and time_cond
    strategy.exit(id="Close Short", stop=shortStop, limit=shortTake, alert_message = message_stoploss)