def get_last_etf(keyword, country='united states'): metal = ['Gold', 'Silver', 'Copper', 'Metal'] etf = pd.DataFrame(investpy.get_etfs_dict()) if keyword == 'Energy': temp = etf[etf.full_name.str.contains(keyword)] temp = temp.append(etf[etf.full_name.str.contains('Oil')]) elif keyword == 'Metal': temp = pd.DataFrame() for i in metal: temp = temp.append(etf[etf.full_name.str.contains(i)]) temp.drop_duplicates(inplace=True) elif keyword == 'commodity': temp = etf[etf.asset_class == keyword] metal.append('Oil') for i in metal: temp = temp.append(etf[~etf.full_name.str.contains(i)]) else: temp = etf[etf.asset_class == keyword] temp = temp[temp.country == country] history = pd.DataFrame() for i in temp.name.values: try: print(i) data = investpy.get_etf_recent_data( i, country=country) data.reset_index(inplace=True) data['name'] = i history = history.append(data.tail(1)) time.sleep(random.choice(list(range(5, 16)))) except: pass return history
def get_price_investpy(sname, sticker, stype, scountry): if stype == "Stock": try: df = investpy.get_stock_recent_data(sname, scountry) except RuntimeError: try: df = investpy.get_stock_recent_data(sticker, scountry) except RuntimeError: raise MethodNotWorkingError elif stype == "Fund": try: df = investpy.get_fund_recent_data(sname, scountry) except RuntimeError: try: df = investpy.get_fund_recent_data(sticker, scountry) except RuntimeError: raise MethodNotWorkingError elif stype == "ETF": try: df = investpy.get_etf_recent_data(sname, scountry) except RuntimeError: try: df = investpy.get_etf_recent_data(sticker, scountry) except RuntimeError: raise MethodNotWorkingError elif stype == "Crypto": try: df = investpy.get_crypto_recent_data(sname) except RuntimeError: try: df = investpy.get_crypto_recent_data(sticker) except RuntimeError: raise MethodNotWorkingError else: raise MethodNotWorkingError return df.iloc[-1]['Close']
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 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')
profile = investpy.get_stock_company_profile(stock='bbva', country='spain', language='english') df_aapl = investpy.get_stock_historical_data(stock='AAPL', country='United States', from_date='01/01/2010', to_date='01/01/2020') df_fund = investpy.get_fund_historical_data( 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,
import investpy import yaml with open('stock.yaml') as f: data = yaml.load(f, Loader=yaml.FullLoader) for item in data["stocks"]: print(item["name"]) df = investpy.get_stock_recent_data(stock=item["name"],country=item["country"]) print(df) for item in data["etfs"]: print(item["name"]) df = investpy.get_etf_recent_data(etf=item["name"],country=item["country"]) print(df) # df = investpy.get_stock_recent_data(stock='PEUP',country='france') # print(df) # search_results = investpy.search_etfs(by='isin', value='LU1681043599') # print(search_results) # df = investpy.get_etf_recent_data(etf='Amundi MSCI World UCITS',country='france') # print(df) # df = investpy.get_etf_historical_data(etf='Amundi MSCI World UCITS',country='france',from_date='01/12/2019',to_date='06/02/2020') # print(df)
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()