-
Notifications
You must be signed in to change notification settings - Fork 1
/
analyze_liquidity.py
338 lines (272 loc) · 10.8 KB
/
analyze_liquidity.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env python3
from json import loads
from vtclear import clear_screen
import matplotlib.pyplot as plt
from typing import List
# from time import sleep
plt.style.use("dark_background")
def get_bids_volume(actual_bids, old_bids):
# print("#" * 50)
# print(f"Actual bids length: {len(actual_bids)}")
# print(f"Old bids length: {len(old_bids)}")
# print("#" * 50)
volume = 0
unmatched_volume = 0
old_bids_offset = 0
i = 0
stop = False
while stop is False:
# print(f"i: {i}")
# print(f"old_bids_offset: {old_bids_offset}")
if i + old_bids_offset < len(old_bids) and i < len(actual_bids):
actual_bid = actual_bids[i]
old_bid = old_bids[i + old_bids_offset]
# print(f"Actual bid: {actual_bid}")
# print(f"Old bid: {old_bid}")
if float(actual_bid[0]) > float(old_bid[0]):
# Highest bids are on the actual bid side.
# print("· Highest bids are on the actual bid side.")
i += 1
old_bids_offset -= 1
if float(actual_bid[0]) < float(old_bid[0]):
# Lowest bids are on the actual bid side.
# print("· Lowest bids are on the actual bid side.")
i += 0
old_bids_offset += 1
# price * qty.
qty_difference = float(old_bid[1]) * float(old_bid[0])
# print(f"\t- Qty difference: {qty_difference}")
unmatched_volume += qty_difference
if float(actual_bid[0]) == float(old_bid[0]):
# print("· Actual bid price and old bid price match.")
if unmatched_volume > 0:
volume += unmatched_volume
unmatched_volume = 0
# price * old_qty - actual_qty.
plevel_volume = (
float(old_bid[1]) - float(
actual_bid[1])) * float(actual_bid[0])
# print(
# f"volume at this price level: {plevel_volume}")
if plevel_volume > 0:
volume += plevel_volume
i += 1
old_bids_offset += 0
else:
stop = True
# input("CONTINUE")
return volume
def get_asks_volume(actual_asks, old_asks):
# clear_screen()
# print("OLD ASKS")
# pprint(old_asks)
# print("ACTUAL ASKS")
# pprint(actual_asks)
# print("#" * 50)
# print(f"Actual asks length: {len(actual_asks)}")
# print(f"Old asks length: {len(old_asks)}")
# print("#" * 50)
volume = 0
unmatched_volume = 0
old_asks_offset = 0
i = 0
stop = False
while stop is False:
# print(f"i: {i}")
# print(f"old_asks_offset: {old_asks_offset}")
if i + old_asks_offset < len(old_asks) and i < len(actual_asks):
actual_ask = actual_asks[i]
old_ask = old_asks[i + old_asks_offset]
# print(f"Actual ask: {actual_ask}")
# print(f"Old ask: {old_ask}")
if float(actual_ask[0]) < float(old_ask[0]):
# Lowest asks are on the actual ask side.
# print("· Lowest asks are on the actual ask side.")
i += 1
old_asks_offset -= 1
if float(actual_ask[0]) > float(old_ask[0]):
# Higher asks are on the actual ask side.
# print("· Higher asks are on the actual ask side.")
i += 0
old_asks_offset += 1
# price * qty.
qty_difference = float(old_ask[1]) * float(old_ask[0])
# print(f"\t- Qty difference: {qty_difference}")
unmatched_volume += qty_difference
if float(actual_ask[0]) == float(old_ask[0]):
# print("· Actual ask price and old ask price match.")
if unmatched_volume > 0:
volume += unmatched_volume
unmatched_volume = 0
# price * old_qty - actual_qty.
plevel_volume = (
float(old_ask[1]) - float(
actual_ask[1])) * float(actual_ask[0])
# print(
# f"volume at this price level: {plevel_volume}")
if plevel_volume > 0:
volume += plevel_volume
i += 1
old_asks_offset += 0
else:
stop = True
# input("CONTINUE")
# print(f"> ASKS VOLUME: {volume}")
# sleep(0.1)
return volume
def analyze_liquidity(EX1, EX2, ob_log_list):
ex1_bids = None
ex1_asks = None
ex2_bids = None
ex2_asks = None
ex1_bids_volume_timestamp_list = []
ex2_bids_volume_timestamp_list = []
ex1_bids_volume_list: List[float] = []
ex2_bids_volume_list: List[float] = []
ex1_asks_volume_timestamp_list = []
ex2_asks_volume_timestamp_list = []
ex1_asks_volume_list: List[float] = []
ex2_asks_volume_list: List[float] = []
for line in range(len(ob_log_list)):
clear_screen()
print(f"Analyzing: {(line / len(ob_log_list)) * 100:.2f}%")
data = loads(ob_log_list[line])
##################
# EXCHANGE 1 #
##################
if data["exchange"] == EX1:
old_ex1_bids = ex1_bids
old_ex1_asks = ex1_asks
ex1_bids = data["bids"]
ex1_asks = data["asks"]
# Bids change analysis.
# print("· BIDS CHANGE ANALYSIS.")
if old_ex1_bids is not None and ex1_bids is not None:
ex1_bids_volume = get_bids_volume(
actual_bids=ex1_bids,
old_bids=old_ex1_bids
)
# print(f"- BIDS VOLUME: {ex1_bids_volume}")
ex1_bids_volume_list.append(ex1_bids_volume)
ex1_bids_volume_timestamp_list.append(data["timestamp"])
# Asks change analysis.
# print("· ASKS CHANGE ANALYSIS.")
if old_ex1_asks is not None and ex1_asks is not None:
ex1_asks_volume = get_asks_volume(
actual_asks=ex1_asks,
old_asks=old_ex1_asks
)
# print(f"- ASKS VOLUME: {ex1_asks_volume}")
ex1_asks_volume_list.append(ex1_asks_volume)
ex1_asks_volume_timestamp_list.append(data["timestamp"])
##################
# EXCHANGE 2 #
##################
if data["exchange"] == EX2:
old_ex2_bids = ex2_bids
old_ex2_asks = ex2_asks
ex2_bids = data["bids"]
ex2_asks = data["asks"]
# Bids change analysis.
# print("· BIDS CHANGE ANALYSIS")
if old_ex2_bids is not None and ex2_bids is not None:
ex2_bids_consumption = get_bids_volume(
actual_bids=ex2_bids,
old_bids=old_ex2_bids
)
# print(f"- BIDS CONSUMPTION: {ex2_bids_consumption}")
ex2_bids_volume_list.append(ex2_bids_consumption)
ex2_bids_volume_timestamp_list.append(data["timestamp"])
# Asks change analysis.
# print("· ASKS CHANGE ANALYSIS.")
if old_ex2_asks is not None and ex2_asks is not None:
ex2_asks_volume = get_asks_volume(
actual_asks=ex2_asks,
old_asks=old_ex2_asks
)
# print(f"- ASKS VOLUME: {ex2_asks_volume}")
ex2_asks_volume_list.append(ex2_asks_volume)
ex2_asks_volume_timestamp_list.append(data["timestamp"])
return ex1_bids_volume_timestamp_list, ex1_bids_volume_list,\
ex2_bids_volume_timestamp_list, ex2_bids_volume_list,\
ex1_asks_volume_timestamp_list, ex1_asks_volume_list,\
ex2_asks_volume_timestamp_list, ex2_asks_volume_list
if __name__ == "__main__":
EX1 = "EX1"
EX2 = "EX2"
ob_log_filename = "ORDER_BOOK.csv"
clear_screen()
print("· LOADING FILES...")
with open(ob_log_filename, "r") as f:
order_book_content = f.read()
ob_log_list = order_book_content.split("\n")[:-1]
ex1_bids_volume_timestamp_list, ex1_bids_volume_list,\
ex2_bids_volume_timestamp_list, ex2_bids_volume_list,\
ex1_asks_volume_timestamp_list, ex1_asks_volume_list,\
ex2_asks_volume_timestamp_list, ex2_asks_volume_list\
= analyze_liquidity(
EX1=EX1,
EX2=EX2,
ob_log_list=ob_log_list
)
with open("analysis.txt", "w") as f:
f.write(str(ex1_bids_volume_timestamp_list) + "\n")
f.write(str(ex1_bids_volume_list) + "\n")
f.write(str(ex2_bids_volume_timestamp_list) + "\n")
f.write(str(ex2_bids_volume_list) + "\n")
f.write(str(ex1_asks_volume_timestamp_list) + "\n")
f.write(str(ex1_asks_volume_list) + "\n")
f.write(str(ex2_asks_volume_timestamp_list) + "\n")
f.write(str(ex2_asks_volume_list))
clear_screen()
print("--- RESULTS ---")
ex1_bids_volume = sum(ex1_bids_volume_list)
ex2_bids_volume = sum(ex2_bids_volume_list)
ex1_asks_volume = sum(ex1_asks_volume_list)
ex2_asks_volume = sum(ex2_asks_volume_list)
print("-" * 30)
print(f"EX1 bids volume: {ex1_bids_volume:.2f}")
print(f"EX2 bids volume: {ex2_bids_volume:.2f}")
print(f"EX1 asks volume: {ex1_asks_volume:.2f}")
print(f"EX2 asks volume: {ex2_asks_volume:.2f}")
print("-" * 30)
ex1_lapse = max(
ex1_asks_volume_timestamp_list[-1],
ex1_bids_volume_timestamp_list[-1]
) - min(
ex1_asks_volume_timestamp_list[0],
ex1_bids_volume_timestamp_list[0]
)
ex1_avg_hourly_volume = (
(ex1_bids_volume + ex1_asks_volume) / ex1_lapse) * 3600
ex2_lapse = max(
ex2_asks_volume_timestamp_list[-1],
ex2_bids_volume_timestamp_list[-1]
) - min(
ex2_asks_volume_timestamp_list[0],
ex2_bids_volume_timestamp_list[0]
)
ex2_avg_hourly_volume = (
(ex2_bids_volume + ex2_asks_volume) / ex2_lapse) * 3600
print(f"EX1 average hourly volume: {ex1_avg_hourly_volume:.2f}")
print(f"EX2 average hourly volume: {ex2_avg_hourly_volume:.2f}")
print("-" * 30)
plt.grid(c="#222222")
plt.plot(
ex1_bids_volume_timestamp_list, ex1_bids_volume_list,
label="EX1 BIDS", lw=0.5, c="#00ff00"
)
plt.plot(
ex2_bids_volume_timestamp_list, ex2_bids_volume_list,
label="EX2 BIDS", lw=0.5, c="#00ffff"
)
plt.plot(
ex1_asks_volume_timestamp_list, ex1_asks_volume_list,
label="EX1 ASKS", lw=0.5, c="#ff0000"
)
plt.plot(
ex2_asks_volume_timestamp_list, ex2_asks_volume_list,
label="EX2 ASKS", lw=0.5, c="#ffff00"
)
plt.legend()
plt.show()