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')
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()
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)
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)
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)
def test_cancel_current_buy_order_should_remove_order_from_buy_orders_and_clear_current_buy_order( self, mock_fetch_order_status, mock_logging): new_order = { 'id': '3f463352-8339-cfbb-3bde-45a63ba43e6c', 'price': 99, 'amount': 20, 'side': 'buy', 'datetime': datetime.datetime.now() } order = holdntrade.Order(new_order) holdntrade.buy_orders = [order] holdntrade.curr_buy_order = order holdntrade.log = mock_logging holdntrade.conf = self.create_default_conf() holdntrade.exchange = ccxt.bitmex mock_fetch_order_status(order.id).return_value = 'open' holdntrade.cancel_current_buy_order() self.assertFalse(holdntrade.curr_buy_order) self.assertFalse(holdntrade.buy_orders) self.assertEqual(0, len(holdntrade.buy_orders))
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)
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))