def test_direct_volume_in_base(self, mocked_market): """ l = [('USDT-BTC', 1000, 1), ('USDT-ETH', 1000, 1), ('BTC-ETH', 1, 1)] """ market = volume.Market(json_blob=market1()) computed = market._direct_volume_in_base('BTC', 'BTC') self.assertEqual(computed, 0) computed = market._direct_volume_in_base('BTC', 'XXX') self.assertEqual(computed, 0) computed = market._direct_volume_in_base('BTC', 'USDT') self.assertEqual(computed, 1) computed = market._direct_volume_in_base('USDT', 'BTC') self.assertEqual(computed, 1000) computed = market._direct_volume_in_base('ETH', 'BTC') self.assertEqual(computed, 1) computed = market._direct_volume_in_base('BTC', 'ETH') self.assertEqual(computed, 1) computed = market._direct_volume_in_base('ETH', 'USDT') self.assertEqual(computed, 1) computed = market._direct_volume_in_base('USDT', 'ETH') self.assertEqual(computed, 1000)
def test_caching(self, mocked_get_summaries): """ Test that caching works, and volume.get_summaries() gets called only once """ mocked_get_summaries.return_value = market1() market = volume.Market() for i in range(10): market.summaries() mocked_get_summaries.assert_called_once()
def test_from_empty_balance(self, mocked_market): """ start_new_portfolio will call get_currencies, we could mock it but there is not need for it as long as we use currencies that do exist for real (unlike 'AAA') """ mocked_market.return_value = market1() market = volume.Market() state = volume.define_state(market, 2, include_usd=False) portfolio = volume.Portfolio() portfolio.ideal_rebalance(market, state) """
def test_uniform(self): """ [('USDT-BTC', 2000, 10), ('USDT-ETH', 500, 10), ('BTC-ETH', 0.25, 10), ('BTC-AAA', 1E-5, 1000), ('BTC-BBB', 1E-3, 1E6)] :return: """ market = volume.Market(json_blob=market3()) state = volume.define_state(market, 4) self.assertEqual(state.shape, (4, 1)) self.assertItemsEqual(state['Weight'], [0.25] * 4)
def test_caching_2(self, mocked_get_summaries): """ Test that caching works, and volume.get_summaries() gets called twice if 'cache_timeout_sec' elapses in between calls """ mocked_get_summaries.return_value = market1() market = volume.Market(0.1) market.summaries() time.sleep(.2) market.summaries() mocked_get_summaries.assert_called_once()
def test_start_portfolio(self): market = volume.Market(json_blob=market1()) state = volume.define_state(market, 2, include_usd=False) portfolio = volume.Portfolio() portfolio.start_portfolio(market, state, 'USDT', 1000) # we are overestimating the cost of the transaction because we are counting 4 transactions instead of 2 # to establish the portfolio (selling USDT to buy BTC pays only once and I'm counting it twice, same for # ETH) self.assertEqual(portfolio.total_value(market, ['USDT']), 995) # 0.5 * (1 - 0.25%/100) = 0.49875 # buy selling all 1000 dollars, due to a fake commission I end up with a -2.5 balance self.assertItemsEqual(portfolio.portfolio['Balance'], [.49875, .49875, -2.5])
def test_value_per_currency(self, mocked_get_balances): """ balance1 is: [('BTC', 2), ('ETH', 3), ('USDT', 4)] market2 is: [('USDT-BTC', 2000, 10), ('USDT-ETH', 500, 10), ('BTC-ETH', 0.25, 10)] :param mocked_get_balances: :return: """ mocked_get_balances.return_value = balance1() market = volume.Market(json_blob=market2()) portfolio = volume.Portfolio() computed = portfolio.value_per_currency(market, ['BTC', 'USDT']) self.assertEqual(type(computed), pd.Series) self.assertItemsEqual(computed.tolist(), [4000, 1500, 4]) computed = portfolio.value_per_currency(market, ['ETH', 'USDT']) self.assertItemsEqual(computed.tolist(), [4000, 1500, 4])
def main(): # 'state' is the desired composition of our portfolio. When we 'rebalance' positions we do it # to keep rations between different currencies matching those of 'state' while True: timestamp = int(time.time()) fid, filename = tempfile.mkstemp(suffix='{}.csv'.format(timestamp)) market = volume.Market() last = market.summaries()['Last'] last['time'] = timestamp last.to_csv(filename) dest_key = hashlib.md5( last.to_string()).hexdigest() + "_" + str(timestamp) s3_client.upload_file(filename, bucket_name, dest_key) break time.sleep(600)
def test_direct_volume_in_base2(self, mocked_market): # Market is: [('USDT-BTC', 2000, 10), ('USDT-ETH', 500, 10), ('BTC-ETH', 0.25, 10)] market = volume.Market(json_blob=market2()) computed = market._direct_volume_in_base('BTC', 'USDT') self.assertEqual(computed, 10) computed = market._direct_volume_in_base('USDT', 'BTC') self.assertEqual(computed, 20000) computed = market._direct_volume_in_base('ETH', 'BTC') self.assertEqual(computed, 10) computed = market._direct_volume_in_base('BTC', 'ETH') self.assertEqual(computed, 2.5) computed = market._direct_volume_in_base('ETH', 'USDT') self.assertEqual(computed, 10) computed = market._direct_volume_in_base('USDT', 'ETH') self.assertEqual(computed, 5000)
def main(): # 'state' is the desired composition of our portfolio. When we 'rebalance' positions we do it # to keep rations between different currencies matching those of 'state' value = 10 market = volume.Market() state = volume.define_state(market, 20, include_usd=True) base_currency = 'BTC' value = 10000 portfolio = volume.Portfolio.from_csv() if portfolio is None: portfolio = volume.Portfolio() portfolio.start_portfolio(market, state, 'USDT', value) header = True else: header = False portfolio.ideal_rebalance(market, state) market.to_csv(header=header) portfolio.to_csv() print "Market value:", portfolio.total_value(market, ['BTC', 'USDT'])
def setUp(self, mocked_get_balances): mocked_get_balances.return_value = fake_get_balances() self.market = volume.Market(json_blob=market0()) self.portfolio = volume.Portfolio()
def test_usd_volumes(self): # Market is: [('USDT-BTC', 1000, 1), ('USDT-ETH', 1000, 1), ('BTC-ETH', 1, 1)] market = volume.Market(json_blob=market1())
def test_currency_volume_in_base_1(self): # Market is: [('USDT-BTC', 2000, 10), ('USDT-ETH', 500, 10), ('BTC-ETH', 0.25, 10)] market = volume.Market(json_blob=market2()) computed = market.currency_volume_in_base('USDT', 'BTC') expected = 10 * 2000 + 10 * .25 * 2000 self.assertEqual(computed, expected)
def setUp(self): self.market = volume.Market(json_blob=market0())
def test_currency_volume_in_base_raises(self): market = volume.Market(json_blob=market2()) self.assertRaises(AssertionError, market.currency_volume_in_base, 'XXX', 'BTC')
def test__volume_in_eth_1(self): # Market is: [('USDT-BTC', 2000, 10), ('USDT-ETH', 500, 10), ('BTC-ETH', 0.25, 10)] market = volume.Market(json_blob=market2()) computed = market._volume_in_eth('USDT') expected = 10 + 10 * 2000 / 500.0 self.assertEqual(computed, expected)