Ejemplo n.º 1
0
 def __init__(self, ctx, location, **kwargs):
     pywingchun.Book.__init__(self)
     self.ctx = ctx
     self.location = location
     self.tags = AccountBookTags.make_from_location(self.location)
     self.subject = Subject()
     self.setup_trading_day(kwargs.pop("trading_day", None))
     self.initial_equity = kwargs.pop("initial_equity", 0.0)
     self.static_equity = kwargs.pop("static_equity", 0.0)
     self.avail = kwargs.pop("avail", 0.0)
     self.realized_pnl = kwargs.pop("realized_pnl", 0.0)
     self._positions = {}
     positions = kwargs.pop("positions", [])
     for pos in positions:
         if isinstance(pos, pywingchun.Position):
             pos = msg_utils.object_as_dict(pos)
         if isinstance(pos, dict):
             try:
                 pos = Position.factory(ctx=self.ctx, book=self, **pos)
             except Exception as err:
                 exc_type, exc_obj, exc_tb = sys.exc_info()
                 self.ctx.logger.error('init position from dict %s, error [%s] %s', pos, exc_type, traceback.format_exception(exc_type, exc_obj, exc_tb))
                 continue
         if isinstance(pos, Position):
             self._positions[pos.uid] = pos
         else:
             raise TypeError("Position object required, but {} provided".format(type(pos)))
Ejemplo n.º 2
0
 def on_positions(self, positions):
     self.ctx.logger.debug(
         "{} [{:08x}] position report received, size: {}".format(
             self.location.uname, self.location.uid, len(positions)))
     for pos in positions:
         self.ctx.logger.info(pos)
     self._positions = {}
     for pos in positions:
         if isinstance(pos, pywingchun.Position):
             pos = msg_utils.object_as_dict(pos)
         if isinstance(pos, dict):
             try:
                 pos = Position.factory(ctx=self.ctx, book=self, **pos)
             except Exception as err:
                 exc_type, exc_obj, exc_tb = sys.exc_info()
                 self.ctx.logger.error(
                     'init position from dict %s, error [%s] %s', pos,
                     exc_type,
                     traceback.format_exception(exc_type, exc_obj, exc_tb))
                 continue
         if isinstance(pos, Position):
             self._positions[pos.uid] = pos
         else:
             raise TypeError(
                 "Position object required, but {} provided".format(
                     type(pos)))
     if self.ctx.name == "ledger":
         self.subject.on_next(self.event)
         for pos in self.positions:
             self.subject.on_next(pos.event)
         self.ctx.db.dump_book(self)
         self.ctx.logger.info("book {} [{:08x}] saved in database".format(
             self.location.uname, self.location.uid))
Ejemplo n.º 3
0
 def __get_inst_info(self, instrument_id):
     return msg_utils.object_as_dict(
         self.book_context.get_inst_info(instrument_id))
Ejemplo n.º 4
0
 def as_dict(self):
     return {"msg_type": self.msg_type, "data": msg_utils.object_as_dict(self.data)}
Ejemplo n.º 5
0
 def on_instruments(self, instruments):
     inst_list = list(set(instruments))
     if inst_list:
         dicts = [msg_utils.object_as_dict(inst) for inst in inst_list]
         self.ctx.db.set_instruments(dicts)
         self.ctx.inst_infos = {inst["instrument_id"]: inst for inst in dicts}