pythonpandasyahoodatareaderticker

How to lookup any index from Yahoo using pandas-datareader


I want to use look ups for any online index, including those with digits. A random example is:

https://uk.finance.yahoo.com/quote/YSM6.AX/futures?p=YSM6.AX

A naive method is to use pandas-datareader:

from pandas_datareader import data as datareader
online_data = datareader.DataReader('YSM6.AX', 'yahoo', start, end)

However, this doesn't work. I think the digits in the ticker aren't handled properly. This command works fine with e.g. "AAPL".

How do I get this to work for any index?


Solution

  • The YSM6.AX link shows that there is no data for this stock. If you want to grab multiple stock, and get specifically the adjusted close, you can use this code. It takes into account any funny stock tickers that have either a "-", or in the case of YSM6.AX, a "." inside the ticker.

        import pandas as pd
        import datetime
        from pandas_datareader import data, wb
        tickers = ["BRK.B", "AAPL", "MSFT", "YHOO", "JPM"]
        series_list = []
    
        start = datetime.datetime(2012, 4, 5)
        end = datetime.datetime(2017, 3, 28)
        for security in tickers:
            s = data.DataReader(security.replace(".","-"),"yahoo",start, end )["Adj Close"]
    
            s.name = security
            series_list.append(s)
    
        df = pd.concat(series_list, axis=1)
    
        stocks= pd.DataFrame(df)
        stocks