-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcrypto.py
609 lines (579 loc) · 21 KB
/
crypto.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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
import ccxt
import secrets
from datetime import datetime
import telegram
import os
import time
import config
from operator import itemgetter
"""
This class is a manager for multiple crypto exchanges.
You can get your balance, create orders...
Most of functions takes exchange as argument, this is the exchnage you want
to use. For example, to use binance:
crypto.get_fees(crypto.binance)
Asset1 and asset2 represents A1/A2 pair. If you want this ETH/BTC price,
asset1 is 'ETH' and asset2 is 'BTC'.
"""
class Crypto:
binance = None
bittrex = None
bot = None
cache_prices = []
cache_order_books = []
ORDER_NOT_FILLED = 0
ORDER_IN_PROGRESS = 1
ORDER_FILLED = 2
def __init__(self):
self.init_ccxt()
self.bot = telegram.Bot(token=secrets.TELEGRAM)
"""
Reset caches.
"""
def flush_cache(self):
self.cache_prices = []
self.cache_order_books = []
"""
Check if a price is cached, if yes, returns it.
exchange: the wanted exchange.
asset1: first asset.
asset2: second asset.
"""
def get_price_cache(self, exchange, asset1, asset2):
for item in self.cache_prices:
if (item['exchange'] == str(exchange) and item['asset1'] == asset1 and item['asset2'] == asset2):
return item['ticker']
return None
"""
Check if an order book is cached, if yes, returns it.
exchange: the wanted exchange.
asset1: first asset.
asset2: second asset.
"""
def get_order_book_cache(self, exchange, asset1, asset2):
for item in self.cache_order_books:
if (item['exchange'] == str(exchange) and item['asset1'] == asset1 and item['asset2'] == asset2):
return item['book']
return None
"""
Put price in cache.
exchange: the wanted exchange.
asset1: first asset.
asset2: second asset.
ticker: the ccxt object that contains the price to cache.
"""
def cache_price(self, exchange, asset1, asset2, ticker):
self.cache_prices.append({
'exchange': str(exchange),
'asset1': asset1,
'asset2': asset2,
'ticker': ticker
})
"""
Put order book in cache.
exchange: the wanted exchange.
asset1: first asset.
asset2: second asset.
book: the ccxt object that contains the order book to cache.
"""
def cache_order_book(self, exchange, asset1, asset2, book):
self.cache_order_books.append({
'exchange': str(exchange),
'asset1': asset1,
'asset2': asset2,
'book': book
})
"""
Init exchanges, create connections with secrets file.
"""
def init_ccxt(self):
self.binance = ccxt.binance({
'apiKey': secrets.BINANCE_KEY,
'secret': secrets.BINANCE_SECRET,
'timeout': 30000,
'enableRateLimit': True,
})
self.bittrex = ccxt.bittrex({
'apiKey': secrets.BITTREX_KEY,
'secret': secrets.BITTREX_SECRET,
'timeout': 30000,
'enableRateLimit': True,
})
self.bitfinex = ccxt.bitfinex2({
'apiKey': secrets.BITFINEX_KEY,
'secret': secrets.BITFINEX_SECRET,
'timeout': 30000,
'enableRateLimit': True,
})
"""
Get your balance for given asset.
exchange: the wanted exchange.
asset: the wanted asset.
returns: the amount of the given asset you own.
"""
def get_balance(self, exchange, asset):
try:
balance = exchange.fetchBalance()
if (asset in balance):
return balance[asset]['free']
return 0
except Exception as e:
self.log("Error while getting balance: {}".format(str(e)))
raise
"""
Get the multiplicator for each exchange. If the fee is 0.1%, then the
multiplicator will be 0.999.
exchange: the wanted exchange.
mode: buy or sell.
returns: the multiplicator for the given exchange.
"""
def get_fees(self, exchange, mode):
if (mode != 'buy' and mode != 'sell'):
print("Get fees: mode should be buy or sell.")
return
if (str(exchange) == "Binance"):
return 0.999
elif (str(exchange) == "Bittrex"):
return 0.998
elif (str(exchange) == "Bitfinex"):
if (mode == "buy"):
return 0.998
else:
return 0.999
"""
Get an asset price.
exchange: the wanted exchange.
asset1: first asset.
asset2: second asset.
mode: can be average, bid or ask.
returns: the current price if success, None if something is wrong.
"""
def get_price(self, exchange, asset1, asset2, mode='average'):
if (mode != 'average' and mode != 'ask' and mode != 'bid'):
print("Mode should be average, ask or bid")
return None
try:
ticker = None
if (self.get_price_cache(exchange, asset1, asset2)):
ticker = self.get_price_cache(exchange, asset1, asset2)
else:
ticker = exchange.fetchTicker('{}/{}'.format(asset1, asset2))
self.cache_price(exchange, asset1, asset2, ticker)
if (mode == 'bid'):
return ticker['bid']
if (mode == 'ask'):
return ticker['ask']
return (ticker['ask'] + ticker['bid']) / 2
except Exception as e:
self.log("Error while fetching price for {}/{}: {}".format(asset1, asset2, str(e)))
return None
"""
Get order book for given asset.
exchange: the wanted exchange.
asset1: first asset.
asset2: second asset.
mode: can be bids or asks
returns: the order book as a 2D array, None if something is wrong.
"""
def get_order_book(self, exchange, asset1, asset2, mode="bids"):
if (mode != 'bids' and mode != 'asks'):
print('Get order book: mode should be bids or asks.')
return None
try:
order_book = self.get_order_book_cache(exchange, asset1, asset2)
if (not order_book):
order_book = exchange.fetchOrderBook('{}/{}'.format(asset1, asset2))
self.cache_order_book(exchange, asset1, asset2, order_book)
return order_book[mode]
except Exception as e:
self.log("Error while fetching order book for {}/{}: {}".format(asset1, asset2, str(e)))
return None
"""
Check if at least one order is open for the given asset and exchange.
exchange: the wanted exchange.
asset1: first asset.
asset2: second asset.
returns: True/False if there is an open order.
"""
def is_open_order(self, exchange, asset1, asset2):
try:
data = exchange.fetchOpenOrders('{}/{}'.format(asset1, asset2))
for item in data:
if (item['filled'] > 0):
return Crypto.ORDER_IN_PROGRESS
if (len(data) > 0):
return Crypto.ORDER_NOT_FILLED
return Crypto.ORDER_FILLED
except Exception as e:
self.log("Error while fetching open orders for {}/{}: {}".format(asset1, asset2, str(e)))
return None
"""
Cancel all orders for given assets.
exchange: the wanted exchange.
asset1: first asset.
asset2: second asset.
returns: True if success, False if something is wrong.
"""
def cancel_orders(self, exchange, asset1, asset2):
for _ in range(5):
try:
orders = exchange.fetchOpenOrders('{}/{}'.format(asset1, asset2))
for order in orders:
exchange.cancelOrder(order['id'], '{}/{}'.format(asset1, asset2))
return True
except Exception as e:
self.log("Error while canceling orders for {}/{}: {}. Retrying.".format(asset1, asset2, str(e)))
self.log("Cannot cancel orders for {}/{}.".format(asset1, asset2, str(e)))
return False
"""
Estimate the profit for forward arbitrage on given asset.
exchange: the wanted exchange.
asset: the asset to try forward triarb.
returns: the estimated percentage difference after triarb.
"""
def estimate_arbitrage_forward(self, exchange, asset):
try:
alt_ETH = self.get_buy_limit_price(exchange, asset, 'ETH')
alt_BTC = self.get_sell_limit_price(exchange, asset, 'BTC')
if (not alt_BTC or not alt_ETH):
self.log("Less than 3 orders for {} on {}, skipping.".format(asset, str(exchange)))
return -100
step1 = (1 / alt_ETH) * self.get_fees(exchange, 'buy')
step2 = (step1 * alt_BTC) * self.get_fees(exchange, 'sell')
step3 = (step2 / self.get_price(exchange, 'ETH', 'BTC', mode='ask')) * self.get_fees(exchange, 'buy')
return (step3 - 1) * 100
except ZeroDivisionError:
return -1
"""
Estimate the profit for backward arbitrage on given asset.
exchange: the wanted exchange.
asset: the asset to try backward triarb.
returns: the estimated percentage difference after triarb.
"""
def estimate_arbitrage_backward(self, exchange, asset):
try:
alt_BTC = self.get_buy_limit_price(exchange, asset, 'BTC')
alt_ETH = self.get_sell_limit_price(exchange, asset, 'ETH')
if (not alt_BTC or not alt_ETH):
self.log("Less than 3 orders for {} on {}, skipping.".format(asset, str(exchange)))
return -100
step1 = (self.get_price(exchange, 'ETH', 'BTC', mode='bid')) * self.get_fees(exchange, 'sell')
step2 = (step1 / alt_BTC) * self.get_fees(exchange, 'buy')
step3 = (step2 * alt_ETH) * self.get_fees(exchange, 'sell')
return (step3 - 1) * 100
except ZeroDivisionError:
return -1
"""
Create a buy order. 'amount' or 'amount_percentage' should be specified.
If limit is specified it will be a limit order, otherwise it will be
a market order.
If timeout is specified, then the limit order will be cancelled if the
order isn't completed after timeout.
exchange: the wanted exchange.
asset1: first asset.
asset2: second asset.
amount_percentage: percentage of first asset you want to buy with.
amount: fixed amount of second asset you want to buy.
limit: the maximum price you want to buy asset2.
timeout: the maximum delay to wait before canceling limit order.
returns: True if the trade has been completed, False if not.
"""
def buy(self, exchange, asset1, asset2, amount_percentage=None, amount=None, limit=None, timeout=None):
try:
if (amount_percentage):
asset2_available = self.get_balance(exchange, asset2) * amount_percentage
amount = asset2_available / self.get_price(exchange, asset1, asset2, mode='ask')
self.log("Buying {:.6} {} with {} on {}.".format(
amount,
asset1,
asset2,
exchange
))
if (limit):
self.log("Limit @{}.".format(limit))
if (not limit):
self.log("Buying at market price.")
exchange.createMarketBuyOrder(
'{}/{}'.format(asset1, asset2),
amount
)
return True
else:
exchange.createLimitBuyOrder(
'{}/{}'.format(asset1, asset2),
amount,
limit
)
if (timeout):
time.sleep(timeout)
result = self.is_open_order(exchange, asset1, asset2)
if (result == Crypto.ORDER_NOT_FILLED):
if (self.cancel_orders(exchange, asset1, asset2)):
self.log("Canceled limit order for {}/{} after timeout.".format(asset1, asset2))
return False
elif (result == Crypto.ORDER_IN_PROGRESS):
n = 0
while (result == Crypto.ORDER_IN_PROGRESS):
n += 1
if (n >= config.WAIT_TIMES_WHEN_FILLED):
self.log("Order cannot be terminated, selling {} to {}.".format(asset1, asset2))
self.sell(exchange, asset1, asset2, amount_percentage=1)
return False
self.log("Order for {}/{} is in progress, waiting...".format(asset1, asset2))
time.sleep(timeout)
result = self.is_open_order(exchange, asset1, asset2)
self.log("Limit order executed.")
return True
else:
self.log("Limit order executed.")
return True
else:
return False
except Exception as e:
self.log("Error while buying: {}".format(str(e)))
return False
"""
Create a sell order. 'amount' or 'amount_percentage' should be specified.
If limit is specified it will be a limit order, otherwise it will be
a market order.
If timeout is specified, then the limit order will be cancelled if the
order isn't completed after timeout.
exchange: the wanted exchange.
asset1: first asset.
asset2: second asset.
amount_percentage: percentage of first asset you want to sell.
amount: fixed amount of first asset you want to sell.
limit: the minimum price you want to sell asset1.
timeout: the maximum delay to wait before canceling limit order.
returns: True if the trade has been completed, False if not.
"""
def sell(self, exchange, asset1, asset2, amount_percentage=None, amount=None, limit=None, timeout=None):
try:
if (amount_percentage):
amount = self.get_balance(exchange, asset1) * amount_percentage
self.log("Selling {:.6f} {} to {} on {}.".format(
amount,
asset1,
asset2,
exchange
))
if (limit):
self.log("Limit @{}.".format(limit))
if (not limit):
self.log("Selling at market price.")
exchange.createMarketSellOrder(
'{}/{}'.format(asset1, asset2),
amount
)
return True
else:
exchange.createLimitSellOrder(
'{}/{}'.format(asset1, asset2),
amount,
limit
)
if (timeout):
time.sleep(timeout)
result = self.is_open_order(exchange, asset1, asset2)
if (result == Crypto.ORDER_NOT_FILLED):
if (self.cancel_orders(exchange, asset1, asset2)):
self.log("Canceled limit order for {}/{} after timeout.".format(asset1, asset2))
return False
elif (result == Crypto.ORDER_IN_PROGRESS):
n = 0
while (result == Crypto.ORDER_IN_PROGRESS):
n += 1
if (n >= config.WAIT_TIMES_WHEN_FILLED):
self.log("Order cannot be terminated, buying {} with {}.".format(asset1, asset2))
self.buy(exchange, asset1, asset2, amount_percentage=1)
return False
self.log("Order for {}/{} is in progress, waiting...".format(asset1, asset2))
time.sleep(timeout)
result = self.is_open_order(exchange, asset1, asset2)
self.log("Limit order executed.")
return True
else:
self.log("Limit order executed.")
return True
else:
return False
except Exception as e:
self.log("Error while selling: {}".format(str(e)))
return False
"""
Summarize arbitrage, calculate loss/gain, print it, and save it on the disk.
exchange: the exchange with whom we did the arbitrage.
balance_before: the balance before the arbitrage.
asset: the asset that have been arbitrate
"""
def summarize_arbitrage(self, exchange, balance_before, asset):
balance_after = self.get_balance(exchange, "ETH")
diff = balance_after - balance_before
self.save_gain(diff)
diff_eur = diff * self.get_price(exchange, 'ETH', 'EUR')
diff_percentage = diff / balance_before * 100
balance_eur = self.get_last_balance() * self.get_price(exchange, 'ETH', 'EUR')
self.log("➡️ Arbitrage {:5} on {:10}, diff: {:8.6f}ETH ({:.2f} EUR), balance: {:7.6f}ETH ({:.2f} EUR), {:.2f}%".format(asset, str(exchange), diff, diff_eur, self.get_last_balance(), balance_eur, diff_percentage), mode="notification")
self.log("Balance: {} --> {} ETH".format(balance_before, balance_after))
"""
Executes forward arbitrage on given asset:
ETH -> ALT -> BTC -> ETH.
exchange: the wanted exchange.
asset: the asset to forward triarb.
"""
def run_arbitrage_forward(self, exchange, asset):
self.log("🔥 Arbitrage on {}: ETH -> {} -> BTC -> ETH".format(exchange, asset))
balance_before = self.get_balance(exchange, "ETH")
result1 = self.best_buy(exchange, asset, 'ETH', config.ETH_PERCENTAGE)
if (not result1):
self.log("❌ Failed to convert {} to ETH, canceling arbitrage.".format(asset), mode="notification")
return
result2 = self.best_sell(exchange, asset, 'BTC', 1)
if (not result2):
self.log("❌ Failed to convert {} to BTC, canceling arbitrage. Will convert back {} to ETH.".format(asset, asset), mode="notification")
self.sell(exchange, asset, 'ETH', amount_percentage=1)
self.summarize_arbitrage(exchange, balance_before, asset)
return
self.buy(exchange, "ETH", "BTC", amount_percentage=1)
self.summarize_arbitrage(exchange, balance_before, asset)
"""
Executes backward arbitrage on given asset:
ETH -> BTC -> ALT -> ETH.
exchange: the wanted exchange.
asset: the asset to backward triarb.
"""
def run_arbitrage_backward(self, exchange, asset):
self.log("🔥 Arbitrage on {}: ETH -> BTC -> {} -> ETH".format(exchange, asset))
balance_before = self.get_balance(exchange, "ETH")
alt_BTC = self.get_buy_limit_price(exchange, asset, 'BTC')
alt_ETH = self.get_sell_limit_price(exchange, asset, 'ETH')
self.sell(exchange, "ETH", "BTC", amount_percentage=config.ETH_PERCENTAGE)
result1 = self.best_buy(exchange, asset, 'BTC', 1)
if (not result1):
self.log("❌ Failed to convert BTC to {}, canceling arbitrage. Will convert BTC to ETH.".format(asset), mode="notification")
self.buy(exchange, 'ETH', 'BTC', amount_percentage=1)
self.summarize_arbitrage(exchange, balance_before, asset)
return
result2 = self.best_sell(exchange, asset, 'ETH', 1)
if (not result2):
self.log("❌ Failed to convert {} to ETH, canceling arbitrage. Forcing convertion from {} to ETH.".format(asset, asset), mode="notification")
self.sell(exchange, asset, 'ETH', amount_percentage=1)
self.summarize_arbitrage(exchange, balance_before, asset)
return
self.summarize_arbitrage(exchange, balance_before, asset)
"""
Get the safest and lowest price to limit buy the given asset.
We start from the third lowest price to avoid volatility, then we
returns the first price with enough value to fullfill the order.
exchange: the wanted exchange.
asset1: first asset.
asset2: second asset.
returns: the advised price to place limit buy order.
"""
def get_buy_limit_price(self, exchange, asset1, asset2, amount=1):
bids = self.get_order_book(exchange, asset1, asset2, mode="asks")
if (not bids):
return None
bids.sort()
if (len(bids) < config.ORDERBOOK_INDEX_ESTIMATION):
return None
for bid in bids[config.ORDERBOOK_INDEX_ESTIMATION:]:
if (bid[1] >= amount):
return bid[0]
"""
Get the safest and highest price to limit sell the given asset.
We start from the third highest price to avoid volatility, then we
returns the first price with enough value to fullfill the order.
exchange: the wanted exchange.
asset1: first asset.
asset2: second asset.
returns: the advised price to place limit sell order.
"""
def get_sell_limit_price(self, exchange, asset1, asset2, amount=1):
asks = self.get_order_book(exchange, asset1, asset2, mode="bids")
if (not asks):
return None
asks.sort(reverse=True)
if (len(asks) < config.ORDERBOOK_INDEX_ESTIMATION):
return None
for ask in asks[config.ORDERBOOK_INDEX_ESTIMATION:]:
if (ask[1] >= amount):
return ask[0]
"""
Logs given string.
text: the string to log.
mode: can be log or notification, if notification it will send a message to the Telegram bot.
"""
def log(self, text, mode="log"):
formatted_text = "[{}] {}".format(datetime.now().strftime("%d/%m/%Y %H:%M:%S"), text)
if (mode == "notification"):
self.bot.sendMessage(chat_id=secrets.TELEGRAM_CHAT, text=formatted_text)
if (mode == "notification" or mode == "log"):
with open('logs.txt', 'a+') as file:
file.write(formatted_text)
file.write("\n")
"""
Get the waiting time needed to bypass DDOS protection.
exchange: the wanted exchange to limit.
returns: None if no need to limit, a number of seconds to wait if there is.
"""
def get_waiting(self, exchange):
if (str(exchange) == "Bitfinex"):
return 2
return None
"""
Get last recorded balance, stored in balance.csv file.
returns: the last recorded balance in balance.csv file. If the file does not exist we create it.
"""
def get_last_balance(self):
if (os.path.isfile('balance.csv')):
with open('balance.csv', 'r') as file:
lines = file.readlines()
balance = float(lines[-1][:-1].split(',')[1])
return balance
else:
with open('balance.csv', 'w+') as file:
file.write('date time,balance in ETH\n')
file.write('{},0\n'.format(datetime.now().strftime("%d/%m/%Y %H:%M:%S")))
return 0
"""
Add a new balance after a trade.
gain: the difference to add to last balance.
"""
def save_gain(self, gain):
last = self.get_last_balance()
new = last + gain
with open("balance.csv", 'a') as file:
file.write("{},{}\n".format(datetime.now().strftime("%d/%m/%Y %H:%M:%S"), new))
"""
Buy at best price possible using decreasing buy limit orders.
"""
def best_buy(self, exchange, asset1, asset2, amount_percentage):
orderbook = self.get_order_book(exchange, asset1, asset2, mode="asks")
orderbook.sort(key=itemgetter(0))
for price in orderbook[:config.MAX_ORDERBOOK_TRIES]:
self.log("Trying to buy {} with {} @{:.8f}.".format(asset1, asset2, price[0]))
result = self.buy(exchange, asset1, asset2, amount_percentage=amount_percentage, limit=price[0], timeout=config.WAIT_LIMIT_ORDER)
if (result):
self.log("✅ Bought {} with {} @{:.8f}.".format(asset1, asset2, price[0]))
return True
else:
self.log("⏳ Failed to buy {} with {} at {:.8f}.".format(asset1, asset2, price[0]))
self.log("❌ Was not able to buy {} with {}".format(asset1, asset2))
return False
"""
Sell at best price possible using decreasing buy sell orders.
"""
def best_sell(self, exchange, asset1, asset2, amount_percentage):
orderbook = self.get_order_book(exchange, asset1, asset2, mode="bids")
orderbook.sort(key=itemgetter(0), reverse=True)
for price in orderbook[:config.MAX_ORDERBOOK_TRIES]:
self.log("Trying to sell {} to {} @{:.8f}.".format(asset1, asset2, price[0]))
result = self.sell(exchange, asset1, asset2, amount_percentage=amount_percentage, limit=price[0], timeout=config.WAIT_LIMIT_ORDER)
if (result):
self.log("✅ Sold {} to {} @{:.8f}.".format(asset1, asset2, price[0]))
return True
else:
self.log("⏳ Failed to sell {} to {} at {:.8f}.".format(asset1, asset2, price[0]))
self.log("❌ Was not able to sell {} to {}".format(asset1, asset2))
return False