Esempio n. 1
0
    def _init_from_body(self, body):
        # H2N
        if 'settings' in body:
            self.settings_getter = \
                lambda: {name: (value, strptime(time_str))
                             for name, (value, time_str)
                                 in body['settings'].iteritems()}

        if 'revive' in body:
            assert not body['revive']
            self.revive = bool(body['revive'])  # AbstractMessage

        self.revive_getter = lambda: self.revive

        if 'msgs' in body:
            self.inbox_update = HeartbeatMessage.deserialize_inbox_update(
                                    body['msgs'])
            logger.verbose('Node receives inbox_update: %s',
                            self.inbox_update)

        if 'last_auth_token_sync_ts' in body:
            self.last_auth_token_sync_ts = \
                    ts_to_dt(body['last_auth_token_sync_ts'])
            logger.verbose('Node receives last_auth_token_sync_ts: %s',
                           self.last_auth_token_sync_ts)
Esempio n. 2
0
    def format_ticks(self,
                     response,
                     *args,
                     symbol: Union[str, Symbol, None] = None,
                     **kwargs) -> Sequence[Tick]:

        if not symbol:
            raise ValueError("Missing Argument: `symbol`")

        symbol = symbol_str_2_orm(symbol) if isinstance(symbol,
                                                        str) else symbol

        ticks = []
        for d in response["data"]:
            for tick in d["data"]:
                _price = Decimal(tick["price"])
                _amount = Decimal(tick["amount"])

                ticks.append(
                    Tick(exchange=self.exchange_orm_obj,
                         symbol=symbol,
                         trade_time=ts_to_dt(tick["ts"] / 1000),
                         price=_price,
                         amount=_amount,
                         value=_price * _amount))
        return ticks
Esempio n. 3
0
        async def on_order(self, order):

            print(f"on_order: {order}")

            if order["type"] in ["received", "match", "done", "change"]:
                return

            try:

                _price = Decimal(order["price"])
                _amount = Decimal(order["size"])

                self.orm_cache.append(
                    Order(
                        exchange=exchange,
                        symbol=symbol,
                        sequence=order["sequence"],
                        order_id=order["orderId"],
                        side=OrderSideEnum.SELL.name
                        if order["side"] == "sell" else OrderSideEnum.BUY.name,
                        time=ts_to_dt(int(order["time"]) / 10**9),
                        price=_price,
                        amount=_amount,
                        value=_price * _amount,
                    ))
            except Exception as e:
                logger.error(e)
            finally:
                if len(self.orm_cache) >= 50:
                    Order.objects.bulk_create(self.orm_cache,
                                              ignore_conflicts=True)

                    self.orm_cache = []
Esempio n. 4
0
 def deserialize_inbox_update(cls, ser):
     """
     @type ser: dict
     """
     result = {
         'last_msg_read_ts': ts_to_dt(ser['last_msg_read_ts'])
                                 if 'last_msg_read_ts' in ser
                                 else None,
         'last_msg_sync_ts': ts_to_dt(ser['last_msg_sync_ts'])
                                 if 'last_msg_sync_ts' in ser
                                 else None,
         'messages': list(LocalizedUserMessage.from_json(m)()
                              for m in ser['messages'])
                         if 'messages' in ser else None,
     }
     return {key: value
                 for key, value in result.iteritems() if value is not None}
Esempio n. 5
0
 def deserialize_inbox_update(cls, ser):
     """
     @type ser: dict
     """
     result = {
         'last_msg_read_ts':
         ts_to_dt(ser['last_msg_read_ts'])
         if 'last_msg_read_ts' in ser else None,
         'last_msg_sync_ts':
         ts_to_dt(ser['last_msg_sync_ts'])
         if 'last_msg_sync_ts' in ser else None,
         'messages':
         list(LocalizedUserMessage.from_json(m)()
              for m in ser['messages']) if 'messages' in ser else None,
     }
     return {
         key: value
         for key, value in result.iteritems() if value is not None
     }
Esempio n. 6
0
 def deserialize_restore_progress(ser):
     """
     @type ser: dict
     """
     result = {
         'started': ts_to_dt(ser['started']),
         'uuid': UUID(ser['uuid']),
         'num': ser['num'],
         'of': ser['of'],
         'num_bytes': ser['num_bytes'],
         'of_bytes': ser['of_bytes'],
         'elapsed': sec_to_td(ser['elapsed']),
         'remaining': sec_to_td(ser['remaining']) if ser['remaining']
                                                  else None
     }
     assert set(result.iterkeys()) == set(ser.iterkeys()), \
            (ser, result)
     return result
Esempio n. 7
0
 def deserialize_restore_progress(ser):
     """
     @type ser: dict
     """
     result = {
         'started': ts_to_dt(ser['started']),
         'uuid': UUID(ser['uuid']),
         'num': ser['num'],
         'of': ser['of'],
         'num_bytes': ser['num_bytes'],
         'of_bytes': ser['of_bytes'],
         'elapsed': sec_to_td(ser['elapsed']),
         'remaining':
         sec_to_td(ser['remaining']) if ser['remaining'] else None
     }
     assert set(result.iterkeys()) == set(ser.iterkeys()), \
            (ser, result)
     return result
Esempio n. 8
0
    def _init_from_body(self, body):
        # H2N
        if 'settings' in body:
            self.settings_getter = \
                lambda: {name: (value, strptime(time_str))
                             for name, (value, time_str)
                                 in body['settings'].iteritems()}

        if 'revive' in body:
            assert not body['revive']
            self.revive = bool(body['revive'])  # AbstractMessage

        self.revive_getter = lambda: self.revive

        if 'msgs' in body:
            self.inbox_update = HeartbeatMessage.deserialize_inbox_update(
                body['msgs'])
            logger.verbose('Node receives inbox_update: %s', self.inbox_update)

        if 'last_auth_token_sync_ts' in body:
            self.last_auth_token_sync_ts = \
                    ts_to_dt(body['last_auth_token_sync_ts'])
            logger.verbose('Node receives last_auth_token_sync_ts: %s',
                           self.last_auth_token_sync_ts)