Workspace
Andy Ly/

Market Index Analysis

0
Beta
Spinner
#Import packages
import pandas as pd
import numpy as np
import seaborn as sns
import pandas_datareader as web 
Run cancelled
#Inspecting data (info, describe, head, tail).

Market_Index=pd.read_csv("Market_index_data.csv")
#Market_Index=pd.read_csv("Market_index_data.csv", parse_dates=['Date'],index_col='Date)'
Market_Index.Date=pd.to_datetime(Market_Index.Date)
Market_Index.set_index('Date',inplace=True)
print(Market_Index.info())
print(Market_Index.describe())
print(Market_Index.head(5))
print(Market_Index.tail(5))
Run cancelled
#Extracting latest market close prices of each market index
NDX=Market_Index["NDX_Close"]
print(NDX.tail(5))
DAX=Market_Index["DAX_Close"]
print(DAX.tail(5))
DJ=Market_Index["DJ_Close"]
print(DJ.mean())
FTSE=Market_Index["FTSE_Close"]
print(FTSE.tail())
SPX=Market_Index["SPX_Close"]
print(SPX.head(5))  

Risk and return characteristics of the 5 indices

Run cancelled
import pandas as pd
print('Annual Average Returns And Risk Since Jan 2010')
NDX_daily_returns = Market_Index["NDX_Close"].pct_change()
NDX_annual_returns=round(NDX_daily_returns.mean() * 250*100,2)
NDX_annual_returns_str=str(NDX_annual_returns)
NDX_annual_risk=round(NDX_daily_returns.std() * 250*100**0.5,2)
NDX_annual_risk_str=str(NDX_annual_risk)
NDX_annual_return = 'Nasdaq:' + NDX_annual_returns_str +'%' + ',' + NDX_annual_risk_str +'%'
print(NDX_annual_return)

DAX_daily_returns = Market_Index["DAX_Close"].pct_change()
DAX_annual_returns=round(DAX_daily_returns.mean() * 250*100,2)
DAX_annual_returns_str=str(DAX_annual_returns)
DAX_annual_risk=round(DAX_daily_returns.std() * 250*100**0.5,2)
DAX_annual_risk_str=str(DAX_annual_risk)
DAX_annual_return = 'DAX:' + DAX_annual_returns_str +'%' + ',' + DAX_annual_risk_str +'%'
print(DAX_annual_return)

DJ_daily_returns = Market_Index["DJ_Close"].pct_change()
DJ_annual_returns=round(DJ_daily_returns.mean() * 250*100,2)
DJ_annual_returns_str=str(DJ_annual_returns)
DJ_annual_risk=round(DJ_daily_returns.std() * 250*100**0.5,2)
DJ_annual_risk_str=str(DJ_annual_risk)
DJ_annual_return = 'DJ:' + DJ_annual_returns_str +'%' + ',' + DJ_annual_risk_str +'%'
print(DJ_annual_return)

FTSE_daily_returns = Market_Index["FTSE_Close"].pct_change()
FTSE_annual_returns=round(FTSE_daily_returns.mean() * 250*100,2)
FTSE_annual_returns_str=str(FTSE_annual_returns)
FTSE_annual_risk=round(FTSE_daily_returns.std() * 250*100**0.5,2)
FTSE_annual_risk_str=str(FTSE_annual_risk)
FTSE_annual_return = 'FTSE:' + FTSE_annual_returns_str +'%' + ',' + FTSE_annual_risk_str +'%'
print(FTSE_annual_return)

SPX_daily_returns = Market_Index["SPX_Close"].pct_change()
SPX_annual_returns=round(SPX_daily_returns.mean() * 250*100,2)
SPX_annual_returns_str=str(SPX_annual_returns)
SPX_annual_risk=round(SPX_daily_returns.std() * 250*100**0.5,2)
SPX_annual_risk_str=str(SPX_annual_risk)
SPX_annual_return = 'S&P 500:' + SPX_annual_returns_str +'%' + ',' + SPX_annual_risk_str +'%'
print(SPX_annual_return)
Run cancelled
# Create a new column that sums the last 4 columns
Market_Index['Sum of Last 4'] = Market_Index.iloc[:, -4:].sum(axis=1)
Market_Index
Run cancelled
# Creating the bar chart
plt.bar(['SPX', 'FTSE', 'DAX', 'DJ', 'NDX'], [SPX_annual_returns, FTSE_annual_returns, DAX_annual_returns, DJ_annual_returns, NDX_annual_returns], color = 'green')
plt.xlabel('Index')
plt.ylabel('Average Annual Returns')
plt.title('Average Annual Returns of Market Indices since 2010')
for i, v in enumerate([SPX_annual_returns, FTSE_annual_returns, DAX_annual_returns, DJ_annual_returns, NDX_annual_returns]):
    plt.text(i, v, str(v), ha='center', va='bottom')
plt.show()
Run cancelled
#Jan 22 to May 22 returns
print('Market Index Year To Date Returns As Of May 6th 2022')
NDX_2022_return =round((NDX[3107] - NDX[3021]) / NDX[3021]*100,2)
NDX_2022_return_str=str(NDX_2022_return)
NDX_YTD_Return = 'Nasdaq:' + NDX_2022_return_str +'%'
print(NDX_YTD_Return)   

DAX_2022_return =round((DAX[3107] - DAX[3021]) / DAX[3021]*100,2)
DAX_2022_return_str=str(DAX_2022_return)
DAX_YTD_Return = 'DAX:' + DAX_2022_return_str +'%'
print(DAX_YTD_Return) 
                        
DJ_2022_return =round((DJ[3107] - DJ[3021]) / DJ[3021]*100,2)
DJ_2022_return_str=str(DJ_2022_return)
DJ_YTD_Return = 'Dow Jones:' + DJ_2022_return_str +'%'
print(DJ_YTD_Return) 

FTSE_2022_return =round((FTSE[3107] - FTSE[3021]) / FTSE[3021]*100,2)
FTSE_2022_return_str=str(FTSE_2022_return)
FTSE_YTD_Return = 'FTSE 100:' + FTSE_2022_return_str +'%'
print(FTSE_YTD_Return) 

SPX_2022_return =round((SPX[3107] - SPX[3021]) / SPX[3021]*100,2)
SPX_2022_return_str=str(SPX_2022_return)
SPX_YTD_Return = 'S&P 500:' + SPX_2022_return_str +'%'
print(SPX_YTD_Return) 
Run cancelled
import matplotlib.pyplot as plt

# PLotting a bar chart of the Jan 22 to May 22 market index returns
# Adding data labels to the bar chart

returns = [SPX_2022_return, FTSE_2022_return, DJ_2022_return, DAX_2022_return, NDX_2022_return] 
indices = ['SPX', 'FTSE', 'DJ', 'DAX', 'NDX']  

colors = ['green' if r >= 0 else 'red' for r in returns]

plt.bar(indices, returns, color=colors)
plt.xlabel('Market Index')
plt.ylabel('Returns')
plt.title('Jan 2022-May 22 Returns')

for i, v in enumerate(returns):
    plt.text(i, v, str(v), ha='center', va='bottom')

plt.show()
Run cancelled
#To rescale the index values to a specific starting point of 1000, you can divide all the index values by the first index value and then multiply by 1000. This will ensure that the first index value becomes 1000 and the rest of the values are scaled accordingly.

# Plotting a line graph of Market_Index.NDX_Close, Market_Index.DAX_Close, Market_Index.DJ_Close, Market_Index.FTSE_Close and Market_Index.SPX_Close

scaled_NDX_Close = Market_Index['NDX_Close'] / Market_Index['NDX_Close'][0] * 1000
scaled_DAX_Close = Market_Index['DAX_Close'] / Market_Index['DAX_Close'][0] * 1000
scaled_DJ_Close = Market_Index['DJ_Close'] / Market_Index['DJ_Close'][0] * 1000
scaled_FTSE_Close = Market_Index['FTSE_Close'] / Market_Index['FTSE_Close'][0] * 1000
scaled_SPX_Close = Market_Index['SPX_Close'] / Market_Index['SPX_Close'][0] * 1000

plt.plot(scaled_NDX_Close, label='NDX_Close')
plt.plot(scaled_DAX_Close, label='DAX_Close')
plt.plot(scaled_DJ_Close, label='DJ_Close')
plt.plot(scaled_FTSE_Close, label='FTSE_Close')
plt.plot(scaled_SPX_Close, label='SPX_Close')

plt.xlabel('Date')
plt.ylabel('Scaled Close Price')
plt.title('Scaled Market Index Close Prices')
plt.legend()
plt.show()
Run cancelled
# Calculate the top 5 largest percentage gains from FTSE_daily_returns and the date.  To search for a different index, change the naming convention.  To change to the worst 5 days, change ascending to True.

# Sort the FTSE_daily_returns in descending order
sorted_returns = FTSE_daily_returns.sort_values(ascending=False)*100

# Get the top 5 largest percentage gains
top_5_gains = sorted_returns.head(5)
print(top_5_gains.round(2).map('{:.2f}%'.format))

Portfolio Analysis:Sharpe

Run cancelled
import pandas as pd
print('Market Index Sharpe Ratios')
NDX_total_return =(NDX[3107]-NDX[0]) / NDX[0]
NDX_annualized_return =((1+ NDX_total_return)**(1/12))-1
rfr=0
NDX_daily_returns = Market_Index["NDX_Close"].pct_change()
Vol_NDX= NDX_daily_returns.std() * 250**0.5
NDX_Sharpe_Ratio = round((NDX_annualized_return -rfr) / Vol_NDX,2)
NDX_Sharpe_Ratio_str=str(NDX_Sharpe_Ratio)
NDX_Sharpe_Ratio_2 = 'NASDAQ:' + NDX_Sharpe_Ratio_str +'%'
print(NDX_Sharpe_Ratio_2)

DAX_total_return =(DAX[3107]-DAX[0]) / DAX[0]
DAX_annualized_return =((1+ DAX_total_return)**(1/12))-1
rfr=0
DAX_daily_returns = Market_Index["DAX_Close"].pct_change()
Vol_DAX= DAX_daily_returns.std() * 250**0.5
DAX_Sharpe_Ratio = round((DAX_annualized_return -rfr) / Vol_DAX,2)
DAX_Sharpe_Ratio_str=str(DAX_Sharpe_Ratio)
DAX_Sharpe_Ratio_2 = 'DAX:' + DAX_Sharpe_Ratio_str +'%'
print(DAX_Sharpe_Ratio_2)

DJ_total_return =(DJ[3107]-DJ[0]) / DJ[0]
DJ_annualized_return =((1+ DJ_total_return)**(1/12))-1
rfr=0
DJ_daily_returns = Market_Index["DJ_Close"].pct_change()
Vol_DJ= DJ_daily_returns.std() * 250**0.5
DJ_Sharpe_Ratio = round((DJ_annualized_return -rfr) / Vol_DJ,2)
DJ_Sharpe_Ratio_str=str(DJ_Sharpe_Ratio)
DJ_Sharpe_Ratio_2 = 'DJ:' + DJ_Sharpe_Ratio_str +'%'
print(DJ_Sharpe_Ratio_2)

FTSE_total_return =(FTSE[3107]-FTSE[0]) / DJ[0]
FTSE_annualized_return =((1+ FTSE_total_return)**(1/12))-1
rfr=0
FTSE_daily_returns = Market_Index["FTSE_Close"].pct_change()
Vol_FTSE= FTSE_daily_returns.std() * 250**0.5
FTSE_Sharpe_Ratio = round((FTSE_annualized_return -rfr) / Vol_FTSE,2)
FTSE_Sharpe_Ratio_str=str(FTSE_Sharpe_Ratio)
FTSE_Sharpe_Ratio_2 = 'FTSE:' + FTSE_Sharpe_Ratio_str +'%'
print(FTSE_Sharpe_Ratio_2)

SPX_total_return =(SPX[3107]-SPX[0]) / SPX[0]
SPX_annualized_return =((1+ SPX_total_return)**(1/12))-1
rfr=0
SPX_daily_returns = Market_Index["SPX_Close"].pct_change()
Vol_SPX= SPX_daily_returns.std() * 250**0.5
SPX_Sharpe_Ratio = round((SPX_annualized_return -rfr) / Vol_SPX,2)
SPX_Sharpe_Ratio_str=str(SPX_Sharpe_Ratio)
SPX_Sharpe_Ratio_2 = 'S&P 500:' + SPX_Sharpe_Ratio_str +'%'
print(SPX_Sharpe_Ratio_2)
Run cancelled
# Creating the bar chart
labels = ['SPX', 'FTSE', 'DAX', 'NDX']
values = [SPX_Sharpe_Ratio, FTSE_Sharpe_Ratio, DJ_Sharpe_Ratio, NDX_Sharpe_Ratio]
plt.bar(labels, values)
plt.xlabel('Index')
plt.ylabel('Sharpe Ratio')
plt.title('Sharpe Ratio Comparison')

for i, v in enumerate(values):
    plt.text(i, v, str(v), ha='center', va='bottom')

plt.show()

Market returns distribution




  • AI Chat
  • Code