OPEN-SOURCE SCRIPT
Static Beta for Pair and Quant Trading

A beta coefficient shows the volatility of an individual stock compared to the systematic risk of the entire market. Beta represents the slope of the line through a regression of data points. In finance, each point represents an individual stock's returns against the market.
Beta effectively describes the activity of a security's returns as it responds to swings in the market. It is used in the capital asset pricing model (CAPM), which describes the relationship between systematic risk and expected return for assets. CAPM is used to price risky securities and to estimate the expected returns of assets, considering the risk of those assets and the cost of capital.
Calculating Beta
A security's beta is calculated by dividing the product of the covariance of the security's returns and the market's returns by the variance of the market's returns over a specified period. The calculation helps investors understand whether a stock moves in the same direction as the rest of the market. It also provides insights into how volatile—or how risky—a stock is relative to the rest of the market.
For beta to provide useful insight, the market used as a benchmark should be related to the stock. For example, a bond ETF's beta with the S&P 500 as the benchmark would not be helpful to an investor because bonds and stocks are too dissimilar.
Beta Values
Beta equal to 1: A stock with a beta of 1.0 means its price activity correlates with the market. Adding a stock to a portfolio with a beta of 1.0 doesn’t add any risk to the portfolio, but it doesn’t increase the likelihood that the portfolio will provide an excess return.
Beta less than 1: A beta value less than 1.0 means the security is less volatile than the market. Including this stock in a portfolio makes it less risky than the same portfolio without the stock. Utility stocks often have low betas because they move more slowly than market averages.
Beta greater than 1: A beta greater than 1.0 indicates that the security's price is theoretically more volatile than the market. If a stock's beta is 1.2, it is assumed to be 20% more volatile than the market. Technology stocks tend to have higher betas than the market benchmark. Adding the stock to a portfolio will increase the portfolio’s risk, but may also increase its return.
Negative beta: A beta of -1.0 means that the stock is inversely correlated to the market benchmark on a 1:1 basis. Put options and inverse ETFs are designed to have negative betas. There are also a few industry groups, like gold miners, where a negative beta is common.[1]
LET'S START
Now I'll give my own definition.
Beta:
If we assume market caps are equal,
it is an indicator that shows how much of the second instrument we should buy if we buy one of the first, taking into account the price volatility of two instruments.
But if the market caps are not equal:
For example, the ETF for A is $300.
The ETF for B is $600.
If static beta predicted by this script is 0.5:
300 * 1 * a = 600 * 0.5 * b
Then we should use 1 b for 1 a.
(Long a and short b or vice versa )
So, we can try pair trading for a/b or a-b.
However, these values are generally close to each other, such as 0.8 and 0.93. However, the closer we can adjust our lot purchases to bring the double beta to a value closer to 1, the higher the hedge ratio will be.
Large commercials use dynamic betas, which are updated periodically, in addition to static betas
However, scaling this is very difficult for individual investors with limited investment tools.
But a static beta of 5,000 bars is still much better than not considering any beta at all.
Note: The presence of a beta value for two instruments does not necessarily mean they can be included in pair trading.
It is also important (%99) to consider historically very high correlations and cointegration relationships, as well as the compatibility of security structures.
Note 2 : This script is designed for low timeframes.
Do not use betas from different timeframes.
Beta dynamics are different for each timeframe.
Note 3 : I created this script with the help of ChatGPT.
Source for beta definition ([1] ) :
investopedia.com/terms/b/beta.asp
Regards.
Beta effectively describes the activity of a security's returns as it responds to swings in the market. It is used in the capital asset pricing model (CAPM), which describes the relationship between systematic risk and expected return for assets. CAPM is used to price risky securities and to estimate the expected returns of assets, considering the risk of those assets and the cost of capital.
Calculating Beta
A security's beta is calculated by dividing the product of the covariance of the security's returns and the market's returns by the variance of the market's returns over a specified period. The calculation helps investors understand whether a stock moves in the same direction as the rest of the market. It also provides insights into how volatile—or how risky—a stock is relative to the rest of the market.
For beta to provide useful insight, the market used as a benchmark should be related to the stock. For example, a bond ETF's beta with the S&P 500 as the benchmark would not be helpful to an investor because bonds and stocks are too dissimilar.
Beta Values
Beta equal to 1: A stock with a beta of 1.0 means its price activity correlates with the market. Adding a stock to a portfolio with a beta of 1.0 doesn’t add any risk to the portfolio, but it doesn’t increase the likelihood that the portfolio will provide an excess return.
Beta less than 1: A beta value less than 1.0 means the security is less volatile than the market. Including this stock in a portfolio makes it less risky than the same portfolio without the stock. Utility stocks often have low betas because they move more slowly than market averages.
Beta greater than 1: A beta greater than 1.0 indicates that the security's price is theoretically more volatile than the market. If a stock's beta is 1.2, it is assumed to be 20% more volatile than the market. Technology stocks tend to have higher betas than the market benchmark. Adding the stock to a portfolio will increase the portfolio’s risk, but may also increase its return.
Negative beta: A beta of -1.0 means that the stock is inversely correlated to the market benchmark on a 1:1 basis. Put options and inverse ETFs are designed to have negative betas. There are also a few industry groups, like gold miners, where a negative beta is common.[1]
LET'S START
Now I'll give my own definition.
Beta:
If we assume market caps are equal,
it is an indicator that shows how much of the second instrument we should buy if we buy one of the first, taking into account the price volatility of two instruments.
But if the market caps are not equal:
For example, the ETF for A is $300.
The ETF for B is $600.
If static beta predicted by this script is 0.5:
300 * 1 * a = 600 * 0.5 * b
Then we should use 1 b for 1 a.
(Long a and short b or vice versa )
So, we can try pair trading for a/b or a-b.
However, these values are generally close to each other, such as 0.8 and 0.93. However, the closer we can adjust our lot purchases to bring the double beta to a value closer to 1, the higher the hedge ratio will be.
Large commercials use dynamic betas, which are updated periodically, in addition to static betas
However, scaling this is very difficult for individual investors with limited investment tools.
But a static beta of 5,000 bars is still much better than not considering any beta at all.
Note: The presence of a beta value for two instruments does not necessarily mean they can be included in pair trading.
It is also important (%99) to consider historically very high correlations and cointegration relationships, as well as the compatibility of security structures.
Note 2 : This script is designed for low timeframes.
Do not use betas from different timeframes.
Beta dynamics are different for each timeframe.
Note 3 : I created this script with the help of ChatGPT.
Source for beta definition ([1] ) :
investopedia.com/terms/b/beta.asp
Regards.
Skrypt open-source
W zgodzie z duchem TradingView twórca tego skryptu udostępnił go jako open-source, aby użytkownicy mogli przejrzeć i zweryfikować jego działanie. Ukłony dla autora. Korzystanie jest bezpłatne, jednak ponowna publikacja kodu podlega naszym Zasadom serwisu.
Wyłączenie odpowiedzialności
Informacje i publikacje nie stanowią i nie powinny być traktowane jako porady finansowe, inwestycyjne, tradingowe ani jakiekolwiek inne rekomendacje dostarczane lub zatwierdzone przez TradingView. Więcej informacji znajduje się w Warunkach użytkowania.
Skrypt open-source
W zgodzie z duchem TradingView twórca tego skryptu udostępnił go jako open-source, aby użytkownicy mogli przejrzeć i zweryfikować jego działanie. Ukłony dla autora. Korzystanie jest bezpłatne, jednak ponowna publikacja kodu podlega naszym Zasadom serwisu.
Wyłączenie odpowiedzialności
Informacje i publikacje nie stanowią i nie powinny być traktowane jako porady finansowe, inwestycyjne, tradingowe ani jakiekolwiek inne rekomendacje dostarczane lub zatwierdzone przez TradingView. Więcej informacji znajduje się w Warunkach użytkowania.