I want to scrape multiple stocks from Yahoo finance, and convert the stocks to euro, if the stock is for instance in USD dollar of GBP. I have a code which converts the historical stocks to euro, with the date, but now I have to make the df that it is possible to plot the data correctly. This is code I have now.
import yfinance as yf
import pandas as pd
import numpy as np
start = '2013-01-01'
end = '2021-01-01'
rates = ['USDEUR=X', 'GBPEUR=X']
tickers = yf.Tickers(' '.join(rates))
exchange_rates = []
for i in range(len(tickers.tickers)):
exchange_rates.append(tickers.tickers[i].history(start=start, end=end).Close)
ex_df = pd.DataFrame(exchange_rates).T
ex_df.columns = rates
ex_df['EUREUR=X'] = 1.0
assets = {'^AEX':'EUR', 'AAPL':'USD', 'AZN.L':'GBP', 'AD.AS':'EUR', 'ASML.AS':'EUR'}
stock_df = pd.DataFrame()
for k,v in assets.items():
data = yf.download(k, start=start, end=end, progress=False).Close.to_frame()
data['ticker'] = k
if v[:3] == 'EUR':
data['rating'] = ex_df['EUREUR=X']
data['price'] = data['Close'] / data['rating']
elif v[:3] == 'USD':
data['rating'] = ex_df['USDEUR=X']
data['price'] = data['Close'] / data['rating']
elif v[:3] == 'GBP':
data['rating'] = ex_df['GBPEUR=X']
data['price'] = data['Close'] / data['rating']
else:
data['rating'] = np.NaN
data['price'] = np.NaN
stocks_df = pd.concat([stock_df, data], axis=0)
I will repost the full version of the code again.
It is the same for both df.fillna()
. I also replaced the graph image.
Good luck.
import yfinance as yf
import pandas as pd
import numpy as np
start = '2020-07-01'
end = '2021-01-01'
rates = ['USDEUR=X', 'GBPEUR=X']
tickers = yf.Tickers(' '.join(rates))
exchange_rates = []
for i in range(len(tickers.tickers)):
exchange_rates.append(tickers.tickers[i].history(start=start, end=end).Close)
ex_df = pd.DataFrame(exchange_rates).T
ex_df.columns = rates
ex_df['EUREUR=X'] = 1.0
assets = {'^AEX':'EUR', 'AAPL':'USD', 'AZN.L':'GBP', 'AD.AS':'EUR', 'ASML.AS':'EUR'}
stock_df = pd.DataFrame()
for k,v in assets.items():
data = yf.download(k, start=start, end=end, progress=False).Close.to_frame()
data['ticker'] = k
if v[:3] == 'EUR':
data['rating'] = ex_df['EUREUR=X']
data['price'] = data['Close'] / data['rating']
elif v[:3] == 'USD':
data['rating'] = ex_df['USDEUR=X']
data['price'] = data['Close'] / data['rating']
elif v[:3] == 'GBP':
data['rating'] = ex_df['GBPEUR=X']
data['price'] = data['Close'] / data['rating']
else:
data['rating'] = np.NaN
data['price'] = np.NaN
stock_df = pd.concat([stock_df, data], axis=0)
my_stocks = stock_df.pivot(columns='ticker', values='price')
my_stocks.head()
ticker AAPL AD.AS ASML.AS AZN.L ^AEX
Date
2020-06-30 102.574478 24.260000 326.899994 7694.486885 559.729980
2020-07-01 102.278087 23.879999 327.149994 7665.306388 562.669983
2020-07-02 102.418480 24.129999 332.750000 7758.488549 570.750000
2020-07-03 102.418480 23.870001 333.950012 7626.217088 568.630005
2020-07-06 105.119162 23.900000 343.549988 7810.244933 578.520020
import matplotlib.pyplot as plt
for c in my_stocks.columns.values[1:]:
plt.plot(my_stocks.index, my_stocks[c], label=c)
plt.title('title')
plt.xlabel('Date',fontsize=18)
plt.ylabel('Adj. Price € (EUR)',fontsize=18)
plt.legend(my_stocks.columns.values[1:], loc='center right')
plt.show()