예제 #1
0
    def init_instmt_snapshot_table(self, instmt):
        table_name = self.get_instmt_snapshot_table_name(
            instmt.get_exchange_name(), instmt.get_instmt_name())

        instmt.set_instmt_snapshot_table_name(table_name)

        for db_client in self.db_clients:
            db_client.create(table_name, ['id'] + Snapshot.columns(False),
                             ['int'] + Snapshot.types(False), [0],
                             is_ifnotexists=True)

            # if isinstance(db_client, (MysqlClient, SqliteClient)):
            if isinstance(db_client, (MysqlClient)):
                with self.lock:
                    r = db_client.execute(
                        'select max(id) from {};'.format(table_name))
                    db_client.conn.commit()
                    if r:
                        res = db_client.cursor.fetchone()
                        max_id = res['max(id)'] if isinstance(
                            db_client, MysqlClient) else res[0]
                        if max_id:
                            self.exch_snapshot_id = max_id
                        else:
                            self.exch_snapshot_id = 0
예제 #2
0
    def init_instmt_snapshot_table(self, instmt):
        table_name = self.get_instmt_snapshot_table_name(
            instmt.get_exchange_name(), instmt.get_instmt_name())

        for db_client in self.db_clients:

            db_client.create(table_name, ['id'] + Snapshot.columns(False),
                             ['int'] + Snapshot.types(False), [0])
예제 #3
0
    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")

        date_time = datetime.strptime(trade.date_time,
                                      "%Y%m%d %H:%M:%S.%f").date()
        if date_time != self.date_time:
            self.date_time = date_time
            self.init_instmt_snapshot_table(instmt)

        # Set the last trade to the current one
        instmt.set_last_trade(trade)

        # Update the snapshot
        if instmt.get_l2_depth() is not None and \
           instmt.get_last_trade() is not None:
            id = self.get_instmt_snapshot_id(instmt)
            for db_client in self.db_clients:
                is_allowed_snapshot = self.is_allowed_snapshot(db_client)
                is_allowed_instmt_record = self.is_allowed_instmt_record(
                    db_client)
                if is_allowed_snapshot:
                    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 is_allowed_instmt_record)

                if is_allowed_instmt_record:
                    db_client.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(),
                                        instmt.get_last_trade(),
                                        Snapshot.UpdateType.TRADES),
                        is_commit=True)
예제 #4
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:
            l2_depth = instmt.get_l2_depth()
            assert (len(l2_depth.asks) == 5)
            assert (len(l2_depth.bids) == 5)

            id = self.get_instmt_snapshot_id(instmt)
            for db_client in self.db_clients:
                if self.is_allowed_snapshot(db_client):
                    print('insert snapshot')
                    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=True)

                if self.is_allowed_instmt_record(db_client):
                    db_client.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)
예제 #5
0
 def init_snapshot_table(cls, db_clients):
     for db_client in db_clients:
         db_client.create(cls.get_snapshot_table_name(),
                          Snapshot.columns(),
                          Snapshot.types(), [0, 1],
                          is_ifnotexists=True)