-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.py
84 lines (61 loc) · 2.18 KB
/
watch.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
#!/usr/bin/python
import sys
import time
import wx
import matplotlib
matplotlib.use("WXAgg")
matplotlib.rcParams['toolbar'] = 'None'
import matplotlib.pyplot as plt
import pylab
import btceapi
class Chart(object):
def __init__(self, symbol):
self.symbol = symbol
self.base = symbol.split("_")[0].upper()
self.alt = symbol.split("_")[1].upper()
self.ticks = btceapi.getTradeHistory(self.symbol)
self.last_tid = max([t.tid for t in self.ticks])
self.fig = plt.figure()
self.axes = self.fig.add_subplot(111)
self.bid_line, = self.axes.plot(*zip(*self.bid),
linestyle='None', marker='o', color='red')
self.ask_line, = self.axes.plot(*zip(*self.ask),
linestyle='None', marker='o', color='green')
self.fig.canvas.draw()
self.timer_id = wx.NewId()
self.actor = self.fig.canvas.manager.frame
self.timer = wx.Timer(self.actor, id=self.timer_id)
self.timer.Start(10000) # update every 10 seconds
wx.EVT_TIMER(self.actor, self.timer_id, self.update)
pylab.show()
@property
def bid(self):
return [(t.date, t.price) for t in self.ticks if t.trade_type == u'bid']
@property
def ask(self):
return [(t.date, t.price) for t in self.ticks if t.trade_type == u'ask']
def update(self, event):
ticks = btceapi.getTradeHistory(self.symbol)
self.ticks += [t for t in ticks if t.tid > self.last_tid]
for t in ticks:
if t.tid > self.last_tid:
print "%s: %s %f at %s %f" % \
(t.trade_type, self.base, t.amount, self.alt, t.price)
self.last_tid = max([t.tid for t in ticks])
x, y = zip(*self.bid)
self.bid_line.set_xdata(x)
self.bid_line.set_ydata(y)
x, y = zip(*self.ask)
self.ask_line.set_xdata(x)
self.ask_line.set_ydata(y)
pylab.gca().relim()
pylab.gca().autoscale_view()
self.fig.canvas.draw()
if __name__ == "__main__":
#symbol = "btc_usd"
symbol = "dsh_btc"
try:
symbol = sys.argv[1]
except IndexError:
pass
chart = Chart(symbol)