top of page

PYTHON CODE: Stock Analysis Basics

In our previous post, we discussed in detail- how to analyze the stock data and constructed an optimum portfolio of stocks using the Microsoft Excel tool. In this post, we will discuss how to calculate the same using Python. Python code and the output has been added for your reference.

Let us understand how to extract historical stock data using Python-

Suppose, we have a stock- HDFC BANK for which we want to extract the data from Yahoo Finance. We take 250 days closing stock price of the period 01-01-2020 to 31-12-2020 and take a risk-free rate of return as 6%. Below is the only inputs needs to be updated by the user in the Jupyter Notebook and rest of the code will work with no change.

For reference,

sticker = stock ticker name (in our case, HDFC Bank)

bticker = benchmark ticker name (in our case, NIFTY Index)

startdate = start date of the stock price data (in our case, 2020-01-01 in YYYY-MM-DD)

enddate = end date of the stock price data (in our case, 2020-12-31 in YYYY-MM-DD)

Rf = risk-free rate of return

Obviously, to use data frames and perform calculations, we have to import the required libraries with Python as shown below-

Below code is required to run to import the stock data and then calculate the daily returns (% Change) of the stock by using '.pct-change()' as shown below-

Below code is required to run to import the benchmark data and then calculate the daily returns (% Change) of the benchmark by using '.pct-change()' as shown below-

We then calculate the expected mean return and standard deviation of both- stock and the benchmark which can be used to calculate the Sharpe Ratio (i.e., the average return in excess of the risk-free rate per unit of volatility).

Sharpe Ratio = ( Expected Return - Risk-free Rate ) / Standard Deviation

More Over, we also calculate Covariance and Correlation of the stock and the benchmark. Covariance is a statistical measure that describes the directional relationship between two random variables (whether they are positively or negatively correlated) and Correlation is a statistical measure that quantifies the direction and strength of linear association between those variables (from -1 to +1).

Each variable (Stock and the Benchmark) has its pase (i.e., volatility) at which it is changing every moment. If both the variables tend to move in the same direction, it is said that they are positively correlated. And the rate at which they are moving in the same direction is identified by correlation.

Finally, to calculate Required Rate of Return using Capital Asset Pricing Model (CAPM), we find Beta using Covariance Matrix and Risk Premium using Benchmark Mean Return and Risk-free Rate as shown below-

A plot of the same is shown below-

The resultant Beta is 1.0908 which means- if the benchmark price changes by 1%, the stock price is expected to changes by 1.0908% in the same direction as correlation is highly positive (i.e., 0.8286), ignoring convexity.


Feel free to share your views and ask for models in the #comment section. If you like our content, please hit a #like button.

351 views0 comments


bottom of page