Portfolio Analysis #1

(Description)

 

I am a student of B.com H Financial Markets (BSE) and in my academics, I’ve been taught how to invest in stock markets and analysis. I’m a third-year student and by the time I have a good knowledge of stocks and Mutual funds.

Me and one of my friends have decided to invest some of our funds in the market but we don’t want an existing portfolio to invest in instead, we want to create our own portfolio and do the same, for we have decided that I will do all the analysis stuff and he will allocate the funds. I am using Python for the analysis part.
How I did the analysis and stock selection will be described in this document.

 

 

 

 

 I have to create my own portfolio so for that I have to select some stocks and I’m going to select only large cap stocks because large cap stocks are stable and mature investments, large cap stocks have lower volatility, greater analyst coverage, and perhaps a steady dividend stream.

 

I have selected stocks from two sectors i.e., Banking and Computer software Sector.

 

 

Stocks in my portfolio & their tickers

 

1.      State Bank of India: SBIN

2.      Kotak Mahindra Bank Ltd: KOTAKBANK

3.      ICICI Bank: ICICIBANK

4.      HDFC Bank: HDFC

5.      Axis Bank: AXISBANK

6.      Tata Consultancy Service Ltd: TCS

7.      Mphasis Ltd: MPHASIS

8.      Infosys Ltd: INFY

 

 

 

Importing Libraries

 

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

import datetime

from pandas_datareader import data as web

from functools import reduce

 

 

 

 

 

 

Getting Historical Stock Data from Yahoo Finance

 

stocks      = {'HDFC.NS':'HDFC''TCS.NS':'TCS''INFY.NS' : 'INFY',  'ICICIBANK.NS''ICICI''SBIN.NS':'STATEBANKOFINDIA','AXISBANK.NS':'AXIS' ,  'KOTAKBANK.NS' : 'KOTAK''HCLTECH.NS' : 'HCL''MPHASIS.NS' : 'MPHASIS'}

instruments = {**stocks}

tickers     = list(instruments.keys())



start = datetime.datetime(2015,1,1)

end   = datetime.datetime(2020,1,1)

 

instruments_data = {}

for tickerinstrument in instruments.items():

    instruments_data[ticker] = web.DataReader(tickerdata_source = 'yahoo'start = startend = end)

 

 

For my analysis, I am taking historical data for the last 6 years, from 2015 to 2021.

My analysis will be based on the performance of the stock for the last 6 years.

 

 

for tickerinstrument in instruments.items():

    instruments_data[ticker] = instruments_data[ticker]["Adj Close"]

 

 

 

tr_days_per_year = data_df['HDFC.NS'].groupby([data_df['HDFC.NS'].index.year]).agg('count')

tr_days_per_year = pd.DataFrame([tr_days_per_year], index = ["All instruments (merged)"])

 

 

data = list(instruments_data.values())

data_df = reduce(lambda xy: pd.merge(x, y, left_index=Trueright_index=Truehow='outer'), data).dropna()

data_df.columns = tickers

 

 

data_df

 

 



Now I have all my stocks with their historical Adjusted Close prices Date wise, I can now visualize the price of these stocks using Matplotlib.

 

figax = plt.subplots(figsize=(12,8))

data_df.plot(ax = plt.gca(),grid = True)

ax.set_title('Adjusted Close for all instruments')

ax.set_facecolor((0.950.950.99))

ax.grid(c = (0.750.750.99))

plt.show()

 



 

 

 

RISK & RETURN ANALYSIS

 

 Return on an Instrument represents a combination of dividends (in stocks) and changes in the price (capital gain or lose)

 

 

 






 We will use log returns. From the formula, we can see that the sum of the log differences can be interpreted as the total change over the period summed.

 

log_returns = data_df.pct_change()

 

 

log_returns.plot(grid = Truefigsize = (15,10)).axhline(y = 0color = "black"lw = 2)

plt.show

 

 



 

 

The above Log Return shows that Returns are quite stable for most of the instruments.

 

 

Now we will calculate Annual Percentage Rate (APR)

 

APR = log_returns.groupby([log_returns.index.year]).agg('sum')

APR_avg = APR.mean()

pd.DataFrame(APR_avgcolumns=['Average APR']).T

 

 



 


 

Now we will calculate the Average Percentage Yield (APY)

 

N = np.array(tr_days_per_year.T)

N_total = np.sum(N)

APY = (1  + APR / N )**N - 1

APY_avg = (1  + APR_avg /N_total  )**N_total - 1

 

 

pd.DataFrame(APY_avgcolumns = ['Average APY']).T

 

 



 

 



 

 

 

RISK CALCULATION

 

 Standard Deviation (Annualized)

 

STD = log_returns.groupby([log_returns.index.year]).agg('std') * np.sqrt(252)

STD_avg = STD.mean()

 

pd.DataFrame(STD_avgcolumns = ['Average STD']).T

 



Variance (Annualized)

 

VAR = STD **2

VAR_avg = VAR.mean()

 

pd.DataFrame(VAR_avgcolumns = ['Average VAR']).T

 



Average Annualized Variance for all 6 years



RISK VS RETURN

 

# configuration - generate different colors & sizes

c = [y + x for yx in zip(APY_avgSTD_avg)]

c = list(map(lambda x : x /max(c), c))

s = list(map(lambda x : x * 600c))

# plot

figax = plt.subplots(figsize = (16,12))

ax.set_title(r"Risk ($\sigma$) vs Return ($APY$) of all  instruments")

ax.set_facecolor((0.950.950.99))

ax.grid(c = (0.750.750.99))

ax.set_xlabel(r"Standard Deviation $\sigma$")

ax.set_ylabel(r"Annualized Percetaneg Yield $APY$ or $R_{effective}$")

ax.scatter(STD_avgAPY_avgs = s , c = c , cmap = "Blues"alpha = 0.4edgecolors="grey"linewidth=2)

ax.axhline(y = 0.0,xmin = 0 ,xmax = 5,c = "blue",linewidth = 1.5,zorder = 0,  linestyle = 'dashed')

ax.axvline(x = 0.0,ymin = 0 ,ymax = 40,c = "blue",linewidth = 1.5,zorder = 0,  linestyle = 'dashed')

for idxinstr in enumerate(list(STD.columns)):

    ax.annotate(instr, (STD_avg[idx] + 0.01APY_avg[idx]))

 

plt.show()

 



 

For Risk VS Return I have taken the Average Percentage Yield and Standard Deviation and the result shows that the stocks I’ve selected are on average risk and return ratio.
Only the stock SBIN is on low scale of risk VS return.
HDFC & MPHASIS are little to high on the scale but they are no threat, they will generate a good interest in future.

 

Now I know which stock to invest in and which to not, I can easily alter my portfolio and start investing.

Thank For Reading. 

Comments

Popular posts from this blog

Net Present Value (NPV) On Python Using Numpy

Commodity Channel Index ( CCI ) Using Python

SMA Trading Strategy Using Python