Chiến lược kiểm tra ngược động đa khung thời gian

Tác giả:ChaoZhang, Ngày: 2023-11-21 17:07:17
Tags:

img

Tổng quan

Chiến lược này sử dụng một cơ chế kiểm tra ngược động nhiều khung thời gian để xác định xu hướng giá bằng cách so sánh giá cao nhất và thấp nhất trong các khoảng thời gian khác nhau, do đó đạt được sự điều chỉnh rủi ro thấp.

Chiến lược logic

Chiến lược lấy giá cao nhất (nhigh) và giá thấp nhất (nlow) trên các khung thời gian khác nhau bằng cách gọi hàm tùy chỉnh f_get_htfHighLow. Cụ thể, dựa trên các đầu vào được xác định bởi người dùng như độ phân giải khoảng thời gian, nhân khoảng thời gian HTFMultiplier, tham số backtesting lookahead và khoảng trống, và offset, nó gọi chức năng bảo mật để có được giá cao nhất và thấp nhất trong các khung thời gian khác nhau.

Ví dụ, một offset của 0 lấy giá cao nhất và thấp nhất của thanh hiện tại, trong khi một offset của 1 lấy giá đó từ thanh trước. Bằng cách so sánh sự thay đổi giá giữa các thanh, hướng xu hướng được xác định.

Nếu cả giá cao nhất và giá thấp nhất tăng, một xu hướng tăng được xác định. Nếu cả hai giá giảm, một xu hướng giảm được nhìn thấy. Các vị trí dài hạn hoặc ngắn được thực hiện dựa trên hướng xu hướng để thực hiện các giao dịch chênh lệch.

Ưu điểm

  1. Cải thiện độ chính xác thông qua phân tích nhiều khung thời gian
  2. Tránh sơn lại thông qua dynamic backtesting
  3. Các tham số linh hoạt phù hợp với những thay đổi trên thị trường
  4. Rủi ro giảm với các vị trí chỉ có xu hướng rõ ràng

Rủi ro

  1. Những sai lầm về nhiều khung thời gian
  2. Sơn lại từ các thông số backtesting không phù hợp
  3. Chi phí cao và trượt từ giao dịch quá mức

Giải pháp:

  1. Tối ưu hóa khoảng thời gian cho độ chính xác
  2. Kiểm tra các thông số nghiêm ngặt để ngăn ngừa sơn lại
  3. Điều kiện nhập trung bình để điều khiển tần số

Cơ hội gia tăng

  1. Thêm ML để tận dụng AI cho xu hướng
  2. Tích hợp các bộ lọc biến động để định kích thước vị trí động
  3. Thiết lập các điểm dừng để hạn chế lỗ hiệu quả

Kết luận

Chiến lược logic là rõ ràng, sử dụng nhiều khung thời gian năng động backtesting để xác định xu hướng và giảm thiểu thiên vị của con người.


/*backtest
start: 2022-11-14 00:00:00
end: 2023-11-20 00:00:00
period: 1d
basePeriod: 1h
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/
// © HeWhoMustNotBeNamed

//@version=4
strategy("HTF High/Low Repaint Strategy", overlay=true, initial_capital = 20000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, pyramiding = 1, commission_value = 0.01)

i_startTime = input(defval = timestamp("01 Jan 2010 00:00 +0000"), title = "Start Time", type = input.time)
i_endTime = input(defval = timestamp("01 Jan 2099 00:00 +0000"), title = "End Time", type = input.time)
inDateRange = true

resolution = input("3M", type=input.resolution)
HTFMultiplier = input(22, minval=1, step=1)
offset = input(0, minval=0, step=1)
lookahead = input(true)
gaps = false

f_secureSecurity_on_on(_symbol, _res, _src, _offset) => security(_symbol, _res, _src[_offset], lookahead = barmerge.lookahead_on, gaps=barmerge.gaps_on)
f_secureSecurity_on_off(_symbol, _res, _src, _offset) => security(_symbol, _res, _src[_offset], lookahead = barmerge.lookahead_on, gaps=barmerge.gaps_off)
f_secureSecurity_off_on(_symbol, _res, _src, _offset) => security(_symbol, _res, _src[_offset], lookahead = barmerge.lookahead_off, gaps=barmerge.gaps_on)
f_secureSecurity_off_off(_symbol, _res, _src, _offset) => security(_symbol, _res, _src[_offset], lookahead = barmerge.lookahead_off, gaps=barmerge.gaps_off)

f_multiple_resolution(HTFMultiplier) => 
    target_Res_In_Min = timeframe.multiplier * HTFMultiplier * (
      timeframe.isseconds   ? 1. / 60. :
      timeframe.isminutes   ? 1. :
      timeframe.isdaily     ? 1440. :
      timeframe.isweekly    ? 7. * 24. * 60. :
      timeframe.ismonthly   ? 30.417 * 24. * 60. : na)

    target_Res_In_Min     <= 0.0417       ? "1S"  :
      target_Res_In_Min   <= 0.167        ? "5S"  :
      target_Res_In_Min   <= 0.376        ? "15S" :
      target_Res_In_Min   <= 0.751        ? "30S" :
      target_Res_In_Min   <= 1440         ? tostring(round(target_Res_In_Min)) :
      tostring(round(min(target_Res_In_Min / 1440, 365))) + "D"

f_get_htfHighLow(resolution, HTFMultiplier, lookahead, gaps, offset)=>
    derivedResolution = resolution == ""?f_multiple_resolution(HTFMultiplier):resolution
    nhigh_on_on = f_secureSecurity_on_on(syminfo.tickerid, derivedResolution, high, offset) 
    nlow_on_on = f_secureSecurity_on_on(syminfo.tickerid, derivedResolution, low, offset)
    
    nhigh_on_off = f_secureSecurity_on_off(syminfo.tickerid, derivedResolution, high, offset) 
    nlow_on_off = f_secureSecurity_on_off(syminfo.tickerid, derivedResolution, low, offset)
    
    nhigh_off_on = f_secureSecurity_off_on(syminfo.tickerid, derivedResolution, high, offset) 
    nlow_off_on = f_secureSecurity_off_on(syminfo.tickerid, derivedResolution, low, offset)
    
    nhigh_off_off = f_secureSecurity_off_off(syminfo.tickerid, derivedResolution, high, offset) 
    nlow_off_off = f_secureSecurity_off_off(syminfo.tickerid, derivedResolution, low, offset)
    
    nhigh = lookahead and gaps ? nhigh_on_on :
             lookahead and not gaps ? nhigh_on_off :
             not lookahead and gaps ? nhigh_off_on :
             not lookahead and not gaps ? nhigh_off_off : na
    nlow = lookahead and gaps ? nlow_on_on :
             lookahead and not gaps ? nlow_on_off :
             not lookahead and gaps ? nlow_off_on :
             not lookahead and not gaps ? nlow_off_off : na
    [nhigh, nlow]
    
[nhigh, nlow] = f_get_htfHighLow(resolution, HTFMultiplier, lookahead, gaps, offset)
[nhighlast, nlowlast] = f_get_htfHighLow(resolution, HTFMultiplier, lookahead, gaps, offset+1)
plot(nhigh , title="HTF High",style=plot.style_circles, color=color.green, linewidth=1) 
plot(nlow , title="HTF Low",style=plot.style_circles, color=color.red, linewidth=1)

buyCondition = nhigh > nhighlast and nlow > nlowlast
sellCondition = nhigh < nhighlast and nlow < nlowlast

strategy.entry("Buy", strategy.long, when= buyCondition and inDateRange, oca_name="oca_buy")
strategy.entry("Sell", strategy.short, when= sellCondition and inDateRange, oca_name="oca_sell")


Thêm nữa