600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > [python]解析通达信盘后数据获取历史日线数据

[python]解析通达信盘后数据获取历史日线数据

时间:2022-10-06 05:43:20

相关推荐

[python]解析通达信盘后数据获取历史日线数据

平时我们在做 离线的模型 回溯测试时候,需要历史的k线数据。

可是通达信 的日线数据如下:

日线数据在通达信的安装目录: vipdoc\sh\lday下面

本地的通达信 是没有开放api和外部的自己的交易回溯测试 工具或框架 进行交互的。

虽然 我们也可以通过 sina 的api ,或者 yahoo,或者 juhe聚合数据,或者 wind 或者 tushare (/index.html),或者 通联金融大数据等 网络的api接口 获取股票的 历史K线数据,但是网络的开销总是会比较耗时一些。

其实可以通过 python来 解析 通达信的这些 day 文件的数据,变成 我们熟悉的csv格式的数据。

#!/usr/bin/python def exactStock(fileName, code):ofile = open(fileName,'rb')buf=ofile.read()ofile.close()num=len(buf)no=num/32b=0e=32items = list()for i in range(int(no)):a=unpack('IIIIIfII',buf[b:e])year = int(a[0]/10000);m = int((a[0]%10000)/100);month = str(m);if m <10 :month = "0" + month;d = (a[0]%10000)%100;day=str(d);if d< 10 :day = "0" + str(d);dd = str(year)+"-"+month+"-"+dayopenPrice = a[1]/100.0high = a[2]/100.0low = a[3]/100.0close = a[4]/100.0amount = a[5]vol = a[6]unused = a[7]if i == 0 :preClose = closeratio = round((close - preClose)/preClose*100, 2)preClose = closeitem=[code, dd, str(openPrice), str(high), str(low), str(close), str(ratio), str(amount), str(vol)]items.append(item)b=b+32e=e+32return itemsexactStock('E:\\new_tdx\\vipdoc\\sh\\lday\\sh000001.day',"000001")

然后调用 这个方法,就可以把day文件变成csv文件,方便pandas来处理。

(在调用这个py文件前, 先在通达信的 软件 菜单里面 ,把通达信的 历史日K线数据都下载到本地,一次即可下载整个市场所有股票品种的数据。。)

批量处理的,请参考下面脚本

# coding: UTF-8from struct import *import osimport sysdef day2csv_data(dirname,fname,targetDir):ofile=open(dirname+os.sep+fname,'rb')buf=ofile.read()ofile.close()ifile=open(targetDir+os.sep+fname+'.csv','w')num=len(buf)no=num/32b=0e=32line=''linename=str('date')+','+str('open')+', '+str('high')+' ,'+str('low')+', '+str('close')+' ,'+str('amout')+', '+str('vol')+' ,'+str('str07')+''+'\n'# print lineifile.write(linename)# for i in xrange(no):for i in range(int(no)):a=unpack('IIIIIfII',buf[b:e])line=str(a[0])+','+str(a[1]/100.0)+', '+str(a[2]/100.0)+' ,'+str(a[3]/100.0)+', '+str(a[4]/100.0)+' ,'+str(a[5])+', '+str(a[6])+' ,'+str(a[7])+''+'\n'# print lineifile.write(line)b=b+32e=e+32ifile.close()# pathdir='/vipdoc/sh/lday'pathdir='X:\\股票\\解析通达信day日线数据\\day'# targetDir='/_python_gp_tdx/data_gupiao/sh/lday'targetDir='X:\\股票\\解析通达信day日线数据\\day'listfile=os.listdir(pathdir)for f in listfile:day2csv_data(pathdir,f,targetDir)else:print ('The for '+pathdir+' to '+targetDir+' loop is over')

调用示例:

pathdir='/vipdoc/sh/lday'targetDir='/python_data_gupiao/sh/lday'listfile=os.listdir(pathdir)forfinlistfile:day2csv_data(pathdir,f,targetDir)

打开这些文件如下:

是不是很熟悉的csv或者excel的格式。。。

大家就 可以用python的数据分析的库 pandas 的pd.read_csv方法来读取了。

这样速度回比较快,而且python调用 通达信的历史数据 ,就很方便了。

【扩展】:如何用python读取通达信的lc1文件

# 通达信5分钟线*.lc5文件和*.lc1文件# 文件名即股票代码# 每32个字节为一个5分钟数据,每字段内低字节在前# 00 ~ 01 字节:日期,整型,设其值为num,则日期计算方法为:#year=floor(num/2048)+;#month=floor(mod(num,2048)/100);#day=mod(mod(num,2048),100);# 02 ~ 03 字节: 从0点开始至目前的分钟数,整型# 04 ~ 07 字节:开盘价,float型# 08 ~ 11 字节:最高价,float型# 12 ~ 15 字节:最低价,float型# 16 ~ 19 字节:收盘价,float型# 20 ~ 23 字节:成交额,float型# 24 ~ 27 字节:成交量(股),整型# 28 ~ 31 字节:(保留)from struct import *import numpy as npimport pandas as pdofile=open('sz000005.lc5','rb')buf=ofile.read()ofile.close()num=len(buf)no=num//32# 原来是这样的,在python2中, '整数 / 整数 = 整数',以上面的 100 / 2 就会等于 50, 并且是整数。# 而在python3中, ‘整数/整数 = 浮点数’, 也就是100 / 2 = 50.0, 不过,使用 '//'就可以达到原python2中'/'的效果。b=0e=32dl = []for i in range(no):a=unpack('hhfffffii',buf[b:e])dl.append([str(int(a[0]/2048)+)+'-'+str(int(a[0]%2048/100)).zfill(2)+'-'+str(a[0]%2048%100).zfill(2),str(int(a[1]/60)).zfill(2)+':'+str(a[1]%60).zfill(2)+':00',a[2],a[3],a[4],a[5],a[6],a[7]])b=b+32e=e+32df = pd.DataFrame(dl, columns=['date','time','open','high','low','close','amount','volume'])print(df)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。