def on_order(self, event, order): self.ctx.logger.info('on order %s from %s', order, self.get_location(event.dest).uname) order_dict = to_dict(order) self.data_proxy.add_order(**order_dict) self.publish( json.dumps({ "msg_type": int(MsgType.Order), "data": order_dict }))
def on_trade(self, event, trade): self.ctx.logger.info('on trade %s from %s', trade, self.get_location(event.dest).uname) client_id = self.get_location(event.dest).name trade_dict = to_dict(trade) trade_dict["client_id"] = client_id self.data_proxy.add_trade(**trade_dict) self.publish(json.dumps({"msg_type": int(MsgType.Trade), "data": trade_dict})) if trade.account_id in self.accounts: self.accounts[trade.account_id].apply_trade(trade) self._get_portfolio_ledger(client_id).apply_trade(trade)
def on_trade(self, event, trade): self.ctx.logger.info('on trade %s', trade) trade_dict = to_dict(trade) self.data_proxy.add_trade(**trade_dict) self.publish( json.dumps({ "msg_type": int(MsgType.Trade), "data": trade_dict })) if trade.account_id in self.accounts: self.accounts[trade.account_id].apply_trade(trade)
def on_assets(self, asset_info, positions): self.ctx.logger.info("on assets, acc: %s", asset_info.account_id) for pos in positions: self.ctx.logger.info("on assets, pos: %s", pos) account = Ledger(trading_day=self.trading_day, ledger_category=asset_info.ledger_category, account_id=asset_info.account_id, source_id=asset_info.source_id, avail=asset_info.avail, positions={ get_symbol_id(pos.instrument_id, pos.exchange_id): StockPosition(**to_dict(pos)) for pos in positions }) self.accounts[asset_info.account_id] = account account.register_callback( lambda messages: [self.publish(json.dumps(message)) for message in messages]) account.register_callback(self.ledger_holder.on_messages)
def on_assets(self, account_info, positions): self.ctx.logger.info("on assets, acc: %s", account_info.account_id) for pos in positions: self.ctx.logger.info("on assets, pos: %s", pos) account = Ledger(avail=account_info.avail, positions={ get_symbol_id(pos.instrument_id, pos.exchange_id): StockPosition(**to_dict(pos)) for pos in positions }) self.accounts[account_info.account_id] = account self.data_proxy.save_account( account_info.account_id, "xtp", account.message, [pos.message for pos in account.positions]) self.publish( json.dumps({ "msg_type": int(MsgType.AccountInfo), "data": { **{ "account_id": account_info.account_id, "source_id": "xtp" }, **account.message } })) for pos in account.positions: self.publish( json.dumps({ "msg_type": int(MsgType.Position), "data": { **{ "account_id": account_info.account_id, "source_id": "xtp" }, **pos.message } }))