def test__place_order_sell_signal(self): """ Test :meth:`ArbitrageStrategy._place_order` Assert a sell order is placed and `True` is returned. """ arbitrage = ArbitrageStrategy() TEST_PRICE = 1.0 arbitrage._get_market_price = MagicMock(return_value=TEST_PRICE) arbitrage.get_currency_balance = MagicMock() trader = MagicMock() arbitrage.trader = trader TEST_SIGNAL = CurrencyGraph.SELL_ORDER TEST_PRODUCT = 'BTC-USD' TEST_DISTANCE = 2.0 success = arbitrage._place_order(TEST_SIGNAL, TEST_PRODUCT, TEST_DISTANCE) self.assertEqual(trader.buy.called, 0) self.assertEqual(trader.sell.called, 1) self.assertTrue(success)
def test__place_order_invalid_signal(self): """ Test :meth:`ArbitrageStrategy._place_order` Assert that no orders are placed and `False` is returned. """ arbitrage = ArbitrageStrategy() TEST_PRICE = 1.0 arbitrage._get_market_price = MagicMock(return_value=TEST_PRICE) arbitrage.get_currency_balance = MagicMock() trader = MagicMock() arbitrage.trader = trader INVALID_SIGNAL = 3 TEST_PRODUCT = 'BTC-USD' TEST_DISTANCE = 2.0 success = arbitrage._place_order(INVALID_SIGNAL, TEST_PRODUCT, TEST_DISTANCE) self.assertEqual(trader.buy.called, 0) self.assertEqual(trader.sell.called, 0) self.assertFalse(success)
def test__update_pending_order_with_change(self): """ Test :meth:`ArbitrageStrategy._update_pending_order` Assert :meth:`ArbitrageStrategy._cancel_order` is called and `True` is returned. """ arbitrage = ArbitrageStrategy() TEST_PRODUCT = 'BTC-USD' TEST_PRICE = 1.0 arbitrage.order = { 'product_id': TEST_PRODUCT, 'price': TEST_PRICE, } NEW_PRICE = 2.0 arbitrage._get_market_price = MagicMock(return_value=NEW_PRICE) cancel_order = MagicMock() arbitrage._cancel_order = cancel_order success = arbitrage._update_pending_order(CurrencyGraph.BUY_ORDER, TEST_PRODUCT) self.assertEqual(cancel_order.called, 1) self.assertTrue(success)
def test__place_order_below_threshold(self): """ Test :meth:`ArbitrageStrategy._place_order` Assert that no orders are placed and `False` is returned. """ arbitrage = ArbitrageStrategy() TEST_PRICE = 1.0 arbitrage._get_market_price = MagicMock(return_value=TEST_PRICE) arbitrage.get_currency_balance = MagicMock() trader = MagicMock() arbitrage.trader = trader TEST_SIGNAL = CurrencyGraph.BUY_ORDER TEST_PRODUCT = 'BTC-USD' TEST_DISTANCE = 1.0 success = arbitrage._place_order(TEST_SIGNAL, TEST_PRODUCT, TEST_DISTANCE) self.assertEqual(trader.buy.called, 0) self.assertEqual(trader.sell.called, 0) self.assertFalse(success)
def test__get_market_price_with_invalid_ticker_data(self): """ Test :meth:`ArbitrageStrategy._get_market_price` Assert the returned market price is the same as the current order price. """ arbitrage = ArbitrageStrategy() TEST_PRICE = 1.0 arbitrage.order = { 'price': TEST_PRICE, } TEST_PRODUCT = 'USD' TEST_BID = 2.0 TEST_ASK = 3.0 arbitrage.ticker = { TEST_PRODUCT: { 'error': 'Invalid ticker data.', }, } market_price = arbitrage._get_market_price(CurrencyGraph.BUY_ORDER, TEST_PRODUCT) self.assertEqual(market_price, TEST_PRICE)
def test__get_market_price_with_sell_signal(self): """ Test :meth:`ArbitrageStrategy._get_market_price` Assert the returned market price is the ticker ask price. """ arbitrage = ArbitrageStrategy() TEST_PRICE = 1.0 arbitrage.order = { 'price': TEST_PRICE, } TEST_PRODUCT = 'USD' TEST_BID = 2.0 TEST_ASK = 3.0 arbitrage.ticker = { TEST_PRODUCT: { 'bid': TEST_BID, 'ask': TEST_ASK, }, } market_price = arbitrage._get_market_price(CurrencyGraph.SELL_ORDER, TEST_PRODUCT) self.assertEqual(market_price, TEST_ASK)
def test__get_market_price_with_no_order_and_no_signal(self): """ Test :meth:`ArbitrageStrategy._get_market_price` Assert the returned market price is `None`. """ arbitrage = ArbitrageStrategy() arbitrage.order = None TEST_PRODUCT = 'USD' TEST_BID = 2.0 TEST_ASK = 3.0 arbitrage.ticker = { TEST_PRODUCT: { 'bid': TEST_BID, 'ask': TEST_ASK, }, } INVALID_SIGNAL = 3 market_price = arbitrage._get_market_price(INVALID_SIGNAL, TEST_PRODUCT) self.assertEqual(market_price, None)
def test__get_market_price_with_no_signal(self): """ Test :meth:`ArbitrageStrategy._get_market_price` Assert the returned market price is the same as the current order price. """ arbitrage = ArbitrageStrategy() TEST_PRICE = 1.0 arbitrage.order = { 'price': TEST_PRICE, } TEST_PRODUCT = 'USD' TEST_BID = 2.0 TEST_ASK = 3.0 arbitrage.ticker = { TEST_PRODUCT: { 'bid': TEST_BID, 'ask': TEST_ASK, }, } INVALID_SIGNAL = 3 market_price = arbitrage._get_market_price(INVALID_SIGNAL, TEST_PRODUCT) self.assertEqual(market_price, TEST_PRICE)
def test__update_pending_order_without_order(self): """ Test :meth:`ArbitrageStrategy._update_pending_order` Assert :meth:`ArbitrageStrategy._cancel_order` is not called and `False` is returned. """ arbitrage = ArbitrageStrategy() TEST_PRODUCT = 'BTC-USD' TEST_PRICE = 1.0 arbitrage.order = None arbitrage._get_market_price = MagicMock(return_value=TEST_PRICE) cancel_order = MagicMock() arbitrage._cancel_order = cancel_order success = arbitrage._update_pending_order(CurrencyGraph.BUY_ORDER, TEST_PRODUCT) self.assertEqual(cancel_order.called, 0) self.assertFalse(success)