Skip to content

Instantly share code, notes, and snippets.

@kemadz
Created July 20, 2015 12:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kemadz/c7b3c4f29d3aa55aed43 to your computer and use it in GitHub Desktop.
Save kemadz/c7b3c4f29d3aa55aed43 to your computer and use it in GitHub Desktop.
解析通达信 K 线文件,Python
# -*- coding: utf-8 -*-
# 数据下载 http://www.tdx.com.cn/download
# 上海 http://www.tdx.com.cn/products/data/data/vipdoc/shlday.zip
# 深圳 http://www.tdx.com.cn/products/data/data/vipdoc/szlday.zip
# 通达信 K 线记录格式
'''
日期, date
开盘, open
最高, high
最低, low
收盘, close
成交, amount
金额, vol
保留, resv
'''
from struct import unpack
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
class KDay(object):
def __init__(self, buf):
self.data = unpack('IIIIIfII', buf)
def __str__(self):
return ('日期: %s, '
'开盘: %s, '
'最高: %s, '
'最低: %s, '
'收盘: %s, '
'成交: %s, '
'金额: %s, '
'保留: %s\n') % self.data
def main():
with open('day/sh600060.day', 'rb') as fp:
m = 0
d = 0
while True:
try:
buf = fp.read(32)
record = unpack('IIIIIfII', buf)
# print record
if record[5] > m:
m = record[5]
r = buf
except:
break
print KDay(r)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment