OPEN-SOURCE SCRIPT

サヤ取り分析 + テクニカル

51
//version=5
indicator("サヤ取り分析 + テクニカル", overlay=false)

// パラメーター設定
symbol2 = input.symbol("USDJPY", "比較する銘柄")
ma_length1 = input.int(5, "短期MA")
ma_length2 = input.int(25, "中期MA")
ma_length3 = input.int(75, "長期MA")
bb_length = input.int(20, "ボリンジャーバンド期間")
bb_std1 = input.float(2.0, "BB標準偏差1")
bb_std2 = input.float(3.0, "BB標準偏差2")
corr_length = input.int(20, "相関係数の期間", minval=2)

// 2つの銘柄の価格データを取得
price1 = close
price2 = request.security(symbol2, timeframe.period, close)

// サヤ比の計算
spread_ratio = price1 / price2

// 移動平均線の計算
sma1 = ta.sma(spread_ratio, ma_length1)
sma2 = ta.sma(spread_ratio, ma_length2)
sma3 = ta.sma(spread_ratio, ma_length3)

// ボリンジャーバンドの計算
bb_basis = ta.sma(spread_ratio, bb_length)
bb_dev1 = ta.stdev(spread_ratio, bb_length) * bb_std1
bb_dev2 = ta.stdev(spread_ratio, bb_length) * bb_std2

bb_upper1 = bb_basis + bb_dev1
bb_lower1 = bb_basis - bb_dev1
bb_upper2 = bb_basis + bb_dev2
bb_lower2 = bb_basis - bb_dev2

// 相関係数の計算
correlation = ta.correlation(price1, price2, corr_length)

// アラート条件
upperTouch1 = ta.crossover(spread_ratio, bb_upper1)
lowerTouch1 = ta.crossunder(spread_ratio, bb_lower1)
upperTouch2 = ta.crossover(spread_ratio, bb_upper2)
lowerTouch2 = ta.crossunder(spread_ratio, bb_lower2)

// アラートメッセージの設定
if upperTouch1
alert("上側ボリンジャーバンド(2σ)にタッチ", alert.freq_once_per_bar)
if lowerTouch1
alert("下側ボリンジャーバンド(2σ)にタッチ", alert.freq_once_per_bar)
if upperTouch2
alert("上側ボリンジャーバンド(3σ)にタッチ", alert.freq_once_per_bar)
if lowerTouch2
alert("下側ボリンジャーバンド(3σ)にタッチ", alert.freq_once_per_bar)

// プロット
plot(spread_ratio, title="サヤ比", color=color.purple, linewidth=2)
plot(sma1, title="短期MA", color=color.red)
plot(sma2, title="中期MA", color=color.yellow)
plot(sma3, title="長期MA", color=color.blue)
plot(bb_upper1, title="BB上限1", color=color.green)
plot(bb_lower1, title="BB下限1", color=color.green)
plot(bb_upper2, title="BB上限2", color=color.orange)
plot(bb_lower2, title="BB下限2", color=color.orange)


// 相関係数を右上に表示
var corrTable = table.new(position.top_right, 1, 1, bgcolor=color.new(color.black, 70))
table.cell(corrTable, 0, 0, "相関係数 (" + str.tostring(corr_length) + "): " + str.tostring(correlation, "#.####"), text_color=correlation >= 0 ? color.green : color.red, text_size=size.normal)

Declinazione di responsabilità

Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.