-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy path通达信数据文件读取.py
243 lines (221 loc) · 7.97 KB
/
通达信数据文件读取.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
from __future__ import division
import os
import struct as st
import tushare as ts
import string
import datetime, re
import time
import pandas as pds
import dateutil as dt
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 将通达信的日线文件转换成CSV格式
def stockdaydata2csv(source_dir, file_name, target_dir):
# 以二进制方式打开源文件
try:
source_file = open(source_dir + os.sep + file_name, 'rb')
buf = source_file.read()
source_file.close()
# 打开目标文件,后缀名为CSV
target_file = open(target_dir + os.sep + file_name + '.csv', 'w')
buf_size = len(buf)
rec_count = int(buf_size / 32)
#print(rec_count)
begin = 0
end = 32
# 4字节 如20091229
# 开盘价*100
# 最高价*100
# 最低价*100
# 收盘价*100
# 成交额
# 成交量
# 保留值
header = str('date') + ', ' + str('open') + ', ' + str('high') + ', ' + str('low') + ', ' \
+ str('close') + ', ' + str('amount') + ', ' + str('vol') + ', ' + str('保留') + '\n'
target_file.write(header)
for i in range(rec_count):
# 将字节流转换成Python数据格式
# I: unsigned int
# f: float
a= st.unpack('IIIIIfII', buf[begin:end])
#print(a)
line = str(a[0]) + ', ' + str(a[1] / 100.0) + ', ' + str(a[2] / 100.0) + ', ' \
+ str(a[3] / 100.0) + ', ' + str(a[4] / 100.0) + ', ' + str(a[5] / 10.0) + ', ' \
+ str(a[6]) + ', ' + str(a[7]) + '\n'
#print(line)
target_file.write(line)
begin += 32
end += 32
target_file.close()
except FileNotFoundError as fnot:
print('file is not found')
except TypeError as tper:
print(tper)
except BaseException as ber:
print(ber)
# source1 = 'C:\\十档行情\\vipdoc\\sz\\lday'
# #source1 = 'E:\\pythondata\\tmp2'
# source2 = 'C:\\十档行情\\vipdoc\\sh\\lday'
# target = 'E:\\pythondata\\tmp1'
# file_list1 = os.listdir(source1)
# for f1 in file_list1:
# stockdaydata2csv(source1, f1, target)
# file_list2 = os.listdir(source2)
# for f2 in file_list2:
# stockdaydata2csv(source2, f2, target)
def stockmindata2csv(source_dir, file_name, target_dir):
# 以二进制方式打开源文件
try:
source_file = open(source_dir + os.sep + file_name, 'rb')
buf = source_file.read()
source_file.close()
# 打开目标文件,后缀名为CSV
target_file = open(target_dir + os.sep + file_name + '.csv', 'w')
buf_size = len(buf)
rec_count = int(buf_size / 32)
#print(rec_count)
begin = 0
end = 32
# 4字节 如20091229
# 开盘价*100
# 最高价*100
# 最低价*100
# 收盘价*100
# 成交额
# 成交量
# 保留值
header = str('date') + ', ' + str('open') + ', ' + str('high') + ', ' + str('low') + ', ' \
+ str('close') + ', ' + str('amount') + ', ' + str('vol') + ', ' + str('保留') + '\n'
target_file.write(header)
for i in range(rec_count):
# 将字节流转换成Python数据格式
# I: unsigned int
# f: float
a= st.unpack('IfffffII', buf[begin:end])
#print(a)
if len(a) <= 0: break
#t = st.unpack('IfffffII', buf[begin:end])
mins = (a[0] >> 16) & 0xffff
mds = a[0] & 0xffff
month = int(mds / 100)
day = mds % 100
hour = int(mins / 60)
minute = mins % 60
# datet = "d-d d:d" % (month,day,hour,minute)
#data.append((stkID, (month, day, hour, minute), t[1], t[2], t[3], t[4], t[5], t[6], t[7]))
#date_format = datetime.datetime.strptime(str(a[0]), '%Y%M%d')
date_format = str(month)+str(day)+str(hour)+str(mins)
line = date_format + ', ' + str(a[1] / 100.0) + ', ' + str(a[2] / 100.0) + ', ' \
+ str(a[3] / 100.0) + ', ' + str(a[4] / 100.0) + ', ' + str(a[5] / 10.0) + ', ' \
+ str(a[6]) + ', ' + str(a[7]) + '\n'
#print(line)
target_file.write(line)
begin += 32
end += 32
target_file.close()
except FileNotFoundError as fnot:
print('file is not found')
except TypeError as tper:
print(tper)
except BaseException as ber:
print(ber)
# source1 = 'C:\\十档行情\\vipdoc\\sz\\lday'
# source1 = 'E:\\pythondata\\tmp2'
# # source2 = 'C:\\十档行情\\vipdoc\\sh\\lday'
# target = 'E:\\pythondata\\tmp1'
# file_list1 = os.listdir(source1)
# for f1 in file_list1:
# stockmindata2csv(source1, f1, target)
# file_list2 = os.listdir(source2)
# for f2 in file_list2:
# stockdaydata2csv(source2, f2, target)
#from readths2 import *
# 2010-09-02 by 厚朴
basedir = r'e:pythondatatmp2' #如果你的安装路径不同,请改这里
exp_dir = basedir + r'T0002export'
#exp_dir = basedir + r'T0002export_back'
lc5_dir_sh = basedir + r'Vipdocshfzline'
#lc5_dir_sh = r'D:2965ydzqwsjyVipdocshfzline'
lc5_dir_sz = basedir + r'Vipdocszfzline'
day_dir_sh = basedir + r'Vipdocshlday'
day_dir_sz = basedir + r'Vipdocszlday'
stkdict = {} #存储股票ID和上海市、深圳市的对照
#############################################################
# read 5分钟数据
# example readlc5(r'E:new_gxzq_v6Vipdocshfzlinesh600000.lc5')
#############################################################
def readlc5(p_name):
"""tdx 5min 数据
日期上低16位表示月日,高16位表示分钟
这个结构个人感觉就不如同花顺做的巧妙
在一个4字节中把 年 月 日 时 分 都记录下来了
"""
f = open(p_name,'rb')
stkID = os.path.split(p_name)[1]
stkID = os.path.splitext(stkID)[0]
if(stkID[0:2]).lower() == 'sh' or (stkID[0:2]).lower() == 'sz':
stkID = stkID[2:]
icnt = 0
data = []
while 1:
raw = f.read(4*8)
if len(raw) <= 0 : break
t = st.unpack('IfffffII',raw)
mins = (t[0] >> 16) & 0xffff
mds = t[0] & 0xffff
month = int(mds / 100)
day =int( mds % 100)
hour = int(mins / 60)
minute =int(mins % 60)
#print(mins,mds,month,day,hour,minute)
#datet = "d-d d:d" % (month,day,hour,minute)
#data=str(stkID)+str(month)+str(day)+str(hour)+str(minute)+t[1]+t[2]+t[3]+t[4]+t[5]+t[6]+t[7]
data.append((stkID,(month,day,hour,minute),t[1],t[2],t[3],t[4],t[5],t[6],t[7]))
#print datet,t[1],t[2],t[3],t[4],t[5],t[6],t[7]
icnt += 1
## end while
f.close()
return data
p_name='E:\\pythondata\\tmp2\\sz002004.lc1'
# data=readlc5(p_name)
# for line in data:
# print(line)
#############################################################
#构造通达信5min数据文件
# data 结构
#[stkID,(月,日,时,分),open,high,low,close,amt,vol,0]
#############################################################
def writelc5(p_name,data,addwrite = True):
if addwrite :
fout = open(p_name,'ab')
else:
fout = open(p_name,'wb')
for i in data:
t = i[1][0]*100+i[1][1] + ( (i[1][2] * 60 + i[1][3]) << 16)
raw = st.unpack('IfffffII',t,i[2],i[3],i[4],i[5],i[6],i[7],i[8])
fout.write(raw)
## end for
fout.close()
'''
上述fmt中,支持的格式为:
FORMAT C TYPE PYTHON TYPE STANDARD SIZE NOTES
x pad byte no value
c char string of length 1 1
b signed char integer 1 (3)
B unsigned char integer 1 (3)
? _Bool bool 1 (1)
h short integer 2 (3)
H unsigned short integer 2 (3)
i int integer 4 (3)
I unsigned int integer 4 (3)
l long integer 4 (3)
L unsigned long integer 4 (3)
q long long integer 8 (2), (3)
Q unsigned long long integer 8 (2), (3)
f float float 4 (4)
d double float 8 (4)
s char[] string
p char[] string
P void * integer (5), (3)
'''