forked from nelso0/barbotine-arbitrage-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot-fake-money.py
231 lines (197 loc) · 11.8 KB
/
bot-fake-money.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
from asyncio import gather, run
import time
import ccxt.pro
import ccxt
import sys
from colorama import Fore, Back, Style,init
init()
from exchange_config import *
bid_prices = {}
ask_prices = {}
total_change_usd = 0
total_change_usd_pct = 0
prec_ask_price = 0
prec_bid_price = 0
i=0
z=0
if len(sys.argv) != 8:
print(f" \nIncorrect usage, this is what it has to look like: $ {how_do_you_usually_launch_python} bot-classic.py [pair] [total_usdt_investment] [stop.delay.minutes] [tlgrm.msg.title] [ex1] [ex2] [ex3]\n ")
print(f" \n This is the list of args you wrote: {sys.argv}")
sys.exit(1)
print(" ")
if first_orders_fill_timeout <= 0:
first_orders_fill_timeout = 3600 # 2.5 days
echanges = [ex[sys.argv[5]],ex[sys.argv[6]],ex[sys.argv[7]]]
echanges_str = [sys.argv[5],sys.argv[6],sys.argv[7]]
currentPair = str(sys.argv[1])
criteria_usd = str(criteria_usd)
criteria_pct = str(criteria_pct)
howmuchusd = float(sys.argv[2])
inputtimeout = int(sys.argv[3])*60
indicatif = str(sys.argv[4])
timeout = time.time() + inputtimeout
def emergency_convert(pair_to_sell):
i=0
for echange in echanges_str:
try:
if ex[echange].has['cancelAllOrders'] and ex[echange].fetchOpenOrders(pair_to_sell) != []:
ex[echange].cancelAllOrders(pair_to_sell)
print(f"{Style.DIM}{get_time()}{Style.RESET_ALL} Successfully canceled all orders on {echange}.")
bal = get_balance(echange,pair_to_sell)
if bal>(float(10)/float(ex[echange].fetch_ticker(pair_to_sell)['last'])):
ex[echange].createMarketSellOrder(symbol=pair_to_sell,amount=bal)
print(f"{Style.DIM}{get_time()}{Style.RESET_ALL} Successfully sold {bal} {pair_to_sell[:len(pair_to_sell)-5]} on {echange}.")
else: print(f"Not enough {pair_to_sell[:len(pair_to_sell)-5]} on {echange}.")
i+=1
except Exception as e:
print(f'{Style.DIM}[{time.strftime("%H:%M:%S", time.gmtime(time.time()))}]{Style.RESET_ALL} Problem on {echange}. Error: {e}')
def emergency_convert_list(pair_to_sell,exlist):
i=0
for echange in exlist:
try:
if ex[echange].has['cancelAllOrders'] and ex[echange].fetchOpenOrders(pair_to_sell) != []:
ex[echange].cancelAllOrders(pair_to_sell)
print(f"{Style.DIM}{get_time()}{Style.RESET_ALL} Successfully canceled all orders on {echange}.")
bal = get_balance(echange,pair_to_sell)
if bal>(float(10)/float(ex[echange].fetch_ticker(pair_to_sell)['last'])):
ex[echange].createMarketSellOrder(symbol=pair_to_sell,amount=bal)
print(f"{Style.DIM}{get_time()}{Style.RESET_ALL} Successfully sold {bal} {pair_to_sell[:len(pair_to_sell)-5]} on {echange}.")
else: print(f"{Style.DIM}{get_time()}{Style.RESET_ALL} Not enough {pair_to_sell[:len(pair_to_sell)-5]} on {echange}.")
i+=1
except Exception as e:
print(f'{Style.DIM}[{time.strftime("%H:%M:%S", time.gmtime(time.time()))}]{Style.RESET_ALL} Problem on {echange}. Error: {e}')
s=0
ordersFilled = 0
while ordersFilled != 3:
if s==1:
sys.exit(1)
usd = {exchange:(howmuchusd/2)/len(echanges) for exchange in echanges_str}
total_usd = 0
for exc in echanges_str:
total_usd+=usd[exc]
all_tickers = []
try:
printandtelegram(f"{Style.DIM}{get_time()}{Style.RESET_ALL} Fetching the global average price for {currentPair}...")
for n in echanges:
ticker = n.fetch_ticker(currentPair)
all_tickers.append(ticker['bid'])
all_tickers.append(ticker['ask'])
average_first_buy_price = moy(all_tickers)
total_crypto = (howmuchusd/2)/average_first_buy_price
printandtelegram(f"{Style.DIM}{get_time()}{Style.RESET_ALL} Average {currentPair} price in USDT: {average_first_buy_price}")
except Exception as e:
print(f"{Style.DIM}{get_time()}{Style.RESET_ALL} Error while fetching average prices. Error: {e}")
for exc in echanges_str:
ex[exc].close()
crypto = {exchange:total_crypto/len(echanges) for exchange in echanges_str}
crypto_per_transaction = (total_crypto/len(echanges_str))
i=0
for n in echanges:
time.sleep(0.7)
printandtelegram(f'{Style.DIM}{get_time()}{Style.RESET_ALL} Buy limit order of {round(total_crypto/len(echanges),3)} {currentPair.split("/")[0]} at {average_first_buy_price} sent to {echanges_str[i]}.')
i+=1
printandtelegram(f"{Style.DIM}{get_time()}{Style.RESET_ALL} All orders sent.")
already_filled = []
for zz in range(3):
time.sleep(2.1)
printandtelegram(f"{Style.DIM}{get_time()}{Style.RESET_ALL} {echanges_str[zz]} order filled.")
ordersFilled+=1
already_filled.append(exc)
printandtelegram(f"{Style.DIM}{get_time()}{Style.RESET_ALL} Starting program with parameters: {[n for n in sys.argv]}")
prec_time = '0000000'
min_ask_price = 0
async def symbol_loop(exchange, symbol):
global crypto_per_transaction,i,z,prec_time,t,time1,bid_prices,ask_prices,total_absolute_profit_pct,min_ask_price,max_bid_price,prec_ask_price,prec_bid_price,timeout,profit_pct,profit_usd,total_crypto
while time.time() <= timeout:
# try:
orderbook = await exchange.watch_order_book(symbol)
now = exchange.milliseconds()
bid_prices[exchange.id] = orderbook["bids"][0][0]
ask_prices[exchange.id] = orderbook["asks"][0][0]
min_ask_ex = min(ask_prices, key=ask_prices.get)
max_bid_ex = max(bid_prices, key=bid_prices.get)
for u in echanges_str:
if crypto[u] < crypto_per_transaction:
min_ask_ex = u
if usd[u] <= 0: # should not happen
max_bid_ex = u
min_ask_price = ask_prices[min_ask_ex]
max_bid_price = bid_prices[max_bid_ex]
change_usd = ((usd[max_bid_ex] - (crypto_per_transaction / (1-fees[min_ask_ex]['quote'])) * min_ask_price * (1+fees[min_ask_ex]['base'])) + (usd[min_ask_ex] + crypto_per_transaction / (1+fees[max_bid_ex]['base']) * max_bid_price * (1-fees[max_bid_ex]['quote']))) - (usd[min_ask_ex]+usd[max_bid_ex])
change_usd_pct = (change_usd/(usd[echanges_str[0]]+usd[echanges_str[1]]+usd[echanges_str[2]]))*100
if max_bid_ex != min_ask_ex and change_usd > float(criteria_usd) and change_usd_pct > float(criteria_pct) and prec_ask_price != min_ask_price and prec_bid_price != max_bid_price:
i+=1
fees_crypto = crypto_per_transaction * (1-fees[min_ask_ex]['quote']) + crypto_per_transaction * (1-fees[max_bid_ex]['base'])
fees_usd = crypto_per_transaction * max_bid_price * (1-fees[max_bid_ex]['quote']) + crypto_per_transaction * min_ask_price * (1-fees[min_ask_ex]['base'])
sys.stdout.write("\033[F")
sys.stdout.write("\033[K")
print("-----------------------------------------------------\n")
ex_balances = ""
for exc in echanges_str:
ex_balances+=f"\n➝ {exc}: {round(crypto[exc],3)} {currentPair.split('/')[0]} / {round(usd[exc],2)} USDT"
print(f"{Style.RESET_ALL}Opportunity n°{i} detected! ({min_ask_ex} {min_ask_price} -> {max_bid_price} {max_bid_ex})\n \nExcepted profit: {Fore.GREEN}+{round(change_usd_pct,4)} % (+{round(change_usd,4)} USD){Style.RESET_ALL}\n \nSession total profit: {Fore.GREEN}+{round(total_change_usd_pct,4)} % (+{round((total_change_usd/100)*howmuchusd,4)} USD){Style.RESET_ALL}\n \nFees paid: {Fore.RED}-{round(fees_usd,4)} USD -{round(fees_crypto,4)} {currentPair.split('/')[0]}\n \n{Style.DIM} {ex_balances}\n \n{Style.RESET_ALL}Time elapsed since the beginning of the session: {time.strftime('%H:%M:%S', time.gmtime(time.time()-st))}\n \n{Style.RESET_ALL}-----------------------------------------------------\n \n")
send_to_telegram(f"[{indicatif} Trade n°{i}]\n \nOpportunity detected!\n \nExcepted profit: {round(change_usd_pct,4)} % ({round(change_usd,4)} USD)\n \n{min_ask_ex} {min_ask_price} -> {max_bid_price} {max_bid_ex}\nTime elapsed: {time.strftime('%H:%M:%S', time.gmtime(time.time()-st))}\nSession total profit: {round(total_change_usd_pct,4)} % ({round(total_change_usd,4)} USDT)\nFees paid: {round(fees_usd,4)} USD {round(fees_crypto,4)} {currentPair.split('/')[0]}\n \n--------BALANCES---------\n \n {ex_balances}")
printandtelegram(f"{Style.DIM}{get_time()}{Style.RESET_ALL} Sell market order filled on {max_bid_ex} for {crypto_per_transaction} {currentPair.split('/')[0]} at {max_bid_price}.")
printandtelegram(f"{Style.DIM}{get_time()}{Style.RESET_ALL} Buy market order filled on {max_bid_ex} for {crypto_per_transaction} {currentPair.split('/')[0]} at {min_ask_price}.")
crypto[min_ask_ex] += crypto_per_transaction
usd[min_ask_ex] -= (crypto_per_transaction / (1-fees[min_ask_ex]['quote'])) * min_ask_price * (1+fees[min_ask_ex]['base'])
crypto[max_bid_ex] -= crypto_per_transaction
usd[max_bid_ex] += crypto_per_transaction / (1+fees[max_bid_ex]['base']) * max_bid_price * (1-fees[max_bid_ex]['quote'])
total_change_usd+=change_usd
total_change_usd_pct+=change_usd_pct
prec_ask_price = min_ask_price
prec_bid_price = max_bid_price
total_crypto = 0
for exc in echanges_str:
total_crypto+=crypto[exc]
crypto_per_transaction = total_crypto/len(echanges_str)
else:
for count in range(0,1):
sys.stdout.write("\033[F")
sys.stdout.write("\033[K")
if change_usd < 0:
color = Fore.RED
elif change_usd > 0:
color = Fore.GREEN
elif change_usd == 0:
color = Fore.WHITE
print(f"{Style.DIM}{get_time()}{Style.RESET_ALL} Best opportunity: {color}{round(change_usd,4)} USD {Style.RESET_ALL}(with fees) buy: {min_ask_ex} at {min_ask_price} sell: {max_bid_ex} at {max_bid_price}")
time1=exchange.iso8601(exchange.milliseconds())
if time1[17:19] == "00" and time1[14:16] != prec_time:
prec_time = time1[11:13]
await exchange.close()
# except Exception as e:
# print(str(e))
# break # you can break just this one loop if it fails
async def exchange_loop(exchange_id, symbols):
exchange = getattr(ccxt.pro, exchange_id)()
loops = [symbol_loop(exchange, symbol) for symbol in symbols]
await gather(*loops)
await exchange.close()
async def main():
exchanges = {
echanges_str[0]: [currentPair],
echanges_str[1]: [currentPair],
echanges_str[2]:[currentPair]
}
loops = [
exchange_loop(exchange_id, symbols)
for exchange_id, symbols in exchanges.items()
]
await gather(*loops)
st = time.time()
print(" \n")
run(main())
for exc in crypto:
ticker = ex[exc].fetchTicker(currentPair)
price = ticker['last']
usd[exc]+=((crypto[exc])*(1-fees[exc]['base'])*price)*(1-fees[exc]['quote'])
crypto[exc]=0
total_usdt_balance = usd[echanges_str[0]]+usd[echanges_str[1]]+usd[echanges_str[2]]
with open('balance.txt', 'r+') as balance_file:
old_balance = float(balance_file.read())
balance_file.seek(0)
balance_file.write(str(total_usdt_balance))
total_session_profit_usd = total_usdt_balance-old_balance
total_session_profit_pct = (total_session_profit_usd/old_balance)*100
printandtelegram(f"{Style.DIM}{get_time()}{Style.RESET_ALL} Session with {currentPair} finished.\n{Style.DIM}{get_time()}{Style.RESET_ALL} Total profit: {round(total_session_profit_pct,4)} % ({total_session_profit_usd} USDT)")