forked from andrei-zgirvaci/Arbitrage-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arbitrage_bot.py
248 lines (205 loc) · 8.88 KB
/
arbitrage_bot.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
import time
import ccxt
import keyboard
from win10toast import ToastNotifier
toaster = ToastNotifier()
exchangesData = {
"bybit": {
"apiKey": "",
"secret": "",
"transactionFee": 0.001
},
# "hitbtc": {
# "apiKey": "",
# "secret": "",
# "transactionFee": 0.001
# },
# "binance": {
# "apiKey": "",
# "secret": "",
# "transactionFee": 0.001
# },
# "bittrex": {
# "apiKey": "",
# "secret": "",
# "transactionFee": 0.0025
# },
# "poloniex": {
# "apiKey": "",
# "secret": "",
# "transactionFee": 0.0025
# },
# "exmo": {
# "apiKey": "",
# "secret": "",
# "transactionFee": 0.002
# },
}
min_spread = 0
min_profit = 0
def main():
exchanges = [
"bybit",
# "binance",
# "bittrex",
# "hitbtc",
# "poloniex",
# "exmo",
# "bitmex",
# "huobi",
]
# symbols = [
# "ETH/USDT",
# "XRP/USDT",
# "BTC/USDT",
# "BCH/USDT",
# "DASH/USDT",
# "XMR/USDT",
# "LTC/USDT",
# ]
# define symbols for bybit exchange
symbols = [
# "ETH/USDT:USDT",
# "XRP/USDT:USDT",
"BTC/USDT:USDT",
# "BCH/USDT:USDT",
# "DASH/USDT:USDT",
# "XMR/USDT:USDT",
# "LTC/USDT:USDT",
]
min_ask_exchange_id = ""
min_ask_price = 99999999
max_bid_exchange_id = ""
max_bid_price = 0
exchange_symbol = ""
max_increase_percentage = 0.0
for symbol in symbols:
print("-----------------------------")
print("Searching for the best opportunity for {0} on {1}".format(
symbol, exchanges))
ask_exchange_id, ask_price, bid_exchange_id, bid_price = get_biggest_spread_by_symbol(
exchanges, symbol)
increase_percentage = (bid_price - ask_price) / ask_price * 100
print("[{0} - {1}] - [{2}] - Price Spread: {3:.20}%".format(ask_exchange_id,
bid_exchange_id, symbol, increase_percentage))
if increase_percentage > max_increase_percentage:
exchange_symbol = symbol
max_increase_percentage = increase_percentage
min_ask_exchange_id = ask_exchange_id
min_ask_price = ask_price
max_bid_exchange_id = bid_exchange_id
max_bid_price = bid_price
if increase_percentage >= min_spread:
break
print("-----------------------------")
if max_increase_percentage > 0:
print("\n----------Settings-----------")
ask_amount = get_min_amount(min_ask_exchange_id, exchange_symbol)
print("Min Ask amount: {0}".format(ask_amount))
bid_amount = get_min_amount(max_bid_exchange_id, exchange_symbol)
print("Min Bid amount: {0}".format(bid_amount))
amount = max(ask_amount, bid_amount)
print("Actual amount: {0}".format(amount))
print("Min spread percentage: {0}%".format(min_spread))
print("Min profit: {0}%".format(min_profit))
print("\n--------Best Spread----------")
print("[{0} - {1}] - [{2}]: Spread percentage: {3:.2}%".format(min_ask_exchange_id,
max_bid_exchange_id, exchange_symbol, max_increase_percentage))
print("\n-----Market Opportunity------")
print("Buy {0} {1} from {2} at {3} {4}".format(amount, exchange_symbol.split(
"/")[0], min_ask_exchange_id, min_ask_price, exchange_symbol.split("/")[0]))
print("Sell {0} {1} on {2} at {3} {4}".format(amount, exchange_symbol.split(
"/")[0], max_bid_exchange_id, max_bid_price, exchange_symbol.split("/")[0]))
print("\n-------------Fees------------")
min_ask_fee = min_ask_price * amount * \
exchangesData[min_ask_exchange_id]["transactionFee"]
print("[{0}] - Trading Fee: {1}% = {2:.4} {3}".format(min_ask_exchange_id,
exchangesData[min_ask_exchange_id]["transactionFee"]*100, min_ask_fee, exchange_symbol.split("/")[0]))
max_bid_fee = max_bid_price * amount * \
exchangesData[max_bid_exchange_id]["transactionFee"]
print("[{0}] - Trading Fee: {1}% = {2:.4} {3}".format(max_bid_exchange_id,
exchangesData[max_bid_exchange_id]["transactionFee"]*100, max_bid_fee, exchange_symbol.split("/")[0]))
print("\n-----------Profit------------")
cost = amount * min_ask_price
print("You will have to spend: {0:.4} {1}".format(
cost, exchange_symbol.split("/")[1]))
profit = ((max_bid_price - min_ask_price) * amount) - \
(max_bid_fee + min_ask_fee)
print("You will make {0:.4} {1} profit(including fees)".format(
profit, exchange_symbol.split("/")[1]))
print("\n-----------Buy/Sell----------")
if profit >= min_profit:
place_buy_sell_order(min_ask_exchange_id, min_ask_price,
max_bid_exchange_id, max_bid_price, exchange_symbol, amount)
else:
print("It seems like you won't make enough profit :(")
else:
print("It seems like there are no opportunities at the moment :(")
def get_min_amount(exchange_id, symbol):
exchange = eval("ccxt.{0}()".format(exchange_id))
exchange.load_markets()
return float(exchange.markets[symbol]["limits"]["amount"]["min"])
def place_buy_sell_order(ask_exchange_id, ask_price, bid_exchange_id, bid_price, symbol, amount):
# buy
ask_exchange = eval("ccxt.{0}()".format(ask_exchange_id))
ask_exchange.apiKey = exchangesData[ask_exchange_id]["apiKey"]
ask_exchange.secret = exchangesData[ask_exchange_id]["secret"]
print("Placing a Buy order on {0} for {1} {2} at price: {3}".format(
ask_exchange_id, amount, symbol.split("/")[0], ask_price))
toaster.show_toast("Buying notification", "Placing a Buy order on {0} for {1} {2} at price: {3}".format(
ask_exchange_id, amount, symbol.split("/")[0], ask_price))
# It will cost you a fee if you want to use `create_market_buy_order`
# ask_exchange.create_market_buy_order(symbol, amount, {'trading_agreement': 'agree'})
ask_exchange.create_limit_buy_order(symbol, amount, ask_price)
# sell
bid_exchange = eval("ccxt.{0}()".format(bid_exchange_id))
bid_exchange.apiKey = exchangesData[bid_exchange_id]["apiKey"]
bid_exchange.secret = exchangesData[bid_exchange_id]["secret"]
print("Placing a Sell order on {0} for {1} {2} at price: {3}".format(
bid_exchange_id, amount, symbol.split("/")[0], bid_price))
toaster.show_toast("Selling notification", "Placing a Sell order on {0} for {1} {2} at price: {3}".format(
bid_exchange_id, amount, symbol.split("/")[0], bid_price))
# It will cost you a fee if you want to use `create_market_buy_order`
# bid_exchange.create_market_buy_order(symbol, amount, {'trading_agreement': 'agree'})
bid_exchange.create_limit_sell_order(symbol, amount, bid_price)
def get_biggest_spread_by_symbol(exchanges, symbol):
ask_exchange_id = ""
min_ask_price = 99999999
bid_exchange_id = ""
max_bid_price = 0
for exchange_id in exchanges:
exchange = eval("ccxt.{0}()".format(exchange_id))
try:
order_book = exchange.fetch_order_book(symbol)
trade = exchange.fetch_trades(symbol, 3000, 1)
print(trade[0]['info']['price'])
bid_price = order_book['bids'][0][0] if len(
order_book['bids']) > 0 else None
ask_price = order_book['asks'][0][0] if len(
order_book['asks']) > 0 else None
if ask_price < min_ask_price:
ask_exchange_id = exchange_id
min_ask_price = ask_price
if bid_price > max_bid_price:
bid_exchange_id = exchange_id
max_bid_price = bid_price
increase_percentage = (bid_price - ask_price) / ask_price * 100
if increase_percentage >= 1:
return ask_exchange_id, min_ask_price, bid_exchange_id, max_bid_price
except Exception as err:
# pass
print("")
print("{0} - There is an error!\n{1}".format(exchange_id, err))
min_ask_price += 0.235
max_bid_price -= 0.235
return ask_exchange_id, min_ask_price, bid_exchange_id, max_bid_price
def get_exchanges_by_symbol(exchanges, symbol_to_find):
for exchange_id in exchanges:
exchange = eval("ccxt.{0}()".format(exchange_id))
exchange.load_markets(True)
for symbol in exchange.symbols:
if symbol == symbol_to_find:
print("{0} - {1} [OK]".format(exchange_id, symbol))
if __name__ == "__main__":
while not keyboard.is_pressed('esc'):
main()