OPEN-SOURCE SCRIPT
Saya Ratio with Bollinger Bands

Indicator Overview
This indicator is an analysis tool designed for pair trading (spread trading) using two highly correlated instruments.
Instead of analyzing each instrument’s price separately, it visualizes the price ratio (spread) as a time series.
Price ratio (spread) = Price of the first instrument ÷ Price of the second instrument
Pair trading (spread trading) is a trading strategy in which two highly correlated instruments are traded simultaneously in opposite directions, aiming to profit from changes in the price difference (spread) between them.
Bollinger Bands use past price movements and the deviation from the average (standard deviation) to distinguish between
ranges where values appear frequently and ranges where they appear infrequently from a statistical perspective.
When the spread remains within the bands, it is considered to be within the range of past price behavior.
On the other hand, when the spread exceeds the upper band or falls below the lower band, it is more likely to represent a temporary statistical extreme.
In pair trading, this type of “extreme” is used as a starting point to anticipate a move back toward the mean (convergence).
This indicator is designed to make such judgments easier through visual representation.
When the spread exceeds the upper band or falls below the lower band, a background color is displayed.
Based on this background color, you can determine which instrument to sell short and which to buy.
How to Use the Indicator
1. When you add the indicator, a new pane separate from the main chart is created, and the indicator is displayed there.
Maximizing the pane will make it easier to view.
2. In the settings dialog, enter the symbols of the two instruments.
Either one may be specified as the primary instrument.
3. The price ratio (spread) and its Bollinger Bands are displayed.
4. The correlation coefficient of the two instruments is shown in the Data Window (Correlation field).
The correlation coefficient indicates:
・Values close to +1: strong positive correlation
・Values close to 0: little or no correlation
・Values close to −1: negative correlation
For pair trading, it is important to use instrument pairs where a high correlation is maintained over time.
Look for pairs with a correlation coefficient of +0.8 or higher.
5. The following parameters can be changed in the settings dialog, but in most cases the default values are sufficient:
・MA length: Period used to calculate the Bollinger Bands (default: 75)
・Bandwidth factor: Width of the upper and lower bands in multiples of standard deviation (default: 2)
・Band color: Color of the Bollinger Bands
・Overbought level: Background color when the spread exceeds the upper band
・Oversold level: Background color when the spread falls below the lower band
・Correlation length: Period used to calculate the correlation coefficient (default: 200)
Trading Method
When trading, always execute trades for both instruments simultaneously.
・At the overbought level → Enter by short-selling the primary instrument and buying the secondary instrument
・At the oversold level → Enter by buying the primary instrument and short-selling the secondary instrument
・Take profit when the combined profit of the two positions reaches +2.5%
・Cut losses when the combined loss of the two positions reaches −1.0%
Please note that this indicator does not calculate position size or profit and loss.
These must be managed separately by the trader.
インジケーターの概要
このインジケーターは、連動性の高い二つの銘柄を使ったサヤ取り(ペアトレード)を行うための分析ツールです。
二つの銘柄の価格を個別に見るのではなく、価格の比率(サヤ)を時系列データとして可視化します。
価格の比率(サヤ)=一つめの銘柄の価格 ÷ 二つめの銘柄の価格
サヤ取り(ペアトレード)とは、
相関性の高い二つの銘柄を両建てで売買し、価格差(サヤ)の変動から利益を得る取引手法です。
ボリンジャーバンドは、過去の値動きをもとに平均値からのブレ(標準偏差)を使って
「統計的に多く出現する範囲」と「出にくい範囲」を分けて考えるための指標です。
サヤがバンドの内側にある間は、過去の値動きの範囲内に収まっている状態と考えられます。
一方で、上限を超えた場合や下限を割った場合は、統計的に見て一時的な行き過ぎである可能性が高くなります。
サヤ取りでは、この「行き過ぎ」を起点に、再び平均付近へ戻る動き(収束)を狙います。
このインジケーターは、その判断を視覚的に行いやすくするためのものです。
また、サヤが上限を超えた場合、下限を割った場合には背景色を表示します。
この背景色によって、どちらの銘柄を空売りし、どちらを買うかを判断できます。
インジケーターの操作方法
1. インジケーターを追加すると、メインチャートとは別のペインが作成され、そこに本インジケーターが表示されます。
ペインを最大化すると、より見やすくなります。
2. 設定ダイアログで、二つの銘柄のシンボルを入力します。
どちらを primary に指定しても構いません。
3. 価格の比率(サヤ)と、そのボリンジャーバンドが表示されます。
4. 二つの銘柄の相関係数が、データウィンドウに表示されます(Correlation欄)。
相関係数は、
+1 に近いほど強い正の相関
0 に近いほど無相関
マイナスに近いほど逆相関
を表します。
サヤ取りでは相関係数が高い状態が継続している銘柄ペアを使うことが重要です。+0.8 以上のペアを探すようにしてください。
5. 設定ダイアログでは以下の項目を変更できますが、基本的には初期値のままで問題ありません。
・MA length:ボリンジャーバンドの計算期間(規定値:75)
・Bandwidth factor:上限・下限の幅を標準偏差の何倍で設定するか(規定値:2)
・Band color:ボリンジャーバンドの色
・買われすぎ水準:ボリンジャーバンド上限超過時の背景色
・売られすぎ水準:ボリンジャーバンド下限割れ時の背景色
・相関係数の計算期間(規定値:200)
トレードの方法
トレードを行う際は、必ず二つの銘柄を同時に売買してください。
・買われすぎ水準では → primary を空売りし、secondary を買ってエントリー
・売られすぎ水準では → primary を買い、secondary を空売りしてエントリー
・二つの銘柄の通算損益が +2.5% に達したら利益確定
・二つの銘柄の通算損益が −1.0% に達したら損切り
なお、このインジケーターでは、売買数量の計算や損益計算は行っていませんので、あらかじめご了承ください。
This indicator is an analysis tool designed for pair trading (spread trading) using two highly correlated instruments.
Instead of analyzing each instrument’s price separately, it visualizes the price ratio (spread) as a time series.
Price ratio (spread) = Price of the first instrument ÷ Price of the second instrument
Pair trading (spread trading) is a trading strategy in which two highly correlated instruments are traded simultaneously in opposite directions, aiming to profit from changes in the price difference (spread) between them.
Bollinger Bands use past price movements and the deviation from the average (standard deviation) to distinguish between
ranges where values appear frequently and ranges where they appear infrequently from a statistical perspective.
When the spread remains within the bands, it is considered to be within the range of past price behavior.
On the other hand, when the spread exceeds the upper band or falls below the lower band, it is more likely to represent a temporary statistical extreme.
In pair trading, this type of “extreme” is used as a starting point to anticipate a move back toward the mean (convergence).
This indicator is designed to make such judgments easier through visual representation.
When the spread exceeds the upper band or falls below the lower band, a background color is displayed.
Based on this background color, you can determine which instrument to sell short and which to buy.
How to Use the Indicator
1. When you add the indicator, a new pane separate from the main chart is created, and the indicator is displayed there.
Maximizing the pane will make it easier to view.
2. In the settings dialog, enter the symbols of the two instruments.
Either one may be specified as the primary instrument.
3. The price ratio (spread) and its Bollinger Bands are displayed.
4. The correlation coefficient of the two instruments is shown in the Data Window (Correlation field).
The correlation coefficient indicates:
・Values close to +1: strong positive correlation
・Values close to 0: little or no correlation
・Values close to −1: negative correlation
For pair trading, it is important to use instrument pairs where a high correlation is maintained over time.
Look for pairs with a correlation coefficient of +0.8 or higher.
5. The following parameters can be changed in the settings dialog, but in most cases the default values are sufficient:
・MA length: Period used to calculate the Bollinger Bands (default: 75)
・Bandwidth factor: Width of the upper and lower bands in multiples of standard deviation (default: 2)
・Band color: Color of the Bollinger Bands
・Overbought level: Background color when the spread exceeds the upper band
・Oversold level: Background color when the spread falls below the lower band
・Correlation length: Period used to calculate the correlation coefficient (default: 200)
Trading Method
When trading, always execute trades for both instruments simultaneously.
・At the overbought level → Enter by short-selling the primary instrument and buying the secondary instrument
・At the oversold level → Enter by buying the primary instrument and short-selling the secondary instrument
・Take profit when the combined profit of the two positions reaches +2.5%
・Cut losses when the combined loss of the two positions reaches −1.0%
Please note that this indicator does not calculate position size or profit and loss.
These must be managed separately by the trader.
インジケーターの概要
このインジケーターは、連動性の高い二つの銘柄を使ったサヤ取り(ペアトレード)を行うための分析ツールです。
二つの銘柄の価格を個別に見るのではなく、価格の比率(サヤ)を時系列データとして可視化します。
価格の比率(サヤ)=一つめの銘柄の価格 ÷ 二つめの銘柄の価格
サヤ取り(ペアトレード)とは、
相関性の高い二つの銘柄を両建てで売買し、価格差(サヤ)の変動から利益を得る取引手法です。
ボリンジャーバンドは、過去の値動きをもとに平均値からのブレ(標準偏差)を使って
「統計的に多く出現する範囲」と「出にくい範囲」を分けて考えるための指標です。
サヤがバンドの内側にある間は、過去の値動きの範囲内に収まっている状態と考えられます。
一方で、上限を超えた場合や下限を割った場合は、統計的に見て一時的な行き過ぎである可能性が高くなります。
サヤ取りでは、この「行き過ぎ」を起点に、再び平均付近へ戻る動き(収束)を狙います。
このインジケーターは、その判断を視覚的に行いやすくするためのものです。
また、サヤが上限を超えた場合、下限を割った場合には背景色を表示します。
この背景色によって、どちらの銘柄を空売りし、どちらを買うかを判断できます。
インジケーターの操作方法
1. インジケーターを追加すると、メインチャートとは別のペインが作成され、そこに本インジケーターが表示されます。
ペインを最大化すると、より見やすくなります。
2. 設定ダイアログで、二つの銘柄のシンボルを入力します。
どちらを primary に指定しても構いません。
3. 価格の比率(サヤ)と、そのボリンジャーバンドが表示されます。
4. 二つの銘柄の相関係数が、データウィンドウに表示されます(Correlation欄)。
相関係数は、
+1 に近いほど強い正の相関
0 に近いほど無相関
マイナスに近いほど逆相関
を表します。
サヤ取りでは相関係数が高い状態が継続している銘柄ペアを使うことが重要です。+0.8 以上のペアを探すようにしてください。
5. 設定ダイアログでは以下の項目を変更できますが、基本的には初期値のままで問題ありません。
・MA length:ボリンジャーバンドの計算期間(規定値:75)
・Bandwidth factor:上限・下限の幅を標準偏差の何倍で設定するか(規定値:2)
・Band color:ボリンジャーバンドの色
・買われすぎ水準:ボリンジャーバンド上限超過時の背景色
・売られすぎ水準:ボリンジャーバンド下限割れ時の背景色
・相関係数の計算期間(規定値:200)
トレードの方法
トレードを行う際は、必ず二つの銘柄を同時に売買してください。
・買われすぎ水準では → primary を空売りし、secondary を買ってエントリー
・売られすぎ水準では → primary を買い、secondary を空売りしてエントリー
・二つの銘柄の通算損益が +2.5% に達したら利益確定
・二つの銘柄の通算損益が −1.0% に達したら損切り
なお、このインジケーターでは、売買数量の計算や損益計算は行っていませんので、あらかじめご了承ください。
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.