investpy icon indicating copy to clipboard operation
investpy copied to clipboard

multiple stocks in a single request

Open tommifi opened this issue 4 years ago • 10 comments

Hi Alvaro, I am able to get historical data on one stock per each request. But I can not get historical data for multiple stocks in a single request. Here below the code I used:

tickers = "aapl, goog"

df = investpy.get_stock_historical_data(stock=tickers, country='united states', from_date='01/01/2010', to_date='01/01/2019')

Here below the error:

Traceback (most recent call last): File "C:\Users\tommaso\miniconda3\envs\3.7\lib\site-packages\IPython\core\interactiveshell.py", line 3326, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 4, in to_date='01/01/2019') File "C:\Users\tommaso\miniconda3\envs\3.7\lib\site-packages\investpy_init_.py", line 522, in get_stock_historical_data raise RuntimeError("ERR#0018: stock " + stock + " not found, check if it is correct.") RuntimeError: ERR#0018: stock aapl, goog not found, check if it is correct.

Regards t

tommifi avatar Oct 18 '19 13:10 tommifi

Hi @tommifi!

The functionality you are proposing is not done yet, since the retrieval function just admits one stock symbol per request, which means that just one str can be sent as stock parameter. Anyways, the improvement you are proposing will be evaluated, and if aproved, it will be available in next releases via allowing stock parameter to be a list of stock symbols in order to retrieve more than one stock pandas.DataFrame of historical data.

Currently, here is the solution to retrieve more than one stock historical data pandas.DataFrame and add them into a list of stock historical data objects:

import investpy

stocks = ['AAPL', 'GOOG']
dfs = list()

for stock in stocks:
    df = investpy.get_stock_historical_data(stock=stock, 
                                            country='united states',
                                            from_date='01/01/2010',
                                            to_date='01/01/2019')

    dfs.append(df)

Hope this helped you! :octocat: Stay tunned for future updates on this issue!

alvarobartt avatar Oct 18 '19 17:10 alvarobartt

Hi @tommifi!

Just let you know that this issue will not be worked on for now, since currently using a for loop seems to be enough. Anyways, if you think that this issue is a relevant feature to include in investpy just let me know and I will re-open the issue whenever I have time to include it.

Thank you for the support! :star: :octocat:

alvarobartt avatar Nov 15 '19 15:11 alvarobartt

Hi Alvaro,

Could you please write the complete code from

  1. extracting multiple stocks historical data (AAPL, GOOG)

  2. structure the data under Column 'AAPL','Date','Open','High','Low','Close','Volume','Currency',GOOG','Date','Open','High','Low','Close','Volume','Currency'

  3. export to CSV or Excel

I'm very new to programming. Appreciate your kind guidance and support along my learning journey.

Thanks in advance!

freddie3399 avatar Jan 16 '21 05:01 freddie3399

Sure @freddie3399! I'll try to prepare a sample Python code for you to do that 🤗

alvarobartt avatar Jan 16 '21 17:01 alvarobartt

Can I also see that python code sample??

ScottJRoberts avatar Jan 25 '21 22:01 ScottJRoberts

Sure! I'll try to share it with you later today as I'm currently working and I need to create that script from scratch! 👍🏻

alvarobartt avatar Jan 26 '21 08:01 alvarobartt

Sure! I'll try to share it with you later today as I'm currently working and I need to create that script from scratch! 👍🏻

Hi Alvaro! Were you able to write the code? I'm looking forward to hearing from you!

gahoccode avatar Sep 22 '21 07:09 gahoccode

Hi @gahoccode, sorry I forgot to share that code, I'll try to prepare a simple code to achieve that so that you can use it! :hugs:

Anyway, could you please share with me what do you exactly want? Is it something like:

- [AAPL,GOOG]
- Retrive latest stock information
- Append both DFs
- Save DF as Excel File

alvarobartt avatar Sep 22 '21 07:09 alvarobartt

Hi @gahoccode, sorry I forgot to share that code, I'll try to prepare a simple code to achieve that so that you can use it! 🤗

Anyway, could you please share with me what do you exactly want? Is it something like:

- [AAPL,GOOG]
- Retrive latest stock information
- Append both DFs
- Save DF as Excel File

So just as what @freddie3399 said, I'd like to see how you 1.extracting multiple stocks historical data (AAPL, GOOG)

2.structure the data under Column 'AAPL','Date','Open','High','Low','Close','Volume','Currency',GOOG','Date','Open','High','Low','Close','Volume','Currency'

  1. export to CSV or Excel

I tried to pull 2 stocks by applying the code you gave but the result turns out not quite what I expected. It seems that it only pulls either pull only 1 of the 2 or include prices in one column. I'd like to separate price by stock column. Trying to do a percent change calculation for multiple stocks in a table. Thanks!

gahoccode avatar Sep 22 '21 08:09 gahoccode

Hi @gahoccode, sorry I forgot to share that code, I'll try to prepare a simple code to achieve that so that you can use it! 🤗

Anyway, could you please share with me what do you exactly want? Is it something like:

- [AAPL,GOOG]
- Retrive latest stock information
- Append both DFs
- Save DF as Excel File

Hello there Alvaro, I too had the same query. I tried your earlier vode of for loop but it shows run time error 520. I am not sure you are aware but there is a similar library nsepy which I used for Indian markets it distinguishes stocks very diligently under different columns and we can append the list with ease

pardeshishivam avatar Apr 18 '22 12:04 pardeshishivam