Fetch data for multiple stocks

Hi Team,



How to import mutiple stocks data using nsepy.



 

Hi Amit,



nsepy's get_history function doesn't take an array type object, so to import muliple stocks, the function needs to be called from a loop structure. 



You may store the required stocks with their start and end date in a csv. Read that file with pandas and pass members of each row to get_history. Sample code is:



stock = pd.read_csv('stock.csv', parse_dates = [1,2])

stock

 

Symbol Start Date End Date
INFY 1/1/2016 1/8/2017
SBIN 1/4/2016 1/9/2017
ABAN 1/5/2016 1/15/2017


stock_data=[get_history(symbol=rows[0], start=rows[1], end = rows[2])
                     for index, rows in stock.iterrows()
                    ]
stock_data[0] would display the first dataframe an so on.
You may also choose to store result of get_history in a dictionary indexed by the symbol.

Thanks,
Ashish

Hi Ashish,



symbol=['SBIN','GAIL']

import datetime

from datetime import date

from nsepy import get_history



for s in symbol:

    data[s]=get_history(start=date('2019-01-25'),end=date('2019-01-25'))

    



i write this  but it's giving error 



an integer is required .



thanks for your suggestion.

Hi Amit,



Please try the following code:



symbol=['SBIN','GAIL']

import datetime

from datetime import date

from nsepy import get_history

data={}

for s in symbol:

    data[s]=get_history(s,start=date(2019,1,25),end=date(2019,1,25))



Please pass the symbol to the get_history function and use an empty dictionary to initiate data, as indexing a list using str is not supported in python.





Thanks,

Ashish