Capital Asset Pricing Model (CAPM) [Loxx]

loxx 已更新   
Capital Asset Pricing Model (CAPM) 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:

  • 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

Details of the calculations used here
  • 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), 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:

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


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


  • 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

Further reading
Fixed table output error.
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.
Removed unused code.

Public Telegram Group, t.me/algxtrading_public

VIP Membership Info: www.patreon.com/algxtrading/membership

本着真正的TradingView精神,该脚本的作者将其开源发布,以便交易者可以理解和验证它。为作者喝彩!您可以免费使用它,但在出版物中重复使用此代码受网站规则的约束。 您可以收藏它以在图表上使用。