スクイーズバックテストトランスフォーマー v2.0


作成日: 2024-04-28 14:09:26 最終変更日: 2024-04-28 14:09:26
コピー: 0 クリック数: 536
1
フォロー
1617
フォロワー

スクイーズバックテストトランスフォーマー v2.0

概要

エクスプレス回測変形金剛v2.0は,エクスプレス型戦略に基づいた量化取引システムである.これは,入場,止損,停止のパーセント,および最大ポジション保持時間などのパラメータを設定することによって,特定の時間枠で戦略を反測する.この戦略は,多方向の取引をサポートし,取引方向を多額のまたは空っぽのように柔軟に設定することができます.同時に,この戦略は,固定時間または範囲の最大反測時間を容易に選択するための豊富な反測期間の設定オプションを提供します.

戦略原則

  1. まず,ユーザが設定した反測期パラメータに基づいて,反測の開始時間と終了時間を決定する.
  2. 回測期間に,当時にポジションを保有していない場合と,価格がエントリー価格に触れた場合 ((開設比率に基づいて計算),ポジションを開設し,同時にストップ・ロズとストップ・ストップの価格を設定します ((ストップ・ロズとストップ・ストップ比率に基づいて計算).
  3. ポジションが既に保有されている場合,以前のストップ・ストラスト・オールを取り消し,新しいストップ・ストラスト価格を再設定します (現在のポジション保有平均価格に基づいて計算されます).
  4. 最大保有時間設定がある場合,保有時間が最大値に達すると,強制的に平仓する.
  5. 戦略は,両方向の取引をサポートします.

戦略的優位性

  1. パラメータの設定は柔軟で,異なる市場状況や取引需要に応じて調整できます.
  2. 多方向取引がサポートされ,異なる市場状況で利益を得ることができます.
  3. 復元期設定の選択肢が豊富で,履歴データを簡単に復元して分析できます.
  4. ストップ・ローズとストップ・ストップの設定は,リスクを効果的に制御し,資金利用の効率を向上させる.
  5. 最大保有時間設定は,長期にわたって保有し,市場リスクを回避する.

戦略リスク

  1. 入場価格,止損価格,およびストップ価格の設定は,戦略的利益に大きく影響し,誤ったパラメータ設定は,損失を引き起こす可能性があります.
  2. 市場が激しく波動すると,ポジション開設後すぐにストップ・ロスを引き起こすことがあり,その結果,損失が発生する.
  3. ポジションを保有する際に最大保有時間平仓を触発した場合,その後の利益を得る機会を逃す危険性がある.
  4. 戦略は,特定の状況 (例えば,波動市場) でうまく機能しない可能性があります.

戦略最適化の方向性

  1. より多くの技術指標や市場情緒指標を導入し,入場,止損,停止条件を最適化し,戦略の安定性や収益性を向上させることが考えられます.
  2. 最大保有時間の設定は,市場の変動と保有利回りの状況に応じて動的に調整することができ,固定時間平仓がもたらす可能性のある機会コストを避ける.
  3. 震動市場の特徴に合わせて,震動区間の突破やトレンド転換確認などの論理を組み込むことで,頻繁に取引がもたらすコストを減らすことができる.
  4. ポジション管理と資金管理戦略を導入し,単一取引リスクの口を制御し,資金利用の効率と安定性を向上させる.

要約する

エクスプレス・リターン・変形金剛 v2.0は,エクスプレス型戦略に基づく量化取引システムで,柔軟なパラメータ設定と多方向取引サポートにより,異なる市場環境で取引が可能である.同時に,豊富なリターン期設定オプションとストップ・ストップ・損失設定は,歴史データ分析とリスク管理をユーザーに助けることができる.しかし,戦略のパフォーマンスはパラメータ設定によって大きく影響され,戦略の安定性と収益性を高めるために,市場の特徴と取引需要に応じて最適化と改善が必要である.将来,より多くの技術指標の導入,最大ポジション保持時間の動態調整,振動性市場の戦略の最適化,ポジションと資金管理の強化などの最適化を考慮することができる.

ストラテジーソースコード
/*backtest
start: 2023-04-22 00:00:00
end: 2024-04-27 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5

strategy(title="Squeeze Backtest by Shaqi v2.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)
R0 = "6 Hours"
R1 = "12 Hours"
R2 = "24 Hours"
R3 = "48 Hours"
R4 = "1 Week"
R5 = "2 Weeks"
R6 = "1 Month"
R7 = "Maximum"

BL = "low"
BH = "high"
BO = "open"
BC = "close"
BHL= "mid (hl)"
BOC = "mid (oc)"

LONG = "LONG"
SHORT = "SHORT"

direction = input.string(title="Direction", defval=LONG, options=[LONG, SHORT], group="Squeeze Settings")
strategy.risk.allow_entry_in(direction == LONG ? strategy.direction.long : strategy.direction.short)
openPercent = input.float(1.4, "Open, %", minval=0.01, maxval=100, step=0.1, inline="Percents", group="Squeeze Settings") * 0.01
closePercent = input.float(0.6, "Close, %", minval=0.01, maxval=100, step=0.1, inline="Percents", group="Squeeze Settings") * 0.01
stopPercent = input.float(0.8, "Stop Loss, %", minval=0.01, maxval=100, step=0.1, inline="Percents", group="Squeeze Settings") * 0.01
isMaxBars = input.bool(true, "Max Bars To Sell", inline="MaxBars", group="Squeeze Settings")
maxBars = input.int(10, title="", minval=0, maxval=1000, step=1, inline="MaxBars", group="Squeeze Settings")
bind = input.string(BC, "Bind", options=[BL, BH, BO, BC, BHL, BOC], group="Squeeze Settings")
isRange = input.bool(true, "Fixed Range", inline="Range", group="Backtesting Period")
rangeStart = input.string(R2, "", options=[R0, R1, R2, R3, R4, R5, R6, R7], inline="Range", group="Backtesting Period")
periodStart = input(timestamp("12 Apr 2024 00:00 +0000"), "Backtesting Start", group="Backtesting Period")
periodEnd = input(timestamp("20 Apr 2024 00:00 +0000"), "Backtesting End", 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
    
float bindOption = na
if bind == BL
    bindOption := low
else if bind == BH
    bindOption := high
else if bind == BO
    bindOption := open
else if bind == BC
    bindOption := close
else if bind == BHL
    bindOption := hl2
else
    bindOption := ohlc4

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

barsFromEntry = ta.barssince(strategy.position_size[0] > strategy.position_size[1])
entry = strategy.position_size[0] > strategy.position_size[1]
entryBar = barsFromEntry == 0
notEntryBar = barsFromEntry != 0
openLimitPrice = direction == LONG ? (bindOption - bindOption * openPercent) : (bindOption + bindOption * openPercent)

closeLimitPriceEntry = openLimitPrice * (direction == LONG ? 1 + closePercent : 1 - closePercent)
closeLimitPrice = strategy.position_avg_price * (direction == LONG ? 1 + closePercent : 1 - closePercent)

stopLimitPriceEntry = direction == LONG ? openLimitPrice - openLimitPrice * stopPercent : openLimitPrice + openLimitPrice * stopPercent
stopLimitPrice = direction == LONG ? strategy.position_avg_price - strategy.position_avg_price * stopPercent : strategy.position_avg_price + strategy.position_avg_price * stopPercent

if periodCondition and notInTrade
    strategy.entry(direction == LONG ? "BUY" : "SELL", direction == LONG ? strategy.long : strategy.short, limit = openLimitPrice, stop = stopLimitPriceEntry)
    strategy.exit("INSTANT", limit = closeLimitPriceEntry, stop = stopLimitPriceEntry, comment_profit = direction == LONG ? 'INSTANT SELL' : 'INSTANT BUY', comment_loss = 'INSTANT STOP')
if inTrade 
    strategy.cancel("INSTANT")
    strategy.exit(direction == LONG ? "SELL" : "BUY", limit = closeLimitPrice, stop = stopLimitPrice, comment_profit = direction == LONG ? "SELL" : "BUY", comment_loss = "STOP")
if isMaxBars and barsFromEntry == maxBars
    strategy.close_all(comment = "TIMEOUT STOP", immediately = true)



showStop = stopPercent <= 0.20

// plot(showStop ? stopLimitPrice : na, title="Stop Loss Limit Order", force_overlay=true, style=plot.style_linebr, color=#c50202, linewidth=1, offset=1)
// plot(closeLimitPrice, title="Take Profit Limit Order", force_overlay=true, style=plot.style_linebr, color = direction == LONG ? color.red : color.blue, linewidth=1, offset=1)
// plot(strategy.position_avg_price, title="Buy Order Filled Price", force_overlay=true, style=plot.style_linebr, color=direction == LONG ? color.blue : color.red, linewidth=1, offset=1)
plot(showStop ? stopLimitPrice : na, title="Stop Loss Limit Order", force_overlay=true, style=plot.style_linebr, color=#c50202, linewidth=1, offset=0)
plot(closeLimitPrice, title="Take Profit Limit Order", force_overlay=true, style=plot.style_linebr, color = direction == LONG ? color.red : color.blue, linewidth=1, offset=0)
plot(strategy.position_avg_price, title="Buy Order Filled Price", force_overlay=true, style=plot.style_linebr, color=direction == LONG ? color.blue : color.red, linewidth=1, offset=0)

plot(openLimitPrice, title="Trailing Open Position Limit Order", style=plot.style_stepline, color=color.new(direction == LONG ? color.blue : color.red, 30), offset=1)
plot(closeLimitPriceEntry, title="Trailing Close Position Limit Order", style=plot.style_stepline, color=color.new(direction == LONG ? color.red : color.blue, 80), offset=1)
plot(stopLimitPriceEntry, title="Trailing Stop Position Limit Order", style=plot.style_stepline, color=color.new(#c50202, 80), offset=1)