-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpyms.py
363 lines (284 loc) · 9.1 KB
/
pyms.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
"""
PyMS
By David Shipman, 2012
Provides a convenient interface for accessing Metastock databases
in python (with additional support for PremiumData).
Based on part ms2txt by themech
https://github.com/themech/ms2txt
GPL Licensed : Please read the enclosed license in COPYING
"""
import struct
import datetime
import os.path
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
def reader(incoming_bytes):
return lambda fh: fh.read(incoming_bytes)
def clampindex(idx, size):
if (type(idx) == slice):
raise Exception('slicing unsupported')
if (idx < 0):
idx = size + idx
if ((idx > size - 1) | (idx < 0)):
raise IndexError('index out of range')
return idx
def fmsbin2ieee(bytes):
"""
Convert an array of 4 bytes containing Microsoft Binary floating point
number to IEEE floating point format (which is used by Python)
"""
as_int = struct.unpack("i", bytes)
if not as_int:
return 0.0
man = long(struct.unpack('H', bytes[2:])[0])
if not man:
return 0.0
exp = (man & 0xff00) - 0x0200
man = man & 0x7f | (man << 8) & 0x8000
man |= exp >> 1
bytes2 = bytes[:2]
bytes2 += chr(man & 255)
bytes2 += chr((man >> 8) & 255)
return struct.unpack("f", bytes2)[0]
def float2date(date):
"""
Metastock stores date as a float number.
Here we convert it to a python datetime.date object.
"""
date = int(date)
year = 1900 + (date / 10000)
month = (date % 10000) / 100
day = date % 100
return datetime.date(year, month, day)
def float2time(time):
"""
Metastock stores date as a float number.
Here we convert it to a python datetime.time object.
"""
time = int(time)
hour = time / 10000
minute = (time % 10000) / 100
return datetime.time(hour, minute)
def int2date(in_date):
date = str(in_date)
year = int(date[0:4])
month = int(date[4:6])
day = int(date[6:8])
return datetime.date(year, month, day)
def c_uchar(x):
return struct.unpack("B", x)[0]
def c_ushort(x):
return struct.unpack("H", x)[0]
def c_uint(x):
return struct.unpack("I", x)[0]
def ms_str(x):
return x.strip('\x00 \t\nda')
def ms_em_date(x):
return float2date(struct.unpack("f", x)[0])
def ms_xm_date(x):
return int2date(struct.unpack("I", x)[0])
def ms_dat_date(x):
return float2date(fmsbin2ieee(x))
def ms_binfloat(x):
return fmsbin2ieee(x)
class RecordFormat(dict):
"""
Mapping of field names to binary data
Keys : field names
Values : DataMap types
len : record length in bytes
"""
def __init__(self, length, records={}):
self.length = length
self.read = reader(length)
dict.__init__(self, records)
class DataMap:
"""
Class mapping a datum in a binary record to
its eventual output.
i : index slice containing the binary data
f : function to convert into native python type
"""
def __init__(self, start, length, f):
self.start = start
self.length = length
self.i = slice(start, start + length)
self.f = f
class MSIndexFileFormat:
def __init__(self, header, record):
self.header = header
self.record = record
EMasterHeader = RecordFormat(192,
{
'record_count': DataMap(0, 2, c_ushort),
'last_record': DataMap(2, 2, c_ushort)
}
)
EMasterRecord = RecordFormat(192,
{
'filenum': DataMap(2, 1, c_uchar),
'numfields': DataMap(6, 1, c_uchar),
'symbol': DataMap(11, 14, ms_str),
'name': DataMap(32, 16, ms_str),
'first_date': DataMap(64, 4, ms_em_date),
'last_date': DataMap(72, 4, ms_em_date)
}
)
XMasterHeader = RecordFormat(150,
{
'record_count': DataMap(10, 2, c_ushort),
}
)
XMasterRecord = RecordFormat(150,
{
'filenum': DataMap(65, 2, c_ushort),
'symbol': DataMap(1, 15, ms_str),
'name': DataMap(16, 46, ms_str),
'first_date': DataMap(108, 4, ms_xm_date),
'last_date': DataMap(116, 4, ms_xm_date)
}
)
DATHeader = RecordFormat(28,
{
'record_count': DataMap(2, 2, lambda x: c_ushort(x) - 1)
}
)
DATRecord = RecordFormat(28,
{
'date': DataMap(0, 4, ms_dat_date),
'open': DataMap(4, 4, ms_binfloat),
'high': DataMap(8, 4, ms_binfloat),
'low': DataMap(12, 4, ms_binfloat),
'close': DataMap(16, 4, ms_binfloat),
'volume': DataMap(20, 4, ms_binfloat),
'unadj': DataMap(24, 4, ms_binfloat),
}
)
EMasterFileFormat = MSIndexFileFormat(EMasterHeader, EMasterRecord)
XMasterFileFormat = MSIndexFileFormat(XMasterHeader, XMasterRecord)
DATFileFormat = MSIndexFileFormat(DATHeader, DATRecord)
def map_record(record, fmt):
out = dict()
for field in fmt:
dmap = fmt[field]
out[field] = dmap.f(record[dmap.i])
return out
class MSFile(object):
def __init__(self, in_file, fmt):
if isinstance(in_file, file) or isinstance(in_file, StringIO):
self.fh = in_file
else:
self.fh = open(in_file, 'rb')
self.fmt = fmt
self.setup()
def setup(self):
self.fh.seek(0)
fmt = self.fmt.header
self.cur_record = 0
self.record_count = map_record(fmt.read(self.fh), fmt)['record_count']
def __iter__(self):
# ~ self.fh.seek(0)
# ~ self.cur_record = 0
self.setup()
return (self)
def next(self):
if (self.cur_record >= self.record_count):
raise StopIteration
else:
self.cur_record += 1
fmt = self.fmt.record
data = fmt.read(self.fh)
return(map_record(data, fmt))
def __getitem__(self, idx):
idx = clampindex(idx, self.record_count)
self.cur_record = idx
self.fh.seek(self.fmt.header.length + self.fmt.record.length * idx)
fmt = self.fmt.record
data = fmt.read(self.fh)
return(map_record(data, fmt))
class MSEMasterFile(MSFile):
def __init__(self, in_file):
super(MSEMasterFile, self).__init__(in_file, EMasterFileFormat)
class MSXMasterFile(MSFile):
def __init__(self, in_file):
super(MSXMasterFile, self).__init__(in_file, XMasterFileFormat)
class MSDATFile(MSFile):
def __init__(self, in_file):
super(MSDATFile, self).__init__(in_file, DATFileFormat)
class MSStock(MSDATFile):
def __init__(self, header, dat_path):
self.first_date = header['first_date']
self.last_date = header['last_date']
self.name = header['name']
self.symbol = header['symbol']
filenum = header['filenum']
ext = '.dat' if filenum < 256 else '.mwd'
filename = dat_path + 'F' + str(filenum) + ext
super(MSStock, self).__init__(filename)
def __repr__(self):
return 'MSStock :\n' + self.symbol + ' (' + self.name + ')'
class MSDirectory:
def __init__(self, path):
try:
if not os.path.exists(path):
raise Exception("Invalid Path")
self.path = path if (path[-1] == '/') else path + '/'
self.emaster = MSEMasterFile(self.path + 'emaster')
self.record_count = self.emaster.record_count
self.xmaster = None
if os.path.exists(self.path + 'xmaster'):
self.xmaster = MSXMasterFile(self.path + 'xmaster')
self.record_count += self.xmaster.record_count
except Exception as e:
raise e
def __iter__(self):
self.emaster.setup()
if (self.xmaster is not None):
self.xmaster.setup()
return(self)
def next(self):
try:
result = self.emaster.next()
return(MSStock(result, self.path))
except StopIteration:
try:
if (self.xmaster is not None):
result = self.xmaster.next()
return(MSStock(result, self.path))
except StopIteration:
pass
raise(StopIteration)
def __getitem__(self, idx):
try:
idx = clampindex(idx, self.record_count)
if (idx < 256):
record = self.emaster[idx]
else:
record = self.xmaster[idx - 255]
return MSStock(record, self.path)
except Exception as e:
print(e)
def __repr__(self):
return "MSDirectory :\n" + self.path
class PremiumDataExchange(dict):
folders = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def __init__(self, path, name):
try:
if not os.path.exists(path):
raise Exception("Invalid Path")
self.path = path if (path[-1] == '/') else path + '/'
self.name = name
self.record_count = 0
for f in PremiumDataExchange.folders:
self[f] = MSDirectory(self.path + f)
self.record_count += self[f].record_count
except:
raise
def __iter__(self):
return self.iter()
def iter(self):
for f in PremiumDataExchange.folders:
for stock in self[f]:
yield stock