VeighNa量化社区
你的开源社区量化交易平台
Member
avatar
加入于:
帖子: 60
声望: 0

有没有人使用论坛里面提供的指数合成代码,发现一些异常的数据的?在合成tick数据的时候经常出现某个价格数值非常大的情况。像这 low_price=2.693714390689281e+305

2021-05-14 15:01:00,008 INFO: INVALID PRICE: symbol=Y8888.DCE, index_tick=TickData(gateway_name='ROHON', symbol='Y8888', exchange=<Exchange.DCE: 'DCE'>, datetime=datetime.datetime(2021, 5, 14, 15, 0, 58, 700000, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>), name='Y指数合约', volume=1210563, open_interest=669368.0, last_price=8929.994789114508, last_volume=0, limit_up=9747.927561520717, limit_down=8472.98650069917, open_price=2.693714390689281e+305, high_price=2.693714390689281e+305, low_price=2.693714390689281e+305, pre_close=9157.514628724408, bid_price_1=8922.475926545636, bid_price_2=0, bid_price_3=0, bid_price_4=0, bid_price_5=0, ask_price_1=8934.2974268265, ask_price_2=0, ask_price_3=0, ask_price_4=0, ask_price_5=0, bid_volume_1=276, bid_volume_2=0, bid_volume_3=0, bid_volume_4=0, bid_volume_5=0, ask_volume_1=112, ask_volume_2=0, ask_volume_3=0, ask_volume_4=0, ask_volume_5=0), price_tick=2.0, tick_data=TickData(gateway_name='ROHON', symbol='y2105', exchange=<Exchange.DCE: 'DCE'>, datetime=datetime.datetime(2021, 5, 14, 15, 0, 58, 700000, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>), name='豆油2105', volume=0, open_interest=1003.0, last_price=9676.0, last_volume=0, limit_up=10212.0, limit_down=8876.0, open_price=1.7976931348623157e+308, high_price=1.7976931348623157e+308, low_price=1.7976931348623157e+308, pre_close=9676.0, bid_price_1=9264.0, bid_price_2=0, bid_price_3=0, bid_price_4=0, bid_price_5=0, ask_price_1=9526.0, ask_price_2=0, ask_price_3=0, ask_price_4=0, ask_price_5=0, bid_volume_1=100, bid_volume_2=0, bid_volume_3=0, bid_volume_4=0, bid_volume_5=0, ask_volume_1=35, ask_volume_2=0, ask_volume_3=0, ask_volume_4=0, ask_volume_5=0)
2021-05-14 15:01:00,013 INFO: INVALID PRICE: symbol=Y8888.DCE, index_tick=TickData(gateway_name='ROHON', symbol='Y8888', exchange=<Exchange.DCE: 'DCE'>, datetime=datetime.datetime(2021, 5, 14, 15, 0, 58, 700000, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>), name='Y指数合约', volume=1210563, open_interest=669368.0, last_price=8929.994789114508, last_volume=0, limit_up=9747.927561520717, limit_down=8472.98650069917, open_price=2.693714390689281e+305, high_price=2.693714390689281e+305, low_price=2.693714390689281e+305, pre_close=9157.514628724408, bid_price_1=8922.475926545636, bid_price_2=0, bid_price_3=0, bid_price_4=0, bid_price_5=0, ask_price_1=8934.2974268265, ask_price_2=0, ask_price_3=0, ask_price_4=0, ask_price_5=0, bid_volume_1=276, bid_volume_2=0, bid_volume_3=0, bid_volume_4=0, bid_volume_5=0, ask_volume_1=112, ask_volume_2=0, ask_volume_3=0, ask_volume_4=0, ask_volume_5=0), price_tick=2.0, tick_data=TickData(gateway_name='ROHON', symbol='y2105', exchange=<Exchange.DCE: 'DCE'>, datetime=datetime.datetime(2021, 5, 14, 15, 0, 58, 700000, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>), name='豆油2105', volume=0, open_interest=1003.0, last_price=9676.0, last_volume=0, limit_up=10212.0, limit_down=8876.0, open_price=1.7976931348623157e+308, high_price=1.7976931348623157e+308, low_price=1.7976931348623157e+308, pre_close=9676.0, bid_price_1=9264.0, bid_price_2=0, bid_price_3=0, bid_price_4=0, bid_price_5=0, ask_price_1=9526.0, ask_price_2=0, ask_price_3=0, ask_price_4=0, ask_price_5=0, bid_volume_1=100, bid_volume_2=0, bid_volume_3=0, bid_volume_4=0, bid_volume_5=0, ask_volume_1=35, ask_volume_2=0, ask_volume_3=0, ask_volume_4=0, ask_volume_5=0)
2021-05-14 15:01:00,014 INFO: INVALID PRICE: symbol=Y8888.DCE, index_tick=TickData(gateway_name='ROHON', symbol='Y8888', exchange=<Exchange.DCE: 'DCE'>, datetime=datetime.datetime(2021, 5, 14, 15, 0, 58, 700000, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>), name='Y指数合约', volume=1210563, open_interest=669368.0, last_price=8929.994789114508, last_volume=0, limit_up=9747.927561520717, limit_down=8472.98650069917, open_price=2.693714390689281e+305, high_price=2.693714390689281e+305, low_price=2.693714390689281e+305, pre_close=9157.514628724408, bid_price_1=8922.475926545636, bid_price_2=0, bid_price_3=0, bid_price_4=0, bid_price_5=0, ask_price_1=8934.2974268265, ask_price_2=0, ask_price_3=0, ask_price_4=0, ask_price_5=0, bid_volume_1=276, bid_volume_2=0, bid_volume_3=0, bid_volume_4=0, bid_volume_5=0, ask_volume_1=112, ask_volume_2=0, ask_volume_3=0, ask_volume_4=0, ask_volume_5=0), price_tick=2.0, tick_data=TickData(gateway_name='ROHON', symbol='y2105', exchange=<Exchange.DCE: 'DCE'>, datetime=datetime.datetime(2021, 5, 14, 15, 0, 58, 700000, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>), name='豆油2105', volume=0, open_interest=1003.0, last_price=9676.0, last_volume=0, limit_up=10212.0, limit_down=8876.0, open_price=1.7976931348623157e+308, high_price=1.7976931348623157e+308, low_price=1.7976931348623157e+308, pre_close=9676.0, bid_price_1=9264.0, bid_price_2=0, bid_price_3=0, bid_price_4=0, bid_price_5=0, ask_price_1=9526.0, ask_price_2=0, ask_price_3=0, ask_price_4=0, ask_price_5=0, bid_volume_1=100, bid_volume_2=0, bid_volume_3=0, bid_volume_4=0, bid_volume_5=0, ask_volume_1=35, ask_volume_2=0, ask_volume_3=0, ask_volume_4=0, ask_volume_5=0)
2021-05-14 15:01:00,015 INFO: self.last_tick_times[Y8888.DCE]:2021-05-14 15:00:00+08:00, tick.datetime.minute:0, tick=TickData(gateway_name='ROHON', symbol='Y8888', exchange=<Exchange.DCE: 'DCE'>, datetime=datetime.datetime(2021, 5, 14, 15, 0, 58, 700000, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>), name='Y指数合约', volume=1210563, open_interest=669368.0, last_price=8930.0, last_volume=0, limit_up=9748.0, limit_down=8472.0, open_price=2.693714390689281e+305, high_price=2.693714390689281e+305, low_price=2.693714390689281e+305, pre_close=9158.0, bid_price_1=8922.0, bid_price_2=0, bid_price_3=0, bid_price_4=0, bid_price_5=0, ask_price_1=8934.0, ask_price_2=0, ask_price_3=0, ask_price_4=0, ask_price_5=0, bid_volume_1=276, bid_volume_2=0, bid_volume_3=0, bid_volume_4=0, bid_volume_5=0, ask_volume_1=112, ask_volume_2=0, ask_volume_3=0, ask_volume_4=0, ask_volume_5=0)

Member
avatar
加入于:
帖子: 60
声望: 0

合成代码:
def process_tick_event(self, event: Event):
tick_data = event.data
vt_symbol = tick_data.vt_symbol

    # 过滤掉指数数据,由于下面会 publish EVENT_TICK,故此处需要过滤掉指数的 tick 数据
    if vt_symbol == vt_symbol_to_index_symbol(vt_symbol):
        return

    sec_id = extract_sec_id(vt_symbol)
    sec_id = sec_id.upper()
    if sec_id not in self.subscribe_sec_id:
        return
    if tick_data.bid_price_1 > 9999999 or tick_data.ask_price_1 > 9999999:
        return

    # 下面合成最新的指数tick:每秒合成1个
    symbol_tick_dict = self.symbol_tick_dict.setdefault(sec_id, {})
    symbol_last_tick = self.symbol_last_tick.get(sec_id)
    if symbol_last_tick and tick_data.datetime.second != symbol_last_tick.datetime.second and symbol_tick_dict:
        index_tick = TickData(
            symbol=f"{sec_id}8888",
            exchange=tick_data.exchange,
            datetime=tick_data.datetime,
            gateway_name=tick_data.gateway_name,
            name=self.subscribe_index_contract[sec_id].name
        )

        for tick in symbol_tick_dict.values():
            index_tick.open_interest += tick.open_interest

        if index_tick.open_interest:
            for tick in symbol_tick_dict.values():
                tick_weight = float(tick.open_interest) / index_tick.open_interest
                index_tick.last_price += tick.last_price * tick_weight
                index_tick.volume += tick.volume
                index_tick.last_volume += tick.last_volume
                index_tick.limit_up += tick.limit_up * tick_weight
                index_tick.limit_down += tick.limit_down * tick_weight

                index_tick.open_price += tick.open_price * tick_weight
                index_tick.high_price += tick.high_price * tick_weight
                index_tick.low_price += tick.low_price * tick_weight
                index_tick.pre_close += tick.pre_close * tick_weight

                index_tick.bid_price_1 += tick.bid_price_1 * tick_weight
                index_tick.ask_price_1 += tick.ask_price_1 * tick_weight
                index_tick.bid_volume_1 += tick.bid_volume_1
                index_tick.ask_volume_1 += tick.ask_volume_1

                # 5档有需要再加进来吧,省点计算资源
                # tick_data.ask_price_2 += tick.ask_price_2 * tick_weight
                # tick_data.ask_price_3 += tick.ask_price_3 * tick_weight
                # tick_data.ask_price_4 += tick.ask_price_4 * tick_weight
                # tick_data.ask_price_5 += tick.ask_price_5 * tick_weight
                # tick_data.bid_price_2 += tick.bid_price_2 * tick_weight
                # tick_data.bid_price_3 += tick.bid_price_3 * tick_weight
                # tick_data.bid_price_4 += tick.bid_price_4 * tick_weight
                # tick_data.bid_price_5 += tick.bid_price_5 * tick_weight
                # tick_data.bid_volume_2 += tick.bid_volume_2 * tick_weight
                # tick_data.bid_volume_3 += tick.bid_volume_3 * tick_weight
                # tick_data.bid_volume_4 += tick.bid_volume_4 * tick_weight
                # tick_data.bid_volume_5 += tick.bid_volume_5 * tick_weight
                # tick_data.ask_volume_2 += tick.ask_volume_2 * tick_weight
                # tick_data.ask_volume_3 += tick.ask_volume_3 * tick_weight
                # tick_data.ask_volume_4 += tick.ask_volume_4 * tick_weight
                # tick_data.ask_volume_5 += tick.ask_volume_5 * tick_weight
            # 价格取整到最小价位变动
            price_tick = self.subscribe_index_contract[sec_id].pricetick

            if is_invalid_price(index_tick.last_price):
                error_msg = f"INVALID PRICE: symbol={index_tick.vt_symbol}, index_tick={index_tick}, price_tick={price_tick}, tick_data={tick_data}"
                #index_tick.last_price = get_max_price()
                self.on_event(EVENT_INVALID_PARAM, error_msg)

            if is_invalid_price(index_tick.bid_price_1):
                error_msg = f"INVALID PRICE: symbol={index_tick.vt_symbol}, index_tick={index_tick}, price_tick={price_tick}, tick_data={tick_data}"
                #index_tick.bid_price_1 = get_max_price()
                self.on_event(EVENT_INVALID_PARAM, error_msg)

            if is_invalid_price(index_tick.ask_price_1):
                error_msg = f"INVALID PRICE: symbol={index_tick.vt_symbol}, index_tick={index_tick}, price_tick={price_tick}, tick_data={tick_data}"
                #index_tick.ask_price_1 = get_max_price()
                self.on_event(EVENT_INVALID_PARAM, error_msg)

            if is_invalid_price(index_tick.limit_up):
                error_msg = f"INVALID PRICE: symbol={index_tick.vt_symbol}, index_tick={index_tick}, price_tick={price_tick}, tick_data={tick_data}"
                #index_tick.limit_up = get_max_price()
                self.on_event(EVENT_INVALID_PARAM, error_msg)

            if is_invalid_price(index_tick.limit_down):
                error_msg = f"INVALID PRICE: symbol={index_tick.vt_symbol}, index_tick={index_tick}, price_tick={price_tick}, tick_data={tick_data}"
                #index_tick.limit_down = get_max_price()
                self.on_event(EVENT_INVALID_PARAM, error_msg)

            if is_invalid_price(index_tick.open_price):
                error_msg = f"INVALID PRICE: symbol={index_tick.vt_symbol}, index_tick={index_tick}, price_tick={price_tick}, tick_data={tick_data}"
                #index_tick.open_price = get_max_price()
                self.on_event(EVENT_INVALID_PARAM, error_msg)

            if is_invalid_price(index_tick.high_price):
                error_msg = f"INVALID PRICE: symbol={index_tick.vt_symbol}, index_tick={index_tick}, price_tick={price_tick}, tick_data={tick_data}"
                #index_tick.high_price = get_max_price()
                self.on_event(EVENT_INVALID_PARAM, error_msg)

            if is_invalid_price(index_tick.low_price):
                error_msg = f"INVALID PRICE: symbol={index_tick.vt_symbol}, index_tick={index_tick}, price_tick={price_tick}, tick_data={tick_data}"
                #index_tick.low_price = get_max_price()
                self.on_event(EVENT_INVALID_PARAM, error_msg)

            if is_invalid_price(index_tick.pre_close):
                error_msg = f"INVALID PRICE: symbol={index_tick.vt_symbol}, index_tick={index_tick}, price_tick={price_tick}, tick_data={tick_data}"
                #index_tick.pre_close = get_max_price()
                self.on_event(EVENT_INVALID_PARAM, error_msg)

            index_tick.last_price = round_to(index_tick.last_price, price_tick)
            index_tick.bid_price_1 = round_to(index_tick.bid_price_1, price_tick)
            index_tick.ask_price_1 = round_to(index_tick.ask_price_1, price_tick)
            index_tick.limit_up = round_to(index_tick.limit_up, price_tick)
            index_tick.limit_down = round_to(index_tick.limit_down, price_tick)
            index_tick.open_price = round_to(index_tick.open_price, price_tick)
            index_tick.high_price = round_to(index_tick.high_price, price_tick)
            index_tick.low_price = round_to(index_tick.low_price, price_tick)
            index_tick.pre_close = round_to(index_tick.pre_close, price_tick)

            event = Event(EVENT_TICK, index_tick)
            self.event_engine.put(event)

    symbol_tick_dict[vt_symbol] = tick_data
    self.symbol_last_tick[sec_id] = tick_data
Member
avatar
加入于:
帖子: 60
声望: 0

def process_tick_event(self, event: Event):
tick_data = event.data
vt_symbol = tick_data.vt_symbol

    # 过滤掉指数数据,由于下面会 publish EVENT_TICK,故此处需要过滤掉指数的 tick 数据
    if vt_symbol == vt_symbol_to_index_symbol(vt_symbol):
        return

    sec_id = extract_sec_id(vt_symbol)
    sec_id = sec_id.upper()
    if sec_id not in self.subscribe_sec_id:
        return
    if tick_data.bid_price_1 > 9999999 or tick_data.ask_price_1 > 9999999:
        return

    # 下面合成最新的指数tick:每秒合成1个
    symbol_tick_dict = self.symbol_tick_dict.setdefault(sec_id, {})
    symbol_last_tick = self.symbol_last_tick.get(sec_id)
    if symbol_last_tick and tick_data.datetime.second != symbol_last_tick.datetime.second and symbol_tick_dict:
        index_tick = TickData(
            symbol=f"{sec_id}8888",
            exchange=tick_data.exchange,
            datetime=tick_data.datetime,
            gateway_name=tick_data.gateway_name,
            name=self.subscribe_index_contract[sec_id].name
        )

        for tick in symbol_tick_dict.values():
            index_tick.open_interest += tick.open_interest

        if index_tick.open_interest:
            for tick in symbol_tick_dict.values():
                tick_weight = float(tick.open_interest) / index_tick.open_interest
                index_tick.last_price += tick.last_price * tick_weight
                index_tick.volume += tick.volume
                index_tick.last_volume += tick.last_volume
                index_tick.limit_up += tick.limit_up * tick_weight
                index_tick.limit_down += tick.limit_down * tick_weight

                index_tick.open_price += tick.open_price * tick_weight
                index_tick.high_price += tick.high_price * tick_weight
                index_tick.low_price += tick.low_price * tick_weight
                index_tick.pre_close += tick.pre_close * tick_weight

                index_tick.bid_price_1 += tick.bid_price_1 * tick_weight
                index_tick.ask_price_1 += tick.ask_price_1 * tick_weight
                index_tick.bid_volume_1 += tick.bid_volume_1
                index_tick.ask_volume_1 += tick.ask_volume_1

                # 5档有需要再加进来吧,省点计算资源
                # tick_data.ask_price_2 += tick.ask_price_2 * tick_weight
                # tick_data.ask_price_3 += tick.ask_price_3 * tick_weight
                # tick_data.ask_price_4 += tick.ask_price_4 * tick_weight
                # tick_data.ask_price_5 += tick.ask_price_5 * tick_weight
                # tick_data.bid_price_2 += tick.bid_price_2 * tick_weight
                # tick_data.bid_price_3 += tick.bid_price_3 * tick_weight
                # tick_data.bid_price_4 += tick.bid_price_4 * tick_weight
                # tick_data.bid_price_5 += tick.bid_price_5 * tick_weight
                # tick_data.bid_volume_2 += tick.bid_volume_2 * tick_weight
                # tick_data.bid_volume_3 += tick.bid_volume_3 * tick_weight
                # tick_data.bid_volume_4 += tick.bid_volume_4 * tick_weight
                # tick_data.bid_volume_5 += tick.bid_volume_5 * tick_weight
                # tick_data.ask_volume_2 += tick.ask_volume_2 * tick_weight
                # tick_data.ask_volume_3 += tick.ask_volume_3 * tick_weight
                # tick_data.ask_volume_4 += tick.ask_volume_4 * tick_weight
                # tick_data.ask_volume_5 += tick.ask_volume_5 * tick_weight
            # 价格取整到最小价位变动
            price_tick = self.subscribe_index_contract[sec_id].pricetick

            index_tick.last_price = round_to(index_tick.last_price, price_tick)
            index_tick.bid_price_1 = round_to(index_tick.bid_price_1, price_tick)
            index_tick.ask_price_1 = round_to(index_tick.ask_price_1, price_tick)
            index_tick.limit_up = round_to(index_tick.limit_up, price_tick)
            index_tick.limit_down = round_to(index_tick.limit_down, price_tick)
            index_tick.open_price = round_to(index_tick.open_price, price_tick)
            index_tick.high_price = round_to(index_tick.high_price, price_tick)
            index_tick.low_price = round_to(index_tick.low_price, price_tick)
            index_tick.pre_close = round_to(index_tick.pre_close, price_tick)

            self.on_event(EVENT_TICK, index_tick)

    symbol_tick_dict[vt_symbol] = tick_data
    self.symbol_last_tick[sec_id] = tick_data
Member
avatar
加入于:
帖子: 126
声望: 14

你先把休市时间的数据过滤掉再说吧。
路还远着呢

© 2015-2022 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号

沪公网安备 31011502017034号

【用户协议】
【隐私政策】
【免责条款】