alvarobartt / investpy

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

US500 Futures #357

Closed cunnial closed 3 years ago

cunnial commented 3 years ago

Hi, loving the tool so thanks a stack! I am however really struggling to pull out S&P500 futures data "live" price (or just the latest price. the only way I can pull it is using the search function and "retrieve_historical_data(...) which intuitively doesn't seem correct? I have read the docs and seen the examples but cannot fund anything on functions that call Futures data?

I have been using the eq_price =investpy.get_index_historical_data(index="S&P 500 Futures", country="United States", from_date="20/04/2021", to_date="21/04/2021")

I have tried to replace the index with the codes and ID from the search results but cannot get it to work?

{"id_": 8839, "name": "S&P 500 Futures", "symbol": "US500", "country": "united states", "tag": "/indices/us-spx-500-futures",

"pair_type": "indices", "exchange": ""}

Would be great to clear this up if you get a moment? US500

cunnial commented 3 years ago

@alvarobartt hi I noticed there is a pull request for this, be awesome if we could get it pushed through.

https://github.com/alvarobartt/investpy/pull/185

alvarobartt commented 3 years ago

Hi @cunnial! Thanks for your kind words :fire:

Yes, I'm aware of that PR #185, I'll review it whenever I have more free time as now I'm pretty busy 😩, in the meantime, you can keep on using the investpy.search_quotes() function, but for the sake of simplicity as you just want to retrieve the live value, you can use search_result.retrieve_recent_data() as it's faster than calling search_result.retrieve_historical_data(), and then keep the Close value from the resulting pandas.DataFrame.

Also, I recommend you refine the search so that the first result is the one you are looking for, you can play around with this either using the parameters that you printed from the search_result or just go to Investing.com and use their search engine as investpy.search_quotes() reproduces its behavior. Whenever you get the result that you want to be the first one you can: investpy.search_quotes(..., n_results=1) so that instead of receiving a list of SearchObj you just receive one, for the sake of simplicity.

cunnial commented 3 years ago

Thanks! I'll run through this tonight. No rush on the amendment I'm sure you're super busy!

alvarobartt commented 3 years ago

Any updates on this @cunnial?

BTW, you should update your investpy version to the latest one which is the v1.0.6 right now! :fire:

As you don't need to access the first element of the list whenever you call investpy.search_quotes() but you just one the first result, you can just set n_results=1 and you'll get an investpy.SearchObj class instance rather than a list of instances.

Here's an example:

>>> import investpy

>>> search_result = investpy.search_quotes(text='S&P 500 Futures', products=['indices'], countries=['United States'], n_results=1)
>>> print(search_result)
{"id_": 8839, "name": "S&P 500 Futures", "symbol": "US500", "country": "united states", "tag": "/indices/us-spx-500-futures", "pair_type": "indices", "exchange": ""}

>>> recent_data = search_result.retrieve_recent_data()
>>> print(recent_data.tail())
               Open     High      Low    Close  Volume
Date                                                  
2021-05-03  4187.88  4202.38  4178.88  4179.38       0
2021-05-04  4179.88  4184.38  4120.62  4159.62       0
2021-05-05  4159.38  4179.88  4153.12  4160.12       0
2021-05-06  4160.38  4198.00  4140.62  4194.12       0
2021-05-07  4194.12  4203.62  4191.88  4199.12       0

>>> current_value = recent_data.iloc[-1]['Close']
>>> print(current_value)
4199.12