def load(self, ctx, ledger_category, source_id="", account_id="", client_id=""): with session_scope(self.session_factory) as session: asset_obj = session.query(Asset).filter( Asset.source_id == source_id, Asset.account_id == account_id, Asset.client_id == client_id, Asset.ledger_category == int(ledger_category)).first() if not asset_obj: return None else: args = object_as_dict(asset_obj) pos_objs = session.query(Position).filter( Position.source_id == source_id, Position.account_id == account_id, Position.client_id == client_id, Position.ledger_category == int(ledger_category)).all() detail_objs = session.query(PositionDetail).filter( PositionDetail.source_id == source_id, PositionDetail.account_id == account_id, PositionDetail.client_id == client_id, PositionDetail.ledger_category == int( ledger_category)).all() pos_dict = { get_position_uid(pos.instrument_id, pos.exchange_id, pos.direction): pos for pos in pos_objs } positions = [] for uid, pos in pos_dict.items(): if pos.instrument_type == int(InstrumentType.Stock): positions.append(StockPosition(**object_as_dict(pos))) for uid, details in groupby( detail_objs, key=lambda e: get_position_uid( e.instrument_id, e.exchange_id, e.direction)): details = [ FuturePositionDetail(**object_as_dict(detail)) for detail in sorted(details, key=lambda obj: (obj.open_date, obj.trade_time)) ] summary = pos_dict[uid] pos = FuturePosition( instrument_id=summary.instrument_id, exchange_id=summary.exchange_id, margin_ratio=summary.margin_ratio, contract_multiplier=summary.contract_multiplier, realized_pnl=summary.realized_pnl, details=details, trading_day=details[0].trading_day) positions.append(pos) args.update({ "positions": positions, "ledger_category": ledger_category }) return AccountBook(ctx=ctx, **args)
def _get_position(self, instrument_id, exchange_id, direction=Direction.Long): uid = get_position_uid(instrument_id, exchange_id, direction) if uid not in self._positions: instrument_type = get_instrument_type(instrument_id, exchange_id) if instrument_type == InstrumentType.Stock: position = StockPosition(ledger=self, instrument_id=instrument_id, exchange_id=exchange_id, trading_day=self.trading_day) else: instrument_info = self._ctx.get_inst_info(instrument_id) margin_ratio = instrument_info[ "short_margin_ratio"] if direction == Direction.Short else instrument_info[ "long_margin_ratio"] position = FuturePosition( ledger=self, instrument_id=instrument_id, exchange_id=exchange_id, trading_day=self.trading_day, margin_ratio=margin_ratio, contract_multiplier=instrument_info["contract_multiplier"]) self._positions[uid] = position return self._positions[uid]
def on_future_account(self, asset, position_details): pos_objects = [] for uid, details in groupby(position_details, key=lambda e: get_position_uid(e.instrument_id, e.exchange_id, e.direction)): detail_objects = [] instrument_info = None for detail in sorted(details, key=lambda detail: (detail.open_date, detail.trade_time)): args = object_as_dict(detail) if instrument_info is None: instrument_info = self.ctx.db.get_instrument_info(detail.instrument_id) args.update({"contract_multiplier": instrument_info["contract_multiplier"], "long_margin_ratio": instrument_info["long_margin_ratio"], "short_margin_ratio": instrument_info["short_margin_ratio"]}) detail_objects.append(FuturePositionDetail(**args)) pos_args = {"details": detail_objects, "trading_day": self.ctx.trading_day} pos_args.update(instrument_info) pos = FuturePosition(**pos_args) pos_objects.append(pos) account_book = AccountBook(ctx=self.ctx, trading_day=self.ctx.trading_day, ledger_category=LedgerCategory.Account, account_id=asset.account_id, source_id=asset.source_id, avail=asset.avail, positions=pos_objects) ledger = self._get_ledger(ledger_category=LedgerCategory.Account, source_id=asset.source_id,account_id=asset.account_id).merge(account_book) self.publish(json.dumps(ledger.message)) self.ctx.db.dump(ledger)
def _get_position(self, instrument_id, exchange_id): symbol_id = get_symbol_id(instrument_id, exchange_id) if symbol_id not in self._positions: instrument_type = get_instrument_type(instrument_id, exchange_id) if instrument_type == InstrumentType.Stock: position = StockPosition(ledger=self, instrument_id=instrument_id, exchange_id=exchange_id, trading_day=self.trading_day) else: instrument_info = self._ctx.data_proxy.get_instrument_info( instrument_id) position = FuturePosition( ledger=self, instrument_id=instrument_id, exchange_id=exchange_id, trading_day=self.trading_day, long_margin_ratio=instrument_info["short_margin_ratio"], short_margin_ratio=instrument_info["short_margin_ratio"], contract_multiplier=instrument_info["contract_multiplier"]) self._positions[symbol_id] = position return self._positions[symbol_id]