Example #1
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)
Example #2
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)