Search code examples
pythonpandas-datareadertickerisin

Python: call pandas_datareader with isin or wkn or translate this into ticker symbol?


I have a real big list of stocks with ISIN and WKN-Number. My aim is to use pandas_datareader to get now historical data from that stocks. My problem is, the function e.g.

import pandas_datareader as web

stock = web.DataReader('ALB', data_source="yahoo", start="01.01.2021", end="30.10.2021")

only can work with ticker-symbols. Is there some way (maybe other library) to solve that or some algorithm to transform the Numbers into ticker symbols?


Solution

  • At least for the ISIN you can use investpy stocks.search_stocks function, which returns a pandas.DataFrame containing (among others information) the symbol for that ISIN code.

    import investpy
    
    df = investpy.stocks.search_stocks(by='isin', value='US0126531013')
    print(df)
    #          country       name              full_name          isin currency symbol
    # 0         mexico  Albemarle         Albemarle Corp  US0126531013      MXN    ALB
    # 1  united states  Albemarle         Albemarle Corp  US0126531013      USD    ALB
    # 2        germany  Albemarle  Albemarle Corporation  US0126531013      EUR   ALLE
    
    from pandas_datareader import data as pdr
    
    symbol = df.loc[df['country']=='united states','symbol'].values[0]
    print(symbol) # ALB
    
    stock = pdr.DataReader(symbol, data_source="yahoo", start="2021-01-01", end="2021-10-30")
    print(stock.head())
    
                      Open        High         Low       Close   Adj Close   Volume
    Date
    2021-01-04  152.899994  156.759995  150.289993  152.630005  151.645477  2898700
    2021-01-05  152.669998  166.779999  152.669998  162.929993  161.879028  2628000
    2021-01-06  165.500000  178.589996  165.500000  175.100006  173.970535  3335500
    2021-01-07  183.479996  187.250000  180.550003  184.000000  182.813126  2947800
    2021-01-08  184.490005  186.929993  178.660004  184.100006  182.912491  1659400