Example #1
0
 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)
Example #2
0
 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]
Example #3
0
 def on_stock_account(self, asset, positions):
     self.ctx.logger.info("asset: {}".format(asset))
     for pos in positions:
         self.ctx.logger.info("pos: {}".format(pos))
     pos_objects = [StockPosition(**object_as_dict(pos)) for pos in positions]
     account = 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)
     self.publish(json.dumps(ledger.message))
     self.ctx.db.dump(ledger)
Example #4
0
 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]