forked from danielktaylor/PyLimitBook
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbookViewer.py
executable file
·313 lines (278 loc) · 10.7 KB
/
bookViewer.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
#!/usr/bin/python
import sys
import traceback
import curses
import cPickle
from collections import deque
from bookViewerBook import BookViewerBook
class BookViewer(object):
def __init__(self):
self.help_text = ' n:Next N:Prev t:Play for time T:Go to time l:Play for lines L:Play to line a:Aggregate .:Repeat'
self.screen = None
self.quotebook = BookViewerBook()
self.max_reverse = 100
self.quotebookHistory = deque(maxlen=self.max_reverse)
self.infile = []
self.currIndex = -1
self.last_skipped = 0
self.aggregate = False
self.keys = {
'EXIT': 113, # q
'REPEAT': 46, # .
'PREVIOUS': 78, # N
'NEXT': 110, # n
'PLAY_FOR_TIME': 116, # t
'PLAY_TO_TIME': 84, # T
'PLAY_FOR_LINES': 108, # l
'PLAY_TO_LINE': 76, # L
'AGGREGATE': 97, # a
'ESCAPE': 27, # <ESC>
}
def reset(self):
self.quotebook = BookViewerBook()
self.quotebookHistory.clear()
self.currIndex = -1
def processLine(self, saveHistory=False):
# Save the history
if saveHistory:
self.quotebookHistory.append(cPickle.dumps(self.quotebook, protocol=-1))
else:
self.quotebookHistory.clear()
# Play line
line = self.infile[self.currIndex + 1]
if line[0] == 'B':
self.quotebook.bid(line.rstrip())
elif line[0] == 'A':
self.quotebook.ask(line.rstrip())
else:
self.quotebook.trade(line.rstrip())
self.currIndex += 1
def reverseBook(self):
if len(self.quotebookHistory) > 0:
self.quotebook = cPickle.loads(self.quotebookHistory.pop())
self.currIndex -= 1
def readFile(self, filename):
try:
reader = open(sys.argv[1], 'r')
for line in reader:
self.infile.append(line)
except IOError:
print 'Cannot open input file: %s' % filename
sys.exit(1)
def playLines(self, number):
targetIndex = self.currIndex + number
if targetIndex > self.currIndex:
maxIndex = targetIndex if targetIndex < len(self.infile) else len(self.infile) - 1
while self.currIndex < maxIndex:
if maxIndex - self.currIndex <= self.max_reverse:
self.processLine(saveHistory=True)
else:
self.processLine(saveHistory=False)
elif targetIndex < self.currIndex and targetIndex >= 0:
# Negative lines
reverseLines = self.currIndex - targetIndex
while reverseLines > 0 and len(self.quotebookHistory) > 0:
self.reverseBook()
reverseLines -= 1
def playToLine(self, line):
# subtract 1 because lines start at 1 not 0
self.playLines(line - self.currIndex - 1)
def playTime(self, time):
targetTime = self.quotebook.lastTimestamp + time
if targetTime >= self.quotebook.lastTimestamp:
while self.quotebook.lastTimestamp < targetTime and \
len(self.infile) > self.currIndex + 1:
if targetTime - self.quotebook.lastTimestamp <= 5000 \
or self.currIndex > len(self.infile) - self.max_reverse:
self.processLine(saveHistory=True)
else:
self.processLine(saveHistory=False)
elif targetTime < self.quotebook.lastTimestamp and targetTime >= 0:
# Reverse time
while self.quotebook.lastTimestamp > targetTime \
and len(self.quotebookHistory) > 0:
self.reverseBook()
if self.quotebook.lastTimestamp > targetTime and self.currIndex > 0:
# Play up to the line
self.reset()
self.playTime(targetTime)
def playToTime(self, time):
self.playTime(time - self.quotebook.lastTimestamp)
def getIntValue(self, message):
dimensions = self.screen.getmaxyx()
if len(message) < dimensions[1]:
empty = dimensions[1] - len(message) - 2
self.screen.addstr(dimensions[0] - 2, len(message) + 1, \
" " * empty, curses.A_STANDOUT)
self.screen.addstr(dimensions[0] - 2, 1, message, curses.A_STANDOUT)
curses.curs_set(1);
curses.echo() # show cursor
value = self.screen.getstr()
self.drawHelp()
curses.curs_set(0);
curses.noecho()
try:
return int(value)
except ValueError:
return None
def processKey(self, character):
if character == self.keys['PREVIOUS']:
self.playLines(-1)
elif character == self.keys['NEXT']:
self.playLines(1)
elif character == self.keys['PLAY_FOR_TIME']:
value = self.getIntValue('Milliseconds: ')
if value != None:
self.last_skipped = value
self.playTime(value)
elif character == self.keys['PLAY_TO_TIME']:
value = self.getIntValue('Milliseconds: ')
if value != None:
self.playToTime(value)
elif character == self.keys['PLAY_FOR_LINES']:
value = self.getIntValue('Lines: ')
if value != None:
self.last_skipped = value
self.playLines(value)
elif character == self.keys['PLAY_TO_LINE']:
value = self.getIntValue('Line: ')
if value != None:
self.playToLine(value)
elif character == self.keys['AGGREGATE']:
# Toggle tier aggregation
self.aggregate = True if self.aggregate == False else False
def drawBook(self):
dimensions = self.screen.getmaxyx()
length = dimensions[0] - 5
width = (dimensions[1] - 3) / 3
# Bids
bidWin = curses.newwin(length, width, 3, 2)
book = ''
if self.aggregate:
book = self.quotebook.bidBookAggregatedStr().split('\n')
else:
book = self.quotebook.bidBookStr().split('\n')
for idx, line in enumerate(book):
if idx == 0:
bidWin.addstr(idx, 1, line, curses.A_STANDOUT)
elif idx == length:
break
else:
bidWin.addstr(idx, 1, line)
bidWin.refresh()
# Asks
askWin = curses.newwin(length, width, 3, 3 + width)
book = ''
if self.aggregate:
book = self.quotebook.askBookAggregatedStr().split('\n')
else:
book = self.quotebook.askBookStr().split('\n')
for idx, line in enumerate(book):
if idx == 0:
askWin.addstr(idx, 1, line, curses.A_STANDOUT)
elif idx == length:
break
else:
askWin.addstr(idx, 1, line)
askWin.refresh()
# Trades
tradeWin = curses.newwin(length, width, 3, 3 + (width * 2) - 1)
book = self.quotebook.tradeBookStr().split('\n')
for idx, line in enumerate(book):
if idx == 0:
tradeWin.addstr(idx, 1, line, curses.A_STANDOUT)
elif idx == length:
break
else:
tradeWin.addstr(idx, 1, line)
tradeWin.refresh()
def drawHelp(self):
dimensions = self.screen.getmaxyx()
self.screen.addstr(dimensions[0] - 2, 1, self.help_text, curses.A_STANDOUT)
if len(self.help_text) < self.screen.getmaxyx()[1] - 4:
empty = self.screen.getmaxyx()[1] - len(self.help_text) - 3
self.screen.addstr(dimensions[0] - 2, len(self.help_text) + 1, " " * empty, \
curses.A_STANDOUT)
def drawScreen(self):
self.screen.clear()
self.screen.border(0)
self.screen.refresh()
self.drawHelp()
def main(self, stdscr):
# Draw the shortcut key bar
self.screen = stdscr
self.drawScreen()
# Read the file
self.readFile(sys.argv[1])
self.playLines(1)
exit = False
last_pressed = None
while not exit:
self.drawBook()
# Print current line
line = self.infile[self.currIndex].rstrip() + ' (line ' \
+ str(self.currIndex + 1) + ')'
if self.aggregate:
line += ' [AGGREGATED VIEW]'
dimensions = self.screen.getmaxyx()
if len(line) < dimensions[1] - 4:
empty = dimensions[1] - len(line) - 10
line += (" " * empty)
self.screen.addstr(1, 3, line.replace(',', ', '))
# Refresh the screen
self.screen.refresh()
character = self.screen.getch()
if character == curses.KEY_RESIZE:
# Window resize
self.drawScreen()
self.drawBook()
elif character == self.keys['REPEAT'] and last_pressed != None:
if last_pressed == self.keys['PLAY_FOR_LINES']:
self.playLines(self.last_skipped)
elif last_pressed == self.keys['PLAY_FOR_TIME']:
self.playTime(self.last_skipped)
else:
self.processKey(last_pressed)
elif character == self.keys['EXIT']:
exit = True
else:
last_pressed = character
self.processKey(character)
if __name__ == '__main__':
if len(sys.argv) != 2:
print "usage: %s input.csv" % sys.argv[0]
sys.exit(0)
try:
# Initiate window
stdscr = curses.initscr()
curses.noecho() # hide cursor
# Turn off echoing of keys, and enter cbreak mode,
# where no buffering is performed on keyboard input
curses.noecho();
curses.cbreak()
curses.curs_set(0) # Hide cursor
# In keypad mode, escape sequences for special keys
# (like the cursor keys) will be interpreted and
# a special value like curses.KEY_LEFT will be returned
stdscr.keypad(1)
bookViewer = BookViewer()
bookViewer.main(stdscr) # Enter the main loop
# Set everything back to normal
stdscr.keypad(0)
curses.echo();
curses.nocbreak()
curses.endwin() # Terminate curses
except KeyboardInterrupt:
# ctrl-c
stdscr.keypad(0)
curses.echo();
curses.nocbreak()
curses.endwin()
except:
# In the event of an error, restore the terminal
# to a sane state.
stdscr.keypad(0)
curses.echo();
curses.nocbreak()
curses.endwin()
traceback.print_exc() # Print the exception