def update_price_label(): price_usd = float(tools.get_usd_price()) curr_fx_cadusd = float( tools.get_fx_cadusd_rates( datetime.datetime.now().strftime('%Y-%m-%d'))[0]) price_cad = int(price_usd / curr_fx_cadusd) return 'BTC: ' + str(price_usd) + ' USD / ' + str(price_cad) + ' CAD'
def test_get_rates_all_week_days(self): resp = tools.get_fx_cadusd_rates(start_date='2019-11-04', end_date='2019-11-08') self.assertEqual(len(resp), 5, 'Should be 5') self.assertEqual(float(resp[4]), 0.7606, "Should be rate of 0.7606") self.assertEqual(float(resp[3]), 0.7603, "Should be rate of 0.7603") self.assertEqual(float(resp[2]), 0.7588, "Should be rate of 0.7588") self.assertEqual(float(resp[1]), 0.7590, "Should be rate of 0.7590") self.assertEqual(float(resp[0]), 0.7563, "Should be rate of 0.7563")
def test_fx_rates(self): resp = tools.get_fx_cadusd_rates(start_date='2020-01-26', end_date='2020-01-31') self.assertEqual(len(resp), 6, 'Should be 6') self.assertEqual(float(resp[0]), 0.7557, "Should be rate of 0.7557") self.assertEqual(float(resp[1]), 0.7566, "Should be rate of 0.7566") self.assertEqual(float(resp[2]), 0.7578, "Should be rate of 0.7578") self.assertEqual(float(resp[3]), 0.7588, "Should be rate of 0.7588") self.assertEqual(float(resp[4]), 0.7586, "Should be rate of 0.7586") self.assertEqual(float(resp[5]), cfg.AVG_FX_CADUSD, "Should be rate of "+str(cfg.AVG_FX_CADUSD))
def update_row_prices(self, date_to_update, btc_price_usd): fx_rate = float( tools.get_fx_cadusd_rates( datetime.datetime.now().strftime('%Y-%m-%d'))[0]) btc_price_cad = round(btc_price_usd / fx_rate, 1) self.stats.loc[self.stats['date'] == date_to_update, 'fx_cadusd'] = fx_rate self.stats.loc[self.stats['date'] == date_to_update, 'btc_price_usd'] = btc_price_usd self.stats.loc[self.stats['date'] == date_to_update, 'btc_price_cad'] = btc_price_cad
def populate_stats(self): loan_start_date = pd.Timestamp(self.start_date) self.stats['date'] = self.df_btcusd[ self.df_btcusd['Date'] >= loan_start_date]['Date'] self.stats['btc_price_usd'] = self.df_btcusd[ self.df_btcusd['Date'] >= loan_start_date]['Last'] fx_rates = tools.get_fx_cadusd_rates(str(self.start_date)) if self.exchange_is_one_day_ahead_from_price_data(fx_rates): fx_rates.pop(0) self.stats['fx_cadusd'] = fx_rates self.stats['btc_price_cad'] = [ round(row['btc_price_usd'] / float(row['fx_cadusd']), 1) for _, row in self.stats.iterrows() ] self.stats['debt_cad'] = self.populate_debt_cad() self.stats['coll_amount'] = self.populate_collateral_amounts() self.stats['interest_cad'] = self.calculate_interest() self.stats['ltv'] = self.calculate_loan_ltv()
def append_new_row_to_stats(self, date_to_update, btc_price_usd): fx_rate = float( tools.get_fx_cadusd_rates( datetime.datetime.now().strftime('%Y-%m-%d'))[0]) btc_price_cad = round(btc_price_usd / fx_rate, 1) interest_cad = self.calculate_new_row_interest() ltv = calculate_ltv(self.current_debt_cad, interest_cad, self.current_collateral, btc_price_cad) new_row = pd.DataFrame({ 'date': [date_to_update], 'btc_price_usd': [btc_price_usd], 'fx_cadusd': [fx_rate], 'btc_price_cad': [btc_price_cad], 'debt_cad': [self.current_debt_cad], 'coll_amount': [self.current_collateral], 'ltv': [ltv], 'interest_cad': [interest_cad] }) self.stats = pd.concat([new_row, self.stats], sort=True).reset_index(drop=True)
def test_get_rates_only_weekend(self): resp = tools.get_fx_cadusd_rates(start_date='2019-11-09', end_date='2019-11-10') self.assertEqual(len(resp), 2, 'Should be 2')
def test_get_rates_weekend_start(self): resp = tools.get_fx_cadusd_rates(start_date='2019-11-02', end_date='2019-11-09') self.assertEqual(len(resp), 8, 'Should be 8')
def test_get_rates_only_holiday(self): # Nov 11th is holiday resp = tools.get_fx_cadusd_rates(start_date='2019-11-11', end_date='2019-11-11') self.assertEqual(len(resp), 1, 'Should be 1') self.assertEqual(resp[0], cfg.AVG_FX_CADUSD, 'Should be equal to AVG_FXCADUSD')
def test_get_rates_holiday_first_day(self): # Nov 11th is holiday resp = tools.get_fx_cadusd_rates(start_date='2019-11-11', end_date='2019-11-14') self.assertEqual(len(resp), 4, 'Should be 4')