Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
    def test__build_currency_graph_with_missing_ticker_data(self, currency_graph_mock):
        """
        Test :meth:`ArbitrageStrategy._build_currency_graph`

        Assert `None` is returned when invalid ticker data is detected.
        """

        arbitrage = ArbitrageStrategy()

        trader = MagicMock()

        TEST_PRODUCT = 'BTC-USD'
        trader.products = [TEST_PRODUCT]
        arbitrage.trader = trader

        ticker = {
            TEST_PRODUCT: {
                'bid': 1.0,
            },
        }
        arbitrage.ticker = ticker

        currency_graph = arbitrage._build_currency_graph()

        self.assertEqual(currency_graph, None)
Ejemplo n.º 5
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)
Ejemplo n.º 6
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)
Ejemplo n.º 7
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)
Ejemplo n.º 8
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)
Ejemplo n.º 9
0
    def test__build_currency_graph_with_valid_ticker_data(self, currency_graph_mock):
        """
        Test :meth:`ArbitrageStrategy._build_currency_graph`

        Assert currency pairs for every product are added to the graph and a
        non-`None` value is returned.
        """

        arbitrage = ArbitrageStrategy()

        trader = MagicMock()

        TEST_PRODUCT = 'BTC-USD'
        trader.products = [TEST_PRODUCT]
        arbitrage.trader = trader

        ticker = {
            TEST_PRODUCT: {
                'bid': 1.0,
                'ask': 1.0,
            },
        }
        arbitrage.ticker = ticker

        currency_graph = arbitrage._build_currency_graph()

        calls = []

        for product in ticker:
            base = CurrencyGraph.get_base(product)
            quote = CurrencyGraph.get_quote(product)
            bid = ticker[product]['bid']
            ask = ticker[product]['ask']
            calls.append(call().add_currency_pair(base, quote, bid, ask))

        currency_graph_mock.assert_has_calls(calls, any_order=True)

        self.assertNotEqual(currency_graph, None)
Ejemplo n.º 10
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)