alvarobartt / investpy

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

Stock returned from `search_quotes` not found in `stocks.get_stock_recent_data` #216

Closed arthur1993 closed 3 years ago

arthur1993 commented 3 years ago

Please find below a simple case illustrated. The symbol "XIN" is returned by the search_quotes method, but get_stock_recent_data is unable to get data for this it.

>>> import investpy
>>> for results in investpy.search.search_quotes("xin", countries=["United States"]):
...     print(results)

{"id_": 512, "name": "iShares China Large-Cap ETF", "symbol": "FXI", "country": "united states", "tag": "/etfs/ishares-ftse-xinhua-china-25", "pair_type": "etfs", "exchange": "NYSE"}
{"id_": 29747, "name": "Xinyuan Real Estate Co Ltd", "symbol": "XIN", "country": "united states", "tag": "/equities/xinyuan-real-estate-co-ltd", "pair_type": "stocks", "exchange": "NYSE"}
{"id_": 29749, "name": "Dunxin Financial Holdings Ltd", "symbol": "DXF", "country": "united states", "tag": "/equities/china-xiniya-fashion-ltd", "pair_type": "stocks", "exchange": "NYSE"}
{"id_": 21525, "name": "iShares China Large-Cap ETF", "symbol": "FXI", "country": "united states", "tag": "/etfs/ishares-ftse-xinhua-china-25?cid=21525", "pair_type": "etfs", "exchange": "NYSE Arca"}
{"id_": 942300, "name": "Xinyi Glass Holdings Ltd", "symbol": "XYIGY", "country": "united states", "tag": "/equities/xinyi-glass-holdings-ltd", "pair_type": "stocks", "exchange": "OTC Markets"}

>>> investpy.stocks.get_stock_recent_data("XIN", "United States")

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/investpy/stocks.py", line 262, in get_stock_recent_data
    raise RuntimeError("ERR#0018: stock " + stock + " not found, check if it is correct.")
RuntimeError: ERR#0018: stock xin not found, check if it is correct.

After having a look at the code behind both method I realized one (search_quotes) was directly relying on the search results of Investying, while another one (get_stock_recent_data) was relying directly on the stocks.csv file. In my opinion this shouldn't happen, only a single source of data should exist across the whole module.

alvarobartt commented 3 years ago

Hi @arthur1993 :fire:

As you may know, the investpy.search_quotes() function uses the Investing.com search engine so as to retrieve any financial product, but as you may know, the investpy.get_funds_historical_data() (applies to any data retrieval function in investpy) retrieves the information from Investing.com based on the IDs stored in the static CSV files.

That means that investpy.search_quotes() contains ALL the information available at Investing.com while all the other functions just some of them, since the Investing.com indexing is tricky and it's hard to keep the script up to date...

So I highly recommend you to just use the investpy.search_quotes() function if the financial product you are looking for is not available in the static CSV files (the error you shared above).

:pushpin: Make sure that you are using the latest investpy version! investpy v1.0.6

Also, in order to retrieve either the recent data, historical data, or the information, you can use the SearchObj class methods! Here's an example of how to do that:

import investpy

search_result = investpy.search_quotes(text='XIN', products=['stocks'], countries=['United States'], 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()

To further explore the functionality of investpy.search_quotes() please check [Wiki - investpy.search_quotes()](https://github.com/alvarobartt/investpy/wiki/investpy.search_quotes())