alvarobartt / investpy

Financial Data Extraction from Investing.com with Python
https://investpy.readthedocs.io/
MIT License
1.59k stars 375 forks source link

Getting stock not found error when it is there #349

Open sirinath opened 3 years ago

sirinath commented 3 years ago

I get

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-19-626f978409c2> in <module>
      5 
      6 for x in mapping.iloc[:, 3]:
----> 7     df = investpy.get_stock_historical_data(stock=x,
      8                                         country='sri lanka',
      9                                         from_date='01/01/2000',

C:\ProgramData\Miniconda3\lib\site-packages\investpy\stocks.py in get_stock_historical_data(stock, country, from_date, to_date, as_json, order, interval)
    517 
    518     if stock not in list(stocks['symbol'].apply(unidecode).str.lower()):
--> 519         raise RuntimeError("ERR#0018: stock " + stock + " not found, check if it is correct.")
    520 
    521     symbol = stocks.loc[(stocks['symbol'].apply(unidecode).str.lower() == stock).idxmax(), 'symbol']

RuntimeError: ERR#0018: stock aspm not found, check if it is correct.

When the stock ASPM is there: https://www.investing.com/equities/aitken-spence-plantation-mana

alvarobartt commented 3 years ago

Hi @sirinath, currently invespty has a big issue, which is related to the static data since it needs to be updated with some sort of frequency but due to the Investing.com limitations is hard to do and takes too long (I may need to rent a server with the buymeacoffee donations so that I can create a script that updates those files lets say once a week or once every two weeks)...

So on, currently, some data may be missing from stocks.csv which is the static file, even though that data is available in Investing.com, so on I developed the function investpy.search_quotes() that solves this issue as it's using Investing.com search engine to look for the financial products you want to.

Here's an example of how to use it for your concrete query (note that you can also run this function inside a loop just like you are doing in the code you originally sent):

import investpy

search_result = investpy.search_quotes(text='Aitken Spence Plantation', countries=['Sri Lanka'], products=['stocks'], n_results=1)

recent_data = search_result.retrieve_recent_data()
historical_data = search_result.retrieve_historical_data(from_date='01/01/2020', to_date='01/01/2021')
information = search_result.retrieve_information()

Let me know whether this worked for you or not! :+1:

More information available at https://github.com/alvarobartt/investpy/wiki/investpy.search_quotes()

:pushpin: Note. In the recent investpy versions, the investpy.search_quotes() function just returns one search result whenever the value for n_results is 1, which means that instead of expecting a list of SearchObj class instances, you should just expect one, as shown in the example presented above.

sirinath commented 3 years ago

Can I use search to get all stocks in Sri Lanka?

alvarobartt commented 3 years ago

Yes you can @sirinath as long as all the Sri Lanka stocks are available in Investing.com :+1:

sirinath commented 3 years ago

Can you make text optional so one can do something like:

import investpy

search_result = investpy.search_quotes(countries=['Sri Lanka'], products=['stocks'])

Maybe you can introduce the exchange as a parameter also to search by exchange.

alvarobartt commented 3 years ago

@sirinath, sorry to inform you that that's not the way to use the search engine neither from Investing.com nor from investpy... So I cannot remove the text parameter and if you leave it empty the function will crash!

The other option is that you go with investpy.get_stocks_list(country='Sri Lanka') so as to retrieve all the symbols for the static stocks in stocks.csv. That's the only option you can go with, and if you want to include new stocks or look for new stocks you should just use investpy.search_quotes() as I described above 👍🏻

sirinath commented 3 years ago

Search also cause issues with the following companies:

Aitken Spence Plantation Managements
City Housing & RE
MTD Walkers PLC
Lucky Lanka Milk Processing
Lucky Lanka Milk Processing NV
Mercantile Investments and Finance
J L Morison Sons & Jones
J L Morison Sons & Jones NV
Namal Acuity
Senkadagala Finance
Finance Company Not Voting
sirinath commented 3 years ago

Also investpy.get_stocks_list(country='Sri Lanka') gives 300 results. The stock screen on the website gives 309 results.

sirinath commented 3 years ago

When I use:

        df = search_result.retrieve_historical_data(
            from_date='01/01/2000', 
            to_date=date.today())

I get error for all tickers.

alvarobartt commented 3 years ago

Ok, let me check that! Make sure that you are properly formatting the date as day/month/year e.g. today's date in this format is 07/05/2021.

I'll run a script to check the following list of financial products just as you mentioned above:

Aitken Spence Plantation Managements
City Housing & RE
MTD Walkers PLC
Lucky Lanka Milk Processing
Lucky Lanka Milk Processing NV
Mercantile Investments and Finance
J L Morison Sons & Jones
J L Morison Sons & Jones NV
Namal Acuity
Senkadagala Finance
Finance Company Not Voting