-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfunc_triangular_arb.py
More file actions
421 lines (360 loc) · 17.9 KB
/
func_triangular_arb.py
File metadata and controls
421 lines (360 loc) · 17.9 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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# Structure trading pair groups
def structure_trading_pairs(pairs, limit):
triangular_pairs_list = []
remove_duplicates_list = []
pairs_list = pairs[:limit]
# Loop through each coin to find potential matches
for pair_a in pairs_list:
# Get pair A
a_base = pair_a["token0"]["symbol"]
a_quote = pair_a["token1"]["symbol"]
a_pair = a_base + "_" + a_quote
a_token_0_id = pair_a["token0"]["id"]
a_token_1_id = pair_a["token1"]["id"]
a_contract = pair_a["id"]
a_token_0_decimals = pair_a["token0"]["decimals"]
a_token_1_decimals = pair_a["token1"]["decimals"]
a_token_0_price = pair_a["token0Price"]
a_token_1_price = pair_a["token1Price"]
# Put A into a box for checking at B
a_pair_box = [a_base, a_quote]
for pair_b in pairs_list:
# Get pair B
b_base = pair_b["token0"]["symbol"]
b_quote = pair_b["token1"]["symbol"]
b_pair = b_base + "_" + b_quote
b_token_0_id = pair_b["token0"]["id"]
b_token_1_id = pair_b["token1"]["id"]
b_contract = pair_b["id"]
b_token_0_decimals = pair_b["token0"]["decimals"]
b_token_1_decimals = pair_b["token1"]["decimals"]
b_token_0_price = pair_b["token0Price"]
b_token_1_price = pair_b["token1Price"]
# Get Pair C
if a_pair != b_pair:
if b_base in a_pair_box or b_quote in a_pair_box:
for pair_c in pairs_list:
c_base = pair_c["token0"]["symbol"]
c_quote = pair_c["token1"]["symbol"]
c_pair = c_base + "_" + c_quote
c_token_0_id = pair_c["token0"]["id"]
c_token_1_id = pair_c["token1"]["id"]
c_contract = pair_c["id"]
c_token_0_decimals = pair_c["token0"]["decimals"]
c_token_1_decimals = pair_c["token1"]["decimals"]
c_token_0_price = pair_c["token0Price"]
c_token_1_price = pair_c["token1Price"]
# Count number of C items
if c_pair != a_pair and c_pair != b_pair:
combine_all = [a_pair, b_pair, c_pair]
pair_box = [a_base, a_quote, b_base, b_quote, c_base, c_quote]
counts_c_base = 0
for i in pair_box:
if i == c_base:
counts_c_base += 1
counts_c_quote = 0
for i in pair_box:
if i == c_quote:
counts_c_quote += 1
if counts_c_base == 2 and counts_c_quote == 2 and c_base != c_quote:
combined = a_pair + "," + b_pair + "," + c_pair
unique_string = ''.join(sorted(combined))
# Output pair
if unique_string not in remove_duplicates_list:
output_dict = {
"aPair": a_pair,
"aBase": a_base,
"aQuote": a_quote,
"bPair": b_pair,
"bBase": b_base,
"bQuote": b_quote,
"cPair": c_pair,
"cBase": c_base,
"cQuote": c_quote,
"combined": combined,
"aToken0Id": a_token_0_id,
"bToken0Id": b_token_0_id,
"cToken0Id": c_token_0_id,
"aToken1Id": a_token_1_id,
"bToken1Id": b_token_1_id,
"cToken1Id": c_token_1_id,
"aContract": a_contract,
"bContract": b_contract,
"cContract": c_contract,
"aToken0Decimals": a_token_0_decimals,
"aToken1Decimals": a_token_1_decimals,
"bToken0Decimals": b_token_0_decimals,
"bToken1Decimals": b_token_1_decimals,
"cToken0Decimals": c_token_0_decimals,
"cToken1Decimals": c_token_1_decimals,
"aToken0Price": a_token_0_price,
"aToken1Price": a_token_1_price,
"bToken0Price": b_token_0_price,
"bToken1Price": b_token_1_price,
"cToken0Price": c_token_0_price,
"cToken1Price": c_token_1_price
}
triangular_pairs_list.append(output_dict)
remove_duplicates_list.append(unique_string)
return triangular_pairs_list
# Calculate surface rate arb potential
def calc_triangular_arb_surface_rate(t_pair, min_rate):
# Set variables
min_surface_rate = min_rate
surface_dict = {}
pool_contract_2 = ""
pool_contract_3 = ""
pool_direction_trade_1 = ""
pool_direction_trade_2 = ""
pool_direction_trade_3 = ""
# Calculate looping through forward and reverse rates
direction_list = ["forward", "reverse"]
for direction in direction_list:
# Set pair info
a_base = t_pair["aBase"]
a_quote = t_pair["aQuote"]
b_base = t_pair["bBase"]
b_quote = t_pair["bQuote"]
c_base = t_pair["cBase"]
c_quote = t_pair["cQuote"]
# Set price info
a_token_0_price = float(t_pair["aToken0Price"])
a_token_1_price = float(t_pair["aToken1Price"])
b_token_0_price = float(t_pair["bToken0Price"])
b_token_1_price = float(t_pair["bToken1Price"])
c_token_0_price = float(t_pair["cToken0Price"])
c_token_1_price = float(t_pair["cToken1Price"])
# Set address info
a_contract = t_pair["aContract"]
b_contract = t_pair["bContract"]
c_contract = t_pair["cContract"]
# Set variables
starting_amount = 1
acquired_coin_t2 = 0
acquired_coin_t3 = 0
calculated = 0
swap_1 = 0
swap_2 = 0
swap_3 = 0
swap_1_rate = 0
swap_2_rate = 0
swap_3_rate = 0
# Assume start with aBase if forward
if direction == "forward":
swap_1 = a_base
swap_2 = a_quote
swap_1_rate = a_token_1_price
pool_direction_trade_1 = "baseToQuote"
# Assume start with aQuote if forward
if direction == "reverse":
swap_1 = a_quote
swap_2 = a_base
swap_1_rate = a_token_0_price
pool_direction_trade_1 = "quoteToBase"
# Place first trade
pool_contract_1 = a_contract
acquired_coin_t1 = starting_amount * swap_1_rate
# Forward: check if aQuote (acquired coin) matches bQuote //////////
if direction == "forward":
if a_quote == b_quote and calculated == 0:
swap_2_rate = b_token_0_price
acquired_coin_t2 = acquired_coin_t1 * swap_2_rate
pool_direction_trade_2 = "quoteToBase"
pool_contract_2 = b_contract
# Forward: check if bBase (acquired coin) matches cBase
if b_base == c_base:
swap_3 = c_base
swap_3_rate = c_token_1_price
pool_direction_trade_3 = "baseToQuote"
pool_contract_3 = c_contract
# Forward: check if bBase (acquired coin) matches cQuote
if b_base == c_quote:
swap_3 = c_quote
swap_3_rate = c_token_0_price
pool_direction_trade_3 = "quoteToBase"
pool_contract_3 = c_contract
acquired_coin_t3 = acquired_coin_t2 * swap_3_rate
calculated = 1
# Forward: check if aQuote (acquired coin) matches bBase //////////
if direction == "forward":
if a_quote == b_base and calculated == 0:
swap_2_rate = b_token_1_price
acquired_coin_t2 = acquired_coin_t1 * swap_2_rate
pool_direction_trade_2 = "baseToQuote"
pool_contract_2 = b_contract
# Forward: check if bBase (acquired coin) matches cBase
if b_quote == c_base:
swap_3 = c_base
swap_3_rate = c_token_1_price
pool_direction_trade_3 = "baseToQuote"
pool_contract_3 = c_contract
# Forward: check if bBase (acquired coin) matches cQuote
if b_quote == c_quote:
swap_3 = c_quote
swap_3_rate = c_token_0_price
pool_direction_trade_3 = "quoteToBase"
pool_contract_3 = c_contract
acquired_coin_t3 = acquired_coin_t2 * swap_3_rate
calculated = 1
# Forward: check if aQuote (acquired coin) matches cQuote //////////
if direction == "forward":
if a_quote == c_quote and calculated == 0:
swap_2_rate = c_token_0_price
acquired_coin_t2 = acquired_coin_t1 * swap_2_rate
pool_direction_trade_2 = "quoteToBase"
pool_contract_2 = c_contract
# Forward: check if cBase (acquired coin) matches bBase
if c_base == b_base:
swap_3 = b_base
swap_3_rate = b_token_1_price
pool_direction_trade_3 = "baseToQuote"
pool_contract_3 = b_contract
# Forward: check if cBase (acquired coin) matches bQuote
if c_base == b_quote:
swap_3 = b_quote
swap_3_rate = b_token_0_price
pool_direction_trade_3 = "quoteToBase"
pool_contract_3 = b_contract
acquired_coin_t3 = acquired_coin_t2 * swap_3_rate
calculated = 1
# Forward: check if aQuote (acquired coin) matches cBase //////////
if direction == "forward":
if a_quote == c_base and calculated == 0:
swap_2_rate = c_token_1_price
acquired_coin_t2 = acquired_coin_t1 * swap_2_rate
pool_direction_trade_2 = "baseToQuote"
pool_contract_2 = c_contract
# Forward: check if cQuote (acquired coin) matches bBase
if c_quote == b_base:
swap_3 = b_base
swap_3_rate = b_token_1_price
pool_direction_trade_3 = "baseToQuote"
pool_contract_3 = b_contract
# Forward: check if cQuote (acquired coin) matches bQuote
if c_quote == b_quote:
swap_3 = b_quote
swap_3_rate = b_token_0_price
pool_direction_trade_3 = "quoteToBase"
pool_contract_3 = b_contract
acquired_coin_t3 = acquired_coin_t2 * swap_3_rate
calculated = 1
# Reverse: check if aBase (acquired coin) matches bBase //////////
if direction == "reverse":
if a_base == b_base and calculated == 0:
swap_2_rate = b_token_1_price
acquired_coin_t2 = acquired_coin_t1 * swap_2_rate
pool_direction_trade_2 = "baseToQuote"
pool_contract_2 = b_contract
# Forward: check if bQuote (acquired coin) matches cQuote
if b_quote == c_quote:
swap_3 = c_quote
swap_3_rate = c_token_0_price
pool_direction_trade_3 = "quoteToBase"
pool_contract_3 = c_contract
# Forward: check if bQuote (acquired coin) matches cBase
if b_quote == c_base:
swap_3 = c_base
swap_3_rate = c_token_1_price
pool_direction_trade_3 = "baseToQuote"
pool_contract_3 = c_contract
acquired_coin_t3 = acquired_coin_t2 * swap_3_rate
calculated = 1
# Reverse: check if aBase (acquired coin) matches bQuote //////////
if direction == "reverse":
if a_base == b_quote and calculated == 0:
swap_2_rate = b_token_0_price
acquired_coin_t2 = acquired_coin_t1 * swap_2_rate
pool_direction_trade_2 = "quoteToBase"
pool_contract_2 = b_contract
# Forward: check if bBase (acquired coin) matches cQuote
if b_base == c_quote:
swap_3 = c_quote
swap_3_rate = c_token_0_price
pool_direction_trade_3 = "quoteToBase"
pool_contract_3 = c_contract
# Forward: check if bBase (acquired coin) matches cBase
if b_base == c_base:
swap_3 = c_base
swap_3_rate = c_token_1_price
pool_direction_trade_3 = "baseToQuote"
pool_contract_3 = c_contract
acquired_coin_t3 = acquired_coin_t2 * swap_3_rate
calculated = 1
# Reverse: check if aBase (acquired coin) matches cBase //////////
if direction == "reverse":
if a_base == c_base and calculated == 0:
swap_2_rate = c_token_1_price
acquired_coin_t2 = acquired_coin_t1 * swap_2_rate
pool_direction_trade_2 = "baseToQuote"
pool_contract_2 = c_contract
# Forward: check if cQuote (acquired coin) matches bQuote
if c_quote == b_quote:
swap_3 = b_quote
swap_3_rate = b_token_0_price
pool_direction_trade_3 = "quoteToBase"
pool_contract_3 = b_contract
# Forward: check if cQuote (acquired coin) matches bBase
if c_quote == b_base:
swap_3 = b_base
swap_3_rate = b_token_1_price
pool_direction_trade_3 = "baseToQuote"
pool_contract_3 = b_contract
acquired_coin_t3 = acquired_coin_t2 * swap_3_rate
calculated = 1
# Reverse: check if aBase (acquired coin) matches cQuote //////////
if direction == "reverse":
if a_base == c_quote and calculated == 0:
swap_2_rate = c_token_0_price
acquired_coin_t2 = acquired_coin_t1 * swap_2_rate
pool_direction_trade_2 = "quoteToBase"
pool_contract_2 = c_contract
# Forward: check if cBase (acquired coin) matches bQuote
if c_base == b_quote:
swap_3 = b_quote
swap_3_rate = b_token_0_price
pool_direction_trade_3 = "quoteToBase"
pool_contract_3 = b_contract
# Forward: check if cBase (acquired coin) matches bBase
if c_base == b_base:
swap_3 = b_base
swap_3_rate = b_token_1_price
pool_direction_trade_3 = "baseToQuote"
pool_contract_3 = b_contract
acquired_coin_t3 = acquired_coin_t2 * swap_3_rate
calculated = 1
# Calculate arbitrage results
profit_loss = acquired_coin_t3 - starting_amount
profit_loss_perc = (profit_loss / starting_amount) * 100 if profit_loss != 0 else 0
# Format Description
trade_description_1 = f"Start with {swap_1} of {starting_amount}. Swap at {swap_1_rate} for {swap_2} acquiring {acquired_coin_t1}."
trade_description_2 = f"Swap {acquired_coin_t1} of {swap_2} at {swap_2_rate} for {swap_3} acquiring {acquired_coin_t2}."
trade_description_3 = f"Swap {acquired_coin_t2} of {swap_3} at {swap_3_rate} for {swap_1} acquiring {acquired_coin_t3}."
# Filter for significant opportunity size
if profit_loss_perc >= min_surface_rate:
# Construct Output
surface_dict = {
"swap1": swap_1,
"swap2": swap_2,
"swap3": swap_3,
"poolContract1": pool_contract_1,
"poolContract2": pool_contract_2,
"poolContract3": pool_contract_3,
"poolDirectionTrade1": pool_direction_trade_1,
"poolDirectionTrade2": pool_direction_trade_2,
"poolDirectionTrade3": pool_direction_trade_3,
"startingAmount": starting_amount,
"acquiredCoinT1": acquired_coin_t1,
"acquiredCoinT2": acquired_coin_t2,
"acquiredCoinT3": acquired_coin_t3,
"swap1Rate": swap_1_rate,
"swap2Rate": swap_2_rate,
"swap3Rate": swap_3_rate,
"profitLoss": profit_loss,
"profitLossPerc": profit_loss_perc,
"direction": direction,
"tradeDesc1": trade_description_1,
"tradeDesc2": trade_description_2,
"tradeDesc3": trade_description_3
}
return surface_dict
return surface_dict