Automation for Calculating Beta || Python

 So, if I have to state in very simple words what beta is? Then I will say it like "Beta is a measure of stock's volatility in relation to the overall market

Beta is a concept that measures the expected move in the stock relative to movements in the overall market.

The higher the beta is the stock will be more volatile, the lower the beta will be the stock will be less volatile.

The formula for calculating beta is the covariance of the return of an asset with the return of the benchmark divided by the variance of the return of the benchmark over a certain period.



Where:
Covariance = measure of a stock's return relative to that of the market.
Variance = Measure of how the market moves relative to its mean


Now after all this detailed theory about beta here I will show you how you can calculate it using python and automation.


import pandas as pd
import numpy as np
import os
import yfinance as yf
from pandas_datareader import data as web

tickers = ['BMW''DAX']
data = pd.DataFrame()
for t in tickers:
  data[t] = yf.download(t, data_source='yahoo', start='2016-1-1', end='2019-12-30')['Adj Close']


sec_returns = np.log( data / data.shift(1) ) 
cov = sec_returns.cov() * 250
cov_with_market = cov.iloc[0,1]
market_var = sec_returns['DAX'].var() * 250


beta = cov_with_market / market_var
beta






This is how you can calculate Beta with automating data and without doing the heavy calculation.

Thank you for reading.


Comments

  1. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information
    Python Online Training
    Python Online Training Hyderabad

    ReplyDelete

Post a Comment

Popular posts from this blog

Net Present Value (NPV) On Python Using Numpy

Commodity Channel Index ( CCI ) Using Python

SMA Trading Strategy Using Python