def evolucion_fondo(isin): import pandas as pd import investpy from datetime import date isin = isin nombre = investpy.search_funds(by='isin', value=isin).loc[0, 'name'] pais = investpy.search_funds(by='isin', value=isin).loc[0, 'country'] df = investpy.get_fund_historical_data( fund=nombre, from_date="01/01/2000", to_date=date.today().strftime("%d/%m/%Y"), country=pais) df = df.drop(['Open', 'High', 'Low'], axis=1) df['returns'] = df['Close'].pct_change() df = df.dropna() return df
def import_funds(successfulPulls): # imports funds in the US search_results = investpy.search_funds(by='country', value='united states') list_of_fund_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_fund_names[:2500]: try: # Have an if statement in place in case if we don't want to pull every fund because there are a lot of funds # Program takes a long time to run if we have to webscrape every fund each time we run fundData = [] fundData = investpy.get_fund_historical_data( fund=name, country='united states', from_date=_configKeys.STARTPULL, to_date=_configKeys.ENDPULL) newIndex = [] for index in fundData.index: newIndex.append( datetime.datetime.strptime( datetime.datetime.strftime((index + timedelta(days=1)), '%Y-%m-%d'), '%Y-%m-%d')) fundData['Date'] = newIndex fundData.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) + "Fund" if fundData.empty == False and fundData.index[ 0] - firstIndex <= timedelta( days=2 ) and lastIndex - fundData.index[-1] <= timedelta(days=3): successfulPulls["Symbol"].append(name.replace("/", "")) successfulPulls["Type"].append("Fund") fundData.to_csv( os.path.join(Path(_configKeys.DATA_FOLDER), name.replace("/", "") + '.csv')) except: print("Something went wrong when importing: " + name)
def get_fund_data(isin: str, start_date: np.datetime64, end_date: np.datetime64) -> pd.DataFrame: """ Gets price data for fund for specified period Args: ticker (str): String ticker with ISIN start_date (np.datetime64): Start date to get price data end_date (np.datetime64): End date to get price data Returns: pd.DataFrame: Dataframe containing close, split, dividend data for ticker from start_date to end_date """ try: fund_search = investpy.search_funds(by='isin', value=isin) name = fund_search.at[0, 'name'] country = fund_search.at[0, 'country'] df = investpy.get_fund_historical_data( fund=name, country=country, from_date=start_date.strftime('%d/%m/%Y'), to_date=end_date.strftime('%d/%m/%Y')) df.drop('Currency', axis=1, inplace=True) df.reset_index(inplace=True) except RuntimeError: # if not in investpy database, check if there is a custom funds module, import and execute custom fund function try: from utils.custom_funds import get_custom_fund_data df = get_custom_fund_data(isin, start_date, end_date) except ImportError: print('No custom funds module available') df = None except ValueError: df = None if isinstance(df, pd.DataFrame): df['Stock Splits'] = 0 df['Dividends'] = 0 df.set_index(['Date'], inplace=True, drop=True) return df
def get_fund_name_from_symbol(symbol: str) -> Tuple[str, str]: """Get fund name from symbol from investpy Parameters ---------- symbol: str Symbol to get fund name of Returns ------- str Name of fund matching provided symbol str Country matching symbol """ symbol_search_results = investpy.search_funds(by="symbol", value=symbol) if symbol_search_results.empty: return "", "" name = symbol_search_results.loc[:, "name"][0] country = symbol_search_results.loc[:, "country"][0] console.print( f"Name: [cyan][italic]{name.title()}[/italic][/cyan] found for {symbol} in country: {country.title()}." ) return name, country
def get_fund_symbol_from_name(name: str) -> Tuple[str, str]: """Get fund symbol from name through investpy Parameters ---------- Name: str Name to get fund symbol of Returns ------- str Name of Symbol matching provided name str Country in which matching symbol was found """ name_search_results = investpy.search_funds(by="name", value=name) if name_search_results.empty: return "", "" symbol = name_search_results.loc[:, "symbol"][0] country = name_search_results.country.values[0] console.print( f"Name: [cyan][italic]{symbol.upper()}[/italic][/cyan] found for {name} in country: {country.title()}." ) return symbol, country
def test_funds_errors(): """ This function raises errors on fund retrieval functions """ try: retrieve_funds(test_mode=None) except: pass try: retrieve_fund_countries(test_mode=None) except: pass params = [ { 'country': ['error'], }, ] for param in params: try: investpy.get_funds(country=param['country']) except: pass try: investpy.get_funds_list(country=param['country']) except: pass params = [ { 'country': ['error'], 'columns': None, 'as_json': False }, { 'country': None, 'columns': None, 'as_json': 'error' }, { 'country': None, 'columns': 0, 'as_json': True }, { 'country': None, 'columns': ['error'], 'as_json': False }, ] for param in params: try: investpy.get_funds_dict(country=param['country'], columns=param['columns'], as_json=param['as_json']) except: pass params = [ { 'fund': None, 'country': 'spain', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'fund': 'quality inversion conservadora fi', 'country': None, 'as_json': False, 'order': 'ascending', 'debug': False }, { 'fund': 'quality inversion conservadora fi', 'country': ['error'], 'as_json': False, 'order': 'ascending', 'debug': False }, { 'fund': 'quality inversion conservadora fi', 'country': 'error', 'as_json': False, 'order': 'ascending', 'debug': False }, { 'fund': 'quality inversion conservadora fi', 'country': 'germany', 'as_json': False, 'order': 'ascending', 'debug': False }, { 'fund': 'quality inversion conservadora fi', 'country': 'spain', 'as_json': 'error', 'order': 'ascending', 'debug': True }, { 'fund': 'quality inversion conservadora fi', 'country': 'spain', 'as_json': True, 'order': 'error', 'debug': True }, { 'fund': 'error', 'country': 'spain', 'as_json': True, 'order': 'ascending', 'debug': True }, { 'fund': ['error'], 'country': 'spain', 'as_json': True, 'order': 'ascending', 'debug': True }, { 'fund': 'quality inversion conservadora fi', 'country': 'spain', 'as_json': True, 'order': 'ascending', 'debug': 'error' }, ] for param in params: try: investpy.get_fund_recent_data(fund=param['fund'], country=param['country'], as_json=param['as_json'], order=param['order'], debug=param['debug']) except: pass params = [ { 'fund': None, 'country': 'spain', 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'fund': 'quality inversion conservadora fi', 'country': None, 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': False }, { 'fund': 'quality inversion conservadora fi', 'country': ['error'], 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': False }, { 'fund': 'quality inversion conservadora fi', 'country': 'error', 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': False }, { 'fund': 'quality inversion conservadora fi', 'country': 'germany', 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': False }, { 'fund': 'quality inversion conservadora fi', 'country': 'spain', 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': 'error', 'order': 'ascending', 'debug': True }, { 'fund': 'quality inversion conservadora fi', 'country': 'spain', 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': 'error', 'order': 'ascending', 'debug': True }, { 'fund': 'quality inversion conservadora fi', 'country': 'spain', 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'error', 'debug': True }, { 'fund': 'quality inversion conservadora fi', 'country': 'spain', 'from_date': 'error', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'fund': 'quality inversion conservadora fi', 'country': 'spain', 'from_date': '01/01/2019', 'to_date': 'error', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'fund': 'error', 'country': 'spain', 'from_date': '01/01/2018', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'fund': ['error'], 'country': 'spain', 'from_date': '01/01/1998', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'fund': 'quality inversion conservadora fi', 'country': 'spain', 'from_date': '01/01/1998', 'to_date': '01/01/2019', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'fund': 'quality inversion conservadora fi', 'country': 'spain', 'from_date': '01/01/2019', 'to_date': '01/01/1998', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'fund': 'quality inversion conservadora fi', 'country': 'spain', 'from_date': '01/01/1900', 'to_date': '01/01/1950', 'as_json': False, 'order': 'ascending', 'debug': True }, { 'fund': 'quality inversion conservadora fi', 'country': 'spain', 'from_date': '01/01/2019', 'to_date': '01/03/2019', 'as_json': False, 'order': 'ascending', 'debug': 'error' }, ] for param in params: try: investpy.get_fund_historical_data(fund=param['fund'], 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 = [ { 'fund': None, 'country': 'spain', 'as_json': False }, { 'fund': 'quality inversion conservadora fi', 'country': None, 'as_json': False }, { 'fund': 'quality inversion conservadora fi', 'country': ['error'], 'as_json': False }, { 'fund': 'quality inversion conservadora fi', 'country': 'error', 'as_json': False }, { 'fund': 'quality inversion conservadora fi', 'country': 'germany', 'as_json': False }, { 'fund': 'quality inversion conservadora fi', 'country': 'spain', 'as_json': 'error' }, { 'fund': 'error', 'country': 'spain', 'as_json': True }, { 'fund': ['error'], 'country': 'spain', 'as_json': True }, ] for param in params: try: investpy.get_fund_information(fund=param['fund'], country=param['country'], as_json=param['as_json']) 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_funds(by=param['by'], value=param['value']) except: pass
def test_investpy_funds(): """ This function checks that fund data retrieval functions listed in investpy work properly. """ 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': ['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_funds_dict(country=param['country'], columns=param['columns'], as_json=param['as_json']) investpy.get_fund_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_fund_recent_data(fund='bbva multiactivo conservador pp', country='spain', as_json=param['as_json'], order=param['order'], interval='Daily') 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'], interval='Daily') 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 = [ { 'country': 'andorra', 'as_json': True, 'n_results': 2 }, { 'country': 'andorra', 'as_json': False, 'n_results': 2 }, { 'country': 'united states', 'as_json': False, 'n_results': 2 }, { 'country': 'united kingdom', 'as_json': False, 'n_results': 2 } ] for param in params: investpy.get_funds_overview(country=param['country'], as_json=param['as_json'], n_results=param['n_results']) investpy.search_funds(by='name', value='bbva')
def test_investpy_funds(): """ This function checks that fund data retrieval functions listed in investpy work properly. """ 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': 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_funds_dict(country=param['country'], columns=param['columns'], as_json=param['as_json']) investpy.get_fund_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_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']) 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']) investpy.search_funds(by='name', value='bbva') retrieve_funds(test_mode=True) retrieve_fund_countries(test_mode=True)