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()

 

posted @   C羽言  阅读(1316)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示