forked from pmaji/crypto-whale-watching-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
215 lines (178 loc) · 8.02 KB
/
app.py
File metadata and controls
215 lines (178 loc) · 8.02 KB
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
# Don't forget, if you find this useful please send ETH donations to: 0xc8a12868f79A5d77530E91247FEC84497890e572
# modules
# dash-related for app itself
import dash
from dash.dependencies import Output, Event
import dash_core_components as dcc
import dash_html_components as html
# non-dash-related libraries
import plotly.graph_objs as go
import pandas as pd
import gdax
import numpy as np
public_client = gdax.PublicClient() # defines public client for all functions; taken from GDAX
# function to get data from GDAX to be referenced in our call-back later
def get_data():
order_book = public_client.get_product_order_book('ETH-USD', level=3)
ask_tbl = pd.DataFrame(data=order_book['asks'], columns=['price', 'volume', 'address'])
bid_tbl = pd.DataFrame(data=order_book['bids'], columns=['price', 'volume', 'address'])
# building subsetted table for ask data only
# sell side (would be Magma)
ask_tbl['price'] = pd.to_numeric(ask_tbl['price'])
ask_tbl['volume'] = pd.to_numeric(ask_tbl['volume'])
first_ask = float(ask_tbl.iloc[1, 0])
perc_above_first_ask = (1.025 * first_ask)
ask_tbl = ask_tbl[(ask_tbl['price'] <= perc_above_first_ask)]
ask_tbl['color'] = 'red'
# building subsetted table for bid data only
# buy side (would be Viridis)
bid_tbl['price'] = pd.to_numeric(bid_tbl['price'])
bid_tbl['volume'] = pd.to_numeric(bid_tbl['volume'])
first_bid = float(bid_tbl.iloc[1, 0])
perc_above_first_bid = (0.975 * first_bid)
bid_tbl = bid_tbl[(bid_tbl['price'] >= perc_above_first_bid)]
bid_tbl['color'] = 'green'
# flip the bid table
bid_tbl = bid_tbl.iloc[::-1]
# append the buy and sell side tables to create one cohesive view
fulltbl = bid_tbl.append(ask_tbl)
# limit our view to only orders greater than or equal to 1 ETH in size
fulltbl = fulltbl[(fulltbl['volume'] >= 1)]
# takes the square root of the volume (to be used later on for the purpose of sizing the orders
fulltbl['sqrt'] = np.sqrt(fulltbl['volume'])
final_tbl = fulltbl.groupby(['price'])[['volume']].sum()
final_tbl['n_unique_orders'] = fulltbl.groupby('price').address.nunique().astype(float)
final_tbl['price'] = final_tbl.index
final_tbl['sqrt'] = np.sqrt(final_tbl['volume'])
# making the tooltip
final_tbl['text'] = ("There are " + final_tbl['volume'].map(str) + " ETH available for $" + final_tbl['price'].map(str) + " being offered by " + final_tbl['n_unique_orders'].map(str) + " ETH addresses")
# get market price
mp = public_client.get_product_ticker(product_id='ETH-USD')
final_tbl['market price'] = mp['price']
# makes the type float so that the next logical comparison can take place
final_tbl['market price'] = final_tbl['market price'].astype(float)
# determine buys / sells relative to last market price
final_tbl['color'] = np.where(final_tbl['price'] > final_tbl['market price'], 'red', 'green')
return final_tbl
######ETH-BTC#######
def get_data_ethbtc():
order_book = public_client.get_product_order_book('ETH-BTC', level=3)
ask_tbl = pd.DataFrame(data=order_book['asks'], columns=['price', 'volume', 'address'])
bid_tbl = pd.DataFrame(data=order_book['bids'], columns=['price', 'volume', 'address'])
# building subsetted table for ask data only
# sell side (would be Magma)
ask_tbl['price'] = pd.to_numeric(ask_tbl['price'])
ask_tbl['volume'] = pd.to_numeric(ask_tbl['volume'])
first_ask = float(ask_tbl.iloc[1, 0])
perc_above_first_ask = (1.025 * first_ask)
ask_tbl = ask_tbl[(ask_tbl['price'] <= perc_above_first_ask)]
ask_tbl['color'] = 'red'
# building subsetted table for bid data only
# buy side (would be Viridis)
bid_tbl['price'] = pd.to_numeric(bid_tbl['price'])
bid_tbl['volume'] = pd.to_numeric(bid_tbl['volume'])
first_bid = float(bid_tbl.iloc[1, 0])
perc_above_first_bid = (0.975 * first_bid)
bid_tbl = bid_tbl[(bid_tbl['price'] >= perc_above_first_bid)]
bid_tbl['color'] = 'green'
# flip the bid table
bid_tbl = bid_tbl.iloc[::-1]
# append the buy and sell side tables to create one cohesive view
fulltbl = bid_tbl.append(ask_tbl)
# limit our view to only orders greater than or equal to 1 ETH in size
fulltbl = fulltbl[(fulltbl['volume'] >= 1)]
# takes the square root of the volume (to be used later on for the purpose of sizing the orders
fulltbl['sqrt'] = np.sqrt(fulltbl['volume'])
final_tbl = fulltbl.groupby(['price'])[['volume']].sum()
final_tbl['n_unique_orders'] = fulltbl.groupby('price').address.nunique().astype(float)
final_tbl['price'] = final_tbl.index
final_tbl['sqrt'] = np.sqrt(final_tbl['volume'])
# making the tooltip
final_tbl['text'] = ("There are " + final_tbl['volume'].map(str) + " ETH available for " + final_tbl['price'].map(
str) + " BTC being offered by " + final_tbl['n_unique_orders'].map(str) + " ETH addresses")
# get market price
mp = public_client.get_product_ticker(product_id='ETH-BTC')
final_tbl['market price'] = mp['price']
# makes the type float so that the next logical comparison can take place
final_tbl['market price'] = final_tbl['market price'].astype(float)
# determine buys / sells relative to last market price
final_tbl['color'] = np.where(final_tbl['price'] > final_tbl['market price'], 'red', 'green')
return final_tbl
# begin building the dash itself
app = dash.Dash()
app.layout = html.Div([
html.H2('WHALE WATCHING APP (support / donations appreciated)'),
html.H3('BTC Address: 1BtEBzRxymw6NvtCfoGheLuh2E2iS5mPuo'),
html.H3('ETH Address: 0x2A817af4F3e562BC3BB7E20e19b5d32E65DC7227'),
html.H3('GitHub: https://github.com/pmaji/eth_python_tracker'),
dcc.Graph(
id='live-graph',
),
dcc.Graph(
id='live-graph-ethbtc',
),
dcc.Interval(
id='interval-component',
interval=1 * 10000 # in milliseconds for the automatic refresh
)
])
# links up the chart creation to the interval for an auto-refresh
@app.callback(Output('live-graph', 'figure'),
events=[Event('interval-component', 'interval')])
def update_graph():
result = get_data()
return {
'data': [
go.Scatter(
x=result['volume'],
y=result['price'],
mode='markers',
text= result['text'],
opacity=0.7,
hoverinfo='text',
marker={
'size': result['sqrt'],
'line': {'width': 0.5, 'color': 'white'},
'color': result['color'] # set color equal to variable
},
)
],
'layout': go.Layout(
# makes it so that title automatically updates with refreshed market price as well
title=("The present market price of ETH is: $" + str(result['market price'].iloc[0])),
xaxis={'title': 'Order Size'},
yaxis={'title': 'ETH Price'},
hovermode='closest'
)
}
# BTCETH#
@app.callback(Output('live-graph-ethbtc', 'figure'),
events=[Event('interval-component', 'interval')])
def update_graph_ethbtc():
result = get_data_ethbtc()
return {
'data': [
go.Scatter(
x=result['volume'],
y=result['price'],
mode='markers',
text=result['text'],
opacity=0.7,
hoverinfo='text',
marker={
'size': result['sqrt'],
'line': {'width': 0.5, 'color': 'white'},
'color': result['color']
},
)
],
'layout': go.Layout(
# makes it so that title automatically updates with refreshed market price as well
title=("The present market price of ETHBTC is: $" + str(result['market price'].iloc[0])),
xaxis={'title': 'Order Size'},
yaxis={'title': 'ETHBTC Price'},
hovermode='closest'
)
}
if __name__ == '__main__':
app.run_server(host='0.0.0.0')