def get_quotes_investing_etf(names, countries, colnames='', begin='1990-01-01', end='2025-01-01', merge='inner', growth_index=False): begin = pd.to_datetime(begin).strftime('%d/%m/%Y') end = pd.to_datetime(end).strftime('%d/%m/%Y') iteration = 0 for i in range(len(names)): iteration += 1 etf = investpy.get_etf_historical_data(etf=names[i], from_date=begin, to_date=end, country=countries[i])[['Close']] if iteration == 1: etfs = etf.copy() else: etfs = merge_time_series(etfs, etf, how=merge) if colnames: etfs.columns = colnames else: etfs.columns = names if growth_index: etfs = compute_growth_index(etfs) return etfs
def main(): parser = argparse.ArgumentParser(description='scrap yahoo earning') parser.add_argument( '-output_dir', type=str, default='../stock_data_local/raw_history_investing_etf/', help='output directory') parser.add_argument('-skip', type=int, help='skip tickers') args = parser.parse_args() country = 'united states' etfs_dict = investpy.get_etfs_dict(country) today = datetime.datetime.now().strftime('%d/%m/%Y') for count, etf_row in enumerate(etfs_dict): if args.skip is not None: if count < args.skip: continue ticker = etf_row['symbol'] print('downloading...', ticker, '-', count, '/', len(etfs_dict)) try: df_etf = investpy.get_etf_historical_data(etf_row['name'], country, '1/1/1990', today) df_etf.to_csv(args.output_dir + ticker + '.csv') except Exception as e: print('FAIL -', e) time.sleep(scrap_delay) return 0
def load_sector_etfs(etfs: List, start_date: dt.date, end_date: dt.date, frequency: str = 'Daily', save_file: str = 'sectors.pkl') -> pd.DataFrame: """ Load data for a list of etfs """ if start_date > end_date: raise ValueError('start date cannot be later than end date') if frequency not in ['Daily', 'Weekly', 'Monthly']: raise ValueError('invalid frequency') for etf in etfs: assert etf in SECTOR_ETFS.keys(), 'etf not found: {}'.format(etf) start_date_str = start_date.strftime('%d/%m/%Y') end_date_str = end_date.strftime('%d/%m/%Y') country = 'united states' output_df = pd.DataFrame(columns=etfs) for etf in etfs: etf_string = SECTOR_ETFS[etf] d = investpy.get_etf_historical_data(etf_string, country, start_date_str, end_date_str, interval=frequency) output_df[etf] = (d['Close'] - d['Open']) / d['Open'] return output_df
def ApiGetAllByIsin(self, isin, tipologia_strumento, periodicita, start): #restituisce il nome di corrispondenza all'isin inserito country_iniziali = isin[:2] country = GLOBE.country_isin.get(country_iniziali) endus = datetime.now() endeu = endus.strftime("%d/%m/%Y") # print(inv.stocks.get_stock_countries()) if tipologia_strumento == GLOBE.mappa_strumenti.get("stock"): stock = inv.stocks.get_stocks(country = country) info_gen = stock.loc[stock["isin"] == isin] df = inv.get_stock_historical_data(stock = info_gen["symbol"].values[0], country = country, from_date = start, to_date = endeu, as_json=False, order='ascending', interval = GLOBE.mappa_periodicita[periodicita]) elif tipologia_strumento == GLOBE.mappa_strumenti.get("etf"): etf = inv.etfs.get_etfs(country = country) info_gen = etf.loc[etf["isin"] == isin] df = inv.get_etf_historical_data(etf = info_gen["name"].values[0], country = country, from_date = start, to_date = endeu, as_json=False, order='ascending', interval = GLOBE.mappa_periodicita[periodicita]) elif tipologia_strumento == GLOBE.mappa_strumenti.get("fund"): fund = inv.funds.get_funds(country = country) info_gen = fund.loc[fund["isin"] == isin] df = inv.get_fund_historical_data(fund = info_gen["name"].values[0], country = country, from_date = start, to_date = endeu, as_json=False, order='ascending', interval = GLOBE.mappa_periodicita[periodicita]) all_info = { #dict con tutte le informazioni "datafetch" : df, "info_gen" : info_gen, "tipo_strumento" : tipologia_strumento } return all_info
def get_asset_data(asset_list: list, from_date: str, to_date: str, asset_type: str, asset_df: pd.DataFrame) -> Tuple[list, pd.DataFrame]: # commodity, bond, currency # etfs and funds need country if asset_type == "Bonds": func = lambda a, s, e: investpy.get_bond_historical_data(a, s, e) elif asset_type == "Currencies": func = lambda a, s, e: investpy.get_currency_cross_historical_data( a, s, e) elif asset_type == "ETFs": func = lambda a, s, e, c: investpy.get_etf_historical_data(a, c, s, e) elif asset_type == "Funds": func = lambda a, s, e, c: investpy.get_fund_historical_data(a, c, s, e) elif asset_type == "Commodities": func = lambda a, s, e: investpy.get_commodity_historical_data(a, s, e) elif asset_type == "Indices": func = lambda a, s, e, c: investpy.get_index_historical_data( a, c, s, e) elif asset_type == "Crypto": func = lambda a, s, e: investpy.get_crypto_historical_data(a, s, e) df_list = [] for asset in asset_list: if asset_type != "ETFs" and asset_type != "Funds" and asset_type != "Indices": df = func(asset, from_date, to_date) df_list.append(df) else: country = get_attribute_investing(asset_df, asset, 'country') df = func(asset, from_date, to_date, country) df_list.append(df) close_list = [df.Close for df in df_list] # print(close_list) close_df = pd.concat(close_list, axis=1) close_df.columns = asset_list return df_list, close_df
def hist_data(name, country): df = investpy.get_etf_historical_data(etf=name, country=country, from_date=oneyr, to_date=tdy)['Close'] df = pd.DataFrame(df) df.columns = [name] return df
def get_data_by_isin(isin: str, dates: Tuple[datetime.date], is_etf: bool) -> Tuple[Optional[np.ndarray], str]: """Retrieves stock/ETF prices in EUR by ISIN for the given dates. Cached to make sure this is only queried once for a given currency & date-range.""" from_date = dates[0].strftime("%d/%m/%Y") to_date = (dates[-1] + datetime.timedelta(days=7)).strftime("%d/%m/%Y") # Retrieves stock/etf information based on the ISIN try: if is_etf: data = investpy.search_etfs(by="isin", value=isin) else: data = investpy.search_stocks(by="isin", value=isin) except RuntimeError: print( f"[DGPC] Warning, could not retrieve {'ETF' if is_etf else 'stock'} data for ISIN {isin}." ) return None, "" # When a stock/ETF is listed in multiple countries, take one of the preferred countries if found for country in PREFERRED_COUNTRIES: local_data = data[data["country"] == country] if local_data.shape[0] > 0: break else: # Taking the first country from the results if none of the preferred countries is found country = data["country"][0] local_data = data # Retrieves the actual historical prices for the stock/etf currency = list(local_data["currency"])[0] symbol = list(local_data["symbol"])[0] if is_etf: name = list(local_data["name"])[0] history = investpy.get_etf_historical_data(name, country=country, from_date=from_date, to_date=to_date) else: history = investpy.get_stock_historical_data(symbol, country=country, from_date=from_date, to_date=to_date) history = history.reset_index() values = densify_history(history, dates) # Convert the results to euro if currency != "EUR": currency_modifier = to_euro_modifier(currency, tuple(dates)) values *= currency_modifier return values, symbol
def get_investing_price(self): try: instrument_nature = self.security.instrument_type asset_country = self.security.country investing_ticker = self.security.investing_ticker start_date = '01/01/{}'.format(self.start_year) end_date = '31/12/{}'.format(self.end_year) if instrument_nature == "STOCK": hist_prices = invest.get_stock_historical_data( stock=investing_ticker, country=asset_country, from_date=start_date, to_date=end_date).reset_index(drop=False) elif instrument_nature == "ETF": hist_prices = invest.get_etf_historical_data( etf=investing_ticker, country=asset_country, from_date=start_date, to_date=end_date).reset_index(drop=False) elif instrument_nature == "index": hist_prices = invest.get_index_historical_data( index=investing_ticker, country=asset_country, from_date=start_date, to_date=end_date).reset_index(drop=False) elif instrument_nature == "Curr": hist_prices = invest.currency_crosses.get_currency_cross_historical_data( currency_cross=investing_ticker, from_date=start_date, to_date=end_date).reset_index(drop=False) elif instrument_nature == 'BOND': hist_prices = invest.bonds.get_bond_historical_data( bond=investing_ticker, from_date=start_date, to_date=end_date).reset_index(drop=False) elif instrument_nature == 'commodity': hist_prices = invest.commodity.get_bond_historical_data( commodity=investing_ticker, from_date=start_date, to_date=end_date).reset_index(drop=False) self.investing_prices = hist_prices except Exception as e: #print(str(e)) #logger.error("Error in get_investing_price: {}".format(str(e))) hist_prices = pd.DataFrame() self.investing_prices = hist_prices
def _download_data(self, asset_type, symbol, name, country, from_date, max_date): if asset_type == "BOND": # name == symbol df = ip.get_bond_historical_data(symbol, from_date=from_date, to_date=max_date) elif asset_type == "CERT": df = ip.get_certificate_historical_data(name, country=country, from_date=from_date, to_date=max_date) elif asset_type == "CRYPTO": df = ip.get_crypto_historical_data(name, from_date=from_date, to_date=max_date) elif asset_type == "COMM": df = ip.get_commodity_historical_data(symbol, from_date=from_date, to_date=max_date) elif asset_type == "ETF": df = ip.get_etf_historical_data(name, country=country, from_date=from_date, to_date=max_date) elif asset_type == "FUND": df = ip.get_fund_historical_data(name, country=country, from_date=from_date, to_date=max_date) elif asset_type == "FX": df = ip.get_currency_cross_historical_data(symbol, from_date=from_date, to_date=max_date) elif asset_type == "INDEX": df = ip.get_index_historical_data(name, country=country, from_date=from_date, to_date=max_date) elif asset_type == "STOCK": df = ip.get_stock_historical_data(symbol, country=country, from_date=from_date, to_date=max_date) return df
def ApiGetAllByIsinPortafoglio(self, isin, tipologia_strumento): years_obs = timedelta(days=365.24) * 5 endus = datetime.now() endeu = endus.strftime("%d/%m/%Y") #conversioni date US a EU startus = endus - years_obs starteu = startus.strftime("%d/%m/%Y") country_iniziali = isin[:2] #convertitore ISIN to country country = GLOBE.country_isin.get(country_iniziali) df = 0 info_gen = 0 info_tech = 0 if tipologia_strumento == GLOBE.mappa_strumenti.get("stock"): stock = inv.stocks.get_stocks(country = country) info_gen = stock.loc[stock["isin"] == isin] info_tech = inv.stocks.get_stock_information(info_gen["symbol"].values[0], country, as_json=False) df = inv.get_stock_historical_data(stock = info_gen["symbol"].values[0], country = country, from_date = starteu, to_date = endeu, as_json=False, order='ascending', interval = GLOBE.mappa_periodicita.get("Monthly")) elif tipologia_strumento == GLOBE.mappa_strumenti.get("etf"): etf = inv.etfs.get_etfs(country = country) info_gen = etf.loc[etf["isin"] == isin] info_tech = inv.etfs.get_etf_information(info_gen["name"].values[0], country, as_json=False) df = inv.get_etf_historical_data(etf = info_gen["name"].values[0], country = country, from_date = starteu, to_date = endeu, as_json=False, order='ascending', interval = GLOBE.mappa_periodicita.get("Monthly")) elif tipologia_strumento == GLOBE.mappa_strumenti.get("fund"): fund = inv.funds.get_funds(country = country) info_gen = fund.loc[fund["isin"] == isin] info_tech = inv.funds.get_fund_information(info_gen["name"].values[0], country, as_json=False) df = inv.get_fund_historical_data(fund = info_gen["name"].values[0], country = country, from_date = starteu, to_date = endeu, as_json=False, order='ascending', interval = GLOBE.mappa_periodicita.get("Monthly")) all_info_portafoglio = { #dict con tutte le informazioni "datafetch" : df, "info_gen" : info_gen, "info_tech" : info_tech, "tipo_strumento" : tipologia_strumento } return all_info_portafoglio
def investing_api(call_type, ticker, from_date, to_date, country='united states'): """ call_type: etf, stock, fund, index ticker: str. ticker name from_date: dd/mm/yyyy """ if call_type == 'etf': # search name from ticker and return etfs = investpy.etfs.get_etfs(country=country) etf_name = etfs.loc[(etfs.symbol == ticker), 'name'].tolist()[0] data = investpy.get_etf_historical_data(etf=etf_name, country=country, from_date=from_date, to_date=to_date).reset_index() if call_type == 'stock': data = investpy.stocks.get_stock_historical_data(stock=ticker, country=country, from_date=from_date, to_date=to_date).reset_index() return data
def import_etfs(successfulPulls): # imports ETFs in the US search_results = investpy.search_etfs(by='country', value='united states') list_of_etf_names = search_results["name"] firstIndex = datetime.datetime.strptime(_configKeys.STARTPULL, '%d/%m/%Y') lastIndex = datetime.datetime.strptime(_configKeys.ENDPULL, '%d/%m/%Y') for name in list_of_etf_names[:2500]: try: # Have an if statement in place in case if we don't want to pull every etf because there are a lot of stocks # Program takes a long time to run if we have to webscrape every etf each time we run etfData = [] etfData = investpy.get_etf_historical_data( etf=name, country='united states', from_date=_configKeys.STARTPULL, to_date=_configKeys.ENDPULL) newIndex = [] for index in etfData.index: newIndex.append( datetime.datetime.strptime( datetime.datetime.strftime((index + timedelta(days=1)), '%Y-%m-%d'), '%Y-%m-%d')) etfData['Date'] = newIndex etfData.set_index('Date', inplace=True) # If there's something that's been loaded into stockData, then the length is no longer 0 # if the differences is under 2~3 days, then it is ok to take this data since there is still enough data in the week to be usable # this timedelta fixes the problem of trying to pull during a long weekend name = str(name) + "Etf" if etfData.empty == False and etfData.index[ 0] - firstIndex <= timedelta( days=2) and lastIndex - etfData.index[-1] <= timedelta( days=3): successfulPulls["Symbol"].append(name.replace("/", "")) successfulPulls["Type"].append("ETF") etfData.to_csv( os.path.join(Path(_configKeys.DATA_FOLDER), name.replace("/", "") + '.csv')) except: print("Something went wrong when importing: " + name)
def download_etfs_historical(etfs: pd.DataFrame, from_date: str) -> Dict: '''Download all etf historical data''' today = pd.to_datetime('today').strftime("%d/%m/%Y") parts = {} warnings.filterwarnings('ignore') for i, row in etfs.iterrows(): file_name = f"etf_{row.symbol_ft}_{row['isin']}" sleep(1) try: parts[file_name] = investpy.get_etf_historical_data( row['name'], row.country, stock_exchange=row.stock_exchange, from_date=from_date, to_date=today, as_json=False, order='ascending') log.debug(f"Download complete (ETF historical): {file_name}") except: log.warning(f"Download FAILED (ETF historical): {file_name}") sleep(30) warnings.filterwarnings('default') log.info(f"{len(parts)} downloaded") return parts
def test_etf_errors(): params = [ { 'country': None, 'columns': None, 'as_json': False }, { 'country': 'spain', 'columns': None, 'as_json': 'error' }, { 'country': 'spain', 'columns': 0, 'as_json': True }, { 'country': 'spain', 'columns': ['error'], 'as_json': False }, ] for param in params: try: investpy.get_etf_dict(country=param['country'], columns=param['columns'], as_json=param['as_json']) except: pass params = [ { 'etf': 'bbva accion dj eurostoxx 50', 'as_json': 'error', 'order': 'ascending' }, { 'etf': 'bbva accion dj eurostoxx 50', 'as_json': True, 'order': 'error' }, { 'etf': 'error', 'as_json': True, 'order': 'ascending' }, { 'etf': ['error'], 'as_json': True, 'order': 'ascending' }, ] for param in params: try: investpy.get_etf_recent_data(etf=param['etf'], as_json=param['as_json'], order=param['order']) except: pass params = [ { 'etf': 'bbva accion dj eurostoxx 50', 'start': '01/01/2019', 'end': '01/01/2019', 'as_json': 'error', 'order': 'ascending' }, { 'etf': 'bbva accion dj eurostoxx 50', 'start': '01/01/2019', 'end': '01/01/2019', 'as_json': False, 'order': 'error' }, { 'etf': 'bbva accion dj eurostoxx 50', 'start': 'error', 'end': '01/01/2019', 'as_json': False, 'order': 'ascending' }, { 'etf': 'bbva accion dj eurostoxx 50', 'start': '01/01/2019', 'end': 'error', 'as_json': False, 'order': 'ascending' }, { 'etf': 'error', 'start': '01/01/2019', 'end': '01/01/2019', 'as_json': False, 'order': 'ascending' }, { 'etf': ['error'], 'start': '01/01/2019', 'end': '01/01/2019', 'as_json': False, 'order': 'ascending' }, { 'etf': 'bbva accion dj eurostoxx 50', 'start': '01/01/1998', 'end': '01/01/2019', 'as_json': False, 'order': 'ascending' }, { 'etf': 'bbva accion dj eurostoxx 50', 'start': '01/01/1900', 'end': '01/01/2019', 'as_json': False, 'order': 'ascending' }, { 'etf': 'bbva accion dj eurostoxx 50', 'start': '01/01/2019', 'end': '01/01/1998', 'as_json': False, 'order': 'ascending' }, { 'etf': 'bbva accion dj eurostoxx 50', 'start': '01/01/1900', 'end': '01/01/1950', 'as_json': False, 'order': 'ascending' }, ] for param in params: try: investpy.get_etf_historical_data(etf=param['etf'], start=param['start'], end=param['end'], as_json=param['as_json'], order=param['order']) except: pass
def test_investpy_etfs(): """ This function checks that etf data retrieval functions listed in investpy work properly. """ params = [ { 'country': 'spain', }, { 'country': None, }, ] for param in params: investpy.get_etfs(country=param['country']) investpy.get_etfs_list(country=param['country']) params = [ { 'country': None, 'columns': ['name'], 'as_json': True }, { 'country': None, 'columns': ['name'], 'as_json': False }, { 'country': 'spain', 'columns': ['name'], 'as_json': True }, { 'country': 'spain', 'columns': ['name'], 'as_json': False }, { 'country': 'spain', 'columns': None, 'as_json': False }, ] for param in params: investpy.get_etfs_dict(country=param['country'], columns=param['columns'], as_json=param['as_json']) investpy.get_etf_countries() params = [ { 'as_json': True, 'order': 'ascending', }, { 'as_json': False, 'order': 'ascending', }, { 'as_json': True, 'order': 'descending', }, { 'as_json': False, 'order': 'descending', }, ] for param in params: investpy.get_etf_recent_data(etf='bbva accion dj eurostoxx 50', country='spain', as_json=param['as_json'], order=param['order'], interval='Daily') investpy.get_etf_historical_data(etf='bbva accion dj eurostoxx 50', country='spain', from_date='01/01/2010', to_date='01/01/2019', as_json=param['as_json'], order=param['order'], interval='Daily') params = [ { 'etf': 'bbva accion dj eurostoxx 50', 'country': 'spain', 'as_json': False }, { 'etf': 'bbva accion dj eurostoxx 50', 'country': 'spain', 'as_json': True } ] for param in params: investpy.get_etf_information(etf=param['etf'], country=param['country'], as_json=param['as_json']) params = [ { 'country': 'united states', 'as_json': True, 'n_results': 2 }, { 'country': 'united kingdom', 'as_json': False, 'n_results': 2 }, ] for param in params: investpy.get_etfs_overview(country=param['country'], as_json=param['as_json'], n_results=param['n_results']) investpy.search_etfs(by='name', value='bbva')
def test_investpy_etfs(): """ This function checks that etf data retrieval functions listed in investpy work properly. """ params = [ { 'country': 'spain', }, { 'country': None, }, ] for param in params: investpy.get_etfs(country=param['country']) investpy.get_etfs_list(country=param['country']) params = [ { 'country': None, 'columns': ['id', 'name'], 'as_json': True }, { 'country': None, 'columns': ['id', 'name'], 'as_json': False }, { 'country': 'spain', 'columns': ['id', 'name'], 'as_json': True }, { 'country': 'spain', 'columns': ['id', 'name'], 'as_json': False }, { 'country': 'spain', 'columns': None, 'as_json': False }, ] for param in params: investpy.get_etfs_dict(country=param['country'], columns=param['columns'], as_json=param['as_json']) investpy.get_etf_countries() params = [ { 'as_json': True, 'order': 'ascending', 'debug': False }, { 'as_json': False, 'order': 'ascending', 'debug': True }, { 'as_json': True, 'order': 'descending', 'debug': False }, { 'as_json': False, 'order': 'descending', 'debug': False }, ] for param in params: investpy.get_etf_recent_data(etf='bbva accion dj eurostoxx 50', country='spain', as_json=param['as_json'], order=param['order'], debug=param['debug']) investpy.get_etf_historical_data(etf='bbva accion dj eurostoxx 50', country='spain', from_date='01/01/2010', to_date='01/01/2019', as_json=param['as_json'], order=param['order'], debug=param['debug']) params = [ { 'country': 'france', 'as_json': True, }, { 'country': 'france', 'as_json': False, }, ] for param in params: investpy.get_etfs_overview(country=param['country'], as_json=param['as_json']) investpy.search_etfs(by='name', value='bbva')
def my_etf_historical_data(etf, country, from_date, to_date): return investpy.get_etf_historical_data(etf=etf, country=country, from_date=from_date, to_date=to_date)
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') def slret(o, h, l, c, sl): hp = ((h - o) / o) * 100 if -hp < sl: return sl else:
df_cryptos = investpy.get_cryptos() ######################################################################################################################## # investpy 패키지를 사용하여 삼성전자 자료 받기 investpy_005930 = investpy.get_stock_historical_data(stock="005930", country="south korea", from_date="30/01/1900", to_date=DD_END_DATE) investpy_005930.to_pickle('./Market_Watch_Data/investpy_005930.pkl') ######################################################################################################################## # investpy 패키지를 사용하여 ETF 자료 받기 (069500) investpy_069500 = investpy.get_etf_historical_data( etf="Samsung KODEX KOSPI 200 Securities", country="south korea", from_date="30/01/1900", to_date=DD_END_DATE) investpy_069500.to_pickle('./Market_Watch_Data/investpy_069500.pkl') # investpy 패키지를 사용하여 미국 국채 ETF 자료 받기 (IEF) investpy_IEF = investpy.get_etf_historical_data( etf="iShares 7-10 Year Treasury Bond", country="United States", from_date="30/01/1900", to_date=DD_END_DATE) investpy_IEF.to_pickle('./Market_Watch_Data/investpy_IEF.pkl') # investpy 패키지를 사용하여 한국 국채 ETF 자료 받기 (Kiwoom KOSEF 10Y Treasury Bond) investpy_148070 = investpy.get_etf_historical_data( etf="Kiwoom KOSEF 10Y Treasury Bond",
def test_investpy(): """ This function checks that main functions of investpy work properly. """ print(investpy.__author__, investpy.__version__) for value in ['spain', None]: investpy.get_equities(country=value) investpy.get_equities_list(country=value) params = [ { 'country': None, 'columns': ['id', 'name'], 'as_json': True }, { 'country': 'spain', 'columns': ['id', 'name'], 'as_json': True }, { 'country': None, 'columns': ['id', 'name'], 'as_json': False }, { 'country': 'spain', 'columns': None, 'as_json': False }, { 'country': 'spain', 'columns': ['id', 'name'], 'as_json': False }, ] for param in params: investpy.get_equities_dict(country=param['country'], columns=param['columns'], as_json=param['as_json']) investpy.get_equity_countries() params = [ { 'as_json': False, 'order': 'ascending', 'debug': True }, { 'as_json': True, 'order': 'descending', 'debug': False }, { 'as_json': True, 'order': 'ascending', 'debug': False }, { 'as_json': False, 'order': 'descending', 'debug': False }, ] for param in params: investpy.get_recent_data(equity='enagás', country='spain', as_json=param['as_json'], order=param['order'], debug=param['debug']) investpy.get_historical_data(equity='enagás', country='spain', from_date='01/01/1990', to_date='01/01/2019', as_json=param['as_json'], order=param['order'], debug=param['debug']) for value in ['spanish', 'english']: investpy.get_equity_company_profile(equity='enagás', country='spain', language=value) retrieve_equities(test_mode=True) retrieve_equity_countries(test_mode=True) params = [ { 'country': 'spain', }, { 'country': None, }, ] for param in params: investpy.get_funds(country=param['country']) investpy.get_funds_list(country=param['country']) params = [ { 'country': None, 'columns': ['id', 'name'], 'as_json': True }, { 'country': 'spain', 'columns': ['id', 'name'], 'as_json': True }, { 'country': None, 'columns': ['id', 'name'], 'as_json': False }, { 'country': 'spain', 'columns': None, 'as_json': False }, { 'country': 'spain', 'columns': ['id', 'name'], 'as_json': False }, ] for param in params: investpy.get_funds_dict(country=param['country'], columns=param['columns'], as_json=param['as_json']) params = [ { 'fund': 'bbva multiactivo conservador pp', 'country': 'spain', 'as_json': True, }, { 'fund': 'bbva multiactivo conservador pp', 'country': 'spain', 'as_json': False, }, ] for param in params: investpy.get_fund_information(fund=param['fund'], country=param['country'], as_json=param['as_json']) params = [ { 'as_json': False, 'order': 'ascending', 'debug': True }, { 'as_json': True, 'order': 'descending', 'debug': False }, { 'as_json': True, 'order': 'ascending', 'debug': False }, { 'as_json': False, 'order': 'descending', 'debug': False }, ] for param in params: investpy.get_fund_recent_data(fund='bbva multiactivo conservador pp', country='spain', as_json=param['as_json'], order=param['order'], debug=param['debug']) investpy.get_fund_historical_data( fund='bbva multiactivo conservador pp', country='spain', from_date='01/01/2010', to_date='01/01/2019', as_json=param['as_json'], order=param['order'], debug=param['debug']) investpy.get_fund_countries() investpy.get_funds() retrieve_funds(test_mode=True) retrieve_fund_countries(test_mode=True) investpy.get_etf_countries() for value in ['spain', None]: investpy.get_etfs(country=value) investpy.get_etfs_list(country=value) params = [ { 'country': None, 'columns': ['id', 'name'], 'as_json': True }, { 'country': 'spain', 'columns': ['id', 'name'], 'as_json': True }, { 'country': None, 'columns': ['id', 'name'], 'as_json': False }, { 'country': 'spain', 'columns': None, 'as_json': False }, { 'country': 'spain', 'columns': ['id', 'name'], 'as_json': False }, ] for param in params: investpy.get_etfs_dict(country=param['country'], columns=param['columns'], as_json=param['as_json']) params = [ { 'as_json': False, 'order': 'ascending', 'debug': True }, { 'as_json': True, 'order': 'descending', 'debug': False }, { 'as_json': True, 'order': 'ascending', 'debug': False }, { 'as_json': False, 'order': 'descending', 'debug': False }, ] for param in params: investpy.get_etf_recent_data(etf='bbva accion dj eurostoxx 50', country='spain', as_json=param['as_json'], order=param['order'], debug=param['debug']) investpy.get_etf_historical_data(etf='bbva accion dj eurostoxx 50', country='spain', from_date='01/01/2010', to_date='01/01/2019', as_json=param['as_json'], order=param['order'], debug=param['debug']) params = [ { 'country': 'france', 'as_json': True, }, { 'country': 'usa', 'as_json': False, }, ] for param in params: investpy.get_etfs_overview(country=param['country'], as_json=param['as_json']) retrieve_etfs(test_mode=True)
def test_investpy(): """ This function checks that main functions of investpy work properly. """ investpy.get_equities() investpy.get_equities_list() params = [ {'as_json': False, 'order': 'ascending'}, {'as_json': True, 'order': 'descending'}, {'as_json': True, 'order': 'ascending'}, {'as_json': False, 'order': 'descending'}, ] for param in params: investpy.get_recent_data(equity='enagás', as_json=param['as_json'], order=param['order']) investpy.get_historical_data(equity='enagás', start='01/01/1990', end='01/01/2019', as_json=param['as_json'], order=param['order']) for value in ['spanish', 'english']: investpy.get_equity_company_profile(equity='enagás', language=value) get_equity_names() investpy.get_funds() investpy.get_funds_list() for value in [True, False]: investpy.get_funds_dict(columns=['id', 'name'], as_json=value) investpy.get_fund_information(fund='bbva multiactivo conservador pp', as_json=value) params = [ {'as_json': False, 'order': 'ascending'}, {'as_json': True, 'order': 'descending'}, {'as_json': True, 'order': 'ascending'}, {'as_json': False, 'order': 'descending'}, ] for param in params: investpy.get_fund_recent_data(fund='bbva multiactivo conservador pp', as_json=param['as_json'], order=param['order']) investpy.get_fund_historical_data(fund='bbva multiactivo conservador pp', start='01/01/2010', end='01/01/2019', as_json=param['as_json'], order=param['order']) get_fund_names() # investpy.get_etfs() # # investpy.get_etfs_list() # # # # for value in [True, False]: # # investpy.get_etfs_dict(columns=['id', 'name'], as_json=value) params = [ {'as_json': False, 'order': 'ascending'}, {'as_json': True, 'order': 'descending'}, {'as_json': True, 'order': 'ascending'}, {'as_json': False, 'order': 'descending'}, ] for param in params: investpy.get_etf_recent_data(etf='bbva accion dj eurostoxx 50', as_json=param['as_json'], order=param['order']) investpy.get_etf_historical_data(etf='bbva accion dj eurostoxx 50', start='01/01/2010', end='01/01/2019', as_json=param['as_json'], order=param['order']) get_etfs()
from_date='01/01/2012', to_date='12/01/2030') lite_df = lite_df.drop(['Open', 'High', 'Low', 'Currency'], axis=1) lite_df.columns = ['lite', 'Volume'] lite_df['lite_mean'] = lite_df['lite'].rolling(21).mean() lite_df['lite_std'] = lite_df['lite'].rolling(21).std() lite_df['lite_low'] = lite_df['lite_mean'] - lite_df['lite_std'] * 2 lite_df['lite_up'] = lite_df['lite_mean'] + lite_df['lite_std'] * 2 print(lite_df.tail()) con_df = investpy.get_etf_historical_data( etf='Consumer Discretionary Select Sector SPDR', country='United States', from_date='01/01/2012', to_date='12/01/2030') con_df = con_df.drop(['Open', 'High', 'Low', 'Currency', 'Exchange'], axis=1) # print(con_df.tail()) fin_df = investpy.get_etf_historical_data(etf='Financial Select Sector SPDR', country='United States', from_date='01/01/2012', to_date='12/01/2030') fin_df = fin_df.drop(['High', 'Low', 'Currency', 'Exchange'], axis=1) # print(fin_df.tail()) health_df = investpy.get_etf_historical_data( etf='Health Care Select Sector SPDR', country='United States',
def test_etfs_errors(): """ This function raises errors on etf retrieval functions """ try: retrieve_etfs(test_mode=None) except: pass params = [ { 'country': ['error'] }, { 'country': 'error' }, ] for param in params: try: investpy.get_etfs(country=param['country']) except: pass try: investpy.get_etfs_list(country=param['country']) except: pass params = [ { 'country': ['error'], 'columns': None, 'as_json': False }, { 'country': 'spain', 'columns': None, 'as_json': 'error' }, { 'country': 'spain', 'columns': 0, 'as_json': True }, { 'country': 'spain', 'columns': ['error'], 'as_json': False }, ] for param in params: try: investpy.get_etfs_dict(country=param['country'], columns=param['columns'], as_json=param['as_json']) except: pass params = [ { 'etf': None, 'country': 'spain', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'etf': ['error'], 'country': 'spain', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'etf': 'bbva accion dj eurostoxx 50', 'country': None, 'as_json': False, 'order': 'ascending', 'debug': True }, { 'etf': 'bbva accion dj eurostoxx 50', 'country': 'error', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'etf': 'bbva accion dj eurostoxx 50', 'country': 'netherlands', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'etf': 'bbva accion dj eurostoxx 50', 'country': ['error'], 'as_json': False, 'order': 'ascending', 'debug': True }, { 'etf': 'bbva accion dj eurostoxx 50', 'country': 'spain', 'as_json': 'error', 'order': 'ascending', 'debug': True }, { 'etf': 'bbva accion dj eurostoxx 50', 'country': 'spain', 'as_json': True, 'order': 'error', 'debug': True }, { 'etf': 'error', 'country': 'spain', 'as_json': True, 'order': 'ascending', 'debug': True }, { 'etf': ['error'], 'country': 'spain', 'as_json': True, 'order': 'ascending', 'debug': True }, { 'etf': 'bbva accion dj eurostoxx 50', 'country': 'spain', 'as_json': True, 'order': 'ascending', 'debug': 'error' }, ] for param in params: try: investpy.get_etf_recent_data(etf=param['etf'], country=param['country'], as_json=param['as_json'], order=param['order'], debug=param['debug']) except: pass params = [ { 'etf': None, 'country': 'spain', 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'etf': 'bbva accion dj eurostoxx 50', 'country': 'error', 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'etf': 'bbva accion dj eurostoxx 50', 'country': 'netherlands', 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'etf': 'bbva accion dj eurostoxx 50', 'country': None, 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'etf': 'bbva accion dj eurostoxx 50', 'country': ['error'], 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'etf': 'bbva accion dj eurostoxx 50', 'country': 'spain', 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': 'error', 'order': 'ascending', 'debug': True }, { 'etf': 'bbva accion dj eurostoxx 50', 'country': 'spain', 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'error', 'debug': True }, { 'etf': 'bbva accion dj eurostoxx 50', 'country': 'spain', 'from_date': 'error', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'etf': 'bbva accion dj eurostoxx 50', 'country': 'spain', 'from_date': '01/01/2019', 'to_date': 'error', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'etf': 'error', 'country': 'spain', 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'etf': ['error'], 'country': 'spain', 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'etf': 'bbva accion dj eurostoxx 50', 'country': 'spain', 'from_date': '01/01/1998', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'etf': 'bbva accion dj eurostoxx 50', 'country': 'spain', 'from_date': '01/01/2019', 'to_date': '01/01/1998', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'etf': 'bbva accion dj eurostoxx 50', 'country': 'spain', 'from_date': '01/01/1900', 'to_date': '01/01/1950', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'etf': 'bbva accion dj eurostoxx 50', 'country': 'spain', 'from_date': '01/01/2019', 'to_date': '01/03/2019', 'as_json': True, 'order': 'ascending', 'debug': 'error' }, ] for param in params: try: investpy.get_etf_historical_data(etf=param['etf'], country=param['country'], from_date=param['from_date'], to_date=param['to_date'], as_json=param['as_json'], order=param['order'], debug=param['debug']) except: pass params = [ { 'country': 'error', 'as_json': False, }, { 'country': None, 'as_json': False, }, { 'country': ['error'], 'as_json': False, }, { 'country': 'spain', 'as_json': None, }, { 'country': 'spain', 'as_json': ['error'], }, ] for param in params: try: investpy.get_etfs_overview(country=param['country']) except: pass params = [ { 'by': None, 'value': 'bbva', }, { 'by': ['error'], 'value': 'bbva', }, { 'by': 'error', 'value': 'bbva', }, { 'by': 'name', 'value': None, }, { 'by': 'name', 'value': ['error'], }, { 'by': 'isin', 'value': 'error', }, ] for param in params: try: investpy.search_etfs(by=param['by'], value=param['value']) except: pass
def index(request): # portfolio = StockPortfolio() # portfolio.add_stock(stock_symbol='BBVA', # stock_country='spain', # purchase_date='04/01/2018', # num_of_shares=2, # cost_per_share=7.2) # print(portfolio.__dict__) movimientos_list = Movimiento.objects.values('empresa__symbol').annotate( total_acciones=Sum('acciones'), ) cartera_actual_list = Movimiento.objects.values( 'empresa__symbol').annotate( total_acciones=Sum('acciones'), #rentabilidad=Sum('coste_total'), coste_operacion=Sum(F('precio') * F('acciones') * F('cambio_moneda'), output_field=FloatField()), precio_promedio=Avg( F('precio') * F('acciones') / F('acciones'), output_field=FloatField())) #.filter(total_acciones>0) # sales_price=Case( # When(discount__isnull=True, then=F('price')), # When(discount__isnull=False, then=(F('price') - (F('discount') * F('price')) / 100)), # output_field=IntegerField(), # ) # THIS_FOLDER = os.path.dirname(os.path.abspath(__file__)) # my_file = os.path.join(THIS_FOLDER, 'test_stock_transactions.csv') empresas_eeuu = Empresa.objects.all( ) #filter(pais='united states') # , tipo='a' empresas_es = Empresa.objects.filter(pais='spain') portfolio_df = read_frame( Movimiento.objects.filter( empresa__in=empresas_eeuu)) #pd.read_csv(my_file) filter(tipo='a') print('aqui;', portfolio_df) symbols = portfolio_df.empresa.unique() #empresas_validas.unique() print(symbols) portfolio_df['fecha'] = pd.to_datetime( portfolio_df['fecha'].dt.normalize()) portfolio_df.sort_values("fecha", inplace=True, ascending=True) print('portfolio_df:', portfolio_df) # empresas = Empresa.objects.filter(nombre__in=empresas_cartera) # symbols = empresas.values_list('symbol', flat=True) stocks_start = datetime(2020, 1, 26) stocks_end = datetime(2020, 2, 7) dfETF = investpy.get_etf_historical_data( etf='ETFS Physical Gold', country='italy', from_date=stocks_start.strftime('%d/%m/%Y'), to_date=stocks_end.strftime('%d/%m/%Y')) dfETF.drop(['Open', 'High', 'Low', 'Currency', 'Exchange'], axis='columns', inplace=True) dfETF["Ticker"] = "PHAU" dfETF['Date'] = pd.date_range(start=stocks_start.strftime('%d/%m/%Y'), periods=len(dfETF), freq='D') #dfETF['Date'] = [d.strftime('%Y-%m-%d') if not pd.isnull(d) else '' for d in dfETF['Date']] print('dataETF:', dfETF) # GETDATA EEUU EN YFINANCE daily_adj_close = pa.get_data(symbols, stocks_start, stocks_end) daily_adj_close = daily_adj_close[['Close']].reset_index() daily_adj_close = daily_adj_close.append(dfETF, ignore_index=True) print('el precio de cierre de todo el periodo seleccionado es:', daily_adj_close ) #Ofrece el precio de cierre de todo el periodo seleccionado daily_benchmark = pa.get_benchmark(['SPY'], stocks_start, stocks_end) daily_benchmark = daily_benchmark[['Date', 'Close']] market_cal = pa.create_market_cal(stocks_start, stocks_end) #print('market_cal:', market_cal) active_portfolio = pa.portfolio_start_balance(portfolio_df, stocks_start) cartera_activa = pa.cartera_start_balance( portfolio_df, stocks_start) #para poner lo de la fecha luego print('active_portfolio:') print(active_portfolio) positions_per_day = pa.time_fill(active_portfolio, market_cal) print('positions_per_day:', positions_per_day) # modified_cost_per_share = pa.modified_cost_per_share(portfolio_df, daily_adj_close, stocks_start) # print('modified_cost_per_share:', modified_cost_per_share) combined_df = pa.per_day_portfolio_calcs(positions_per_day, daily_benchmark, daily_adj_close, stocks_start) print('combined_df:', combined_df) positions_summary = pu.get_position_summary( combined_df) #Movimiento.objects.all()) valor_cartera_total_diaria = pu.get_valor_cartera_total_diaria(combined_df) #print(positions_summary) # print(positions_summary["Total Gain/Loss ($)"].iloc[-1]) accounts = Cartera.objects.all() total_cash = sum((acct.capital_inicial for acct in accounts)) estado_cuenta_cartera = pu.get_estado_cuenta_cartera( portfolio_df, total_cash) context = { "estado_cuenta_cartera": estado_cuenta_cartera.to_html(index=False, float_format=lambda x: '%.2f' % x), "valor_cartera_total_diaria": valor_cartera_total_diaria.to_html(index=False, float_format=lambda x: '%.2f' % x), "positions": positions_summary.to_html(index=False, float_format=lambda x: '%.2f' % x), "accounts": [acct.nombre for acct in accounts], "cash_balances": {acct: acct.capital_inicial for acct in accounts}, "total_cash": "{:,.2f}".format(total_cash), #"total_value": "{:,.2f}".format(total_cash + positions_summary["Market Value ($)"].iloc[0]),# + positions_summary["Total Gain/Loss ($)"].iloc[0]), "num_positions": positions_summary.shape[0] - 1, 'latest_movimientos_list': movimientos_list, 'cartera_actual_list': cartera_actual_list, } return render(request, 'micartera/index.html', context)