Esempio n. 1
0
    def parse_trade(cls, instmt, raw):
        """
        :param instmt: Instrument
        :param raw: Raw data in JSON
        :return:
        """
        trade = Trade()
        
        # Trade price
        trade.trade_price = float(str(raw[0]))
        
        # Trade volume
        trade.trade_volume = float(str(raw[1]))
        
        # Timestamp
        date_time = float(raw[2])
        trade.date_time = datetime.utcfromtimestamp(date_time).strftime("%Y%m%d %H:%M:%S.%f")
        
        # Trade side
        trade.trade_side = Trade.parse_side(raw[3])

        # Trade id
        trade.trade_id = trade.date_time + '-' + str(instmt.get_exch_trade_id())

        return trade
Esempio n. 2
0
    def insert_order_book(self, instmt):
        """
        Insert order book row into the database storage
        :param instmt: Instrument
        """
        # If local timestamp indicator is on, assign the local timestamp again
        if self.is_local_timestamp:
            instmt.get_l2_depth().date_time = datetime.utcnow().strftime(
                "%Y%m%d %H:%M:%S.%f")

        # Update the snapshot
        if instmt.get_l2_depth() is not None:
            id = self.get_instmt_snapshot_id(instmt)
            for db_storage in self.db_storages:
                if self.is_allowed_snapshot(db_storage):
                    db_storage.insert(
                        table=self.get_snapshot_table_name(),
                        columns=Snapshot.columns(),
                        types=Snapshot.types(),
                        values=Snapshot.values(
                            instmt.get_exchange_name(),
                            instmt.get_instmt_name(), instmt.get_l2_depth(),
                            Trade() if instmt.get_last_trade() is None else
                            instmt.get_last_trade(),
                            Snapshot.UpdateType.ORDER_BOOK),
                        primary_key_index=[0, 1],
                        is_orreplace=True,
                        is_commit=True)

                if self.is_allowed_instmt_record(db_storage):
                    db_storage.insert(
                        table=instmt.get_instmt_snapshot_table_name(),
                        columns=['id'] + Snapshot.columns(False),
                        types=['int'] + Snapshot.types(False),
                        values=[id] + Snapshot.values(
                            '', '', instmt.get_l2_depth(),
                            Trade() if instmt.get_last_trade() is None else
                            instmt.get_last_trade(),
                            Snapshot.UpdateType.ORDER_BOOK),
                        is_commit=True)
Esempio n. 3
0
    def parse_trade(cls, instmt, raw):
        """
        :param instmt: Instrument
        :param raw: Raw data in JSON
        :return:
        """
        trade = Trade()
        keys = list(raw.keys())
        if cls.get_trades_timestamp_field_name() in keys and \
           cls.get_trade_price_field_name() in keys and \
           cls.get_trade_volume_field_name() in keys:

            # Date time
            date_time = float(raw[cls.get_trades_timestamp_field_name()])
            date_time = date_time / cls.get_timestamp_offset()
            trade.date_time = datetime.utcfromtimestamp(date_time).strftime(
                "%Y%m%d %H:%M:%S.%f")

            # Trade side
            trade.trade_side = Trade.parse_side(
                raw[cls.get_trade_side_field_name()])

            # Trade price
            trade.trade_price = float(
                str(raw[cls.get_trade_price_field_name()]))

            # Trade volume
            trade.trade_volume = float(
                str(raw[cls.get_trade_volume_field_name()]))

            # Trade id
            trade.trade_id = trade.date_time + '-P' + str(
                trade.trade_price) + '-V' + str(trade.trade_volume)

        else:
            raise Exception('Does not contain trade keys in instmt %s-%s.\nOriginal:\n%s' % \
                (instmt.get_exchange_name(), instmt.get_instmt_name(), \
                 raw))

        return trade
Esempio n. 4
0
    def parse_trade(cls, instmt, raw):
        """
        :param instmt: Instrument
        :param raw: Raw data in JSON
        :return:
        """
        trade = Trade()
        keys = list(raw.keys())

        if cls.get_trades_timestamp_field_name() in keys and \
           cls.get_trade_id_field_name() in keys and \
           cls.get_trade_side_field_name() in keys and \
           cls.get_trade_price_field_name() in keys and \
           cls.get_trade_volume_field_name() in keys:

            # Date time
            timestamp = raw[cls.get_trades_timestamp_field_name()]
            timestamp = timestamp.replace('T',
                                          ' ').replace('Z',
                                                       '').replace('-', '')
            trade.date_time = timestamp

            # Trade side
            trade.trade_side = Trade.parse_side(
                raw[cls.get_trade_side_field_name()])

            # Trade id
            trade.trade_id = raw[cls.get_trade_id_field_name()]

            # Trade price
            trade.trade_price = raw[cls.get_trade_price_field_name()]

            # Trade volume
            trade.trade_volume = raw[cls.get_trade_volume_field_name()]
        else:
            raise Exception('Does not contain trade keys in instmt %s-%s.\nOriginal:\n%s' % \
                (instmt.get_exchange_name(), instmt.get_instmt_name(), \
                 raw))

        return trade
Esempio n. 5
0
    def parse_trade(cls, instmt, raw):
        """
        :param instmt: Instrument
        :param raw: Raw data in JSON
        :return:
        """
        trade = Trade()
        keys = list(raw.keys())

        if cls.get_trades_timestamp_field_name() in keys and \
           cls.get_trade_id_field_name() in keys and \
           cls.get_trade_side_field_name() in keys and \
           cls.get_trade_price_field_name() in keys and \
           cls.get_trade_volume_field_name() in keys:

            # Date time
            date_time = raw[cls.get_trades_timestamp_field_name()]
            if len(date_time) == 19:
                date_time += '.'
            date_time += '0' * (26 - len(date_time))
            date_time = datetime.strptime(date_time,
                                          cls.get_trades_timestamp_format())
            trade.date_time = date_time.strftime("%Y%m%d %H:%M:%S.%f")

            # Trade side
            trade.trade_side = 1 if raw[
                cls.get_trade_side_field_name()] == 'BUY' else 2

            # Trade id
            trade.trade_id = str(raw[cls.get_trade_id_field_name()])

            # Trade price
            trade.trade_price = float(
                str(raw[cls.get_trade_price_field_name()]))

            # Trade volume
            trade.trade_volume = float(
                str(raw[cls.get_trade_volume_field_name()]))
        else:
            raise Exception('Does not contain trade keys in instmt %s-%s.\nOriginal:\n%s' % \
                (instmt.get_exchange_name(), instmt.get_instmt_name(), \
                 raw))

        return trade
Esempio n. 6
0
    def parse_trade(cls, instmt, raw):
        """
        :param instmt: Instrument
        :param raw: Raw data in JSON
        :return:
        """
        trade = Trade()
        trade_id = raw[0]
        trade_price = float(raw[1])
        trade_volume = float(raw[2])
        timestamp = raw[3]
        trade_side = raw[4]

        trade.trade_id = trade_id + timestamp
        trade.trade_price = trade_price
        trade.trade_volume = trade_volume
        trade.trade_side = Trade.parse_side(trade_side)

        return trade
Esempio n. 7
0
    def parse_trade(cls, instmt, raw):
        """
        :param instmt: Instrument
        :param raw: Raw data in JSON
        :return:
        """
        trade = Trade()
        trade_id = raw[0]
        timestamp = raw[1]
        trade_price = raw[2]
        trade_volume = raw[3]

        trade.date_time = datetime.utcfromtimestamp(timestamp).strftime(
            "%Y%m%d %H:%M:%S.%f")
        trade.trade_side = Trade.Side.BUY if trade_volume > 0 else Trade.Side.SELL
        trade.trade_volume = abs(trade_volume)
        trade.trade_id = str(trade_id)
        trade.trade_price = trade_price

        return trade
Esempio n. 8
0
    def parse_trade(cls, instmt, raw):
        """
        :param instmt: Instrument
        :param raw: Raw data in JSON
        :return:
        """

        trade = Trade()
        trade_id = raw['order_id']
        trade_price = float(raw['counter']) / float(raw['base'])
        trade_volume = float(raw['base'])

        timestamp = float(raw[cls.get_trades_timestamp_field_name()]) / 1000.0
        trade.date_time = datetime.utcfromtimestamp(timestamp).strftime(
            "%Y%m%d %H:%M:%S.%f")
        trade.trade_volume = trade_volume
        trade.trade_id = trade_id
        trade.trade_price = trade_price

        return trade