Skip to content

Commit

Permalink
split code into parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
rainx committed Jun 20, 2017
1 parent 1351a64 commit 31d9120
Show file tree
Hide file tree
Showing 9 changed files with 592 additions and 452 deletions.
111 changes: 111 additions & 0 deletions pytdx/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import struct


#### XXX: 分析了一下,貌似是类似utf-8的编码方式保存有符号数字
def get_price(data, pos):
pos_byte = 6
bdata = data[pos]
intdata = bdata & 0x3f

if bdata & 0x40:
sign = True
else:
sign = False

if bdata & 0x80:
while True:
pos += 1
bdata = data[pos]
intdata += (bdata & 0x7f) << pos_byte
pos_byte += 7

if bdata & 0x80:
pass
else:
break

pos += 1

if sign:
intdata = -intdata

return intdata, pos


def get_volume(ivol):
logpoint = ivol >> (8 * 3)
hheax = ivol >> (8 * 3); # [3]
hleax = (ivol >> (8 * 2)) & 0xff; # [2]
lheax = (ivol >> 8) & 0xff; # [1]
lleax = ivol & 0xff; # [0]

dbl_1 = 1.0
dbl_2 = 2.0
dbl_128 = 128.0

dwEcx = logpoint * 2 - 0x7f;
dwEdx = logpoint * 2 - 0x86;
dwEsi = logpoint * 2 - 0x8e;
dwEax = logpoint * 2 - 0x96;
if dwEcx < 0:
tmpEax = - dwEcx
else:
tmpEax = dwEcx

dbl_xmm6 = 0.0
dbl_xmm6 = pow(2.0, tmpEax)
if dwEcx < 0:
dbl_xmm6 = 1.0 / dbl_xmm6

dbl_xmm4 = 0
if hleax > 0x80:
tmpdbl_xmm3 = 0.0
tmpdbl_xmm1 = 0.0
dwtmpeax = dwEdx + 1
tmpdbl_xmm3 = pow(2.0, dwtmpeax)
dbl_xmm0 = pow(2.0, dwEdx) * 128.0
dbl_xmm0 += (hleax & 0x7f) * tmpdbl_xmm3
dbl_xmm4 = dbl_xmm0

else:
dbl_xmm0 = 0.0
if dwEdx >= 0:
dbl_xmm0 = pow(2.0, dwEdx) * hleax
else:
dbl_xmm0 = (1 / pow(2.0, dwEdx)) * hleax
dbl_xmm4 = dbl_xmm0

dbl_xmm3 = pow(2.0, dwEsi) * lheax
dbl_xmm1 = pow(2.0, dwEax) * lleax
if hleax & 0x80:
dbl_xmm3 *= 2.0
dbl_xmm1 *= 2.0

dbl_ret = dbl_xmm6 + dbl_xmm4 + dbl_xmm3 + dbl_xmm1
return dbl_ret


def get_datetime(category, buffer, pos):
year = 0
month = 0
day = 0
hour = 15
minute = 0
if category < 4 or category == 7 or category == 8:
(zipday, tminutes) = struct.unpack("<HH", buffer[pos: pos + 4])
year = (zipday >> 11) + 2004
month = int((zipday % 2048) / 100)
day = (zipday % 2048) % 100

hour = int(tminutes / 60)
minute = tminutes % 60
else:
(zipday,) = struct.unpack("<I", buffer[pos: pos + 4])

year = int(zipday / 10000);
month = int((zipday % 10000) / 100)
day = zipday % 100

pos += 4

return year, month, day, hour, minute, pos
Loading

0 comments on commit 31d9120

Please sign in to comment.