def search(txt: str, n: int): """Função que coleta as 'n' primeiras buscas referentes a txt = 'tesouro' ou txt = 'bvsp'. Args: txt (str): objeto a ser pesquisado: 'tesouro' ou 'bvsp'. n (int): número de resultados. Returns: iv..utils.search_obj.SearchObj: iterator de dicionários """ pdt = [] if txt == 'tesouro': pdt = ['bonds'] elif txt == 'bvsp': pdt = ['indices'] search_results = iv.search_quotes( text=txt, products=pdt, countries=['brazil'], n_results=n ) return search_results
def technical(symbol): ta = investpy.search_quotes(text=symbol, products=['stocks'], countries=['united states'], n_results=1) today = datetime.today().strftime('%d/%m/%Y') t_indicator_1d = ta.retrieve_technical_indicators(interval="daily") t_indicator_1d.to_csv("ta_1d.csv", index=False)
def test_investpy_search(): """ This function checks that investpy search function works properly. """ params = [{ 'text': 'bbva', 'products': None, 'countries': None, 'n_results': 5 }, { 'text': 'spain 3y', 'products': None, 'countries': None, 'n_results': 5 }, { 'text': 'ibex 35', 'products': None, 'countries': None, 'n_results': 5 }, { 'text': 'bnp daxplus', 'products': None, 'countries': None, 'n_results': None }, { 'text': 'apple', 'products': ['stocks'], 'countries': ['united states'], 'n_results': 1 }] for param in params: results = investpy.search_quotes(text=param['text'], products=param['products'], countries=param['countries'], n_results=param['n_results']) dates = [ { 'from_date': '01/01/2018', 'to_date': '01/01/2019' }, { 'from_date': '01/01/1990', 'to_date': '01/01/2019' }, ] for result in results: print(result) result.retrieve_recent_data() for date in dates: result.retrieve_historical_data(from_date=date['from_date'], to_date=date['to_date']) break
def get_data(name, prod, start_date, end_date): k1 = investpy.search_quotes(text=name, products=[prod], countries=['India'], n_results=2)[0].retrieve_historical_data( from_date=start_date, to_date=end_date) stockfile = name + '_' + prod + '.pkl' k1.to_pickle(stockfile) return k1 print('stock data downloaded')
def get_stock_data(name): k1 = investpy.search_quotes(text=name, products=['stocks'], countries=['India'], n_results=2)[0] k1 = investpy.get_stock_historical_data(stock=k1.symbol, country='India', from_date='01/01/2015', to_date='20/03/2021') return k1
def pesquisa(self, ativo, pais=None): if pais is not None and not isinstance(pais, str): return "ERR#001: país especificado inválido" if ativo is None: return "ERR#002: Ativo não pode ser vazio" try: dados = investpy.search_quotes(text=ativo, countries=[self.pais]) except ValueError: return "ERR#003: Ativo " + ativo + " não encontrado" values = {} for dado in dados: values = dado.__dict__ return values
def get_historical_values(self, from_date, to_date): """Function that gets the historical closing value of the asset at investing.com through the investpy. Args: from_date (str): start date of the series in the format %d/%m/%Y to_date (str): ending date of the series in the format %d/%m/%Y Returns: pd.Series: of the historical values from_date to_date """ engine = investpy.search_quotes(text=self.code, products=["funds"], countries=["brazil"]) closings = engine.retrieve_historical_data(from_date=from_date, to_date=to_date)["Close"] return closings
def update_stock_price_history(self, ticker, country): # It today is not a business day, instead set use the latest b.day today = dt.datetime.now() today_m7 = (today - dt.timedelta(days=7)).strftime('%Y-%m-%d') today = (pd.bdate_range( start=today_m7, end=today.strftime('%Y-%m-%d'))[-1].strftime('%d/%m/%Y')) # By default get data starting 5 years ago begin = (dt.datetime.strptime(today, '%d/%m/%Y') - dt.timedelta(days=365 * 5)).strftime('%d/%m/%Y') path = 'price_history\\{}.csv'.format(ticker) # If some data was already gotten, start from the latest date if os.path.isfile(path): data0 = pd.read_csv(path, index_col=0, header=0, sep=';') data0.index = pd.to_datetime(data0.index) begin = (data0.index[-1] + dt.timedelta(days=1)).strftime('%d/%m/%Y') else: data0 = pd.DataFrame(columns=['Price'], index=pd.Series([], name='Date')) if (dt.datetime.strptime(begin, '%d/%m/%Y') < dt.datetime.strptime( today, '%d/%m/%Y')): print('Updating history for {}...'.format(ticker)) # try: # data = investpy.get_stock_historical_data(stock=ticker, # country=country, # from_date=begin, # to_date=today) # except: try: search_result = investpy.search_quotes(text=ticker, countries=[country], n_results=1) data = search_result.retrieve_historical_data(from_date=begin, to_date=today) data.index = pd.to_datetime(data.index) data = data[['Close']] data = data.rename(columns={'Close': 'Price'}) data = pd.concat([data0, data], axis=0).sort_index() data.index = data.index.strftime('%Y-%m-%d') data = data.sort_index() data.to_csv(path, index=True, header=True, sep=';') except: pass
def get_hist_prices(pf, days_back=0): print('Retrieving prices...') if days_back == 0: from_date = pf['date'].min().strftime('%d/%m/%Y') elif days_back > len(_prices): print("More back than existing prices") from_date = (exclude_weekends() - datetime.timedelta(days=days_back)).strftime('%d/%m/%Y') else: print("prices already exist") return _prices.iloc[-days_back:] # from_date = _prices.iloc[-1].Date.strftime('%d/%m/%Y') to_date = exclude_weekends().date().strftime('%d/%m/%Y') #(exclude_weekends() - datetime.timedelta(days=1)) all_prices = [] pf = shrink_pf(pf) for i, row in pf.iterrows(): search_results = investpy.search_quotes(text=row['ticker'], products=[row['product']], countries=[row['country']]) # from_date = row['date'].strftime('%d/%m/%Y') for sr in search_results[:1]: hist_p = sr.retrieve_historical_data(from_date=from_date, to_date=to_date)['Close'].to_frame().rename({'Close':row['ticker']}, axis=1) all_prices.append(hist_p.interpolate(axis=1)) return pd.concat(all_prices, axis=1)
from scipy.special import gamma from arch import arch_model import pyflux as pf import sys import investpy from sklearn.ensemble import AdaBoostClassifier, RandomForestClassifier import feats from supervised.automl import AutoML #Main.include("try3.jl") #pos=int(input("enter stock pos: ")) #lb = int(input("enter no of returns: ")) #k=pd.read_pickle('/home/sahil/projdir/dailydata.pkl') #k1=k[k.Symbol==k.Symbol.unique()[pos]].iloc[-lb:] automl = AutoML(mode='Compete', total_time_limit=600) k1 = investpy.search_quotes(text='Kotak NIFTY ETF', products=['etfs'], countries=['India'], n_results=2)[0] k1 = investpy.get_etf_historical_data('KOTAKNIFTY', country='India', from_date='01/01/2010', to_date='20/03/2021') #k1=investpy.search_quotes(text='AARTIIND',products=['stocks'],countries=['India'],n_results=2)[0].retrieve_historical_data(from_date='01/01/2019',to_date='07/12/2020') k2 = investpy.get_index_historical_data(index='India VIX', country='India', from_date='01/01/2010', to_date='20/03/2021')
fund='bbva plan multiactivo moderado pp', country='spain', from_date='01/01/2010', to_date='01/01/2019') df_bbva_etf = investpy.get_etf_recent_data(etf='bbva accion dj eurostoxx 50', country='spain') etfs_ger = investpy.get_etfs_list(country='germany') investpy.get_stock_countries() #%% get finax tickers from_date = '01/01/1990' to_date = '21/03/2021' search = investpy.search_quotes('FRCK') df_frck = search[0].retrieve_historical_data(from_date=from_date, to_date=to_date) #df_frck.columns = 'frck_' + df_frck.columns search = investpy.search_quotes('XXSC') df_xxsc = search[0].retrieve_historical_data(from_date=from_date, to_date=to_date) # etf - xetra #df_xxsc.columns = 'xxsc_' + df_xxsc.columns search = investpy.search_quotes('XSX6') df_xsx6 = search[1].retrieve_historical_data(from_date=from_date, to_date=to_date) #df_xsx6.columns = 'xsx6_' + df_xsx6.columns search = investpy.search_quotes('XHYA')
#from julia import Man from scipy.integrate import quad, dblquad #from pybayes.pdfs import CPdf from scipy.special import gamma from arch import arch_model import pyflux as pf import sys import investpy #Main.include("try3.jl") #pos=int(input("enter stock pos: ")) #lb = int(input("enter no of returns: ")) #k=pd.read_pickle('/home/sahil/projdir/dailydata.pkl') #k1=k[k.Symbol==k.Symbol.unique()[pos]].iloc[-lb:] k1 = investpy.search_quotes(text='AARTIIND', products=['stocks'], countries=['India'], n_results=2)[0].retrieve_historical_data( from_date='01/01/2014', to_date='07/12/2020') #k1=investpy.search_quotes(text='AARTIIND',products=['stocks'],countries=['India'],n_results=2)[0].retrieve_historical_data(from_date='01/01/2019',to_date='07/12/2020') k1['retlog'] = np.log(k1.Close / k1.Open) * 100 k1['ret'] = (k1.Close / k1.Open - 1) * 100 filename = "/home/sahil/pythonbackup/todays_stock1.pkl" if os.path.exists(filename): os.remove(filename) k1.to_pickle(filename) print("saved") else: k1.to_pickle(filename) #k1.to_pickle("todays_stock1.pkl") #k1 = pd.read_pickle("todays_stock.pkl")
import investpy search_results = investpy.search_quotes(text='msci', products=['stocks'], countries=['united states'], n_results=10)
import investpy res = investpy.search_quotes(text='WSP', countries=['brazil']) res = res[0] if type(res) == list else res # pick the first one ret = investpy.get_stock_historical_data(stock=res.symbol, name=res.name, country=res.country, stock_currency='USD', id_=res.id_, from_date='01/01/2021', to_date='13/04/2021', as_json=True) print(ret)
def test_investpy_search(): """ This function checks that investpy search function works properly. """ params = [{ 'text': 'bbva', 'products': None, 'countries': None, 'n_results': 5 }, { 'text': 'spain 3y', 'products': None, 'countries': None, 'n_results': 5 }, { 'text': 'ibex 35', 'products': None, 'countries': None, 'n_results': 5 }, { 'text': 'bnp daxplus', 'products': None, 'countries': None, 'n_results': None }, { 'text': 'apple', 'products': ['stocks'], 'countries': ['united states'], 'n_results': 1 }, { 'text': 'apple', 'products': ['stocks'], 'countries': ['united states'], 'n_results': 5 }] for param in params: results = investpy.search_quotes(text=param['text'], products=param['products'], countries=param['countries'], n_results=param['n_results']) dates = [ { 'from_date': '01/01/2018', 'to_date': '01/01/2019' }, { 'from_date': '01/01/1990', 'to_date': '01/01/2019' }, ] if isinstance(results, list): result = results[0] else: result = results print(result) assert result.retrieve_recent_data() is not None for date in dates: assert result.retrieve_historical_data( from_date=date['from_date'], to_date=date['to_date']) is not None assert result.retrieve_currency() is not None assert result.retrieve_technical_indicators() is not None financial_products = [('stocks', 'apple'), ('etfs', 'apple'), ('commodities', 'apple'), ('currencies', 'usd'), ('funds', 'apple'), ('bonds', 'apple'), ('cryptos', 'bitcoin'), ('certificates', 'apple'), ('indices', 'apple'), ('fxfutures', 'usd')] for product_type, product_name in financial_products: search_result = investpy.search_quotes(text=product_name, products=[product_type], n_results=1) assert search_result.retrieve_information() is not None
import pdb from mle import * #import julia import scipy.optimize as opt from julia.api import Julia #j=julia.Julia(compiled_modules=False) #from julia import Man from scipy.integrate import quad,dblquad #from pybayes.pdfs import CPdf from scipy.special import gamma from arch import arch_model import pyflux as pf import sys import investpy import scipy k1=investpy.search_quotes(text='GAYAPROJ',products=['stocks'],countries=['India'],n_results=2)[0].retrieve_historical_data(from_date='01/01/2018',to_date='16/12/2018') if path.exists("/home/sahil/pythonbackup/todays_stock.pkl"): os.remove("/home/sahil/pythonbackup/todays_stock.pkl") else: k1.to_pickle("todays_stock.pkl") k1['dayret'] = ((k1.Close/k1.Open)-1)*100 k1['logdayret'] = np.log((k1.Close/k1.Open))*100 #k1['dayret'] = k1.Close.pct_change()*100 k1 = k1.dropna() print("long term hold ret") print(((k1.Close[-1]/k1.Open[0])-1)*100) print("intraday hold ret") print((((k1.Close/k1.Open)-1)*100).sum()) print("normal ret skew") print(scipy.stats.skew(k1.dayret))
import investpy res = investpy.search_quotes(text='eimi', countries=['united kingdom']) res = res[0] # pick the first one ret = investpy.get_stock_historical_data(stock=res.symbol, name=res.name, country=res.country, stock_currency='USD', id_=res.id_, from_date='01/01/2021', to_date='13/04/2021', as_json=True) for row_index, row in ret.iteritems(): print(row)