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 ytm(country, maturity): df = pd.DataFrame( investpy.get_bond_historical_data(bond=str(country) + ' ' + str(maturity), from_date=oneyr, to_date=tdy)['Close']) df.columns = [str(country)] df.index = pd.to_datetime(df.index) return pd.DataFrame(df)
def get_bond(argv): start = argv[0] end = argv[1] df = investpy.get_bond_historical_data(bond='South Korea 10Y', from_date=start, to_date=end) df.reset_index(level=0, inplace=True) df = df[['Date', 'Close']] return df
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 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')
def get_bond(bond_name, start, end): bond = investpy.get_bond_historical_data(bond_name, from_date=start, to_date=end) return bond
def DataHandler(mode): if mode == 'run': dataVec = [1, 1, 1, 1] if dataVec[0] == 1: BondsList = [] for bond in BondsTickers: print(bond) df = investpy.get_bond_historical_data( bond=bond, from_date=fromDate, to_date=toDate).reset_index().rename(columns={ "Date": "Dates", "Close": bond }).set_index('Dates')[bond] BondsList.append(df) BondsDF = pd.concat(BondsList, axis=1) BondsDF[BondsDF == 0] = np.nan BondsDF[BondsDF.abs() > 100] = np.nan BondsDF = BondsDF.ffill().sort_index() BondsDF.to_sql('Bonds_Prices', conn, if_exists='replace') if dataVec[1] == 1: EqsList = [] for EqIndex in EqsTickers: print(EqIndex) df = investpy.get_index_historical_data( index=EqIndex[0], country=EqIndex[1], from_date=fromDate, to_date=toDate).reset_index().rename(columns={ "Date": "Dates", "Close": EqIndex[0] }).set_index('Dates')[EqIndex[0]] EqsList.append(df) EqsDF = pd.concat(EqsList, axis=1) EqsDF[EqsDF == 0] = np.nan EqsDF = EqsDF.ffill().sort_index() EqsDF.to_sql('Eqs_Prices', conn, if_exists='replace') if dataVec[2] == 1: FxList = [] for fx in FxTickers: print(fx) name = fx.replace('/', '') df = investpy.get_currency_cross_historical_data( currency_cross=fx, from_date=fromDate, to_date=toDate).reset_index().rename(columns={ "Date": "Dates", "Close": name }).set_index('Dates')[name] FxList.append(df) FxDF = pd.concat(FxList, axis=1) FxDF[FxDF == 0] = np.nan FxDF = FxDF.ffill().sort_index() FxDF["JPYUSD"] = FxDF["USDJPY"].apply(lambda x: 1 / x) FxDF["CNYUSD"] = FxDF["USDCNY"].apply(lambda x: 1 / x) FxDF[['EURUSD', 'GBPUSD', 'USDJPY', 'USDCNY']].to_sql('Fx_Prices_raw', conn, if_exists='replace') FxDF[['EURUSD', 'GBPUSD', 'CNYUSD', 'JPYUSD']].to_sql('Fx_Prices', conn, if_exists='replace') if dataVec[3] == 1: CustomList = [] for customProduct in CustomTickers: print(customProduct[1]) search_results = investpy.search(text=customProduct[1], n_results=5) for search_result in search_results: jResult = json.loads(str(search_result)) if ((jResult["id_"] == customProduct[0]) & (jResult["name"] == customProduct[1])): df = search_result.retrieve_historical_data( from_date=fromDate, to_date=toDate).reset_index().rename( columns={ "Date": "Dates", "Close": customProduct[1] }).set_index('Dates')[customProduct[1]] CustomList.append(df) CustomDF = pd.concat(CustomList, axis=1) CustomDF[CustomDF == 0] = np.nan CustomDF = CustomDF.ffill().sort_index() CustomDF.to_sql('Custom_Prices', conn, if_exists='replace') elif mode == 'plot': dataAll = [] for x in ['Bonds_Prices', 'Eqs_Prices', 'Fx_Prices', 'Custom_Prices']: df = pd.read_sql('SELECT * FROM ' + x, conn) if x == 'Bonds_Prices': df = df.rename(columns={"index": "Dates"}) df['Dates'] = pd.to_datetime(df['Dates']) df = df.set_index('Dates', drop=True) dataAll.append(df) Prices = pd.concat(dataAll, axis=1) Prices.to_sql('Prices', conn, if_exists='replace') Rates = sl.d(Prices[BondsTickers]).fillna(0) Rates.to_sql('Rates', conn, if_exists='replace') rets = sl.dlog(Prices.drop(BondsTickers, axis=1)).fillna(0) rets.to_sql('AssetsRets', conn, if_exists='replace') # TO PLOT Prices = pd.read_sql('SELECT * FROM Prices', conn).set_index('Dates', drop=True) Prices.index = [ x.replace("00:00:00", "").strip() for x in Prices.index ] for subset in ['A', 'B']: if subset == 'A': df = Prices[BondsTickers] ylabel = '$B_t$' returnTs = pd.read_sql('SELECT * FROM Rates', conn).set_index('Dates', drop=True) returnTs.index = [ x.replace("00:00:00", "").strip() for x in returnTs.index ] returnTs_ylabel = '$r_t$' else: df = Prices[[ x for x in Prices.columns if x not in BondsTickers ]].ffill() ylabel = '$S_t$' returnTs = pd.read_sql('SELECT * FROM AssetsRets', conn).set_index('Dates', drop=True) returnTs.index = [ x.replace("00:00:00", "").strip() for x in returnTs.index ] returnTs_ylabel = '$x_t$' # Plot 1 fig, ax = plt.subplots() mpl.pyplot.locator_params(axis='x', nbins=35) df.plot(ax=ax) for label in ax.get_xticklabels(): label.set_fontsize(25) label.set_ha("right") label.set_rotation(45) ax.set_xlim(xmin=0.0, xmax=len(Prices) + 1) mpl.pyplot.ylabel(ylabel, fontsize=32) plt.legend(loc=2, bbox_to_anchor=(1, 1.02), frameon=False, prop={'size': 17}) plt.subplots_adjust(top=0.95, bottom=0.2, right=0.8, left=0.08, hspace=0, wspace=0) plt.margins(0, 0) plt.grid() plt.show() # Plot 2 fig, ax = plt.subplots() mpl.pyplot.locator_params(axis='x', nbins=35) returnTs.plot(ax=ax) for label in ax.get_xticklabels(): label.set_fontsize(25) label.set_ha("right") label.set_rotation(45) ax.set_xlim(xmin=0.0, xmax=len(Prices) + 1) mpl.pyplot.ylabel(returnTs_ylabel, fontsize=32) plt.legend(loc=2, bbox_to_anchor=(1, 1.02), frameon=False, prop={'size': 17}) plt.subplots_adjust(top=0.95, bottom=0.2, right=0.8, left=0.08, hspace=0, wspace=0) plt.margins(0, 0) plt.grid() plt.show()
def bonds(request): price = 0.0 m = money.objects.get(pk=1) amount = m.mymoney amount = round(amount, 2) rem = 1000000 - amount rem = round(rem, 2) close = [] close1 = [] close2 = [] no = [] no1 = [] no2 = [] n = "Funds name" s = "" labels = [] datap = [] DataFrame3 = "Table for Particular Stock" indianfunds = ['India 10Y', 'India 30Y', 'India 24Y', 'India 15Y'] #fetch recent 10 investments query_results = investmentsinbonds.objects.all().order_by('-id')[:10] last_stock = 0 v1 = 0 if request.method == 'POST': #for buy stock,available assets,investments and pushing purcahses into investment table if ('stockname' in request.POST): n = request.POST['stockname'] #api_key = 'BJQZ9I2H012Q7FDD' #code for profit loss returns none if no query found so used if condition last_stock = investmentsinbonds.objects.filter(name=n).values( 'id', 'price').last() #ts = TimeSeries(key=api_key, output_format='json') #data, meta_data = ts.get_quote_endpoint(n) #get price real-time if (n in indianfunds): v1 = investpy.bonds.get_bond_recent_data( bond=n, as_json=False, order='descending', interval='Daily').iloc[0, :]['Close'] v1 = float(v1) c = CurrencyRates() inrtousd = c.get_rate('INR', 'USD') v1 = v1 * inrtousd price = round(v1, 2) else: v1 = float( investpy.bonds.get_bond_recent_data( bond=n, as_json=False, order='descending', interval='Daily').iloc[0, :]['Close']) price = round(v1, 2) if (last_stock != None): last_stock = float(last_stock['price']) last_stock = round(last_stock, 2) else: #assigned price because profit or loss=0 if we are adding it to table first time(assumption) last_stock = price if (price != 0.0): if (amount > price): amount = amount - price amount = round(amount, 2) m.mymoney = amount m.save() t = time.localtime() current_time = str(time.strftime("%H:%M:%S", t)) last_stock = price - last_stock last_stock = round(last_stock, 2) a = investmentsinbonds(name=n.upper(), price=price, date_created=str(date.today()), current_time=current_time, gain_loss=last_stock) a.save() rem = 1000000 - amount rem = round(rem, 2) #for table start to end elif ('sn' in request.POST): a11 = request.POST['sdate1'] import datetime darevy = str(datetime.datetime.strptime(a11, "%Y-%m-%d").year) darevd = str(datetime.datetime.strptime(a11, "%Y-%m-%d").day) darevm = str(datetime.datetime.strptime(a11, "%Y-%m-%d").month) a11 = request.POST['edate1'] edarevy = str(datetime.datetime.strptime(a11, "%Y-%m-%d").year) edarevd = str(datetime.datetime.strptime(a11, "%Y-%m-%d").day) edarevm = str(datetime.datetime.strptime(a11, "%Y-%m-%d").month) if (request.POST['sn'] in indianfunds): DataFrame3 = investpy.get_bond_historical_data( bond=request.POST['sn'], from_date=darevd + '/' + darevm + '/' + darevy, to_date=edarevd + '/' + edarevm + '/' + edarevy) else: DataFrame3 = investpy.get_bond_historical_data( bond=request.POST['sn'], from_date=darevd + '/' + darevm + '/' + darevy, to_date=edarevd + '/' + edarevm + '/' + edarevy) DataFrame3 = DataFrame3.to_html() #for graphs start to end else: a11 = request.POST['sdate'] import datetime darevy = str(datetime.datetime.strptime(a11, "%Y-%m-%d").year) darevd = str(datetime.datetime.strptime(a11, "%Y-%m-%d").day) darevm = str(datetime.datetime.strptime(a11, "%Y-%m-%d").month) a11 = request.POST['edate'] edarevy = str(datetime.datetime.strptime(a11, "%Y-%m-%d").year) edarevd = str(datetime.datetime.strptime(a11, "%Y-%m-%d").day) edarevm = str(datetime.datetime.strptime(a11, "%Y-%m-%d").month) if (request.POST['s'] in indianfunds): DataFrame = investpy.get_bond_historical_data( bond=request.POST['s'], from_date=darevd + '/' + darevm + '/' + darevy, to_date=edarevd + '/' + edarevm + '/' + edarevy) else: DataFrame = investpy.get_bond_historical_data( bond=request.POST['s'], from_date=darevd + '/' + darevm + '/' + darevy, to_date=edarevd + '/' + edarevm + '/' + edarevy) #DataFrame = yf.download(request.POST['s'],request.POST['sdate'],request.POST['edate']) close = DataFrame['Close'].tolist() s = str(request.POST['s']) sdate2 = date(int(darevy), int(darevm), int(darevd)) edate2 = date(int(edarevy), int(edarevm), int(edarevd)) delta2 = edate2 - sdate2 for i in range(delta2.days + 1): day = sdate2 + timedelta(days=i) no.append(str(day)) diff = len(no) - len(close) for i in range(1, diff + 1): no.remove(no[i]) d = dict() #for pie chart getting all investemnts summing up the stock by their labels queryset = investmentsinbonds.objects.order_by('name') for stock in queryset: labels.append(stock.name) datap.append(stock.price) for i in range(len(labels)): if (labels[i] in d.keys()): d[labels[i]] += datap[i] else: d[labels[i]] = datap[i] labels = list(d.keys()) datap = list(d.values()) #returning all the calculates values to html return render( request, 'portfolio/bonds.html', { 'n': n, 'labels': labels, 'datap': datap, 'price': price, 'amount': amount, 'query_results': query_results, 'rem': rem, 'close': close, 'no': no, 's': s, 'close1': close1, 'close2': close2, 'no1': no1, 'no2': no2, 'DataFrame3': DataFrame3 })
def get_Bond(assetname): asset = investpy.get_bond_historical_data(bond=assetname, from_date='01/01/2010', to_date='01/01/2021') filename = assetname.replace(".", "") filename = filename.replace("/", "") filename = filename.replace(" ", "") asset.to_csv(wd+'\\toto_data\\'+'bond-'+filename+'.csv')
if index and index_country and index_from_date and index_to_date != "": index_data_historical = investpy.get_index_historical_data( index=index, country=index_country, from_date=index_from_date, to_date=index_to_date) if stock and stock_country and stock_from_date and stock_to_date != "": stock_data_historical = investpy.get_stock_historical_data( stock=stock, country=stock_country, from_date=stock_from_date, to_date=stock_to_date) if bond and bond_country and bond_from_date and bond_to_date != "": bond_data_historical = investpy.get_bond_historical_data( bond=bond, from_date=bond_from_date, to_date=bond_to_date) def GetFundHistorical(): wb = xw.Book.caller() wb.sheets[fund_sheet].range('R1').value = fund_data_historical def GetIndexHistorical(): wb = xw.Book.caller() wb.sheets[index_sheet].range('R1').value = index_data_historical def GetStockHistorical(): wb = xw.Book.caller() wb.sheets[stock_sheet].range('R1').value = stock_data_historical