def test_bigger_commutative(self): """ Same as bigger, testing commutivity again. """ result = arbitrage.detect_cross(self.more_ob_2(), self.more_ob_1()) result.volume.should.equal(Money('1', 'BTC')) result.revenue.should.equal(Money('0.75', 'USD'))
def test_bigger(self): """ OB1 has a bit at 600 which should cross at 599 for 0.5btc and 599.5 for 0.5 btc for a volume of 2btc and a profit of $0.75. """ result = arbitrage.detect_cross(self.more_ob_1(), self.more_ob_2()) result.volume.should.equal(Money('1', 'BTC')) result.revenue.should.equal(Money('0.75', 'USD'))
def test_commutative(self): """ Same orderbooks as test_basic, double check it doesn't matter which order we give this function the orderbooks. """ result = arbitrage.detect_cross(self.basic_ob_1(), self.basic_ob_2()) result.volume.should.equal(Money('1', 'BTC')) result.revenue.should.equal(Money('1', 'USD'))
def test_basic(self): """ OB1 has a bid at 600 and OB2 has an ask at 599, both at 1btc. These should cross for a volume of 2btc and a profit of $1 USD. """ result = arbitrage.detect_cross(self.basic_ob_2(), self.basic_ob_1()) result.volume.should.equal(Money('1', 'BTC')) result.revenue.should.equal(Money('1', 'USD'))
def test_get_executable_no_sell_balance_b(self): cross = arbitrage.detect_cross(self.basic_ob_2(), self.basic_ob_1()) cross.volume.should.equal(Money('1', 'BTC')) cross.revenue.should.equal(Money('1', 'USD')) buy_balance = {'USD': Money('599', 'USD'), 'BTC': Money('1000000', 'BTC')} sell_balance = {'USD': Money('0', 'USD'), 'BTC': Money('0', 'BTC')} result = arbitrage.get_executable_volume(cross, buy_balance, sell_balance) result.should.equal(Money('0', 'BTC'))
def test_get_executable_restriction(self): cross = arbitrage.detect_cross(self.more_ob_1(), self.more_ob_2()) cross.volume.should.equal(Money('1', 'BTC')) cross.revenue.should.equal(Money('0.75', 'USD')) buy_balance = {'USD': Money('299.5', 'USD'), 'BTC': Money('0', 'BTC')} sell_balance = {'USD': Money('0', 'USD'), 'BTC': Money('1', 'BTC')} result = arbitrage.get_executable_volume(cross, buy_balance, sell_balance) result.should.equal(Money('0.5', 'BTC'))
def test_get_executable_partial_second_order(self): cross = arbitrage.detect_cross(self.more_ob_1(), self.more_ob_2()) cross.volume.should.equal(Money('1', 'BTC')) cross.revenue.should.equal(Money('0.75', 'USD')) buy_balance = {'USD': Money('449.375', 'USD'), 'BTC': Money('0', 'BTC')} sell_balance = {'USD': Money('0', 'USD'), 'BTC': Money('1', 'BTC')} result = arbitrage.get_executable_volume(cross, buy_balance, sell_balance) result.should.equal(Money('0.75', 'BTC'))
def test_real_data_crypto_crypto(self): """ Test based on real data taken from itbit and bitstamp at about 4:30 on September 26 2016 against math done by hand. """ result = arbitrage.detect_cross( self.real_orderbook_1(price_currency='BTC', vol_currency='ETH'), self.real_orderbook_2(price_currency='BTC', vol_currency='ETH'), ) result.volume.should.equal(Money('12.9087178', 'ETH')) result.revenue.should.equal(Money('5.2679425048', 'BTC'))
def test_bigger_crypto_crypto(self): """ OB1 has a bit at 600 which should cross at 599 for 0.5btc and 599.5 for 0.5 btc for a volume of 2btc and a profit of $0.75. """ result = arbitrage.detect_cross( self.more_ob_1(price_currency='BTC', vol_currency='ETH'), self.more_ob_2(price_currency='BTC', vol_currency='ETH'), ) result.volume.should.equal(Money('1', 'ETH')) result.revenue.should.equal(Money('0.75', 'BTC'))
def test_basic_crypto_crypto(self): """ OB1 has a bid at 600 and OB2 has an ask at 599, both at 1btc. These should cross for a volume of 2btc and a profit of $1 USD. """ result = arbitrage.detect_cross( self.basic_ob_2(price_currency='BTC', vol_currency='ETH'), self.basic_ob_1(price_currency='BTC', vol_currency='ETH'), ) result.volume.should.equal(Money('1', 'ETH')) result.revenue.should.equal(Money('1', 'BTC'))
def test_real_data_unprofitable(self): """ Test based on real data taken from itbit and bitstamp at about 4:30 on September 26 2016 against math done by hand. """ self.itbit.market_order_fee = Decimal('0.01') self.bitstamp.market_order_fee = Decimal('0.01') result = arbitrage.detect_cross( self.real_orderbook_1(), self.real_orderbook_2(), ) result.should.equal(None)
def test_basic_crypto_crypto_unprofitable(self): """ OB1 has a bid at 600 and OB2 has an ask at 599, both at 1btc. These should cross for a volume of 2btc and a profit of $1 USD. """ self.itbit.market_order_fee = Decimal('0.1') self.bitstamp.market_order_fee = Decimal('0.1') result = arbitrage.detect_cross( self.basic_ob_2(price_currency='BTC', vol_currency='ETH'), self.basic_ob_1(price_currency='BTC', vol_currency='ETH'), ) result.should.equal(None)
def test_get_executable_half(self): cross = arbitrage.detect_cross( self.basic_ob_2(price_currency='BTC', vol_currency='ETH'), self.basic_ob_1(price_currency='BTC', vol_currency='ETH'), ) cross.volume.should.equal(Money('1', 'ETH')) cross.revenue.should.equal(Money('1', 'BTC')) buy_balance = {'BTC': Money('299.5', 'BTC'), 'ETH': Money('1000000', 'ETH')} sell_balance = {'BTC': Money('0', 'BTC'), 'ETH': Money('1', 'ETH')} result = arbitrage.get_executable_volume(cross, buy_balance, sell_balance) result.should.equal(Money('0.5', 'ETH'))
def test_get_executable_fee_complex(self): self.itbit.market_order_fee = Decimal('0.0001') self.bitstamp.market_order_fee = Decimal('0.0001') cross = arbitrage.detect_cross(self.more_ob_1(), self.more_ob_2()) cross.volume.should.equal(Money('1', 'BTC')) cross.revenue.should.equal(Money('0.75', 'USD')) buy_balance = {'USD': Money('599.25', 'USD'), 'BTC': Money('0', 'BTC')} sell_balance = {'USD': Money('0', 'USD'), 'BTC': Money('1', 'BTC')} result = arbitrage.get_executable_volume(cross, buy_balance, sell_balance) result.round_to_decimal_places(8, ROUND_TRUNC)\ .should.equal(Money('0.99990005', 'BTC'))
def test_basic_crypto_crypto_minimally_profitable(self): """ OB1 has a bid at 600 and OB2 has an ask at 599, both at 1btc. These should cross for a volume of 2btc and a profit of $1 USD. """ self.itbit.market_order_fee = Decimal('0.0008') self.bitstamp.market_order_fee = Decimal('0.0008') result = arbitrage.detect_cross( self.basic_ob_2(price_currency='BTC', vol_currency='ETH'), self.basic_ob_1(price_currency='BTC', vol_currency='ETH'), ) result.volume.should.equal(Money('1', 'ETH')) result.revenue.should.equal(Money('1', 'BTC')) result.fees.should.equal(Money('0.9592', 'BTC')) result.profit.should.equal(Money('0.0408', 'BTC'))
def test_real_data_unprofitable_flag_off(self): """ Test based on real data taken from itbit and bitstamp at about 4:30 on September 26 2016 against math done by hand. """ self.itbit.market_order_fee = Decimal('0.10') self.bitstamp.market_order_fee = Decimal('0.10') result = arbitrage.detect_cross( self.real_orderbook_1(), self.real_orderbook_2(), ignore_unprofitable=False, ) result.volume.should.equal(Money('12.9087178', 'BTC')) result.revenue.should.equal(Money('5.2679425048', 'USD')) assert result.profit < Money('0', 'USD')
def test_basic_crypto_crypto_unprofitable_with_ignore_flag_off(self): """ OB1 has a bid at 600 and OB2 has an ask at 599, both at 1btc. These should cross for a volume of 2btc and a profit of $1 USD. """ self.itbit.market_order_fee = Decimal('0.1') self.bitstamp.market_order_fee = Decimal('0.1') result = arbitrage.detect_cross( self.basic_ob_2(price_currency='BTC', vol_currency='ETH'), self.basic_ob_1(price_currency='BTC', vol_currency='ETH'), ignore_unprofitable=False, ) result.volume.should.equal(Money('1', 'ETH')) result.revenue.should.equal(Money('1', 'BTC')) result.fees.should.equal(Money('119.9', 'BTC'))
def get_test_executable_realistic_fee(self): self.itbit.market_order_fee = Decimal('0.0001') self.bitstamp.market_order_fee = Decimal('0.0001') cross = arbitrage.detect_cross( self.basic_ob_2(), self.basic_ob_1(), ignore_unprofitable=False, ) cross.volume.should.equal(Money('1', 'BTC')) cross.revenue.should.equal(Money('1', 'USD')) buy_balance = {'USD': Money('599', 'USD'), 'BTC': Money('0', 'BTC')} sell_balance = {'USD': Money('0', 'USD'), 'BTC': Money('1', 'BTC')} result = arbitrage.get_executable_volume(cross, buy_balance, sell_balance) result.round_to_decimal_places(8).should.equal(Money('0.99990001', 'BTC'))
def test_get_executable_simple_fee(self): self.itbit.market_order_fee = 1 self.bitstamp.market_order_fee = 1 cross = arbitrage.detect_cross( self.basic_ob_2(), self.basic_ob_1(), ignore_unprofitable=False, ) cross.volume.should.equal(Money('1', 'BTC')) cross.revenue.should.equal(Money('1', 'USD')) buy_balance = {'USD': Money('599', 'USD'), 'BTC': Money('0', 'BTC')} sell_balance = {'USD': Money('0', 'USD'), 'BTC': Money('1', 'BTC')} result = arbitrage.get_executable_volume(cross, buy_balance, sell_balance) result.should.equal(Money('0.5', 'BTC'))
def tick(self, open_orders): self.logger.debug("--Strategy Tick--") self.logger.info("Current Orders: " + str(open_orders)) btc_crosses = arb.detect_cross( self.harness.bitstamp_btc_usd.get_orderbook(), self.harness.bitstamp_btc_eur.get_orderbook()) for cross in btc_crosses: print("Cross: " + str(cross)) executable_volume = arb.get_executable_volume( cross, cross.buy_exchange.get_balance(), cross.sell_exchange.get_balance(), ) print("Executable Volume:" + str(executable_volume)) if executable_volume: cross.buy_exchange.market_order(executable_volume, Consts.BID) cross.sell_exchange.market_order(executable_volume, Consts.ASK)
def test_trivial(self): result = arbitrage.detect_cross(self.trivial, self.trivial) result.should.equal(None)