Search code examples
pythonpython-3.xpandasloopsquandl

Get closing stock prices for yesterday for multiple stocks


I'm trying to get yesterday's closing stock prices for all stocks listed on the BSE using https://www.quandl.com/data/BSE-Bombay-Stock-Exchange as the data source. I have a list of company codes that I can use to pull that data but I need to figure out how to iterate over it correctly.

According to the quandl documentation, I can use quandl.get('BSE/BOM500002', column_index = '4', start_date='2019-03-19', end_date='2019-03-20') to get yesterday's closing price for a stock where BOM500002 would be the company code. If my list of company codes is listed in companyCodes['code'], could you help me figure out how to generate the company code dynamically to get yesterday's closing stock prices for all stocks listed on this exchange?

Bonus Question: How would I list the name of the stock next to the closing price?

enter image description here


Solution

  • Here is way to get date name together with the results:

    import quandl
    
    df = pd.DataFrame([("BOM500002", "ABB India Limited"),("BOM500003", "AEGIS")], columns=["Code", "Name"])
    
    
    results = []
    for i, r in df.iterrows():
        result = quandl.get('BSE/'+r["Code"], column_index = '4', start_date='2019-03-19', end_date='2019-03-20')
        result["Name"] = r["Name"]
        results.append(result)
    
    final = pd.concat(results)