Python下5分钟k线数据转15、30、60分钟线数据的探索

 在做股票相关的项目,需要把通达信的5分钟k线数据转为15、30、60分钟线来做后续处理,参考了一些资料,发现pandas的resample可以实现。

#通过5分钟线生成15、30、60分钟线
def changeLc5Cycle(stockid,cycle):
    cycle_list = ['15min', '30min', '60min']
    if cycle not in cycle_list:
        return
    # 获取股票5分钟数据
    conn = dbopen()
    sql = "select tradingday,id,stockid,open,close,high,low,volume from stock_5min where stockid=%s"
    val = (
        stockid
    )
    df = SQLQuery(conn,sql,val)
    df.set_index(['tradingday'], inplace=True)
    if cycle=='60min':
        date = []
        for i in df.index:
            if i.hour<12:
                i = i + pandas.Timedelta('00:30:00')
            date.append(i)
        df = df.assign(Date=pd.Series(date, index=df.index))
        df.set_index(['Date'], inplace=True)

    #print(df)

    # 重新采样各列数据
    df_open = round(df['open'].resample(rule=cycle, closed='right', label='right').first(), 2)
    df_close = round(df['close'].resample(rule=cycle, closed='right', label='right').last(), 2)
    df_high = round(df['high'].resample(rule=cycle, closed='right', label='right').max(), 2)
    df_low = round(df['low'].resample(rule=cycle, closed='right', label='right').min(), 2)
    df_volume = round(df['volume'].resample(rule=cycle, closed='right', label='right').sum(), 2)
    
    # 生成新周期数据
    df_new = pd.DataFrame()
    df_new = df_new.assign(open=df_open)
    df_new = df_new.assign(close=df_close)
    df_new = df_new.assign(high=df_high)
    df_new = df_new.assign(low=df_low)
    df_new = df_new.assign(volume=df_volume)
    # 去除空值
    df_new = df_new.dropna()

    if cycle=='60min':
        date = []
        for i in df_new.index:
            if i.hour<=12:
                i = i - pandas.Timedelta('00:30:00')
            date.append(i)
        df_new = df_new.assign(Date=pd.Series(date, index=df_new.index))
        df_new.set_index(['Date'], inplace=True)

    #写入数据库
    for index, row in df_new.iterrows():
        #此处代码省略


    conn.close()
    return

发现resample在处理60分钟重采样时是按小时采样的,股市上午是9:30到11:30,直接重采样的话会出3段数据,因此在处理60分钟数据时先对早上时段的全部5分钟线加了30分钟,重采样完后再对早上的各60分钟线减回30分钟,这样就只出现2段数据了。

参考了

python通达信5分钟转,10分钟,15分钟,30分钟,60分钟,量化交易,K线_通达信15分钟止损-CSDN博客

Pandas中resample方法详解_pandas resample-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值