示例#1
0
    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)
示例#2
0
    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)
示例#3
0
    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)
示例#4
0
    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)
示例#5
0
    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)
示例#6
0
    def test__track_order__that_has_been_filled(self):
        """
        Test :meth:`ArbitrageStrategy._track_order`

        Assert the current order data is cleared and `False` is returned.
        """

        arbitrage = ArbitrageStrategy()

        test_order = {
            'id': 1,
        }
        arbitrage.order = test_order

        trader = MagicMock()

        test_order_update = {
            'id': 2,
            'status': 'done',
            'settled': True,
        }
        trader.get_order.return_value = test_order_update

        arbitrage.trader = trader

        success = arbitrage._track_order()

        self.assertFalse(success)
        self.assertEqual(arbitrage.order, None)
示例#7
0
    def test__track_order_success(self):
        """
        Test :meth:`ArbitrageStrategy._track_order`

        Assert the current order data is updated and `True` is returned.
        """

        arbitrage = ArbitrageStrategy()

        test_order = {
            'id': 1,
        }
        arbitrage.order = test_order

        trader = MagicMock()

        test_order_update = {
            'id': 2,
        }
        trader.get_order.return_value = test_order_update

        arbitrage.trader = trader

        success = arbitrage._track_order()

        self.assertTrue(success)
        self.assertEqual(arbitrage.order, test_order_update)
示例#8
0
    def test__cancel_order_without_order(self):
        """
        Test :meth:`ArbitrageStrategy._cancel_order`

        Assert `False` is returned.
        """

        arbitrage = ArbitrageStrategy()

        arbitrage.order = None

        trader = MagicMock()
        arbitrage.trader = trader

        success = arbitrage._cancel_order()

        self.assertFalse(success)
示例#9
0
    def test__cancel_order_with_valid_order(self):
        """
        Test :meth:`ArbitrageStrategy._cancel_order`

        Assert :meth:`GDAXTrader.cancel_order` is called with current order ID
        and `True` is returned.
        """

        arbitrage = ArbitrageStrategy()

        TEST_ID = 1
        arbitrage.order = {
            'order_id': TEST_ID,
        }

        trader = MagicMock()
        arbitrage.trader = trader

        success = arbitrage._cancel_order()

        trader.cancel_order.assert_called_with(TEST_ID)
        self.assertTrue(success)
示例#10
0
    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)
示例#11
0
    def test__track_order_with_no_current_order(self):
        """
        Test :meth:`ArbitrageStrategy._track_order`

        Assert `False` is returned.
        """

        arbitrage = ArbitrageStrategy()

        arbitrage.order = None

        trader = MagicMock()

        test_order_update = {
            'id': 2,
        }
        trader.get_order.return_value = test_order_update

        arbitrage.trader = trader

        success = arbitrage._track_order()

        self.assertFalse(success)
        self.assertEqual(arbitrage.order, None)