Python提取通达信分钟线数据
方案一:读取分钟数据,在2020年后都正常,2020年前的年显示错误。访问方案一
方案二:根据二进制前两段拿到日期分时,解决了方案一中时间错误问题。访问方案二
解决方案:将方案一和方案二进行整理,得到如下代码
import struct,os,pickle
import pandas as pd
import math
# 根据二进制前两段拿到日期分时
def get_date_str(h1, h2) -> str: # H1->0,1字节; H2->2,3字节;
year = math.floor(h1 / 2048) + 2004 # 解析出年
month = math.floor(h1 % 2048 / 100) # 月
day = h1 % 2048 % 100 # 日
hour = math.floor(h2 / 60) # 小时
minute = h2 % 60 # 分钟
if hour < 10: # 如果小时小于两位, 补0
hour = "0" + str(hour)
if minute < 10: # 如果分钟小于两位, 补0
minute = "0" + str(minute)
return str(year) + "-" + str(month) + "-" + str(day) + " " + str(hour) + ":" + str(minute)
def get_data():
file_path='D:/new_jyplug/vipdoc/sh/minline/xxx.lc1'
ofile=open(file_path,'rb')
buf=ofile.read()
ofile.close()
num=len(buf)
no=num//32
b=0
e=32
dl=[]
for i in range(no):
a=struct.unpack('HHffffllf',buf[b:e])
date_str = get_date_str(a[0], a[1]) # 解析日期和分时
dl.append([date_str,a[2], a[3], a[4], a[5], a[6], a[7]])
b=b+32
e=e+32
df=pd.DataFrame(dl,columns=['date','open', 'high', 'low', 'close', 'amount', 'volume'])
return df
get_data()

浙公网安备 33010602011771号