示例#1
0
class Instrument:
    """
    Instrument
    """
    def __init__(self, name, info):
        self.name = name
        self.info = info
        self.prev_l2_depth = L2Depth()
        self.l2_depth = L2Depth()
        self.last_trade = Trade()
        self.trade_history = []
        self.trade_history_max_sec = datetime.timedelta(seconds=60)

    def update_depth(self, row):
        self.prev_l2_depth = self.l2_depth.copy()
        self.l2_depth.update(row)

    def update_trade(self, row):
        self.last_trade.update(row)
        self.trade_history.append(self.last_trade.copy())

        index = -1
        for i in range(len(self.trade_history)-1, 0, -1):
            if self.last_trade.date_time - self.trade_history[i].date_time > self.trade_history_max_sec:
                index = i
                break

        if index > -1:
            del self.trade_history[0:index+1]
示例#2
0
 def __init__(self, name, info):
     self.name = name
     self.info = info
     self.prev_l2_depth = L2Depth()
     self.l2_depth = L2Depth()
     self.last_trade = Trade()
     self.trade_history = []
     self.trade_history_max_sec = datetime.timedelta(seconds=60)
 def init_trades_table(self, instmt):
     if self.data_mode & ExchangeGateway.DataMode.TRADES_ONLY:
         table_name = self.get_trades_table_name(instmt.get_exchange_name(),
                                                 instmt.get_instmt_name())
         self.db_client.create(table_name,
                               ['id'] + Trade.columns(),
                               ['int'] + Trade.types(),
                               [0])  
示例#4
0
    def parse_trade(cls, instmt, raws):
        """
        :param instmt: Instrument
        :param raw: Raw data in JSON
        :return:
        """

        trades = []
        for item in raws:
            trade = Trade()
            today = datetime.today().date()
            time = item[3]
            #trade.date_time = datetime.utcfromtimestamp(date_time/1000.0).strftime("%Y%m%d %H:%M:%S.%f")
            #Convert local time as to UTC.
            date_time = datetime(today.year,
                                 today.month,
                                 today.day,
                                 *list(map(lambda x: int(x), time.split(':'))),
                                 tzinfo=get_localzone())
            trade.date_time = date_time.astimezone(
                pytz.utc).strftime('%Y%m%d %H:%M:%S.%f')
            # Trade side
            # Buy = 0
            # Side = 1
            trade.trade_side = Trade.parse_side(item[4])
            # Trade id
            trade.trade_id = str(item[0])
            # Trade price
            trade.trade_price = item[1]
            # Trade volume
            trade.trade_volume = item[2]
            trades.append(trade)
        return trades
示例#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_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()])
            trade.date_time = datetime.utcfromtimestamp(date_time).strftime(
                '%Y%m%d %H:%M:%S.%f')
            # Trade side
            #trade.trade_side = 1
            trade.trade_side = Trade.parse_side(
                raw[cls.get_trade_side_field_name()])
            # 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
示例#6
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
 def insert_order_book(self, instmt):
     """
     Insert order book row into the database client
     :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 self.data_mode & ExchangeGateway.DataMode.SNAPSHOT_ONLY and \
        instmt.get_l2_depth() is not None:
         self.db_client.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=not(self.data_mode & ExchangeGateway.DataMode.ORDER_BOOK_ONLY))
         
     # Update its order book table
     if self.data_mode & ExchangeGateway.DataMode.ORDER_BOOK_ONLY:
         self.db_client.insert(table=instmt.get_order_book_table_name(),
                               columns=['id'] + L2Depth.columns(),
                               types=['int'] + L2Depth.types(),
                               values=[instmt.get_order_book_id()] + instmt.get_l2_depth().values())
示例#8
0
    def insert_order_book(self, instmt):
        """
        Insert order book row into the database client
        :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_client in self.db_clients:
                if self.is_allowed_snapshot(db_client):
                    db_client.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,  # maybe I could change this one to get a history
                        is_commit=True)

                if self.is_allowed_instmt_record(db_client):
                    db_client.insert(
                        table=instmt.get_instmt_snapshot_table_name(),
                        columns=['id'] + Snapshot.columns(),
                        types=['int'] + Snapshot.types(),
                        values=[id] + 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, 2],
                        is_commit=True)
    def insert_trade(self, instmt, trade):
        """
        Insert trade row into the database client
        :param instmt: Instrument
        """
        # If the instrument is not recovered, skip inserting into the table
        if not instmt.get_recovered():
            return
        
        # If local timestamp indicator is on, assign the local timestamp again
        if self.is_local_timestamp:
            trade.date_time = datetime.utcnow().strftime("%Y%m%d %H:%M:%S.%f")
        
        # Set the last trade to the current one
        instmt.set_last_trade(trade)

        # Update the snapshot
        if self.data_mode & ExchangeGateway.DataMode.SNAPSHOT_ONLY and \
           instmt.get_l2_depth() is not None and \
           instmt.get_last_trade() is not None:
            self.db_client.insert(table=self.get_snapshot_table_name(),
                                  columns=Snapshot.columns(),
                                  values=Snapshot.values(instmt.get_exchange_name(),
                                                         instmt.get_instmt_name(),
                                                         instmt.get_l2_depth(),
                                                         instmt.get_last_trade(),
                                                         Snapshot.UpdateType.TRADES),
                                  types=Snapshot.types(),
                                  primary_key_index=[0,1],
                                  is_orreplace=True,
                                  is_commit=not(self.data_mode & ExchangeGateway.DataMode.TRADES_ONLY))
        
        # Update its trade table
        if self.data_mode & ExchangeGateway.DataMode.TRADES_ONLY:
            self.db_client.insert(table=instmt.get_trades_table_name(),
                                  columns=['id']+Trade.columns(),
                                  types=['int']+Trade.types(),
                                  values=[instmt.get_trade_id()]+trade.values())
    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
    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
    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
示例#13
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
示例#14
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
示例#15
0
    def parse_trade(cls, instmt, raw):
        """
        :param instmt: Instrument
        :param raw: Raw data in JSON
        :return:
        """
        keys = list(raw.keys())
        ret = []

        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:

            tradeTime = raw[cls.get_trades_timestamp_field_name()]
            tradeId = raw[cls.get_trade_id_field_name()]
            tradeSide = raw[cls.get_trade_side_field_name()]
            tradePrice = raw[cls.get_trade_price_field_name()]
            tradeQty = raw[cls.get_trade_volume_field_name()]

            for i in range(0, len(tradeTime)):
                trade = Trade()

                # Date time
                # timestamp = tradeTime[i]
                # timestamp = datetime.utcfromtimestamp(timestamp).strftime("%Y%m%d %H:%M:%S.%f")
                # trade.date_time = timestamp

                # Trade side
                if tradeSide[i] == 3:
                    trade.trade_side = Trade.parse_side(1)
                elif tradeSide[i] == 4:
                    trade.trade_side = Trade.parse_side(2)
                else:
                    trade.trade_side = Trade.parse_side(tradeSide[i])

                # Trade id
                trade.trade_id = str(tradeId[i])

                # Trade price
                trade.trade_price = tradePrice[i]

                # Trade volume
                trade.trade_volume = tradeQty[i]

                ret.append(trade)

        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 ret