-
Notifications
You must be signed in to change notification settings - Fork 381
investpy.search_quotes()
investpy.search_quotes()
function was created due to the necessity of supporting the retrieval of all the products available in Investing.com since the listings do not provide the complete data.
So as to contextualize, investpy's static data is the one indexed in Investing.com listings, but as reported by some users, those listings are not complete as Investing.com provides a lot more information that the one that appears in the listings. So on, static data contains a lot of financial products data, whose usage and retrieval is implemented in the main investpy modules as it follows: stocks
, funds
, etfs
, indices
, currency_crosses
, cryptos
, commodities
, bonds
and certificates
.
Then investpy.search_quotes()
function was developed so as to provide access to all the Investing.com data. Also, this function allows the user to search for multiple product types at once, since the search engine is just based on the input text.
Now some investpy.search()
samples will be presented so as to explain its usage.
investpy.search_quotes(text, products=None, countries=None, n_results=None)
, as it can be seen, this function has 4 parameters, but just one of them is mandatory, the text
, which means that the function will just work if this parameter is specified. The remaining parameters named: products
, countries
and n_results
, filter the search results by product type, by country and set the number of search results to be returned, respectively.
A sample piece of code will be provided so as to present search engine usage:
import investpy
search_results = investpy.search_quotes(text='apple', products=['stocks', 'bonds'],
countries=['united states'], n_results=5)
Once the search results are obtained, their information can be easily checked printing the SearchObj
class instances as it follows:
for search_result in search_results:
print(search_result)
>>> {"id_": 6408, "name": "Apple Inc", "symbol": "AAPL", "country": "united states", "tag": "apple-computer-inc", "pair_type": "stocks", "exchange": "NASDAQ"}
>>> {"id_": 1088256, "name": "Apple Inc AAPL 3.2% 13-May-2025", "symbol": "037833BG4=", "country": "united states", "tag": "aapl-3.2-13-may-2025", "pair_type": "bonds", "exchange": "OTC Bonds"}
>>> {"id_": 1088257, "name": "Apple Inc AAPL 1.55% 04-Aug-2021", "symbol": "037833CC2=", "country": "united states", "tag": "aapl-1.55-04-aug-2021", "pair_type": "bonds", "exchange": "OTC Bonds"}
>>> {"id_": 1088258, "name": "Apple Inc AAPL 2.655% 06-May-2020", "symbol": "037833BE9=", "country": "united states", "tag": "aapl-2.655-06-may-2020", "pair_type": "bonds", "exchange": "OTC Bonds"}
>>> {"id_": 1088259, "name": "Apple Inc AAPL 3.35% 09-Feb-2027", "symbol": "037833CJ7=", "country": "united states", "tag": "aapl-3.35-09-feb-2027", "pair_type": "bonds", "exchange": "OTC Bonds"}
And, in order to retrieve historical data from any of them (for example from the AAPL
stock symbol, which corresponds to the first search_result found):
data = search_results[0].retrieve_historical_data(from_date='01/01/2019', to_date='01/01/2020')
print(data.head()) # or print(search_results[0].data.head())
Open High Low Close Volume
Date
2019-01-02 154.89 158.85 154.23 157.92 37039736
2019-01-03 143.98 145.72 142.00 142.19 91312192
2019-01-04 144.53 148.55 143.80 148.26 58607072
2019-01-07 148.70 148.83 145.90 147.93 54777764
2019-01-08 149.56 151.82 148.52 150.75 41025312
Even though just the data retrieval from search results is intended to be used, the SearchObj
object contains the following structure:
class SearchObj(object):
"""Class which contains each search result when searching data in Investing.
This class contains the search results of the Investing.com search made with the function
call `investpy.search_quotes(text)` which returns a :obj:`list` of instances of this class
with the formatted retrieved information. Additionally, data can either be retrieved or not
including both recent and historical data, which will be included in the `SearchObj.data`
attribute when calling either `SearchObj.retrieve_recent_data()` or
`SearchObj.retrieve_historical_data(from_date, to_date)`, respectively.
Attributes:
id_ (:obj:`int`): ID value used by Investing to retrieve data.
name (:obj:`str`): name of the retrieved financial product.
symbol (:obj:`str`): symbol of the retrieved financial product.
tag (:obj:`str`): tag (which is the Investing URL) of the retrieved financial product.
country (:obj:`str`): name of the country from where the retrieved financial product is.
pair_type (:obj:`str`): type of retrieved financial product (equities, fund, etf, etc.).
exchange (:obj:`str`): name of the stock exchange of the retrieved financial product.
data (:obj:`pandas.DataFrame`, optional):
recent or historical data to retrieve from the current financial product.
"""
def __init__(self, id_, name, symbol, tag, country, pair_type, exchange):
self.id_ = id_
self.name = name
self.symbol = symbol
self.country = country
self.tag = tag
self.pair_type = pair_type
self.exchange = exchange
This means that the attribute values can be accessed from every SearchObj
class instance as SearchObj.name
for example. Also note that the data attribute is not available by default, since it is created whenever any data retrieval function, either SearchObj.retrieve_recent_data()
or SearchObj.retrieve_historical_data(from_date, to_date)
, is called from the SearchObj class instance.