Multi Timeframe Dynamic Backtesting Strategi

Penulis:ChaoZhang, Tanggal: 2023-11-21 17:07:17
Tag:

img

Gambaran umum

Strategi ini menggunakan mekanisme backtesting dinamis multi-frame untuk menentukan tren harga dengan membandingkan harga tertinggi dan terendah di berbagai periode waktu, sehingga mencapai arbitrage berisiko rendah.

Logika Strategi

Strategi ini mengambil harga tertinggi (nhigh) dan harga terendah (nlow) di rentang waktu yang berbeda dengan memanggil fungsi kustom f_get_htfHighLow. Secara khusus, berdasarkan input yang ditentukan pengguna seperti resolusi periode waktu, pengganda periode waktu HTFMultiplier, parameter backtesting lookhead dan gap, dan offset, ia memanggil fungsi keamanan untuk mendapatkan harga tertinggi dan terendah di rentang waktu yang berbeda.

Sebagai contoh, offset 0 mengambil harga tertinggi dan terendah dari bar saat ini, sementara offset 1 mengambil harga dari bar sebelumnya.

Jika kedua harga tertinggi dan terendah naik, tren bullish diidentifikasi. Jika kedua harga turun, tren bearish terlihat. Posisi lama atau pendek diambil berdasarkan arah tren untuk menerapkan perdagangan arbitrage.

Keuntungan

  1. Keakuratan yang ditingkatkan melalui analisis multi timeframe
  2. Menghindari repainting melalui backtesting dinamis
  3. Parameter yang fleksibel untuk menyesuaikan perubahan pasar
  4. Risiko yang berkurang dengan posisi hanya dalam tren yang jelas

Risiko

  1. Kesalahan penilaian dalam beberapa periode waktu
  2. Menggambar ulang dari parameter backtesting yang tidak benar
  3. Biaya tinggi dan tergelincir dari perdagangan yang berlebihan

Solusi:

  1. Optimalkan periode waktu untuk akurasi
  2. Parameter uji ketat untuk mencegah melukis ulang
  3. Kondisi masuk yang moderat untuk mengontrol frekuensi

Peluang Peningkatan

  1. Tambahkan ML untuk memanfaatkan AI untuk tren
  2. Mengintegrasikan filter volatilitas untuk ukuran posisi dinamis
  3. Memperkenalkan stop untuk membatasi kerugian secara efektif

Kesimpulan

Logika strategi jelas, menggunakan backtesting dinamis multi timeframe untuk menentukan tren dan meminimalkan bias manusia. Dengan penyempurnaan melalui optimasi parameter dan perluasan fitur, ini menunjukkan potensi signifikan untuk meningkatkan stabilitas dan profitabilitas yang layak untuk penelitian lebih lanjut dan pelacakan.


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


Lebih banyak