- 通达信安装路径,常见位置:C:\TdxW、D:\TdxW、C:\Program Files\TdxW
- 数据目录
- 通常位于 vipdoc 文件夹内
- 上海股票:vipdoc/sh/lday/
- 深圳股票:vipdoc/sz/lday/
上海市场:vipdoc/sh/lday/,数据文件以sh开头,如,sh600100.day 深圳市场:vipdoc/sz/lday/,数据文件以sz开头,如,sz000016.day,这些本地的通达信数据 是没有开放api和外部的 ,直接无法打开获取,这里我们就需要使用到本文的方法了。下面大家可以参考参考:
day文件内容的构成
通达信的日线数据文件(.day文件)是一种二进制文件,每条记录包含32字节的数据。每条记录的格式如下:
日期(4字节,整数),格式YYYYMMDD 开盘价(4字节,浮点数),需要除以100 最高价(4字节,浮点数),需要除以100 最低价(4字节,浮点数),需要除以100 收盘价(4字节,浮点数),需要除以100 成交额(4字节,整数) 成交量(4字节,浮点数),需要除以100 保留字段(4字节,整数)
- Python代码识别day文件
以下是一个完整的Python脚本,用于读取和解析通达信的日线数据文件(.day文件):
import struct
from datetime import datetime
def read_day_file(file_path):
"""
读取日线文件并解析数据。
参数:
file_path (str): 日线文件的路径。
返回值:
tuple: 包含两个元素,第一个元素是解析后的数据列表,第二个元素是列名列表。
"""
# 使用with语句打开文件,确保文件在读取后正确关闭
with open(file_path, "rb") as ofile:
# 读取文件内容
buf = ofile.read()
# 计算文件中记录的数量
num_records = len(buf) // 32
# 初始化数据列表和列名列表
items = []
cols = ["date", "open", "high", "low", "close", "amount", "vol"]
# 遍历每条记录
for i in range(num_records):
# 计算当前记录的起始和结束位置
b = i * 32
e = b + 32
# 解析当前记录的数据
a = struct.unpack("IIIIIfII", buf[b:e])
# 将整数日期转换为字符串格式
date_int = a[0]
date_str = datetime.strptime(str(date_int), "%Y%m%d").strftime("%Y-%m-%d")
# 解析开盘价、最高价、最低价、收盘价等数据,并四舍五入保留两位小数
_open = round(a[1] / 100.0, 2)
_high = round(a[2] / 100.0, 2)
_low = round(a[3] / 100.0, 2)
_close = round(a[4] / 100.0, 2)
_amount = a[5]
_vol = a[6] / 100.0
# 构建当前记录的数据字典
item = {
cols[0]: date_str, # 日期
cols[1]: str(_open), # 开盘价
cols[2]: str(_high), # 最高价
cols[3]: str(_low), # 最低价
cols[4]: str(_close), # 收盘价
cols[5]: str(_amount), # 成交额
cols[6]: str(_vol), # 成交量
}
# 将当前记录的数据字典添加到数据列表中
items.append(item)
# 返回解析后的数据列表和列名列表
return items, cols
4、将识别结果输出为csv文件
解析完day文件后,我们可以将结果输出为csv文件,以便进行进一步的数据分析和处理。以下是一个将解析结果写入csv文件的函数:
def write_to_csv(kline_data, cols, csv_file_path):
import csv
# 打开CSV文件并写入数据
with open(csv_file_path, "w", newline="") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=cols)
writer.writeheader()
for kline in kline_data:
writer.writerow(kline)
最终结果展示
运行上述代码后,你将得到一个包含日线数据的csv文件。以下是一个示例输出(002354):
date,open,high,low,close,amount,vol
2025-01-13,5.7,5.96,5.43,5.6,1488412032.0,2638310.33
2025-01-14,5.7,6.16,5.61,6.15,2165473792.0,3654233.28
2025-01-15,6.2,6.77,6.12,6.55,2810684416.0,4333975.68
2025-01-16,6.68,6.69,6.2,6.47,2689251840.0,4163141.44
2025-01-17,6.26,6.3,5.98,6.01,1717530240.0,2810773.76
...
© 版权声明
THE END


















메이저사이트4年前1
I was able to find good info from your content. My web site - 메이저사이트trung tam nha khoa quoc te kaiyen4年前1
Hello my friend! I want to say that this post is amazing, nice written and include approximately all vital infos. I'd like to peer extra posts like this .Give Me A Little Sexy Hug4年前0
Hey there! Someone in my Facebook group shared this site with us so I came to look it over. I'm definitely loving the information. I'm book-marking and will be tweeting this to my followers! Outstanding blog and excellent design.พอกันที ท่ามิชชันนารีสุดเชย ถึงเวลาพลิกจุดให้ปลุกเร้า4年前0
Hello there! This is my first visit to your blog! We are a team of volunteers and starting a new project in a community in the same niche. Your blog provided us beneficial information to work on. You have done a marvellous job!สล็อตออนไลน์4年前0
This excellent website really has all the info I needed concerning this subject and didn't know who to ask. เล่นสล็อตต้อง https://queenslot.com/หลังจากนั้นค่อย ๆ เอาขาลง ทีนี้ก็เริ่มประกอบกิจกรรมอย่างเข้ากันดีได้เลย4年前0
Hi there to every body, it's my first go to see of this weblog; this web site includes awesome and actually excellent data designed for readers.ดูหนังออนไลน์4年前1
This is my first time pay a quick visit at here and i am truly pleassant to read everthing at alone place.situs judi online indonesia terpercaya4年前0
bookmarked!!, I really like your blog!