Пример #1
0
    def onRtnDepthMarketData(self, data: dict):
        """
        Callback of tick data update.
        """
        # Filter data update with no timestamp
        if not data["UpdateTime"]:
            return

        symbol = data["InstrumentID"]
        exchange = symbol_exchange_map.get(symbol, "")
        if not exchange:
            return

        timestamp = f"{self.current_date} {data['UpdateTime']}.{int(data['UpdateMillisec']/100)}"
        dt = datetime.strptime(timestamp, "%Y%m%d %H:%M:%S.%f")
        dt = CHINA_TZ.localize(dt)

        tick = TickData(
            symbol=symbol,
            exchange=exchange,
            datetime=dt,
            name=symbol_name_map[symbol],
            volume=data["Volume"],
            open_interest=data["OpenInterest"],
            last_price=adjust_price(data["LastPrice"]),
            limit_up=data["UpperLimitPrice"],
            limit_down=data["LowerLimitPrice"],
            open_price=adjust_price(data["OpenPrice"]),
            high_price=adjust_price(data["HighestPrice"]),
            low_price=adjust_price(data["LowestPrice"]),
            pre_close=adjust_price(data["PreClosePrice"]),
            bid_price_1=adjust_price(data["BidPrice1"]),
            ask_price_1=adjust_price(data["AskPrice1"]),
            bid_volume_1=data["BidVolume1"],
            ask_volume_1=data["AskVolume1"],
            gateway_name=self.gateway_name
        )

        if data["BidVolume2"] or data["AskVolume2"]:
            tick.bid_price_2 = adjust_price(data["BidPrice2"])
            tick.bid_price_3 = adjust_price(data["BidPrice3"])
            tick.bid_price_4 = adjust_price(data["BidPrice4"])
            tick.bid_price_5 = adjust_price(data["BidPrice5"])

            tick.ask_price_2 = adjust_price(data["AskPrice2"])
            tick.ask_price_3 = adjust_price(data["AskPrice3"])
            tick.ask_price_4 = adjust_price(data["AskPrice4"])
            tick.ask_price_5 = adjust_price(data["AskPrice5"])

            tick.bid_volume_2 = data["BidVolume2"]
            tick.bid_volume_3 = data["BidVolume3"]
            tick.bid_volume_4 = data["BidVolume4"]
            tick.bid_volume_5 = data["BidVolume5"]

            tick.ask_volume_2 = data["AskVolume2"]
            tick.ask_volume_3 = data["AskVolume3"]
            tick.ask_volume_4 = data["AskVolume4"]
            tick.ask_volume_5 = data["AskVolume5"]

        self.gateway.on_tick(tick)
Пример #2
0
    def OnRtnDepthMarketData(self, data: dict) -> None:
        """
        Callback of tick data update.
        """
        current_date = data["TradingDay"]
        current_time = data["UpdateTime"]
        dt = datetime.strptime(
            f'{current_date}-{current_time}', "%Y%m%d-%H:%M:%S"
        )
        dt = CHINA_TZ.localize(dt)

        tick = TickData(
            symbol=data["SecurityID"],
            exchange=EXCHANGE_TORA2VT[bytes.decode(data["ExchangeID"])],
            datetime=dt,
            name=data["SecurityName"],
            volume=0,
            open_interest=data["OpenInterest"],
            last_price=data["LastPrice"],
            last_volume=data["Volume"],
            limit_up=data["UpperLimitPrice"],
            limit_down=data["LowerLimitPrice"],
            open_price=data["OpenPrice"],
            high_price=data["HighestPrice"],
            low_price=data["LowestPrice"],
            pre_close=data["PreClosePrice"],
            bid_price_1=data["BidPrice1"],
            ask_price_1=data["AskPrice1"],
            bid_volume_1=data["BidVolume1"],
            ask_volume_1=data["AskVolume1"],
            gateway_name=self.gateway_name
        )

        if data["BidVolume2"] or data["AskVolume2"]:
            tick.bid_price_2 = data["BidPrice2"]
            tick.bid_price_3 = data["BidPrice3"]
            tick.bid_price_4 = data["BidPrice4"]
            tick.bid_price_5 = data["BidPrice5"]

            tick.ask_price_2 = data["AskPrice2"]
            tick.ask_price_3 = data["AskPrice3"]
            tick.ask_price_4 = data["AskPrice4"]
            tick.ask_price_5 = data["AskPrice5"]

            tick.bid_volume_2 = data["BidVolume2"]
            tick.bid_volume_3 = data["BidVolume3"]
            tick.bid_volume_4 = data["BidVolume4"]
            tick.bid_volume_5 = data["BidVolume5"]

            tick.ask_volume_2 = data["AskVolume2"]
            tick.ask_volume_3 = data["AskVolume3"]
            tick.ask_volume_4 = data["AskVolume4"]
            tick.ask_volume_5 = data["AskVolume5"]

        self.gateway.on_tick(tick)
Пример #3
0
    def onRtnDepthMarketData(self, data: dict):
        """
        Callback of tick data update.
        """
        # 这个传过来的数据data是什么样子的
        symbol = data["InstrumentID"]
        exchange = symbol_exchange_map.get(symbol, "")
        if not exchange:
            return

        timestamp = f"{data['ActionDay']} {data['UpdateTime']}.{int(data['UpdateMillisec']/100)}"

        tick = TickData(symbol=symbol,
                        exchange=exchange,
                        datetime=datetime.strptime(timestamp,
                                                   "%Y%m%d %H:%M:%S.%f"),
                        name=symbol_name_map[symbol],
                        volume=data["Volume"],
                        open_interest=data["OpenInterest"],
                        last_price=data["LastPrice"],
                        limit_up=data["UpperLimitPrice"],
                        limit_down=data["LowerLimitPrice"],
                        open_price=adjust_price(data["OpenPrice"]),
                        high_price=adjust_price(data["HighestPrice"]),
                        low_price=adjust_price(data["LowestPrice"]),
                        pre_close=adjust_price(data["PreClosePrice"]),
                        bid_price_1=adjust_price(data["BidPrice1"]),
                        ask_price_1=adjust_price(data["AskPrice1"]),
                        bid_volume_1=data["BidVolume1"],
                        ask_volume_1=data["AskVolume1"],
                        gateway_name=self.gateway_name)

        if data["BidVolume2"] or data["AskVolume2"]:
            tick.bid_price_2 = adjust_price(data["BidPrice2"])
            tick.bid_price_3 = adjust_price(data["BidPrice3"])
            tick.bid_price_4 = adjust_price(data["BidPrice4"])
            tick.bid_price_5 = adjust_price(data["BidPrice5"])

            tick.ask_price_2 = adjust_price(data["AskPrice2"])
            tick.ask_price_3 = adjust_price(data["AskPrice3"])
            tick.ask_price_4 = adjust_price(data["AskPrice4"])
            tick.ask_price_5 = adjust_price(data["AskPrice5"])

            tick.bid_volume_2 = adjust_price(data["BidVolume2"])
            tick.bid_volume_3 = adjust_price(data["BidVolume3"])
            tick.bid_volume_4 = adjust_price(data["BidVolume4"])
            tick.bid_volume_5 = adjust_price(data["BidVolume5"])

            tick.ask_volume_2 = adjust_price(data["AskVolume2"])
            tick.ask_volume_3 = adjust_price(data["AskVolume3"])
            tick.ask_volume_4 = adjust_price(data["AskVolume4"])
            tick.ask_volume_5 = adjust_price(data["AskVolume5"])

        # 这个on_tick函数是BaseGateway基类里面定义的
        self.gateway.on_tick(tick)
Пример #4
0
    def onRtnDepthMarketData(self, data: dict) -> None:
        """
        Callback of tick data update.
        """
        symbol = data["InstID"]
        exchange = symbol_exchange_map.get(symbol, "")
        if not exchange:
            return

        timestamp = f"{data['QuoteDate']} {data['QuoteTime']}.{int(data['UpdateMillisec']/100)}"
        dt = datetime.strptime(timestamp, "%Y%m%d %H:%M:%S.%f")
        dt = CHINA_TZ.localize(dt)

        tick = TickData(symbol=symbol,
                        exchange=exchange,
                        datetime=dt,
                        name=symbol_name_map[symbol],
                        volume=data["Volume"],
                        open_interest=data["OpenInt"],
                        last_price=data["Last"],
                        limit_up=data["highLimit"],
                        limit_down=data["lowLimit"],
                        open_price=adjust_price(data["Open"]),
                        high_price=adjust_price(data["Highest"]),
                        low_price=adjust_price(data["Low"]),
                        pre_close=adjust_price(data["PreClose"]),
                        bid_price_1=adjust_price(data["Bid1"]),
                        ask_price_1=adjust_price(data["Ask1"]),
                        bid_volume_1=data["BidLot1"],
                        ask_volume_1=data["AskLot1"],
                        gateway_name=self.gateway_name)

        if data["BidLot2"] or data["AskLot2"]:
            tick.bid_price_2 = adjust_price(data["Bid2"])
            tick.bid_price_3 = adjust_price(data["Bid3"])
            tick.bid_price_4 = adjust_price(data["Bid4"])
            tick.bid_price_5 = adjust_price(data["Bid5"])

            tick.ask_price_2 = adjust_price(data["Ask2"])
            tick.ask_price_3 = adjust_price(data["Ask3"])
            tick.ask_price_4 = adjust_price(data["Ask4"])
            tick.ask_price_5 = adjust_price(data["Ask5"])

            tick.bid_volume_2 = adjust_price(data["BidLot2"])
            tick.bid_volume_3 = adjust_price(data["BidLot3"])
            tick.bid_volume_4 = adjust_price(data["BidLot4"])
            tick.bid_volume_5 = adjust_price(data["BidLot5"])

            tick.ask_volume_2 = adjust_price(data["AskLot2"])
            tick.ask_volume_3 = adjust_price(data["AskLot3"])
            tick.ask_volume_4 = adjust_price(data["AskLot4"])
            tick.ask_volume_5 = adjust_price(data["AskLot5"])

        self.gateway.on_tick(tick)
Пример #5
0
    def onRtnDepthMarketData(self, data: dict) -> None:
        """
        Callback of tick data update.
        """
        symbol = data["InstrumentID"]
        exchange = EXCHANGE_UFT2VT[data["ExchangeID"]]
        if not exchange:
            return

        timestamp = f"{data['TradingDay']} {data['UpdateTime']}000"
        dt = datetime.strptime(timestamp, "%Y%m%d %H%M%S%f")
        dt = dt.replace(tzinfo=CHINA_TZ)

        tick = TickData(symbol=symbol,
                        exchange=exchange,
                        datetime=dt,
                        name=symbol_name_map[symbol],
                        volume=data["TradeVolume"],
                        open_interest=data["OpenInterest"],
                        last_price=data["LastPrice"],
                        limit_up=data["UpperLimitPrice"],
                        limit_down=data["LowerLimitPrice"],
                        open_price=adjust_price(data["OpenPrice"]),
                        high_price=adjust_price(data["HighestPrice"]),
                        low_price=adjust_price(data["LowestPrice"]),
                        pre_close=adjust_price(data["PreClosePrice"]),
                        bid_price_1=adjust_price(data["BidPrice1"]),
                        ask_price_1=adjust_price(data["AskPrice1"]),
                        bid_volume_1=data["BidVolume1"],
                        ask_volume_1=data["AskVolume1"],
                        gateway_name=self.gateway_name)

        if data["BidVolume2"] or data["AskVolume2"]:
            tick.bid_price_2 = adjust_price(data["BidPrice2"])
            tick.bid_price_3 = adjust_price(data["BidPrice3"])
            tick.bid_price_4 = adjust_price(data["BidPrice4"])
            tick.bid_price_5 = adjust_price(data["BidPrice5"])

            tick.ask_price_2 = adjust_price(data["AskPrice2"])
            tick.ask_price_3 = adjust_price(data["AskPrice3"])
            tick.ask_price_4 = adjust_price(data["AskPrice4"])
            tick.ask_price_5 = adjust_price(data["AskPrice5"])

            tick.bid_volume_2 = adjust_price(data["BidVolume2"])
            tick.bid_volume_3 = adjust_price(data["BidVolume3"])
            tick.bid_volume_4 = adjust_price(data["BidVolume4"])
            tick.bid_volume_5 = adjust_price(data["BidVolume5"])

            tick.ask_volume_2 = adjust_price(data["AskVolume2"])
            tick.ask_volume_3 = adjust_price(data["AskVolume3"])
            tick.ask_volume_4 = adjust_price(data["AskVolume4"])
            tick.ask_volume_5 = adjust_price(data["AskVolume5"])

        self.gateway.on_tick(tick)
Пример #6
0
    def onRtnDepthMarketData(self, data: dict) -> None:
        """
        Callback of tick data update.
        """
        symbol = data["InstrumentID"]
        exchange = symbol_exchange_map.get(symbol, "")

        if not exchange:
            return

        timestamp = f"{data['TradingDay']} {data['UpdateTime']}.{int(data['UpdateMillisec']/100)}"

        tick = TickData(
            symbol=symbol,
            exchange=exchange,
            datetime=datetime.strptime(timestamp, "%Y%m%d %H:%M:%S.%f"),
            name=symbol_name_map.get(symbol, ""),
            volume=data["Volume"],
            open_interest=data["OpenInterest"],
            last_price=data["LastPrice"],
            limit_up=data["UpperLimitPrice"],
            limit_down=data["LowerLimitPrice"],
            open_price=data["OpenPrice"],
            high_price=data["HighestPrice"],
            low_price=data["LowestPrice"],
            pre_close=data["PreClosePrice"],
            bid_price_1=data["BidPrice1"],
            ask_price_1=data["AskPrice1"],
            bid_volume_1=data["BidVolume1"],
            ask_volume_1=data["AskVolume1"],
            gateway_name=self.gateway_name
        )

        tick.bid_price_2 = data["BidPrice2"]
        tick.bid_price_3 = data["BidPrice3"]
        tick.bid_price_4 = data["BidPrice4"]
        tick.bid_price_5 = data["BidPrice5"]

        tick.ask_price_2 = data["AskPrice2"]
        tick.ask_price_3 = data["AskPrice3"]
        tick.ask_price_4 = data["AskPrice4"]
        tick.ask_price_5 = data["AskPrice5"]

        tick.bid_volume_2 = data["BidVolume2"]
        tick.bid_volume_3 = data["BidVolume3"]
        tick.bid_volume_4 = data["BidVolume4"]
        tick.bid_volume_5 = data["BidVolume5"]

        tick.ask_volume_2 = data["AskVolume2"]
        tick.ask_volume_3 = data["AskVolume3"]
        tick.ask_volume_4 = data["AskVolume4"]
        tick.ask_volume_5 = data["AskVolume5"]

        self.gateway.on_tick(tick)
Пример #7
0
        def to_tick(self):
            """
            Generate TickData object from DbTickData.
            从数据库获取的Tick数据生成 tickData数据对象。
            """
            tick = TickData(
                symbol=self.symbol,
                exchange=Exchange(self.exchange),
                # timestamp=self.timestamp,
                datetime=self.datetime,
                name=self.name,
                volume=self.volume,
                open_interest=self.open_interest,
                last_price=self.last_price,
                last_volume=self.last_volume,
                limit_up=self.limit_up,
                limit_down=self.limit_down,
                open_price=self.open_price,
                high_price=self.high_price,
                low_price=self.low_price,
                pre_close=self.pre_close,
                bid_price_1=self.bid_price_1,
                ask_price_1=self.ask_price_1,
                bid_volume_1=self.bid_volume_1,
                ask_volume_1=self.ask_volume_1,
                gateway_name="DB",
            )

            if self.bid_price_2:
                tick.bid_price_2 = self.bid_price_2
                tick.bid_price_3 = self.bid_price_3
                tick.bid_price_4 = self.bid_price_4
                tick.bid_price_5 = self.bid_price_5

                tick.ask_price_2 = self.ask_price_2
                tick.ask_price_3 = self.ask_price_3
                tick.ask_price_4 = self.ask_price_4
                tick.ask_price_5 = self.ask_price_5

                tick.bid_volume_2 = self.bid_volume_2
                tick.bid_volume_3 = self.bid_volume_3
                tick.bid_volume_4 = self.bid_volume_4
                tick.bid_volume_5 = self.bid_volume_5

                tick.ask_volume_2 = self.ask_volume_2
                tick.ask_volume_3 = self.ask_volume_3
                tick.ask_volume_4 = self.ask_volume_4
                tick.ask_volume_5 = self.ask_volume_5

            return tick
Пример #8
0
    def onStockMarketData(self, data: dict) -> None:
        """"""
        timestamp = str(data["tradingDay"]) + str(data["updateTime"])
        dt = datetime.strptime(timestamp, "%Y%m%d%H:%M:%S.%f")
        dt = CHINA_TZ.localize(dt)

        tick = TickData(symbol=data["securityID"],
                        exchange=EXCHANGE_SEC2VT[data["exchangeID"]],
                        datetime=dt,
                        volume=data["tradeQty"],
                        last_price=data["latestPrice"],
                        limit_up=data["upperLimitPrice"],
                        limit_down=data["lowerLimitPrice"],
                        open_price=data["openPrice"],
                        high_price=data["highestPrice"],
                        low_price=data["lowestPrice"],
                        pre_close=data["preClosePrice"],
                        gateway_name=self.gateway_name)
        tick.bid_price_1 = data["bidPrice1"]
        tick.bid_price_2 = data["bidPrice2"]
        tick.bid_price_3 = data["bidPrice3"]
        tick.bid_price_4 = data["bidPrice4"]
        tick.bid_price_5 = data["bidPrice5"]

        tick.ask_price_1 = data["askPrice1"]
        tick.ask_price_2 = data["askPrice2"]
        tick.ask_price_3 = data["askPrice3"]
        tick.ask_price_4 = data["askPrice4"]
        tick.ask_price_5 = data["askPrice5"]

        tick.bid_volume_1 = data["bidQty1"]
        tick.bid_volume_2 = data["bidQty2"]
        tick.bid_volume_3 = data["bidQty3"]
        tick.bid_volume_4 = data["bidQty4"]
        tick.bid_volume_5 = data["bidQty5"]

        tick.ask_volume_1 = data["askQty1"]
        tick.ask_volume_2 = data["askQty2"]
        tick.ask_volume_3 = data["askQty3"]
        tick.ask_volume_4 = data["askQty4"]
        tick.ask_volume_5 = data["askQty5"]

        tick.name = symbol_name_map.get(tick.vt_symbol, tick.symbol)
        self.gateway.on_tick(tick)
Пример #9
0
    def qute_stock_QUT(self, code, data):
        tick = self.ticks.get(code, None)
        if tick is None:
            contract = self.code2contract[code]
            tick = TickData(
                symbol=code,
                exchange=Exchange.TSE,
                name=f"{contract['name']}{contract['delivery_month']}",
                datetime=datetime.now(),
                gateway_name=self.gateway_name,
            )
            self.ticks[code] = tick
        tick.bid_price_1 = data["BidPrice"][0]
        tick.bid_price_2 = data["BidPrice"][1]
        tick.bid_price_3 = data["BidPrice"][2]
        tick.bid_price_4 = data["BidPrice"][3]
        tick.bid_price_5 = data["BidPrice"][4]

        tick.ask_price_1 = data["AskPrice"][0]
        tick.ask_price_2 = data["AskPrice"][1]
        tick.ask_price_3 = data["AskPrice"][2]
        tick.ask_price_4 = data["AskPrice"][3]
        tick.ask_price_5 = data["AskPrice"][4]

        tick.bid_volume_1 = data["BidVolume"][0]
        tick.bid_volume_2 = data["BidVolume"][1]
        tick.bid_volume_3 = data["BidVolume"][2]
        tick.bid_volume_4 = data["BidVolume"][3]
        tick.bid_volume_5 = data["BidVolume"][4]

        tick.ask_volume_1 = data["AskVolume"][0]
        tick.ask_volume_2 = data["AskVolume"][1]
        tick.ask_volume_3 = data["AskVolume"][2]
        tick.ask_volume_4 = data["AskVolume"][3]
        tick.ask_volume_5 = data["AskVolume"][4]
        return tick
Пример #10
0
 def quote_futures_Q(self, data):
     code = data.get('Code', None)
     if code is None:
         return
     tick = self.ticks.get(code, None)
     if tick is None:
         contract = self.code2contract[code]
         tick = TickData(
             symbol=data['Code'],
             exchange=Exchange.TFE,
             name=f"{contract['name']}{contract['delivery_month']}",
             datetime=datetime.now(),
             gateway_name=self.gateway_name,
         )
         self.ticks[code] = tick
     tick.bid_price_1 = data["BidPrice"][0]
     tick.bid_price_2 = data["BidPrice"][1]
     tick.bid_price_3 = data["BidPrice"][2]
     tick.bid_price_4 = data["BidPrice"][3]
     tick.bid_price_5 = data["BidPrice"][4]
     tick.ask_price_1 = data["AskPrice"][0]
     tick.ask_price_2 = data["AskPrice"][1]
     tick.ask_price_3 = data["AskPrice"][2]
     tick.ask_price_4 = data["AskPrice"][3]
     tick.ask_price_5 = data["AskPrice"][4]
     tick.bid_volume_1 = data["BidVolume"][0]
     tick.bid_volume_2 = data["BidVolume"][1]
     tick.bid_volume_3 = data["BidVolume"][2]
     tick.bid_volume_4 = data["BidVolume"][3]
     tick.bid_volume_5 = data["BidVolume"][4]
     tick.ask_volume_1 = data["AskVolume"][0]
     tick.ask_volume_2 = data["AskVolume"][1]
     tick.ask_volume_3 = data["AskVolume"][2]
     tick.ask_volume_4 = data["AskVolume"][3]
     tick.ask_volume_5 = data["AskVolume"][4]
     return tick
Пример #11
0
    def on_data_update(self, data):
        """"""
        channel_id = data[0]
        channel, symbol = self.channels[channel_id]
        symbol = str(symbol.replace("t", ""))

        # Get the Tick object
        if symbol in self.ticks:
            tick = self.ticks[symbol]
        else:
            tick = TickData(
                symbol=symbol,
                exchange=Exchange.BITFINEX,
                name=symbol,
                datetime=datetime.now(UTC_TZ),
                gateway_name=self.gateway_name,
            )

            self.ticks[symbol] = tick

        l_data1 = data[1]

        # Update general quote
        if channel == "ticker":
            tick.volume = float(l_data1[-3])
            tick.high_price = float(l_data1[-2])
            tick.low_price = float(l_data1[-1])
            tick.last_price = float(l_data1[-4])
            tick.open_price = float(tick.last_price - l_data1[4])

        # Update deep quote
        elif channel == "book":
            bid = self.bids.setdefault(symbol, {})
            ask = self.asks.setdefault(symbol, {})

            if len(l_data1) > 3:
                for price, count, amount in l_data1:
                    price = float(price)
                    count = int(count)
                    amount = float(amount)

                    if amount > 0:
                        bid[price] = amount
                    else:
                        ask[price] = -amount
            else:
                price, count, amount = l_data1
                price = float(price)
                count = int(count)
                amount = float(amount)

                if not count:
                    if price in bid:
                        del bid[price]
                    elif price in ask:
                        del ask[price]
                else:
                    if amount > 0:
                        bid[price] = amount
                    else:
                        ask[price] = -amount

            try:
                # BID
                bid_keys = bid.keys()
                bidPriceList = sorted(bid_keys, reverse=True)

                tick.bid_price_1 = bidPriceList[0]
                tick.bid_price_2 = bidPriceList[1]
                tick.bid_price_3 = bidPriceList[2]
                tick.bid_price_4 = bidPriceList[3]
                tick.bid_price_5 = bidPriceList[4]

                tick.bid_volume_1 = bid[tick.bid_price_1]
                tick.bid_volume_2 = bid[tick.bid_price_2]
                tick.bid_volume_3 = bid[tick.bid_price_3]
                tick.bid_volume_4 = bid[tick.bid_price_4]
                tick.bid_volume_5 = bid[tick.bid_price_5]

                # ASK
                ask_keys = ask.keys()
                askPriceList = sorted(ask_keys)

                tick.ask_price_1 = askPriceList[0]
                tick.ask_price_2 = askPriceList[1]
                tick.ask_price_3 = askPriceList[2]
                tick.ask_price_4 = askPriceList[3]
                tick.ask_price_5 = askPriceList[4]

                tick.ask_volume_1 = ask[tick.ask_price_1]
                tick.ask_volume_2 = ask[tick.ask_price_2]
                tick.ask_volume_3 = ask[tick.ask_price_3]
                tick.ask_volume_4 = ask[tick.ask_price_4]
                tick.ask_volume_5 = ask[tick.ask_price_5]
            except IndexError:
                return

        dt = datetime.now(UTC_TZ)
        tick.datetime = dt

        self.gateway.on_tick(copy(tick))
Пример #12
0
    def to_tick(self):
        """
        Generate TickData object from DbTickData.
        """
        tick = TickData(
            symbol=self.symbol,
            exchange=Exchange(self.exchange),
            datetime=self.datetime.replace(tzinfo=DB_TZ),
            name=self.name,
            volume=self.volume,
            open_interest=self.open_interest,
            last_price=self.last_price,
            last_volume=self.last_volume,
            limit_up=self.limit_up,
            limit_down=self.limit_down,
            open_price=self.open_price,
            high_price=self.high_price,
            low_price=self.low_price,
            pre_close=self.pre_close,
            bid_price_1=self.bid_price_1,
            ask_price_1=self.ask_price_1,
            bid_volume_1=self.bid_volume_1,
            ask_volume_1=self.ask_volume_1,
            gateway_name="DB",
        )

        if self.bid_price_2:
            tick.bid_price_2 = self.bid_price_2
            tick.bid_price_3 = self.bid_price_3
            tick.bid_price_4 = self.bid_price_4
            tick.bid_price_5 = self.bid_price_5
            tick.bid_price_6 = self.bid_price_6
            tick.bid_price_7 = self.bid_price_7
            tick.bid_price_8 = self.bid_price_8
            tick.bid_price_9 = self.bid_price_9
            tick.bid_price_10 = self.bid_price_10
            tick.bid_price_11 = self.bid_price_11
            tick.bid_price_12 = self.bid_price_12
            tick.bid_price_13 = self.bid_price_13
            tick.bid_price_14 = self.bid_price_14
            tick.bid_price_15 = self.bid_price_15
            tick.bid_price_16 = self.bid_price_16
            tick.bid_price_17 = self.bid_price_17
            tick.bid_price_18 = self.bid_price_18
            tick.bid_price_19 = self.bid_price_19
            tick.bid_price_20 = self.bid_price_20

            tick.ask_price_2 = self.ask_price_2
            tick.ask_price_3 = self.ask_price_3
            tick.ask_price_4 = self.ask_price_4
            tick.ask_price_5 = self.ask_price_5
            tick.ask_price_6 = self.ask_price_6
            tick.ask_price_7 = self.ask_price_7
            tick.ask_price_8 = self.ask_price_8
            tick.ask_price_9 = self.ask_price_9
            tick.ask_price_10 = self.ask_price_10
            tick.ask_price_11 = self.ask_price_11
            tick.ask_price_12 = self.ask_price_12
            tick.ask_price_13 = self.ask_price_13
            tick.ask_price_14 = self.ask_price_14
            tick.ask_price_15 = self.ask_price_15
            tick.ask_price_16 = self.ask_price_16
            tick.ask_price_17 = self.ask_price_17
            tick.ask_price_18 = self.ask_price_18
            tick.ask_price_19 = self.ask_price_19
            tick.ask_price_20 = self.ask_price_20

            tick.bid_volume_2 = self.bid_volume_2
            tick.bid_volume_3 = self.bid_volume_3
            tick.bid_volume_4 = self.bid_volume_4
            tick.bid_volume_5 = self.bid_volume_5
            tick.bid_volume_6 = self.bid_volume_6
            tick.bid_volume_7 = self.bid_volume_7
            tick.bid_volume_8 = self.bid_volume_8
            tick.bid_volume_9 = self.bid_volume_9
            tick.bid_volume_10 = self.bid_volume_10
            tick.bid_volume_11 = self.bid_volume_11
            tick.bid_volume_12 = self.bid_volume_12
            tick.bid_volume_13 = self.bid_volume_13
            tick.bid_volume_14 = self.bid_volume_14
            tick.bid_volume_15 = self.bid_volume_15
            tick.bid_volume_16 = self.bid_volume_16
            tick.bid_volume_17 = self.bid_volume_17
            tick.bid_volume_18 = self.bid_volume_18
            tick.bid_volume_19 = self.bid_volume_19
            tick.bid_volume_20 = self.bid_volume_20

            tick.ask_volume_2 = self.ask_volume_2
            tick.ask_volume_3 = self.ask_volume_3
            tick.ask_volume_4 = self.ask_volume_4
            tick.ask_volume_5 = self.ask_volume_5
            tick.ask_volume_6 = self.ask_volume_6
            tick.ask_volume_7 = self.ask_volume_7
            tick.ask_volume_8 = self.ask_volume_8
            tick.ask_volume_9 = self.ask_volume_9
            tick.ask_volume_10 = self.ask_volume_10
            tick.ask_volume_11 = self.ask_volume_11
            tick.ask_volume_12 = self.ask_volume_12
            tick.ask_volume_13 = self.ask_volume_13
            tick.ask_volume_14 = self.ask_volume_14
            tick.ask_volume_15 = self.ask_volume_15
            tick.ask_volume_16 = self.ask_volume_16
            tick.ask_volume_17 = self.ask_volume_17
            tick.ask_volume_18 = self.ask_volume_18
            tick.ask_volume_19 = self.ask_volume_19
            tick.ask_volume_20 = self.ask_volume_20

        return tick
Пример #13
0
    def onRtnDepthMarketData(self, data: dict):
        """
        Callback of tick data update.
        """
        symbol = data["InstrumentID"]
        exchange = symbol_exchange_map.get(symbol, "")
        if not exchange:
            return

        timestamp = f"{data['ActionDay']} {data['UpdateTime']}.{int(data['UpdateMillisec']/100)}"
        dt = datetime.strptime(timestamp, "%Y%m%d %H:%M:%S.%f")

        # 不处理开盘前的tick数据
        if dt.hour in [8, 20] and dt.minute < 59:
            return
        if exchange is Exchange.CFFEX and dt.hour == 9 and dt.minute < 14:
            return

        tick = TickData(symbol=symbol,
                        exchange=exchange,
                        datetime=dt,
                        date=dt.strftime('%Y-%m-%d'),
                        time=dt.strftime('%H:%M:%S.%f'),
                        trading_day=get_trading_date(dt),
                        name=symbol_name_map[symbol],
                        volume=data["Volume"],
                        open_interest=data["OpenInterest"],
                        last_price=data["LastPrice"],
                        limit_up=data["UpperLimitPrice"],
                        limit_down=data["LowerLimitPrice"],
                        open_price=adjust_price(data["OpenPrice"]),
                        high_price=adjust_price(data["HighestPrice"]),
                        low_price=adjust_price(data["LowestPrice"]),
                        pre_close=adjust_price(data["PreClosePrice"]),
                        bid_price_1=adjust_price(data["BidPrice1"]),
                        ask_price_1=adjust_price(data["AskPrice1"]),
                        bid_volume_1=data["BidVolume1"],
                        ask_volume_1=data["AskVolume1"],
                        gateway_name=self.gateway_name)

        if data["BidVolume2"] or data["AskVolume2"]:
            tick.bid_price_2 = adjust_price(data["BidPrice2"])
            tick.bid_price_3 = adjust_price(data["BidPrice3"])
            tick.bid_price_4 = adjust_price(data["BidPrice4"])
            tick.bid_price_5 = adjust_price(data["BidPrice5"])

            tick.ask_price_2 = adjust_price(data["AskPrice2"])
            tick.ask_price_3 = adjust_price(data["AskPrice3"])
            tick.ask_price_4 = adjust_price(data["AskPrice4"])
            tick.ask_price_5 = adjust_price(data["AskPrice5"])

            tick.bid_volume_2 = adjust_price(data["BidVolume2"])
            tick.bid_volume_3 = adjust_price(data["BidVolume3"])
            tick.bid_volume_4 = adjust_price(data["BidVolume4"])
            tick.bid_volume_5 = adjust_price(data["BidVolume5"])

            tick.ask_volume_2 = adjust_price(data["AskVolume2"])
            tick.ask_volume_3 = adjust_price(data["AskVolume3"])
            tick.ask_volume_4 = adjust_price(data["AskVolume4"])
            tick.ask_volume_5 = adjust_price(data["AskVolume5"])

        self.gateway.on_tick(tick)