Created
July 20, 2015 12:54
-
-
Save kemadz/c7b3c4f29d3aa55aed43 to your computer and use it in GitHub Desktop.
解析通达信 K 线文件,Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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