Download historical data for 3 ETFs. 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. 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).
I tried to do it with Python but i got a error can you help me ?
import pandas as pd
import yfinance as yf
import numpy as np
import matplotlib.pyplot as plt
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()
Download historical data for 3 ETFs. 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. 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).
I tried to do it with Python but i got a error can you help me ?
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['AdjClose'] / data['AdjClose'].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()