マルチタイムフレームダイナミックバックテスト戦略


作成日: 2023-11-21 17:07:17 最終変更日: 2023-11-21 17:07:17
コピー: 1 クリック数: 698
1
フォロー
1617
フォロワー

マルチタイムフレームダイナミックバックテスト戦略

概要

この戦略は,複数のタイムフレームのダイナミック・バックグラウンド・メカニズムを利用し,異なるタイムピークの最高価格と最低価格を比較し,価格の傾向を判断し,低リスクの利を実現する.

戦略原則

この策略は,カスタム関数f_get_htfHighLowを呼び出すことで,異なる時間周期の最高値 nhighと最低値 nlowを取得する.具体的には,ユーザが設定した時間周期の resolution,時間周期の倍数 HTFMultiplier,回帰パラメータのlookaheadとgaps,および偏移量オフセットに応じて,security関数を呼び出すことで,異なる時間周期の最高値と最低値を取得する.

例えば,オフセットが0であるとき,現在のK線の最高値と最低値が得られます.オフセットが1であるとき,前K線の最高値と最低値が得られます.二つのK線間の価格の変化を比較して,トレンドの方向を判断します.

最高値が上がり,最低値が上がった場合,看板トレンドと判断する.最高値が下がり,最低値が下がった場合,看板トレンドと判断する.傾向の方向に応じてロングまたはショートし,利取引を実現する.

戦略的優位性

  1. 複数の時間枠分析を用いて判断の正確性を向上させる
  2. Repainting を避けるために,動的な回帰を適用する
  3. 市場の変化に対応するために,異なるパラメータの組み合わせを柔軟に設定する
  4. トレンドがはっきりしたときにのみポジションを開き,リスクを効果的にコントロールする

戦略リスク

  1. 複数の時間枠で判断すると 誤判の危険性があります
  2. 逆行パラメータの設定が不適切で,repainting が起こる可能性があります.
  3. 取引頻度が高くなり,取引コストと滑り込みリスクが増加する

解決策は

  1. タイムサイクルパラメータを最適化し,判断の正確性を向上させる
  2. 厳格にテストして,再塗装を避ける
  3. ポジション開設条件を適切に調整し,取引頻度を制御する

戦略最適化の方向性

  1. 機械学習モジュールを追加し,AIを活用してトレンド判断
  2. 株価の変動とポジションの動的調整
  3. 損失のリスクを効果的にコントロールする為の 止損メカニズムへの参加

要約する

この戦略は,全体的な考えが明確で,複数の時間枠を動的に遡って株価のトレンドを判断し,人為判断誤りを最大限に減らす.これは典型的なプログラム化された取引戦略である.パラメータの最適化と機能拡張により,戦略の安定性と利益の余地がさらに強化され,深入な研究と追跡に値する.

ストラテジーソースコード
/*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")