def test_several_filled_orders_at_the_same_time(self): order_1 = Order(order_type='Limit', side='Sell', qty=228, price=1000) order_1.order_id = 123456 order_2 = Order(order_type='Limit', side='Sell', qty=229, price=1001) order_2.order_id = 123457 order_3 = Order(order_type='Limit', side='Sell', qty=2210, price=1002) order_3.order_id = 123458 order_4 = Order(order_type='Limit', side='Sell', qty=2211, price=1003) order_4.order_id = 123459 callback_1 = Mock() callback_2 = Mock() callback_3 = Mock() callback_4 = Mock() order_1._on_fill = callback_1 order_2._on_fill = callback_2 order_3._on_fill = callback_3 order_4._on_fill = callback_4 self.supervisor.add_order(order_1) self.supervisor.add_order(order_2) self.supervisor.add_order(order_3) self.supervisor.add_order(order_4) self.exchange_mock.get_order_status_ws.return_value = 'Filled' self.supervisor.run_cycle() sleep(1) callback_1.assert_called_once() callback_2.assert_called_once() callback_3.assert_called_once() callback_4.assert_called_once() self.assertListEqual([], self.supervisor._orders)
def test_filled_callback_will_be_called(self): order = Order(order_type='Limit', side='Sell', qty=228, price=1000) order.order_id = 123456 callback = Mock() order._on_fill = callback self.supervisor.add_order(order) self.exchange_mock.get_order_status_ws.return_value = 'Filled' self.supervisor.run_cycle() sleep(1) callback.assert_called_once() self.assertListEqual([], self.supervisor._orders)
def test_check_filled_stop_order(self): def order_status_mock(_order): if _order == order: return 'Triggered' on_filled_mock = Mock() self.exchange_mock.get_order_status_ws.side_effect = order_status_mock order = Order(order_type='Stop', qty=228, stop_px=1000, side='Buy') order.order_id = '1234' order._on_fill = on_filled_mock self.supervisor.add_order(order) self.supervisor.check_needed_orders() # assert that we didn`t place this order self.exchange_mock.place_order.not_called(order) # assert that Supervisor forget this order self.assertNotIn(order, self.supervisor.orders) # assert that Supervisor call matching callback on_filled_mock.assert_called_once()
# you can disable managing position or orders by set to False needed properties # supervisor.manage_position = False # supervisor.manage_orders = False # create Order objects stop_loss = Order(order_type='Stop', stop_px=2000, qty=10, side='Sell') tp1 = Order(order_type='Limit', price=15000, qty=6, side='Sell', passive=True) tp2 = Order(order_type='Limit', price=20000, qty=4, side='Sell', hidden=True) trailing_stop = Order(order_type='Stop', stop_px=3000, qty=10, side='Sell') # attach some callbacks to stop-loss, note that events starts with "_" # DO NOT USE stop_cycle() method in callbacks!!! It causes deadlock stop_loss._on_reject = lambda: print('Rejected') stop_loss._on_fill = lambda: print('We lost position(') input('Enter to run cycle') supervisor.run_cycle() input('Enter to add orders to needed') supervisor.add_order(stop_loss) supervisor.add_order(tp1) supervisor.add_order(tp2) supervisor.add_trailing_order(trailing_stop, offset=10) # order will trail on 10% distance from current price input('Enter to enter position') supervisor.stop_cycle() supervisor.enter_by_market_order(10) supervisor.run_cycle() input('Enter to exit cycle') supervisor.exit_cycle()