-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXRPLmonitor.py
185 lines (128 loc) · 5.88 KB
/
XRPLmonitor.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
import time
import json
import display
import RippleDataAPI
tft = None
def initDisplay():
global tft
if tft is None:
# Instantiate TFT
tft = display.TFT()
# Initialize TFT (set pins)
tft.init(tft.ILI9341, spihost=tft.VSPI, speed= 40000000, width=240,
height=320, miso=19, mosi=23, clk=18, dc=27, cs=14, rst_pin=33,
backl_pin=32, backl_on=1, hastouch=tft.TOUCH_XPT, tcs=12, splash=False, bgr=True)
# Load XRP logo
tft.image(tft.CENTER, 0, 'xrp.jpg')
# Set font
tft.font('consola18', color=tft.WHITE)
# Initialize display
initDisplay()
class Plot:
def __init__(self, size, x0, y0, x1, y1, color):
self.data = [None for i in range(size)]
self.maxVal = 0
self.minVal = 1e10
self.scale = 1
self.barWidth = 4
self.color = color
self.coordinates = [[x0, y0], [x1, y1]]
self.limitHeight = self.coordinates[1][1] - self.coordinates[0][1]
# Set actual window to plot
tft.setwin(self.coordinates[0][0],
self.coordinates[0][1],
self.coordinates[1][0],
self.coordinates[1][1])
# Clear with grey
tft.clearwin(tft.DARKGREY)
# Show WAIT word centered on the screen
tft.text(tft.CENTER, tft.CENTER, 'WAIT', color=tft.GREEN, transparent=True)
# Reset window
tft.resetwin()
def update(self, newData):
# Update data values
self.data.pop(0)
self.data.append(newData)
# Update max and min values
# OptionA: scale bars according to the whole history
# if newData > self.maxVal:
# self.maxVal = newData
# if newData < self.minVal:
# self.minVal = newData
# OptionB: scale bars according to the current displayed data
notNoneData = [i for i in self.data if i is not None]
self.maxVal = max(notNoneData)
self.minVal = min(notNoneData)
# Recalculate scale factor: 20% of the plot height is reserved for the minimum bar height
if self.maxVal != self.minVal:
self.scale = 0.8 * self.limitHeight / (self.maxVal - self.minVal)
# Set actual window to plot
tft.setwin(self.coordinates[0][0],
self.coordinates[0][1],
self.coordinates[1][0],
self.coordinates[1][1])
# Clear with grey
tft.clearwin(tft.DARKGREY)
xStart = 0
yStart = self.limitHeight
# Shift data to fit min value with zero
shiftedData = [None if i is None else (i - self.minVal) for i in self.data]
# Draw bars: 20% of the plot height is the minimum bar height
for i in shiftedData:
if i is not None:
barHeight = int(round(0.2 * self.limitHeight + self.scale * i))
#tft.line(xStart, yStart - barHeight, xStart + self.barWidth, yStart - barHeight, self.color)
tft.rect(xStart, yStart - barHeight, self.barWidth, barHeight, tft.BLACK, self.color)
xStart += self.barWidth
# Reset window
tft.resetwin()
def run():
tft.clear()
# Initialize UI
tft.image(tft.CENTER, 10, 'xrp_basic.jpg', scale=2)
tft.text(tft.CENTER, 60, 'LIVE XRPL 24h STATS', color=tft.WHITE)
tft.text(tft.CENTER, 80, 'XRP/USD: WAIT')
tft.text(tft.CENTER, 100, 'Exchange volume: WAIT')
tft.text(tft.CENTER, 120, 'Payment volume: WAIT')
tft.text(tft.CENTER, 140, 'External volume: WAIT')
tft.rect(0, 160, 240, 160, tft.BLUE, tft.BLUE)
# Initialize plots
exchangeVolumePlot = Plot(58, 4, 164, 236, 212, tft.YELLOW)
paymentVolumePlot = Plot(58, 4, 216, 236, 264, tft.RED)
externalMarketsVolumePlot = Plot(58, 4, 268, 236, 316, tft.PURPLE)
avgUSDXRPrate = 0
while True:
# Instantiate API
api = RippleDataAPI.RippleDataAPI()
# Get last 50 trades on Bitstamp
exchanges = api.getExchanges(['XRP', 'USD+rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B'], {'limit': '50', 'descending': 'true'})
if exchanges['result'] == 'success':
# Calculate XRP/USD price averaged by volume during the last 50 trades on Bitstamp
avgUSDXRPrate = 0
total_counter_amount = 0
for i in exchanges['exchanges']:
counter_amount = float(i['counter_amount'])
avgUSDXRPrate += float(i['rate']) * counter_amount
total_counter_amount += counter_amount
avgUSDXRPrate /= total_counter_amount
# Print XRP/USD price
tft.text(tft.CENTER, 80, 'XRP/USD: {:.4f}'.format(avgUSDXRPrate), color=tft.GREEN)
# Get and print exchange volume
exchangeVolumes = api.getExchangeVolume()
if exchangeVolumes['result'] == 'success':
exchangeVolume = float(exchangeVolumes['rows'][0]['total'])
tft.text(tft.CENTER, 100, 'Exchange volume: {:.2f}M$'.format(exchangeVolume * avgUSDXRPrate / 1e6), color=tft.YELLOW)
# Get and print payment volume
paymentVolumes = api.getPaymentVolume()
if paymentVolumes['result'] == 'success':
paymentVolume = float(paymentVolumes['rows'][0]['total'])
tft.text(tft.CENTER, 120, 'Payment volume: {:.2f}M$'.format(paymentVolume * avgUSDXRPrate / 1e6), color=tft.RED)
# Get and print external volume
externalMarketsVolumes = api.getExternalMarkets()
if externalMarketsVolumes['result'] == 'success':
externalMarketsVolume = float(externalMarketsVolumes['data']['total'])
tft.text(tft.CENTER, 140, 'External volume: {:.2f}M$'.format(externalMarketsVolume * avgUSDXRPrate / 1e6), color=tft.PURPLE)
# Update plots
exchangeVolumePlot.update(exchangeVolume)
paymentVolumePlot.update(paymentVolume)
externalMarketsVolumePlot.update(externalMarketsVolume)