-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtdx_tool.py
366 lines (309 loc) · 12.1 KB
/
tdx_tool.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/python
#-*- encoding: gbk -*-
## 使用新的日期格式
## TODO: 名称文件的解读
## 通达信股本变迁文件(复权) gbbq .
from __future__ import division
import struct
import os,time ,datetime,string,sys,math,re,shutil,glob
import zipfile,StringIO,getopt
from tdx_const import *
from tdx_utils import *
b_have_wx = True
try :
import wx
except ImportError :
b_have_wx = False
def myputs(sss,obj=None):
if b_have_wx and obj != None:
obj.AppendText(sss)
obj.AppendText('\n')
else:
print sss
## file format error
class TdxFileTypeError(Exception):
pass
## Tdx Error
class TDXError(Exception):
pass
## Tdx 基本文件目录
class Tdx():
def __init__(self,root,verNew = True):
self._setpath(root,verNew)
def _setpath(self,root,verNew = True):
self.root= root
self.ExportPath = os.path.join(root,r'T0002\export')
self.DayBinPaths = {
'SH' : os.path.join(root,'Vipdoc','sh','lday'),
'SZ' : os.path.join(root,'Vipdoc','sz','lday')
}
self.Min5BinPaths = {
'SH' : os.path.join(root,'Vipdoc','sh','fzline'),
'SZ' : os.path.join(root,'Vipdoc','sz','fzline')
}
self.Min1BinPaths = {
'SH' : os.path.join(root,'Vipdoc','sh','minline'),
'SZ' : os.path.join(root,'Vipdoc','sz','minline')
}
if verNew :
self.LineFile = os.path.join(root,'T0002','tdxline.dat')
else:
self.linefile = os.path.join(root,'T0002','line.dat')
self.NamesFile = {
'SH' : os.path.join(root,r'T0002\hq_cache\shex.tnf'),
'SZ' : os.path.join(root,r'T0002\hq_cache\szex.tnf')
}
## Tdx 名称文件解读
class TdxNames(Tdx):
def __init__(self,root):
Tdx.__init__(self,root)
self.namedict = {}
self.namelist = []
self._readname()
def _readname(self):
self.namedict.clear() # clear
self.namelist = [] # clear
data = get_tdxNames(self.NamesFile['SH'])
for ii in data :
self.namelist.append(('SH' + ii[0],ii[1],ii[2]))
data = get_tdxNames(self.NamesFile['SZ'])
for ii in data :
self.namelist.append(( 'SZ' + ii[0],ii[1],ii[2]))
for i in self.namelist:
self.namedict[i[0]] = i[1]
data = None
def get_id_like_list(self,pattern):
"""pattern可以为 SH9|SH6|SH58|SH77|SZ39|SZ30|SZ0"""
pats = string.split(pattern,'|')
if len(pats) == 0 :
return self.namelist
namelist = []
def _is_stkid_like(stkid,plist):
for p in plist:
if stkid.startswith(p):
return True
return False
namelist = filter(lambda x : _is_stkid_like(x[0],pats),self.namelist)
return namelist
def get_id_like_dict(self,pattern):
"""pattern可以为 SH9|SH6|SH58|SH77|SZ39|SZ30|SZ0"""
pats = string.split(pattern,'|')
if len(pats) == 0 :
return self.namedict
namedict = {}
def _is_stkid_like(stkid,plist):
for p in plist:
if stkid.startswith(p):
return True
return False
for k,v in self.namedict.items():
if _is_stkid_like(k,pats):
namedict[k] = v
return namedict
def get_id_onlystock(self,typ = 'dict'):
if typ == 'dict':
return self.get_id_like_dict('SH6|SZ30|SZ0')
return self.get_id_like_list('SH6|SZ30|SZ0')
def get_id_mostuse(self,typ = 'dict'):
if typ == 'dict':
return self.get_id_like_dict('SH9|SH6|SH58|SH77|SZ39|SZ30|SZ0')
return self.get_id_like_list('SH9|SH6|SH58|SH77|SZ39|SZ30|SZ0')
def get_anlylike(self,pattern):
"""支持正则 return list"""
rec = re.compile(pattern)
data = []
for ii in self.namelist:
if rec.search(ii[0]):
data.append(ii)
elif rec.search(ii[1]):
data.append(ii)
elif rec.search(ii[2]):
data.append(ii)
return data
def updateName(self,idnames):
"""依据ID,name 更改文件"""
sh_items = []
sz_items = []
for ii in idnames:
if len(ii) < 3 :
continue
if ii[0].startswith('SH') and len(ii[0]) >=3 :
sh_items.append((ii[0][2:],ii[1],ii[2]))
elif ii[0].startswith('SZ') and len(ii[0]) >=3 :
sz_items.append((ii[0][2:],ii[1],ii[2]))
if len(sh_items) > 0 :
# do update sh_file
sh_file = self.NamesFile['SH']
sh_NamesFile = NamesFile(sh_file)
sh_NamesFile.update(sh_items)
if len(sz_items) > 0 :
# do update sz_file
sz_file = self.NamesFile['SZ']
sz_NamesFile = NamesFile(sz_file)
sz_NamesFile.update(sz_items)
## re read the names
self._readname()
#endupdateName
## 分钟数据转换等等
class TdxMin(Tdx):
def __init__(self,root,TdxOut=None):
Tdx.__init__(self,root)
self.clear()
self.stkdict = {}
self.stkid = '999999'
self.mkt = 'SH'
self.TdxOut = TdxOut
def clear(self):
self.data_orig = [] #文本数据
self.data_fb = [] #分笔OHLC数据
self.data_01 = [] #1分钟OHLC数据
self.data_05 = [] #5分钟OHLC数据
self.data_day = [] #日线数据OHLC数据
def setID(self,p_mkt , p_stkid):
self.mkt = p_mkt
self.stkid = p_stkid
self.clear()
def readFromText(self,dt1 = None,dt2 = None):
"""对给定的ID号和日期进行读取数据
优先从ZIP文件中读取然后再读取TXT文本
"""
thesefiles = {}
if dt1 and dt2 :
if type(dt1) == datetime.date:
dt1 = dt1.strftime('%Y%m%d')
if type(dt2) == datetime.date:
dt2 = dt2.strftime('%Y%m%d')
zipedfiles = []
try:
fzip = zipfile.ZipFile(os.path.join(self.ExportPath,self.stkid+'.zip'))
except IOError,e:
myputs('Warning Can not open file! ' + str(e),self.TdxOut)
#return
else:
zipedfiles = fzip.namelist()
zipedfiles = map(lambda x : os.path.basename(x),zipedfiles)
if dt1 and dt2 :
zipedfiles = filter(lambda x : len(x) >= 8 and x[0:8] >= dt1 and x[0:8] <= dt2,zipedfiles)
for f in zipedfiles:
thesefiles[string.upper(f)] = 'ZIP'
#处理textfile
txtfiles = glob.glob(os.path.join(self.ExportPath,'*-'+self.stkid+'.TXT'))
txtfiles = map(lambda x : os.path.basename(x),txtfiles)
if dt1 and dt2 :
txtfiles = filter(lambda x : len(x) >= 8 and x[0:8] >= dt1 and x[0:8] <= dt2,txtfiles)
#txtfiles = filter(lambda x : not thesefiles.has_key(string.upper(x)),txtfiles)
for f in txtfiles:
if not thesefiles.has_key(string.upper(f)):
thesefiles[string.upper(f)] = 'TXT'
self.file_names = thesefiles.keys()
self.file_names.sort()
for f in self.file_names: # 这些都是短文件名
myputs(f,self.TdxOut)
if thesefiles[f] == 'ZIP': # Frome ziped files
doc = fzip.read(f)
doc_lines = StringIO.StringIO(doc).readlines()
else: # Frome Text files
try :
doc_lines = file(os.path.join(self.ExportPath,f)).readlines()
except IOError , e :
myputs('Error'+str(e) ,self.TdxOut)
doc_lines = []
#end if zip or txt
data_orig = readfbtxt(doc_lines,f)
data_fb = fbtxt2lc0(data_orig)
data_01 = fbtxt2lc1(data_orig)
data_05 = lc1tolc5(data_01)
#self.data_orig.extend(data_orig)
#self.data_fb.extend(data_fb)
self.data_01.extend(data_01)
self.data_05.extend(data_05)
if len(zipedfiles) > 0 :
fzip.close()
return len(thesefiles)
#分别转化分笔、1分钟、5分钟数据 # 由于while 循环的原因,放在这里太慢
#因为要检索已经存入的整个Data
#self.data_fb = fbtxt2lc0(self.data_orig)
#self.data_01 = fbtxt2lc1(self.data_orig)
#self.data_05 = lc1tolc5(self.data_01)
def readDaydata(self,dt1=None,dt2=None):
"""读日线数据"""
fname = os.path.join(self.DayBinPaths[self.mkt],self.mkt + self.stkid + '.day')
self.data_day = readDayBin(fname)
if dt1 and dt2 :
if type(dt1) == str :
tt = time.strptime(dt1,"%Y%m%d")
dt1 = datetime.date(tt.tm_year,tt.tm_mon,tt.tm_mday)
if type(dt2) == str :
tt = time.strptime(dt2,"%Y%m%d")
dt2 = datetime.date(tt.tm_year,tt.tm_mon,tt.tm_mday)
self.data_day = filter(lambda x : x[M_DT] >= dt1 and x[M_DT] <= dt2,self.data_day)
def fuQuan(self,p_convfq):
"""对数据进行复权,仅仅作前复权
p_convfq 为复权处理列表[(日期,比率)]
"""
## TODO : 当前的复权仅仅处理一个,以后要处理多个情况
## 并考虑成交量等的关系
l_fq = p_convfq[0]
for i,dd in enumerate(self.data_fb):
if dd[M_DT].date() <= l_fq[0] : # 前复权
self.data_fb[i][M_OPEN] /= l_fq[1]
self.data_fb[i][M_HIGH] /= l_fq[1]
self.data_fb[i][M_LOW] /= l_fq[1]
self.data_fb[i][M_CLOSE] /= l_fq[1]
for i,dd in enumerate(self.data_01):
if dd[M_DT].date() <= l_fq[0] : # 前复权
self.data_01[i][M_OPEN] /= l_fq[1]
self.data_01[i][M_HIGH] /= l_fq[1]
self.data_01[i][M_LOW] /= l_fq[1]
self.data_01[i][M_CLOSE] /= l_fq[1]
for i,dd in enumerate(self.data_05):
if dd[M_DT].date() <= l_fq[0] : # 前复权
self.data_05[i][M_OPEN] /= l_fq[1]
self.data_05[i][M_HIGH] /= l_fq[1]
self.data_05[i][M_LOW] /= l_fq[1]
self.data_05[i][M_CLOSE] /= l_fq[1]
for i,dd in enumerate(self.data_day):
if dd[M_DT] <= l_fq[0] : # 前复权
self.data_day[i][M_OPEN] /= l_fq[1]
self.data_day[i][M_HIGH] /= l_fq[1]
self.data_day[i][M_LOW] /= l_fq[1]
self.data_day[i][M_CLOSE] /= l_fq[1]
def writeMin1ToBin(self,fname= None):
"""将1分钟数据写入文件 """
if len(self.data_01) == 0 :
return
if fname == None:
fname = os.path.join(self.Min1BinPaths[self.mkt],self.mkt + self.stkid + '.lc1')
writeMinBin_new(self.data_01,fname)
myputs( 'Write to ' + fname,self.TdxOut)
def writeMin5ToBin(self,fname= None):
"""将5分钟数据写入文件名p_file """
if len(self.data_05) == 0 :
return
if fname == None :
fname = os.path.join(self.Min5BinPaths[self.mkt],self.mkt + self.stkid + '.lc5')
writeMinBin_new(self.data_05,fname)
myputs( 'Write to ' + fname,self.TdxOut)
if __name__ == '__main__':
import pprint
#aa = TdxMin(r'E:\cwork\tdx_study')
#aa.setID('SH','999999')
##aa.readDaydata('999999','sh',dt1='20130101',dt2='20130131')
#aa.readFromText(dt1='20070101',dt2='20070131')
#aa.writeMin1ToBin()
#fname = os.path.join(aa.Min1BinPaths[aa.mkt],aa.mkt + aa.stkid + '.lc1')
#data = readMinBin_new(fname)
#for i in data:
#pprint.pprint(i)
print '='*70
nn=TdxNames(r'd:\tdx_study')
nn.updateName([('SH770002','标普指数','BPZS'),('SZ370001','其他指数','QTZS')])
nn.id_like('SH77|SZ37')
keys = nn.namedict.keys()
keys.sort()
for k in keys:
print k,nn.namedict[k]
print '='*70
data = nn.get_anlylike(r'.*')
for i in data :
print i[0],i[1],i[2]