def import_bonds(successfulPulls): # imports bonds in the US search_results = investpy.search_bonds(by='country', value='united states') list_of_bond_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_bond_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 bondData = [] bondData = investpy.get_bond_historical_data( bond=name, from_date=_configKeys.STARTPULL, to_date=_configKeys.ENDPULL) newIndex = [] for index in bondData.index: newIndex.append( datetime.datetime.strptime( datetime.datetime.strftime((index + timedelta(days=1)), '%Y-%m-%d'), '%Y-%m-%d')) bondData['Date'] = newIndex bondData.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) + "Bond" if (bondData.empty == False) and ( bondData.index[0] - firstIndex.date() <= timedelta(days=2) ) and (lastIndex.date() - bondData.index[-1] <= timedelta(days=3)): successfulPulls["Symbol"].append(name.replace("/", "")) successfulPulls["Type"].append("Bond") bondData.to_csv( os.path.join(Path(_configKeys.DATA_FOLDER), name.replace("/", "") + '.csv')) except: print("Something went wrong when importing: " + name)
def test_investpy_bonds(): """ This function checks that bond data retrieval functions listed in investpy work properly. """ params = [ { 'country': 'spain', }, { 'country': None, }, ] for param in params: investpy.get_bonds(country=param['country']) investpy.get_bonds_list(country=param['country']) params = [ { 'country': None, 'columns': ['full_name', 'name'], 'as_json': True }, { 'country': None, 'columns': ['full_name', 'name'], 'as_json': False }, { 'country': 'spain', 'columns': ['full_name', 'name'], 'as_json': True }, { 'country': 'spain', 'columns': ['full_name', 'name'], 'as_json': False }, { 'country': 'spain', 'columns': None, 'as_json': False }, ] for param in params: investpy.get_bonds_dict(country=param['country'], columns=param['columns'], as_json=param['as_json']) investpy.get_bond_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_bond_recent_data(bond='Spain 30Y', as_json=param['as_json'], order=param['order'], interval='Daily') investpy.get_bond_historical_data(bond='Spain 30Y', from_date='01/01/1990', to_date='01/01/2019', as_json=param['as_json'], order=param['order'], interval='Daily') params = [ { 'bond': 'spain 30y', 'as_json': False }, { 'bond': 'argentina 3y', 'as_json': True }, { 'bond': 'germany 3m', 'as_json': False }, ] for param in params: investpy.get_bond_information(bond=param['bond'], as_json=param['as_json']) params = [ { 'country': 'united states', 'as_json': True, }, { 'country': 'united kingdom', 'as_json': False, } ] for param in params: investpy.get_bonds_overview(country=param['country'], as_json=param['as_json']) investpy.search_bonds(by='name', value='Spain')
def test_investpy_bonds(): """ This function checks that bond data retrieval functions listed in investpy work properly. """ params = [ { 'country': 'spain', }, { 'country': None, }, ] for param in params: investpy.get_bonds(country=param['country']) investpy.get_bonds_list(country=param['country']) params = [ { 'country': None, 'columns': ['full_name', 'name'], 'as_json': True }, { 'country': None, 'columns': ['full_name', 'name'], 'as_json': False }, { 'country': 'spain', 'columns': ['full_name', 'name'], 'as_json': True }, { 'country': 'spain', 'columns': ['full_name', 'name'], 'as_json': False }, { 'country': 'spain', 'columns': None, 'as_json': False }, ] for param in params: investpy.get_bonds_dict(country=param['country'], columns=param['columns'], as_json=param['as_json']) investpy.get_bond_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_bond_recent_data(bond='Spain 30Y', country='spain', as_json=param['as_json'], order=param['order'], debug=param['debug']) investpy.get_bond_historical_data(bond='Spain 30Y', country='spain', from_date='01/01/1990', to_date='01/01/2019', as_json=param['as_json'], order=param['order'], debug=param['debug']) investpy.search_bonds(by='name', value='Spain')