OPEN-SOURCE SCRIPT

Updated

Capital Asset Pricing Model (CAPM) [Loxx] demonstrates how to calculate the Cost of Equity for an underlying asset using Pine Script. This script will only work on the monthly timeframe. While you can change the default inputs, you should study what CAPM is and how this works before doing so. This indicator pulls various types of data from SPY from various timeframes to calculate risk-free rates, market premiums, and log returns. Alpha and Beta are computed using the regression between underlying asset and SPY. This indicator only calculates on the most recent data. If you wish to change this, you'll have to save the script and make adjustments. A few examples where CAPM is used:

**Details of the calculations used here **

In the past, typically this data is purchased from investments banks whose research arms produce values for beta, alpha, risk free rate, and risk premiums. In 2022 ,you can find free estimates for each parameter but these values might not reflect the most current data or research.

**History**

The CAPM was introduced by Jack Treynor (1961, 1962),[4] William F. Sharpe (1964), John Lintner (1965) and Jan Mossin (1966) independently, building on the earlier work of Harry Markowitz on diversification and modern portfolio theory. Sharpe, Markowitz and Merton Miller jointly received the 1990 Nobel Memorial Prize in Economics for this contribution to the field of financial economics. Fischer Black (1972) developed another version of CAPM, called Black CAPM or zero-beta CAPM, that does not assume the existence of a riskless asset. This version was more robust against empirical testing and was influential in the widespread adoption of the CAPM.

**Usage**

The CAPM is used to calculate the amount of return that investors need to realize to compensate for a particular level of risk. It subtracts the risk-free rate from the expected rate and weighs it with a factor – beta – to get the risk premium. It then adds the risk premium to the risk-free rate of return to get the rate of return an investor expects as compensation for the risk. The CAPM formula is expressed as follows:

*r = Rf + beta (Rm – Rf) + Alpha*

Therefore,

*Alpha = R – Rf – beta (Rm-Rf)*

Where:

For example, assuming that the actual return of the fund is 30, the risk-free rate is 8%, beta is 1.1, and the benchmark index return is 20%, alpha is calculated as:

Alpha = (0.30-0.08) – 1.1 (0.20-0.08) = 0.088 or 8.8%

The result shows that the investment in this example outperformed the benchmark index by 8.8%.

The alpha of a portfolio is the excess return it produces compared to a benchmark index. Investors in mutual funds or ETFs often look for a fund with a high alpha in hopes of getting a superior return on investment (ROI).

The alpha ratio is often used along with the beta coefficient, which is a measure of the volatility of an investment. The two ratios are both used in the Capital Assets Pricing Model (CAPM) to analyze a portfolio of investments and assess its theoretical performance.

To see CAPM in action in terms of calculate WACC, see here for an example: https://finbox.com/NASDAQGS:GOOG/models/wacc

**Further reading**

https://en.wikipedia.org/wiki/Capital_asset_pricing_model

- Used as the mu factor Geometric Brownian Motion models for options pricing and forecasting price ranges and decay
- Calculating the Weighted Average Cost of Capital
- Asset pricing
- Efficient frontier
- Risk and diversification
- Security market line
- Discounted Cashflow Analysis
- Investment bankers use CAPM to value deals
- Account firms use CAPM to verify asset prices and assumptions
- Real estate firms use variations of CAPM to value properties
- ... and more

- Rm is calculated using yearly simple returns data from SPY, typically this is just hard coded as 10%.
- Rf is pulled from US 10 year bond yields
- Beta and Alpha are pulled form monthly returns data of the asset and SPY

In the past, typically this data is purchased from investments banks whose research arms produce values for beta, alpha, risk free rate, and risk premiums. In 2022 ,you can find free estimates for each parameter but these values might not reflect the most current data or research.

The CAPM was introduced by Jack Treynor (1961, 1962),[4] William F. Sharpe (1964), John Lintner (1965) and Jan Mossin (1966) independently, building on the earlier work of Harry Markowitz on diversification and modern portfolio theory. Sharpe, Markowitz and Merton Miller jointly received the 1990 Nobel Memorial Prize in Economics for this contribution to the field of financial economics. Fischer Black (1972) developed another version of CAPM, called Black CAPM or zero-beta CAPM, that does not assume the existence of a riskless asset. This version was more robust against empirical testing and was influential in the widespread adoption of the CAPM.

The CAPM is used to calculate the amount of return that investors need to realize to compensate for a particular level of risk. It subtracts the risk-free rate from the expected rate and weighs it with a factor – beta – to get the risk premium. It then adds the risk premium to the risk-free rate of return to get the rate of return an investor expects as compensation for the risk. The CAPM formula is expressed as follows:

Therefore,

Where:

- R represents the portfolio return
- Rf represents the risk-free rate of return
- Beta represents the systematic risk of a portfolio
- Rm represents the market return, per a benchmark

For example, assuming that the actual return of the fund is 30, the risk-free rate is 8%, beta is 1.1, and the benchmark index return is 20%, alpha is calculated as:

Alpha = (0.30-0.08) – 1.1 (0.20-0.08) = 0.088 or 8.8%

The result shows that the investment in this example outperformed the benchmark index by 8.8%.

The alpha of a portfolio is the excess return it produces compared to a benchmark index. Investors in mutual funds or ETFs often look for a fund with a high alpha in hopes of getting a superior return on investment (ROI).

The alpha ratio is often used along with the beta coefficient, which is a measure of the volatility of an investment. The two ratios are both used in the Capital Assets Pricing Model (CAPM) to analyze a portfolio of investments and assess its theoretical performance.

To see CAPM in action in terms of calculate WACC, see here for an example: https://finbox.com/NASDAQGS:GOOG/models/wacc

https://en.wikipedia.org/wiki/Capital_asset_pricing_model

Release Notes

Fixed table output error.Release Notes

Updated to work on all timeframes. This won't actually make the indicator work on ll timeframes, but allows you the user to use the constant output on all timeframes. Past beta values are not calculated, only the current month beta is calculated. Release Notes

Removed unused code. Public Telegram Group, t.me/algxtrading_public

VIP Membership Info: patreon.com/algxtrading/membership

VIP Membership Info: patreon.com/algxtrading/membership

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

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.