Search code examples
pythonpandasyfinance

How to Convert Automatically Currency to USD and Label the Legend with the Index Name with Python, pandas, yfinance


I want to label the legend of the index stocks with its name like "Euronext 100 Index" instead of "^N100" because it is easier to be read that way for me. How to modify the label on the legend for each chart?

Second, I want to convert the price if it is in GBP it will be converted to USD, if it is in Euro it will be USD too, is there an automatic converter or should I put it manually?

Thank You.

This is the code:

from pandas_datareader import data as pdr
import yfinance as yf

yf.pdr_override()
y_symbols = ['^FTSE', '^GDAXI', '^FCHI', '^STOXX50E','^N100', '^BFX']
from datetime import datetime
startdate = datetime(2000,1,1)
enddate = datetime(2023,1,31)
data = pdr.get_data_yahoo(y_symbols, start=startdate, end=enddate)

#print(data)
#data['Close'].plot()

plt.figure(figsize=(20,10))
plt.plot(data.index, data['Close'], label=data["Close"].columns)

plt.xlabel("Date")
plt.ylabel("Price (in its own currency)")
plt.title("Europe Indexes 1/1/00 - 1/1/23")
plt.legend()
plt.show()

1


Solution

  • For currency conversion, you can use the forex-python library to convert between currencies. You'll need to install it first by running !pip install forex-python.

    from forex_python.converter import CurrencyRates
    cr = CurrencyRates()
    
    # Get exchange rate between GBP and USD and EUR and USD
    gbp_to_usd = cr.get_rate('GBP', 'USD')
    eur_to_usd = cr.get_rate('EUR', 'USD')
    

    For renaming the label, you can use pandas .rename(). before plotting.

    data.rename(columns={'^FTSE': 'FTSE 100 Index', 
                         '^GDAXI': 'DAX Index', 
                         '^FCHI': 'CAC 40 Index', 
                         '^STOXX50E': 'EURO STOXX 50 Index',
                         '^N100': 'Euronext 100 Index', 
                         '^BFX': 'BEL 20 Index'}, inplace=True)
    

    Here's the complete code:

    import yfinance as yf
    import matplotlib.pyplot as plt
    
    from pandas_datareader import data as pdr
    from forex_python.converter import CurrencyRates
    from datetime import datetime
    
    
    cr = CurrencyRates()
    
    # Get exchange rate between GBP and USD and EUR and USD
    gbp_to_usd = cr.get_rate('GBP', 'USD')
    eur_to_usd = cr.get_rate('EUR', 'USD')
    
    yf.pdr_override()
    y_symbols = ['^FTSE', '^GDAXI', '^FCHI', '^STOXX50E','^N100', '^BFX']
    startdate = datetime(2000,1,1)
    enddate = datetime(2023,1,31)
    data = pdr.get_data_yahoo(y_symbols, start=startdate, end=enddate)
    
    data.rename(columns={'^FTSE': 'FTSE 100 Index', 
                         '^GDAXI': 'DAX Index', 
                         '^FCHI': 'CAC 40 Index', 
                         '^STOXX50E': 'EURO STOXX 50 Index',
                         '^N100': 'Euronext 100 Index', 
                         '^BFX': 'BEL 20 Index'}, inplace=True)
    
    plt.figure(figsize=(20,10))
    plt.plot(data.index, data['Close'], label=data["Close"].columns)
    
    plt.xlabel("Date")
    plt.ylabel("Price (in its own currency)")
    plt.title("Europe Indexes 1/1/00 - 1/1/23")
    plt.legend()
    plt.show()