loicdaloz / etfdata

Personal Portofolio / Data Science applied to ETF
0 stars 1 forks source link

ETF Portofolio work #3

Closed MkFint closed 1 year ago

MkFint commented 1 year ago

Hello i'm a student in Finance and i have a work to do, but i would like if you want to help me if you can on coding with Python.

Here the work : Download historical data for 3 ETFs of your choice. Use weekly quotes over the longest common period. Additionally, create a portfolio (P) consisting of an equal part of these 3 ETFs. Warning: choose ETFs that have been listed for at least 10 years and that are not too correlated with each other. Exercise 1 a- Present in a few lines the characteristics of the selected ETFs and portfolio P. Specify the calculation used to constitute portfolio P. b- Graphically represent the evolution of the value of your ETFs and portfolio P, by normalizing the initial value to 100 euros (or $ in the case of trading on the US market). Exercise 2 The following calculations will apply to weekly geometric returns (not annualized). a- Calculate the descriptive statistics of the returns. b- Apply the Jarque-Bera normality test. c- Represent the autocorrelation function of the returns. d- Calculate the correlation matrix of the returns of the 3 ETFs and portfolio P. e- Calculate the approximate fourth-order expected utility of a logarithmic utility function for the 3 ETFs and portfolio P. Exercise 3 a- Calculate the 90% VaR for a horizon of 5 business days (1 week) using 3 methods: Historical, Normal and Corrected (Cornish-Fisher) assuming an exposure of 20,000 euros. Calculate the Normal 90% VaR for a horizon of 15 business days (3 weeks). b- Calculate the 90% CVaR (or Expected Shortfall) for a horizon of 5 business days (1 week) using 3 methods: Historical, Normal and Corrected (Cornish-Fisher) assuming an exposure of 20,000 euros.

For the first question i tried this :

import pandas as pd
import yfinance as yf
import numpy as np
import matplotlib.pyplot as plt

# Télécharger les données historiques des ETFs
start_date = '2011-01-01'
end_date = '2023-03-28'
tickers = ['SPY', 'EFA', 'IYR']

data = yf.download(tickers, start=start_date, end=end_date, interval='1wk', group_by='ticker', auto_adjust=True)

# Normaliser la valeur initiale à 100
normalized_data = (data['Adj Close'] / data['Adj Close'].iloc[0]) * 100

# Créer le portefeuille P
portfolio = (normalized_data['SPY'] + normalized_data['EFA'] + normalized_data['IYR']) / 3
normalized_data['Portfolio'] = portfolio

# Représentation graphique
plt.figure(figsize=(12, 6))
for ticker in tickers + ['Portfolio']:
    plt.plot(normalized_data.index, normalized_data[ticker], label=ticker)

plt.xlabel('Date')
plt.ylabel('Valeur normalisée (€)')
plt.title('Évolution de la valeur des ETFs et du portefeuille P')
plt.legend()
plt.grid()
plt.show()

but i got an error with KeyError: 'Adj Close', but i tried to data.info but it doesnt work any help ? :)

loicdaloz commented 1 year ago

Hi, when you have this kind of error always check your columns headers of you data frame via print(XXX.columns) in this case print(data.columns) you will see that the dataframe don't contain any 'Adj Close' column

MultiIndex([('EFA', 'Open'), ('EFA', 'High'), ('EFA', 'Low'), ('EFA', 'Close'), ('EFA', 'Volume'), ('SPY', 'Open'), ('SPY', 'High'), ('SPY', 'Low'), ('SPY', 'Close'), ('SPY', 'Volume'), ('IYR', 'Open'), ('IYR', 'High'), ('IYR', 'Low'), ('IYR', 'Close'), ('IYR', 'Volume')], )

I don't think the 'Adj Close' is available when extracting weekly data. Some data sources, such as Yahoo Finance, only provide adjusted closing prices for daily data. Issue is that taking only 'Close' column provides the unadjusted closing price that don't take into consideration Dividends and Stock splits, which will give you wrong data over 10 years You can try using a daily interval instead of a weekly interval. This will provide the 'Adj Close' column, which you can then resample to weekly data using the resample() method in pandas.

MkFint commented 1 year ago

Thanks you, it's worked perfectly now!