Python读取通达信本地数据 - pu369com

时间:2024-03-11 18:04:53

Python读取通达信本地数据

一、通达信日线*.day文件


    文件名即股票代码
    每32个字节为一天数据
    每4个字节为一个字段,每个字段内低字节在前
    00 ~ 03 字节:年月日, 整型
    04 ~ 07 字节:开盘价*1000, 整型
    08 ~ 11 字节:最高价*1000,  整型
    12 ~ 15 字节:最低价*1000,  整型
    16 ~ 19 字节:收盘价*1000,  整型
    20 ~ 23 字节:成交额(元),float型
    24 ~ 27 字节:成交量(手),整型
    28 ~ 31 字节:上日收盘*1000, 整型


二、通达信5分钟线*.5文件
    每32个字节为一个5分钟数据,每字段内低字节在前
    00 ~ 01 字节:日期,整型,设其值为num,则日期计算方法为:
                  year=floor(num/2048)+2004;
                  month=floor(mod(num,2048)/100);
                  day=mod(mod(num,2048),100);
   02 ~ 03 字节: 从0点开始至目前的分钟数,整型
    04 ~ 07 字节:开盘价(分),整型
    08 ~ 11 字节:最高价(分),整型
    12 ~ 15 字节:最低价(分),整型
    16 ~ 19 字节:收盘价(分),整型
    20 ~ 23 字节:成交额(元),float型
    24 ~ 27 字节:成交量(股)
    28 ~ 31 字节:(保留)

 

三、在 vipdoc/sh 目录下新建了个 pythondata 文件夹,注意文件路径分隔符是 /

import struct
import datetime


def stock_csv(filepath, name):
    data = []
    with open(filepath, \'rb\') as f:
        file_object_path = \'D:/通达信/vipdoc/sh/pythondata/\' + name +\'.csv\'
        file_object = open(file_object_path, \'w+\')
        while True:
            stock_date = f.read(4)
            stock_open = f.read(4)
            stock_high = f.read(4)
            stock_low= f.read(4)
            stock_close = f.read(4)
            stock_amount = f.read(4)
            stock_vol = f.read(4)
            stock_reservation = f.read(4)

            # date,open,high,low,close,amount,vol,reservation

            if not stock_date:
                break
            stock_date = struct.unpack("l", stock_date)     # 4字节 如20091229
            stock_open = struct.unpack("l", stock_open)     #开盘价*100
            stock_high = struct.unpack("l", stock_high)     #最高价*100
            stock_low= struct.unpack("l", stock_low)        #最低价*100
            stock_close = struct.unpack("l", stock_close)   #收盘价*100
            stock_amount = struct.unpack("f", stock_amount) #成交额
            stock_vol = struct.unpack("l", stock_vol)       #成交量
            stock_reservation = struct.unpack("l", stock_reservation) #保留值

            date_format = datetime.datetime.strptime(str(stock_date[0]),\'%Y%M%d\') #格式化日期
            list= date_format.strftime(\'%Y-%M-%d\')+","+str(stock_open[0]/100)+","+str(stock_high[0]/100.0)+","+str(stock_low[0]/100.0)+","+str(stock_close[0]/100.0)+","+str(stock_vol[0])+"\r\n"
            file_object.writelines(list)
        file_object.close()

stock_csv(\'D:/通达信/vipdoc/sh/lday/sh000001.day\', \'1\')

看了一下1.CSV 文件的输出数据,正确!

这么多年数据格式没变,也没加密。感谢通达信!祝通达信事业蒸蒸日上!

 

四、批量解析

import os
import struct
import datetime


def stock_csv(filepath, name):
    data = []
    with open(filepath, \'rb\') as f:
        file_object_path = \'D:/通达信/vipdoc/sh/pythondata/\' + name +\'.csv\'
        file_object = open(file_object_path, \'w+\')
        while True:
            stock_date = f.read(4)
            stock_open = f.read(4)
            stock_high = f.read(4)
            stock_low= f.read(4)
            stock_close = f.read(4)
            stock_amount = f.read(4)
            stock_vol = f.read(4)
            stock_reservation = f.read(4)

            # date,open,high,low,close,amount,vol,reservation

            if not stock_date:
                break
            stock_date = struct.unpack("l", stock_date)     # 4字节 如20091229
            stock_open = struct.unpack("l", stock_open)     #开盘价*100
            stock_high = struct.unpack("l", stock_high)     #最高价*100
            stock_low= struct.unpack("l", stock_low)        #最低价*100
            stock_close = struct.unpack("l", stock_close)   #收盘价*100
            stock_amount = struct.unpack("f", stock_amount) #成交额
            stock_vol = struct.unpack("l", stock_vol)       #成交量
            stock_reservation = struct.unpack("l", stock_reservation) #保留值

            date_format = datetime.datetime.strptime(str(stock_date[0]),\'%Y%M%d\') #格式化日期
            list= date_format.strftime(\'%Y-%M-%d\')+","+str(stock_open[0]/100)+","+str(stock_high[0]/100.0)+","+str(stock_low[0]/100.0)+","+str(stock_close[0]/100.0)+","+str(stock_vol[0])+"\r\n"
            file_object.writelines(list)
        file_object.close()


path = \'D:/通达信/vipdoc/sh/lday/\'
listfile = os.listdir(\'D:/通达信/vipdoc/sh/lday/\')
for i in listfile:
    stock_csv(path+i, i[:-4])

 顺便写个简单的公式,其实与macd差不多

BASE:0;
J10:SUM(C,10)/10 NODRAW;
PL10:(C-J10)/J10*100 COLORSTICK;
PL10SUM:IF(PL10>=0,SUM(PL10,BARSLASTCOUNT(PL10>=0)),SUM(PL10,BARSLASTCOUNT(PL10<0)));
PL60:=(C-MA(C,60))/MA(C,60)*100;
PL60SUM:IF(PL60>=0,SUM(PL60,BARSLASTCOUNT(PL60>=0)),SUM(PL60,BARSLASTCOUNT(PL60<0)))  NODRAW;
PL10DAY:IF(PL10>=0,BARSLASTCOUNT(PL10>=0),0-BARSLASTCOUNT(PL10<0)) NODRAW;
MMAX:=MAX(MAX(MAX(MAX(MAX(MA(C,5),MA(C,10)),MA(C,20)),MA(C,30)),MA(C,60)),MA(C,120));
XL60:(MA(C,60)-REF(MA(C,60),10))/REF(MA(C,60),10)*100 NODRAW;
PL250:(C-MA(C,250))/MA(C,250)*100 NODRAW;
PL10SUMA:IF(C>MA(C,250),PL10SUM,1000)  NODRAW;

  

 

参考:https://blog.csdn.net/weixin_33755554/article/details/86306394

https://www.jianshu.com/p/9dd3ef74fe96