通达信期货数据(日线,分钟线,品种列表)导出

因1分钟数据量较大,采用cython格式
tdx_reader.pyx

代码如下

点击查看代码
import struct
import pandas as pd
import numpy as np

def get_futrue_min(filename,date_dict,trade_date_dict): #获取期货分钟线数据  date_dict字典 内容大概是{20240724:20240725,...} int:int 交易日对应下个交易日
                                                        #trade_date_dict 格式如date_dict 但内容是 自然日对应下个自然日
    # filename='D:\\new_zx_allin1\\vipdoc\\ds\\minline\\30#SP2409.lc1'
    
    ofile=open(filename,'rb')
    buf=ofile.read()
    
    cdef int no=int(len(buf)/32)
    cdef int b=0
    cdef int e=32
    
    cdef int ix1
    cdef long[:] date_list=np.full(no,0)
    cdef int[:] time_list=np.full(no,0)
    cdef double[:] open_list=np.full(no,0.0)
    cdef double[:] high_list=np.full(no,0.0)
    cdef double[:] low_list=np.full(no,0.0)
    cdef double[:] close_list=np.full(no,0.0)
    cdef long[:] openint_list=np.full(no,0)
    cdef long[:] vol_list=np.full(no,0)
    cdef long[:] true_date_list=np.full(no,0)
    cdef int year
    cdef int month
    cdef int day
    cdef int hour
    cdef int minute
    cdef int num
    cdef int t_num
    cdef int dt
    
    for ix1 in range(no):
        a=struct.unpack('HHffffllf',buf[b:e]) #分钟数据格式 日期 时间 open high low close openint vol 0
        num=a[0]
        year = num // 2048 + 2004
        month = (num % 2048) // 100
        day = (num % 2048) % 100        

        t_num=a[1]
        hour=(t_num // 60)
        minute=(t_num % 60)
        dt=year*10000+month*100+day
        
        if hour>=21: #夜间
            date_0=trade_date_dict[dt]
        elif hour>=8:
            date_0=dt
        else:
            date_0=date_dict[trade_date_dict[dt]]
        
        date_list[ix1]=date_0
        true_date_list[ix1]=dt
        time_list[ix1]=hour*100+minute
        open_list[ix1]=a[2]
        high_list[ix1]=a[3]
        low_list[ix1]=a[4]
        close_list[ix1]=a[5]
        openint_list[ix1]=a[6]
        vol_list[ix1]=a[7]
        b=b+32
        e=e+32
    x_df=pd.DataFrame({'date':np.array(date_list),'time':np.array(time_list),'open':np.array(open_list),'high':np.array(high_list),'low':np.array(low_list),'close':np.array(close_list),'openint':np.array(openint_list),'vol':np.array(vol_list),'turedate':true_date_list})

    ofile.close()
    return x_df

def get_futrue_day(filename):   #获取期货日线数据
    # filename='D:\\new_zx_allin1\\vipdoc\\ds\\lday\\30#SP2405.day'
    
    ofile=open(filename,'rb')
    buf=ofile.read()
    
    cdef int no=int(len(buf)/32)
    cdef int b=0
    cdef int e=32

    cdef int ix1
    cdef long[:] date_list=np.full(no,0)
    cdef int[:] time_list=np.full(no,0)
    cdef double[:] open_list=np.full(no,0.0)
    cdef double[:] high_list=np.full(no,0.0)
    cdef double[:] low_list=np.full(no,0.0)
    cdef double[:] close_list=np.full(no,0.0)
    cdef long[:] openint_list=np.full(no,0)
    cdef long[:] vol_list=np.full(no,0)
    cdef double[:] settle_list=np.full(no,0.0)
    

    for ix1 in range(int(no)):
        a=struct.unpack('lffffllf',buf[b:e]) #日数据格式 日期  open high low close openint vol  结算价


        date_list[ix1]=a[0]
        open_list[ix1]=a[1]
        high_list[ix1]=a[2]
        low_list[ix1]=a[3]
        close_list[ix1]=a[4]
        
        openint_list[ix1]=a[5]
        vol_list[ix1]=a[6] 
        settle_list[ix1]=a[7]
        b=b+32
        e=e+32
    
    x_df=pd.DataFrame({'date':np.array(date_list),'open':np.array(open_list),'high':np.array(high_list),
                       'low':np.array(low_list),'close':np.array(close_list),'openint':np.array(openint_list),
                       'vol':np.array(vol_list),'settle':np.array(settle_list)})
    return x_df

def get_futrue_list(filename):  #获取期货品种列表
    # filename='D:\\new_zx_allin1\\T0002\\hq_cache\\ds_code.dat'
    ofile=open(filename,'rb')
    buf=ofile.read()
    

    cdef long b=32
    code_dict={}
    cdef long no=int(len(buf)/64)
    cdef long ix1
    for ix1 in range(no):
        if b+64>len(buf):
            break
        a=struct.unpack('bbbb9s28sccccccccccccbbbbbbbbbbb',buf[b:b+64])
        
        if a[0] in code_dict.keys():
            code_dict[a[0]].append(a[4].decode('gbk').replace('\x00',''))
        else:
            code_dict[a[0]]=[a[4].decode('gbk').replace('\x00','')]
            
        b=b+64

    ofile.close() 
    futrues_list=[]
    ex_code_dict={'28':[],'29':[],'30':[],'66':[],'47':[]}
    code_ex_dict={}
    for k_ in [28,29,30,66,47]:
        for s_ in code_dict[k_]:  

            code=s_
            futrues_list.append(code)
            ex_code_dict[str(k_)].append(code)
            code_ex_dict[code]=str(k_)
 
    return [futrues_list,code_ex_dict,ex_code_dict]



posted @ 2024-08-10 08:56  远方_2408  阅读(224)  评论(0)    收藏  举报