10月7日VIX65%

PineScript
// @version=6
strategy("可変TSIバックテスト", overlay=false, initial_capital=1000000)

// 通常時のTSIパラメータ
spanLong = input.int(25, "TSI長期", minval=1)
spanShort = input.int(13, "TSI短期", minval=1)
spanSignal = input.int(6, "TSIシグナル", minval=1)

// 可変時のTSIのパラメータ
spanLong2 = input.int(12, "TSI長期", minval=1)
spanShort2 = input.int(6, "TSI短期", minval=1)
spanSignal2 = input.int(3, "TSIシグナル", minval=1)

// 通常時のMACDパラメータ
//macdSlow = input.int(26, "MACD長期", minval=1)
//macdFast = input.int(12, "MACD短期", minval=1)
//macdSignal = input.int(9, "MACDシグナル", minval=1)

// 可変時のMACDパラメータ
//macdSlow2 = input.int(13, "MACD長期", minval=1)
//macdFast2 = input.int(6, "MACD短期", minval=1)
//macdSignal2 = input.int(4, "MACDシグナル", minval=1)

// ボリンジャーバンドのパラメータ(上下10%付近でMACDを調整)
bbLength = input.int(20, "BB期間", minval=1)
bbMult = input.float(2.0, "BB倍率", minval=0.1, step=0.1)
bbUpperThresh = 1.0
bbLowerThresh = 0.0

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

// ボリンジャーバンド(BB位置は0が下限、1が上限)
basis = ta.sma(close, bbLength)
dev = bbMult * ta.stdev(close, bbLength)
upper = basis + dev
lower = basis - dev
bbPosition = upper != lower ? (close - lower) / (upper - lower) : 0.5

// TSI値の計算
var float tsi = 0.0
var float tsiSignal = 0.0
if bbPosition > bbUpperThresh or bbPosition < bbLowerThresh
    tsi_tmp = ta.tsi(close, spanLong, spanShort)
    tsiSignal_tmp = ta.ema(tsi, spanSignal)
    tsi := tsi_tmp
    tsiSignal := tsiSignal_tmp
else
    tsi_tmp = ta.tsi(close, spanLong, spanShort)
    tsiSignal_tmp = ta.ema(tsi, spanSignal)
    tsi := tsi_tmp
    tsiSignal := tsiSignal_tmp

// MACD値の計算
//var float macdLine = 0.0
//var float signalLine = 0.0
//if bbPosition > bbUpperThresh or bbPosition < bbLowerThresh
//    [macdLine_tmp, signalLine_tmp, _] = ta.macd(close, macdFast2, macdSlow2, macdSignal2)
//    macdLine := macdLine_tmp
//    signalLine := signalLine_tmp
//else
//    [macdLine_tmp, signalLine_tmp, _] = ta.macd(close, macdFast, macdSlow, macdSignal)
//    macdLine := macdLine_tmp
//    signalLine := signalLine_tmp

// MACDクロス判定
//macdGolden = ta.crossover(macdLine, signalLine)
//macdDead = ta.crossunder(macdLine, signalLine)

// TSIクロス判定
goldenCross = ta.crossover(tsi, tsiSignal)
deadCross = ta.crossunder(tsi, tsiSignal)

// TSIに応じた購入金額
var float buyAmount = 0.0
if tsi >= -0.1 and tsi < -0.0
    buyAmount := 100000.0
else if tsi >= -0.2 and tsi < -0.1
    buyAmount := 400000.0
else if tsi >= -0.3 and tsi < -0.2
    buyAmount := 900000.0
else if tsi < -0.3
    buyAmount := math.min(1600000.0, strategy.equity) // 初期資本を超えないように制限
else
    buyAmount := 0.0

// トレード期間
startYear = 2024
startMonth = 9
startDay = 30
endYear = 2025
endMonth = 9
endDay = 30

startTime = timestamp(startYear, startMonth, startDay, 0, 0)
endTime = timestamp(endYear, endMonth, endDay, 23, 59)

inPeriod = (time >= startTime) and (time <= endTime)

var float totalInvested = 0.0
var float totalQty = 0.0

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

// 売却
if deadCross and totalQty > 0 and tsi > 0.2 and inPeriod
    strategy.close("Long", comment=" ")
    totalInvested := 0.0
    totalQty := 0.0

// プロット
plot(tsi, "TSI", color=color.blue)
plot(tsiSignal, "TSIシグナル", color=color.orange)
//plot(macdLine, "MACD Line", color=color.red)
//plot(signalLine, "MACD Signal", color=color.yellow)
hline(0, "基準", color=color.gray)
//plot(basis, "BB Basis", color=color.green)
//plot(upper, "BB Upper", color=color.purple)
//plot(lower, "BB Lower", color=color.purple)
BACK