-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreadDayRecord.py
225 lines (209 loc) · 8.37 KB
/
readDayRecord.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
from __future__ import division
from Output import *
import os
from fuquan import *
from GenerateFuQuanInfo import *
def resetfilePointertoHead(FilePointer):
FilePointer.seek(0)
return
TDX_One_Day_Record_Len = 32
def GetOneDayContent(FilePointer):
return FilePointer.read(TDX_One_Day_Record_Len)
def Convert4ByteString2Int(timestring):
result = ord(timestring[0]) + ord(timestring[1])*0x100 + ord(timestring[2])*0x10000 + ord(timestring[3])*0x1000000
return result
def getElementFromrecordLine(inputLine, startOffset, endOffset):
element = inputLine[startOffset: endOffset]
result = Convert4ByteString2Int(element)
return result
def getTime(recordline):
length_of = len(recordline)
return getElementFromrecordLine(recordline, 0, 4)
def getStartPrice(recordline):
return getElementFromrecordLine(recordline, 4, 8)
def getHighestPrice(recordline):
return getElementFromrecordLine(recordline, 8, 12)
def getLowestPrice(recordline):
return getElementFromrecordLine(recordline, 12, 16)
def getEndPrice(recordline):
return getElementFromrecordLine(recordline, 16, 20)/100
def findYesterDayRecord(filepointer, inputtime):
resetfilePointertoHead(filepointer)
oneday = GetOneDayContent(filepointer)
Yesterday = oneday
i = 1
while len(oneday) <> 0:
if getTime(oneday) == inputtime:
#yesterday record is the price before fuquan happen
oneday = GetOneDayContent(filepointer)
return Yesterday
Yesterday = oneday
oneday = GetOneDayContent(filepointer)
i = i + 1
print "no found match record" + "with " + str(i) + "attem"
return []
def GetYesterDayEndPrice(filepointer, inputtime):
record = findYesterDayRecord(filepointer, inputtime)
if record == []:
return []
else:
return getEndPrice(record)
def findMatchedTimeRecord(filepointer, inputtime):
resetfilePointertoHead(filepointer)
oneday = GetOneDayContent(filepointer)
i = 1
while len(oneday) <> 0:
if getTime(oneday) == inputtime:
return oneday
oneday = GetOneDayContent(filepointer)
i = i + 1
print "no found match record" + "with " + str(i) + "attem"
return []
def LoadMatchedPeriod(filepointer, start, end):
resetfilePointertoHead(filepointer)
oneday = GetOneDayContent(filepointer)
res = []
while len(oneday) <> 0:
if getTime(oneday) >= start and getTime(oneday) <= end:
res.append(oneday)
oneday = GetOneDayContent(filepointer)
return res
def findMatched_OrLastTimeRecordGroup(filepointer, starttime, endtime):
tmp = []
resetfilePointertoHead(filepointer)
oneday = GetOneDayContent(filepointer)
while len(oneday) <> 0:
if getTime(oneday) >= starttime:
tmp.append(getTime(oneday))
if getTime(oneday) == endtime:
tmp.append(getTime(oneday))
break
if getTime(oneday) > endtime:
print "not found: " + str(endtime) + "in current file"
break;
oneday = GetOneDayContent(filepointer)
return tmp
def findMatched_OrLastTimeRecord(filepointer, inputtime):
resetfilePointertoHead(filepointer)
oneday = GetOneDayContent(filepointer)
i = 1
tmp = []
while len(oneday) <> 0:
if getTime(oneday) < inputtime:
tmp = oneday
if getTime(oneday) == inputtime:
return oneday
oneday = GetOneDayContent(filepointer)
i = i + 1
print "no found match record" + "with " + str(inputtime) + "time"
if tmp <> []:
print "just found last trading time record with time: " + str(getTime(tmp))
return tmp
def GetFileNameFromStockID(stockid):
if stockid[0] == '6' or stockid[0] == '9' or stockid[0] == '8':
stockid_in_block = '1'+stockid
if stockid[0] == '0' or stockid[0] == '3':
stockid_in_block = '0'+stockid
filename = readBlock.GetStockDayKFileNameFromCodeInBlock(stockid_in_block)
return filename
def GetFilePointerFromStockID(stockid):
fname = GetFileNameFromStockID(stockid)
filepointer = open(fname, 'rb')
return filepointer
def GetGroupEndPrice(starttime, endtime, stockid):
fpointer = GetFilePointerFromStockID(stockid)
timelist = findMatched_OrLastTimeRecordGroup(fpointer, starttime, endtime)
tmp = []
for i in timelist:
tmp.append(GetFuquanPrice_byStockID(i, endtime, stockid))
return tmp
def GetFuquanPrice_byStockID(starttime, endtime, stockid):
"""stock id is an string like '600834'"""
if stockid[0] == '6'or stockid[0] == '9' or stockid[0] == '8':
stockid_in_block = '1'+stockid
if stockid[0] == '0' or stockid[0] == '3':
stockid_in_block = '0'+stockid
filename = readBlock.GetStockDayKFileNameFromCodeInBlock(stockid_in_block)
filepointer = open(filename, 'rb')
fqfilename = 'fq\\' + stockid+'.csv'
if os.path.isfile(fqfilename):
fuquanfilepointer = open(fqfilename,'r')
else:
print "not found fuquan info: " + fqfilename
fuquanfilepointer = []
return getFuquanedPrice_fromFile(starttime, endtime, filepointer, fuquanfilepointer)
def Percentage_byStockID(starttime, endtime, stockid):
"""stock id is an string like '600834'"""
if stockid[0] == '6':
stockid_in_block = '1'+stockid
if stockid[0] == '0' or stockid[0] == '3':
stockid_in_block = '0'+stockid
filename = readBlock.GetStockDayKFileNameFromCodeInBlock(stockid_in_block)
filepointer = open(filename, 'rb')
fqfilename = 'fq\\' + stockid+'.csv'
if os.path.isfile(fqfilename):
fuquanfilepointer = open(fqfilename,'r')
else:
print "not found fuquan info: " + fqfilename
fuquanfilepointer = []
return Percentage(starttime, endtime, filepointer, fuquanfilepointer)
def GetInfoGroup_byStockID(starttime, endtime, stockid):
"""stock id is an string like '600834'"""
if stockid[0] == '6':
stockid_in_block = '1'+stockid
if stockid[0] == '0' or stockid[0] == '3':
stockid_in_block = '0'+stockid
filename = readBlock.GetStockDayKFileNameFromCodeInBlock(stockid_in_block)
filepointer = open(filename, 'rb')
fqfilename = 'fq\\' + stockid+'.csv'
if os.path.isfile(fqfilename):
fuquanfilepointer = open(fqfilename,'r')
else:
print "not found fuquan info: " + fqfilename
fuquanfilepointer = []
return GetGroupInfo_withFilePointer(starttime, endtime, filepointer, fuquanfilepointer)
def GetGroupInfo_withFilePointer(starttime, endtime, filepointer, fuquanfilepointer = []):
matchedrecord_withoutFuquan = LoadMatchedPeriod(filepointer, starttime, endtime)
start_price = getFuquanedPrice_fromFile(starttime, endtime, filepointer, fuquanfilepointer)
return []
def Percentage(starttime, endtime, filepointer, fuquanfilepointer = []):
start_record = findMatchedTimeRecord(filepointer,starttime)
end_record = findMatched_OrLastTimeRecord(filepointer, endtime)
if start_record <> []:
if end_record <> []:
end_price = getEndPrice(end_record)
start_price = getEndPrice(start_record)
if(fuquanfilepointer <> []):
start_price = getFuquanedPrice_fromFile(starttime, endtime, filepointer, fuquanfilepointer)
if start_price == []:
#if not found that time price
return []
absvalue = abs(start_price - end_price)
finalrate = absvalue/start_price
finalrate = (finalrate * 100)//1
if start_price < end_price:
return finalrate
else:
return finalrate * -1
return []
def GroupPercentage(starttime, endtime, filegroup):
""" filegroup is: ["sh600000.day", "sh999999.day"]
"""
result = []
for i in filegroup:
if os.path.isfile(i):
KDayfilepointer = open(i, 'rb')
stockid = i[-10:-4]
fuquanfilename = 'fq\\' + stockid + '.csv'
if os.path.isfile(fuquanfilename):
fuquanpointer = open(fuquanfilename, 'r')
else:
#CreateFquanByStockID(stockid)
fuquanpointer = []
per = Percentage(starttime, endtime, KDayfilepointer, fuquanpointer)
result.append([i, per])
else:
result.append([i, []])
return result
def GetSortedTable(inputtable):
return sorted(inputtable, key = lambda per:-1*per[1], reverse=False)