Esempio n. 1
0
    def test_connect_to_exchange_should_fail_if_param_test_is_true_but_not_supported(
            self, mock_logging):
        holdntrade.log = mock_logging
        conf = holdntrade.ExchangeConfig
        conf.exchange = 'kraken'
        conf.api_key = 'key'
        conf.api_secret = 'secret'
        conf.test = True

        with self.assertRaises(SystemExit) as context:
            holdntrade.connect_to_exchange(conf)

        self.assertTrue('Test not supported' in str(context.exception))
Esempio n. 2
0
    def test_connect_to_exchange_should_fail_if_exchange_not_supported(
            self, mock_logging):
        holdntrade.log = mock_logging
        conf = holdntrade.ExchangeConfig
        conf.exchange = 'unknown'
        conf.api_key = 'key'
        conf.api_secret = 'secret'
        conf.test = False

        with self.assertRaises(Exception) as context:
            holdntrade.connect_to_exchange(conf)

        self.assertTrue('unknown' in str(context.exception))
Esempio n. 3
0
    def test_buy_executed_first_run(self, mock_create_limit_sell_order,
                                    mock_create_limit_buy_order,
                                    mock_fetch_balance, mock_fetch_ticker,
                                    mock_set_initial_leverage, mock_logging):
        holdntrade.conf = self.create_default_conf()
        holdntrade.conf.base = 'BTC'
        holdntrade.log = mock_logging
        holdntrade.exchange = holdntrade.connect_to_exchange(holdntrade.conf)
        market_price = 9000
        mock_fetch_ticker.return_value = {'bid': market_price}
        mock_fetch_balance.return_value = {
            'BTC': {
                'used': 300,
                'free': 300,
                'total': 600
            }
        }
        price = 9999
        buy_price = round(price * (1 - holdntrade.conf.change))
        sell_price = round(price * (1 + holdntrade.conf.change))

        holdntrade.buy_executed(price, 200)

        mock_set_initial_leverage.assert_called()
        self.assertTrue(holdntrade.initial_leverage_set)
        mock_create_limit_sell_order.assert_called_with(
            holdntrade.conf.pair, 200, sell_price)
        mock_create_limit_buy_order.assert_called_with(holdntrade.conf.pair,
                                                       200, buy_price)
Esempio n. 4
0
    def test_sell_executed_still_open(self, mock_fetch_order_status,
                                      mock_logging):
        holdntrade.conf = self.create_default_conf()
        holdntrade.log = mock_logging
        holdntrade.exchange = holdntrade.connect_to_exchange(holdntrade.conf)
        holdntrade.sell_orders = [
            holdntrade.Order({
                'side': 'sell',
                'id': '1s',
                'price': 10000,
                'amount': 10,
                'datetime': datetime.datetime.today().isoformat()
            }),
            holdntrade.Order({
                'side': 'sell',
                'id': '2s',
                'price': 15000,
                'amount': 10,
                'datetime': datetime.datetime.today().isoformat()
            })
        ]

        return_values = {'1s': 'open', '2s': 'open'}
        mock_fetch_order_status.side_effect = return_values.get

        holdntrade.sell_executed(8888, 99)

        mock_logging.debug.assert_called_with('Sell still open')
Esempio n. 5
0
    def test_cancel_orders(self, mock_fetch_order_status, mock_cancel_order,
                           mock_logging):
        holdntrade.conf = self.create_default_conf()
        holdntrade.log = mock_logging
        holdntrade.exchange = holdntrade.connect_to_exchange(holdntrade.conf)

        orders = [
            holdntrade.Order({
                'side': 'sell',
                'id': '1s',
                'price': 10000,
                'amount': 10,
                'datetime': datetime.datetime.today().isoformat()
            }),
            holdntrade.Order({
                'side': 'sell',
                'id': '2s',
                'price': 15000,
                'amount': 10,
                'datetime': datetime.datetime.today().isoformat()
            })
        ]

        return_values = {'1s': 'open', '2s': 'canceled'}
        mock_fetch_order_status.side_effect = return_values.get
        holdntrade.cancel_orders(orders)

        mock_logging.debug.assert_called()
        mock_logging.warning.assert_called_with('Cancel %s was in state %s',
                                                str(orders[1]), 'canceled')
        mock_cancel_order.assert_called()
Esempio n. 6
0
    def test_connect_to_exchange_params_liquid(self, mock_logging):
        holdntrade.log = mock_logging
        conf = holdntrade.ExchangeConfig
        conf.exchange = 'liquid'
        conf.api_key = 'key'
        conf.api_secret = 'secret'
        conf.test = False

        exchange = holdntrade.connect_to_exchange(conf)

        self.assertEqual(conf.exchange, exchange.id)
        self.assertEqual(conf.api_key, exchange.apiKey)
        self.assertEqual(conf.api_secret, exchange.secret)
Esempio n. 7
0
    def test_market_sell_order_bitmex(self, mock_create_market_sell_order,
                                      mock_fetch_ticker, mock_logging):
        holdntrade.conf = self.create_default_conf()
        holdntrade.conf.base = 'BTC'
        holdntrade.log = mock_logging
        holdntrade.exchange = holdntrade.connect_to_exchange(holdntrade.conf)
        market_price = 9000
        mock_fetch_ticker.return_value = {'bid': market_price}

        holdntrade.create_market_sell_order(0.01)

        mock_create_market_sell_order.assert_called_with(
            holdntrade.conf.pair, round(0.01 * market_price))
        mock_logging.info.assert_called()
Esempio n. 8
0
    def test_connect_to_exchange_params_bitmex(self, mock_logging):
        holdntrade.log = mock_logging
        conf = holdntrade.ExchangeConfig
        conf.exchange = 'bitmex'
        conf.api_key = 'key'
        conf.api_secret = 'secret'
        conf.test = True

        exchange = holdntrade.connect_to_exchange(conf)

        self.assertEqual(conf.exchange, exchange.id)
        self.assertEqual(conf.api_key, exchange.apiKey)
        self.assertEqual(conf.api_secret, exchange.secret)
        self.assertEqual(exchange.urls['test'], exchange.urls['api'])
Esempio n. 9
0
    def test_get_margin_balance(self, mock_fetch_balance, mock_logging):
        holdntrade.conf = self.create_default_conf()
        holdntrade.conf.base = 'BTC'
        holdntrade.log = mock_logging
        holdntrade.exchange = holdntrade.connect_to_exchange(holdntrade.conf)

        mock_fetch_balance.return_value = {
            holdntrade.conf.base: {
                'free': 100,
                'total': 150
            }
        }
        holdntrade.get_margin_balance()

        mock_fetch_balance.assert_called()
Esempio n. 10
0
    def test_buy_executed_regular(self, mock_create_limit_sell_order,
                                  mock_create_limit_buy_order,
                                  mock_fetch_order_status, mock_fetch_balance,
                                  mock_fetch_ticker, mock_set_initial_leverage,
                                  mock_logging):
        holdntrade.conf = self.create_default_conf()
        holdntrade.conf.base = 'BTC'
        holdntrade.log = mock_logging
        holdntrade.exchange = holdntrade.connect_to_exchange(holdntrade.conf)
        holdntrade.initial_leverage_set = True
        holdntrade.curr_buy_order = holdntrade.Order({
            'side':
            'buy',
            'id':
            '1B',
            'price':
            15000,
            'amount':
            222,
            'datetime':
            datetime.datetime.today().isoformat()
        })
        holdntrade.buy_orders.append(holdntrade.curr_buy_order)

        holdntrade.curr_buy_order_size = 222
        market_price = 9000
        mock_fetch_ticker.return_value = {'bid': market_price}
        mock_fetch_balance.return_value = {
            'BTC': {
                'used': 300,
                'free': 400,
                'total': 700
            }
        }
        mock_fetch_order_status.return_value = 'closed'
        price = 9999
        buy_price = round(price * (1 - holdntrade.conf.change))
        sell_price = round(price * (1 + holdntrade.conf.change))

        holdntrade.buy_executed(price, 100)

        mock_logging.debug.assert_called()
        mock_logging.warning.assert_not_called()
        mock_set_initial_leverage.assert_not_called()
        mock_create_limit_sell_order.assert_called_with(
            holdntrade.conf.pair, 222, sell_price)
        mock_create_limit_buy_order.assert_called_with(holdntrade.conf.pair,
                                                       100, buy_price)
Esempio n. 11
0
    def test_calculate_used_margin_percentage_with_fetch(
            self, mock_fetch_balance, mock_logging):
        holdntrade.conf = self.create_default_conf()
        holdntrade.conf.base = 'BTC'
        holdntrade.log = mock_logging
        holdntrade.exchange = holdntrade.connect_to_exchange(holdntrade.conf)

        mock_fetch_balance.return_value = {
            holdntrade.conf.base: {
                'free': 20,
                'total': 100
            }
        }

        percentage = holdntrade.calculate_used_margin_percentage()

        mock_fetch_balance.assert_called()
        self.assertTrue(percentage == 80)
Esempio n. 12
0
    def test_get_balance(self, mock_fetch_balance, mock_logging):
        holdntrade.conf = self.create_default_conf()
        holdntrade.conf.base = 'BTC'
        holdntrade.log = mock_logging
        holdntrade.exchange = holdntrade.connect_to_exchange(holdntrade.conf)
        mock_fetch_balance.return_value = {
            'BTC': {
                'used': None,
                'free': None,
                'total': 100
            }
        }

        balance = holdntrade.get_balance()

        self.assertEqual(0, balance['used'])
        self.assertEqual(0, balance['free'])
        self.assertEqual(100, balance['total'])
Esempio n. 13
0
    def test_sell_executed(self, mock_create_limit_buy_order,
                           mock_fetch_order_status, mock_fetch_ticker,
                           mock_fetch_balance, mock_logging):
        holdntrade.conf = self.create_default_conf()
        holdntrade.log = mock_logging
        holdntrade.exchange = holdntrade.connect_to_exchange(holdntrade.conf)
        holdntrade.sell_orders = [
            holdntrade.Order({
                'side': 'sell',
                'id': '1s',
                'price': 10000,
                'amount': 10,
                'datetime': datetime.datetime.today().isoformat()
            }),
            holdntrade.Order({
                'side': 'sell',
                'id': '2s',
                'price': 15000,
                'amount': 10,
                'datetime': datetime.datetime.today().isoformat()
            })
        ]

        mock_fetch_balance.return_value = {
            'BTC': {
                'used': 300,
                'free': 300,
                'total': 600
            }
        }
        return_values = {'1s': 'closed', '2s': 'open'}
        mock_fetch_order_status.side_effect = return_values.get
        market_price = 9000
        mock_fetch_ticker.return_value = {'bid': market_price}
        price = 8888
        buy_price = round(price * (1 - holdntrade.conf.change))

        holdntrade.sell_executed(price, 99)

        mock_logging.info.assert_called()
        mock_create_limit_buy_order.assert_called_with(holdntrade.conf.pair,
                                                       99, buy_price)
Esempio n. 14
0
    def test_calculate_all_sold_balance(self, mock_logging):
        holdntrade.conf = self.create_default_conf()
        holdntrade.log = mock_logging
        holdntrade.exchange = holdntrade.connect_to_exchange(holdntrade.conf)
        poi = {'homeNotional': 0.464}
        orders = [
            holdntrade.Order({
                'side': 'sell',
                'id': '1',
                'price': 40000,
                'amount': 4444,
                'datetime': datetime.datetime.today().isoformat()
            })
        ]
        wallet_balance = 0.1995
        margin_balance = 0.1166
        net_deposits = 0.2

        all_sold_balance = holdntrade.calculate_all_sold_balance(
            poi, orders, wallet_balance, margin_balance, net_deposits)

        self.assertAlmostEqual(0.47, all_sold_balance, 2)
Esempio n. 15
0
    def test_create_divided_sell_order(self, mock_create_limit_sell_order,
                                       mock_get_used_balance, mock_logging):
        holdntrade.conf = self.create_default_conf()
        holdntrade.conf.base = 'BTC'
        holdntrade.log = mock_logging
        holdntrade.exchange = holdntrade.connect_to_exchange(holdntrade.conf)
        mock_get_used_balance.return_value = 10000
        holdntrade.sell_price = 11110
        order = {
            'side': 'sell',
            'id': '1s',
            'price': holdntrade.sell_price,
            'amount': round(10000 / holdntrade.conf.quota),
            'datetime': datetime.datetime.today().isoformat()
        }
        mock_create_limit_sell_order.return_value = order

        holdntrade.create_divided_sell_order()

        mock_logging.info.assert_called_with('Created %s',
                                             str(holdntrade.Order(order)))
        mock_create_limit_sell_order.assert_called_with(
            holdntrade.conf.pair, round(10000 / holdntrade.conf.quota),
            holdntrade.sell_price)
Esempio n. 16
0
    def test_spread_should_cancel_highest_buy_order_and_create_a_new_sell_and_buy_order(
            self, mock_create_limit_sell_order, mock_create_limit_buy_order,
            mock_fetch_order_status, mock_fetch_ticker, mock_fetch_balance,
            mock_cancel_order, mock_logging):
        holdntrade.conf = self.create_default_conf()
        holdntrade.conf.base = 'BTC'
        buy1 = holdntrade.Order({
            'id': '1',
            'price': 100,
            'amount': 101,
            'side': 'buy',
            'datetime': datetime.datetime.now()
        })
        buy2 = holdntrade.Order({
            'id': '2',
            'price': 200,
            'amount': 102,
            'side': 'buy',
            'datetime': datetime.datetime.now()
        })
        holdntrade.buy_orders = [buy1, buy2]
        sell1 = holdntrade.Order({
            'id': '3',
            'price': 400,
            'amount': 103,
            'side': 'sell',
            'datetime': datetime.datetime.now()
        })
        sell2 = holdntrade.Order({
            'id': '4',
            'price': 500,
            'amount': 104,
            'side': 'sell',
            'datetime': datetime.datetime.now()
        })
        holdntrade.sell_orders = [sell1, sell2]
        holdntrade.log = mock_logging
        market_price = 300
        return_values = {'2': 'open'}
        mock_fetch_order_status.side_effect = return_values.get
        mock_fetch_ticker.return_value = {'bid': market_price}
        mock_fetch_balance.return_value = {
            'BTC': {
                'used': 300,
                'free': 200,
                'total': 500
            }
        }
        buy_price = round(market_price * (1 - holdntrade.conf.change))
        sell_price = round(market_price * (1 + holdntrade.conf.change))
        holdntrade.exchange = holdntrade.connect_to_exchange(holdntrade.conf)

        holdntrade.spread(market_price)

        mock_fetch_order_status.assert_called_with(buy2.id)
        mock_cancel_order.assert_called_with(buy2.id)
        mock_create_limit_buy_order.assert_called_with('BTC/USD', 102,
                                                       buy_price)
        mock_create_limit_sell_order.assert_called_with(
            'BTC/USD', 102, sell_price)
        self.assertEqual(2, len(holdntrade.buy_orders))
        self.assertEqual(buy_price, holdntrade.buy_price)
        self.assertEqual(3, len(holdntrade.sell_orders))