コード例 #1
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)
コード例 #2
0
    def test__set_current_node_with_valid_balance(self):
        """
        Test :meth:`ArbitrageStrategy._set_current_node`

        Assert :attr:`ArbitrageStrategy.current_node` is set to the first
        account currency with a positive balance.
        """

        arbitrage = ArbitrageStrategy()

        UNCHANGED_NODE = 'FAKE'
        arbitrage.current_node = UNCHANGED_NODE

        TEST_CURRENCY = 'USD'
        TEST_2ND_CURRENCY = 'BTC'
        TEST_BALANCE = 1.0

        arbitrage.accounts = [
            {
                'currency': TEST_CURRENCY,
                'balance': TEST_BALANCE,
            }, {
                'currency': TEST_2ND_CURRENCY,
                'balance': TEST_BALANCE,
            }
        ]

        arbitrage.ticker = {}

        arbitrage._set_current_node()

        self.assertEqual(arbitrage.current_node, TEST_CURRENCY)
コード例 #3
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)
コード例 #4
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)
コード例 #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__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)
コード例 #7
0
    def test__set_current_node_with_empty_balances(self):
        """
        Test :meth:`ArbitrageStrategy._set_current_node`

        Assert :attr:`ArbitrageStrategy.current_node` is unchanged.
        """

        arbitrage = ArbitrageStrategy()

        UNCHANGED_NODE = 'FAKE'
        arbitrage.current_node = UNCHANGED_NODE

        TEST_CURRENCY = 'USD'
        TEST_2ND_CURRENCY = 'BTC'
        TEST_BALANCE = 0.0

        arbitrage.accounts = [
            {
                'currency': TEST_CURRENCY,
                'balance': TEST_BALANCE,
            }, {
                'currency': TEST_2ND_CURRENCY,
                'balance': TEST_BALANCE,
            }
        ]

        arbitrage.ticker = {}

        arbitrage._set_current_node()

        self.assertEqual(arbitrage.current_node, UNCHANGED_NODE)
コード例 #8
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)
コード例 #9
0
    def test__set_current_node_with_invalid_account_data(self):
        """
        Test :meth:`ArbitrageStrategy._set_current_node`

        Assert :attr:`ArbitrageStrategy.current_node` is unchanged.
        """

        arbitrage = ArbitrageStrategy()

        UNCHANGED_NODE = 'FAKE'
        arbitrage.current_node = UNCHANGED_NODE

        TEST_CURRENCY = 'USD'
        TEST_BALANCE = 1.0

        arbitrage.accounts = [
            { 'error': 'Invalid account data.', }
        ]

        arbitrage.ticker = {}

        arbitrage._set_current_node()

        self.assertEqual(arbitrage.current_node, UNCHANGED_NODE)