【失敗】TSIゴールデンクロスとTSIデッドクロスで売買

PineScript

バックテスト

対象銀CFD
対象期間不明
純利益0.15%
勝率297回中110回

バックテストからわかったこと

・レンジ相場だと利益にならない細かい売買がたくさん発生する(→ボリンジャーバンドによる可変MACDの利用)

・TSIのゴールデンクロスは上昇相場だと購入タイミングとしてはかなり役に立つが、レンジ相場だと微妙

ソースコード

//@version=5
strategy("TSI積立法バックテスト", overlay=false, initial_capital=10000000)

// パラメータ(初期資金1000万と仮定)
spanLong = input.int(12, "長期EMA", minval=1)
spanShort = input.int(6, "短期EMA", minval=1)
spanSignal = input.int(3, "シグナルEMA", minval=1)

// TSIの値を計算
tsi = ta.tsi(close, spanLong, spanShort)
tsiSignal = ta.ema(tsi, spanSignal)

// ゴールデンクロスとデッドクロスの判定
goldenCross = ta.crossover(tsi, tsiSignal)
deadCross = ta.crossunder(tsi, tsiSignal)

// 購入金額の計算
var float buyAmount = 0.0
if tsi >= -10 and tsi < -0
    buyAmount := 100000.0
else if tsi >= -20 and tsi < -10
    buyAmount := 400000.0
else if tsi >= -30 and tsi < -20
    buyAmount := 900000.0
else if tsi < -30
    buyAmount := 1600000.0
else
    buyAmount := 0.0

// 保有情報の計算
var float totalInvested = 0.0  // 累計投資額
var float totalQty = 0.0       // 保有数量

// 購入条件
if goldenCross and buyAmount > 0
    strategy.entry("Long", strategy.long, qty=buyAmount / close)
    totalInvested := totalInvested + buyAmount
    totalQty := totalQty + (buyAmount / close)

// ポジションを閉じる条件
if deadCross
    strategy.close("Long")
    totalInvested := 0.0
    totalQty := 0.0

// 含み損益(円)
unrealizedPL = totalQty * close - totalInvested

// 最後のバーで最終結果を表示
if barstate.islast
    label.new(bar_index, high, 
         text="最終結果: 累計投資=" + str.tostring(totalInvested, "#,##0") + "円, 保有数量=" + str.tostring(totalQty, "#.####") + ", 含み損益=" + str.tostring(unrealizedPL, "#,##0") + "円",
         yloc=yloc.abovebar, 
         style=label.style_label_down, 
         color=color.new(color.green, 80), 
         textcolor=color.white, 
         size=size.small)

// TSI表示
plot(tsi, "TSI短期", color=color.blue)
plot(tsiSignal, "TSI長期", color=color.orange)
hline(0, "基準", color=color.gray)

BACK