def test_trader_counter() -> None: stat = MagicMock() exchange = MagicMock() account = MagicMock() account.exchange = exchange trade_counter = TradeCounter(stat) exchange.last_price.return_value = 20. order1 = LimitOrder(account=account, order_id=gen_unique_id(), instrument=MagicMock(), quantity=100, price=10) order2 = LimitOrder(account=account, order_id=gen_unique_id(), instrument=MagicMock(), quantity=200, price=20) order3 = MarketOrder(account=account, order_id=gen_unique_id(), instrument=MagicMock(), quantity=100) trade_counter.submit_order(order1) trade_counter.submit_order(order2) trade_counter.submit_order(order3) assert len(trade_counter.open_orders()) == 3 trade_counter.cancel_order(order1.order_id) with pytest.raises(NotImplementedError): trade_counter.amend_order(order2.order_id, 100, 20) assert len(trade_counter.open_orders()) == 2 trade_counter.match(utc_datetime(2018, 1, 1)) assert len(order2.trades) == 1 assert len(order3.trades) == 1 assert len(trade_counter.open_orders()) == 0 trades = [] trades.extend(order2.trades) trades.extend(order3.trades) trade_calls = [call(t) for t in trades] account.deal.assert_has_calls(trade_calls)
def mock_httpinterface() -> Generator[MagicMock, None, None]: m = MagicMock() m.get_instrument_info = CoroutineMock(return_value=[{'lastPrice': 100}]) m.place_limit_order = CoroutineMock(return_value=gen_unique_id()) m.place_market_order = CoroutineMock(return_value=gen_unique_id()) resp = MagicMock() resp.status = 200 m.amend_order = CoroutineMock(return_value=resp) m.cancel_order = CoroutineMock(return_value=resp) m.open_orders_http = CoroutineMock(return_value=[]) with open(get_resource_path('bitmex/active_instrument.json')) as f: instruments = json.load(f) m.active_instruments = CoroutineMock(return_value=instruments) with open(get_resource_path('bitmex/kline_data.json')) as f: klines = json.load(f) m.get_kline = CoroutineMock(return_value=klines) trades = json.loads("""[ { "timestamp": "2019-03-02T02:38:58.304Z", "symbol": "XBTUSD", "side": "Sell", "size": 1297, "price": 3828.5, "tickDirection": "ZeroMinusTick", "trdMatchID": "8260edc2-26d6-e8b7-616a-1d5c21ffa37f", "grossValue": 33877640, "homeNotional": 0.3387764, "foreignNotional": 1297 }]""") m.get_recent_trades = CoroutineMock(return_value=trades) yield m
def match(self, match_time: datetime.datetime) -> None: close_order_ids = [] for order in self._open_orders.values(): if isinstance(order, MarketOrder): trade = Trade(order, order.account.exchange.last_price(order.instrument), order.quantity, gen_unique_id(), match_time) elif isinstance(order, LimitOrder): trade = Trade(order, order.price, order.quantity, gen_unique_id(), match_time) else: raise ImpossibleError("Unsupported order type {}".format(type(order))) self.stat.collect_trade(trade) logger.debug("Trade counter match a trade {}".format(trade)) order.deal(trade) if order.remain_quantity == 0: close_order_ids.append(order.order_id) for order_id in close_order_ids: self._open_orders.pop(order_id)
def test_base_order_to_dict() -> None: instrument = MagicMock() instrument.symbol = "XBTUSD" order = BaseOrder(MagicMock(), gen_unique_id(), instrument, 100) d_order = base_order_to_dict(order) assert d_order == { 'order_id': order.order_id, 'quantity': 100, 'traded_quantity': 0, 'trades': [], 'symbol': "XBTUSD" }
def tests_limit_order_to_dict() -> None: instrument = MagicMock() instrument.symbol = "XBTUSD" order = LimitOrder(MagicMock(), gen_unique_id(), instrument, 100, price=10) d_order = limit_order_to_dict(order) assert d_order == { 'order_id': order.order_id, 'quantity': 100, 'traded_quantity': 0, 'trades': [], 'symbol': "XBTUSD", 'price': 10, 'order_type': 'limit' }
async def place_market_order(self, account: FutureAccount, instrument: FutureInstrument, quantity: float, text: str = '') -> str: if isinstance(instrument, FutureInstrument): order = FutureMarketOrder(account=account, instrument=instrument, quantity=quantity, order_id=gen_unique_id(), submit_datetime=self.context.now, text=text) else: raise NotImplementedError() self._trade_counter.submit_order(order) return order.order_id