VeighNa量化社区
你的开源社区量化交易平台
Member
avatar
加入于:
帖子: 70
声望: 0
`def QA_data_futuremin_resample20201205(
        min_data,
        type_='5min',
        exchange_id=Exchange.CFFEX
):
    """期货分钟线采样成大周期
    分钟线采样成子级别的分钟线
    future:
    vol ==> trade
    amount X
    期货一般两种模式:
    中金所 股指期货: 9:30 - 11:30/ 13:00 -15:00
    其他期货: -1 21:00: 2:30  /  9:00 - 11:30 / 13:30-15:00


    输入demo
                open       high        low      close  open_interest  volume
    datetime
    2020-11-24 09:33:00+08:00  3465.9598  3466.3631  3463.9408  3465.0981        68138.0   588.0
    2020-11-24 09:34:00+08:00  3465.5208  3470.3931  3464.5840  3469.7117        67968.0   676.0
    2020-11-24 09:35:00+08:00  3469.9932  3470.6966  3466.8064  3468.5448        67851.0   427.0
    2020-11-24 09:36:00+08:00  3468.4432  3470.1731  3463.9361  3464.2188        67684.0   431.0
    2020-11-24 09:37:00+08:00  3464.0211  3464.3037  3456.2692  3457.5784        67481.0   532.0
    2020-11-24 09:38:00+08:00  3457.8283  3457.8737  3451.8422  3453.3229        67163.0   603.0
    2020-11-24 09:39:00+08:00  3453.7302  3456.1880  3453.2948  3455.7669        66960.0   358.0
    2020-11-24 09:40:00+08:00  3455.5569  3455.6181  3450.4431  3450.4431        66642.0   568.0

    """
    CONVERSION = {
        # 'code': 'first',
        'open': 'first',
        'high': 'max',
        'low': 'min',
        'close': 'last',
        # 'datetime': 'last',
        'open_interest': 'last',
        'volume': 'sum'
    }
    min_data = min_data.loc[:, list(CONVERSION.keys())]
    idx = min_data.index
    if exchange_id == Exchange.CFFEX:
        part_1 = min_data.iloc[idx.indexer_between_time('9:30', '11:30')]
        part_1_res = part_1.resample(
            type_,
            base=30,
            closed='right',
            loffset=type_
        ).apply(CONVERSION)
        # part_2 = min_data.iloc[idx.indexer_between_time('13:00', '15:00')]
        part_2 = min_data.iloc[idx.indexer_between_time('13:00', '15:15')]#为了适配中金所国债期货交易时间20201205
        part_2_res = part_2.resample(
            type_,
            base=0,
            closed='right',
            loffset=type_
        ).agg(CONVERSION)
        return pd.concat(
            [part_1_res,
             part_2_res]
        # ).dropna().sort_index().reset_index().set_index(['datetime','code'])
        ).dropna().sort_index()
    else:
        part_1 = min_data.iloc[np.append(
            idx.indexer_between_time('0:00',
                                     '11:30'),
            idx.indexer_between_time('0:00',
                                     '11:30')
        )]
        part_1_res = part_1.resample(
            type_,
            base=0,
            closed='right',
            loffset=type_
        ).apply(CONVERSION)
        part_2 = min_data.iloc[idx.indexer_between_time('13:30', '15:00')]
        part_2_res = part_2.resample(
            type_,
            base=30,
            closed='right',
            loffset=type_
        ).agg(CONVERSION)
        part_3 = min_data.iloc[idx.indexer_between_time('21:00', '23:59')]
        part_3_res = part_3.resample(
            type_,
            base=0,
            closed='right',
            loffset=type_
        ).agg(CONVERSION)
        return pd.concat(
            [part_1_res,
             part_2_res,
             part_3_res]
        # ).dropna().sort_index().reset_index().set_index(['datetime','code'])
        ).dropna().sort_index()`
Member
avatar
加入于:
帖子: 70
声望: 0

这样的写法挺好,注意resample只能重采样等分1天的分钟级别,不过对于函数里的base偏移理解的不是很到位,已经存在futurewarning,不过不影响使用
+
+
目前我只用到1分钟聚合5分钟,别的还没测试

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

沪公网安备 31011502017034号

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