-
Notifications
You must be signed in to change notification settings - Fork 9
/
cli_multithreaded.py
261 lines (207 loc) · 10.7 KB
/
cli_multithreaded.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
import threading
import bybit_usdt_execution_multithreaded as bybit
import binance_spot_execution_multithreaded as binance_spot
from threading import Thread
def get_all_running_threads():
if len(threading.enumerate()) == 1:
print("No running proceses")
else:
print("Current running processes:")
for thread in threading.enumerate():
if thread.name != "MainThread":
print(thread.name)
def binance_cli():
client = binance_spot.auth()
usdt_tickers = binance_spot.get_spot_tickers(client)
connection_name = "Binance_SPOT"
print("Connected to >> Binance SPOT")
exit = False
while not exit:
print("What do you want to do:"
"\n 1 >> display positions"
"\n 2 >> buy/sell spot"
"\n 0 >> exit - binance spot"
"\n 99 >> restart client and refresh tickers"
"\n 999 >> check current running processes")
mode = int(input("input number >>> "))
if mode == 0:
exit = True
print("Binance SPOT - closing")
elif mode == 999:
print("\n")
get_all_running_threads()
print("\n")
elif mode == 1:
open_positions = binance_spot.get_spot_balances(client)
binance_spot.display_positions(open_positions)
elif mode == 2:
print("\n")
print("Open position mode selected")
print("Select order type:"
"\n 1 >> twap"
"\n 2 >> market order"
"\n 3 >> buy/sell by account/position %")
order_mode = int(input("input number >>> "))
if order_mode == 1:
print("\n")
print("twap mode selected")
ticker = binance_spot.select_ticker(usdt_tickers)
order_amount = binance_spot.select_order_amount() # number or orders
side = binance_spot.select_side() # buy/sell
total_size = binance_spot.select_usdt_size() # usd amount
twap_duration = binance_spot.select_duration() # minutes
twap_thread = Thread(target=binance_spot.basic_twap, args=(client, ticker, order_amount, side ,total_size, twap_duration ), name=f"{connection_name}_{ticker}_{side}_{total_size}_twap{twap_duration}min").start()
elif order_mode == 2:
print("\n")
print("market order mode selected")
ticker = binance_spot.select_ticker(usdt_tickers)
side = binance_spot.select_side() # buy/sell
total_size = binance_spot.select_usdt_size() # usd amount (client, ticker:str, side:str, total_size:float)
market_order_thread = Thread(target=binance_spot.market_order, args=(client, ticker, side, total_size), name=f"{connection_name}_{ticker}_{side}_{total_size}_market").start()
elif order_mode == 3:
print("\n")
print("buy/sell by account/position % selected")
side = binance_spot.select_side() # buy/sell
if side == "Buy":
ticker = binance_spot.select_ticker(usdt_tickers)
spot_balances = binance_spot.get_spot_balances(client)
if "USDT" in spot_balances.keys():
open_pct = binance_spot.select_open_pct()
twap_duration = binance_spot.select_duration()
open_by_pct_thread = Thread(target=binance_spot.open_spot_position_by_account_pct, args=(client, spot_balances, ticker, open_pct, twap_duration), name=f"{connection_name}_{ticker}_{side}_{open_pct}pct_{twap_duration}min_twap").start()
else:
print(f"No USDT available for Buying {ticker}")
elif side == "Sell":
ticker = binance_spot.select_ticker(usdt_tickers)
spot_balances = binance_spot.get_spot_balances(client)
if ticker.replace("USDT", "") in spot_balances.keys():
close_pct = binance_spot.select_close_pct()
twap_duration = binance_spot.select_duration()
close_by_pct_thread = Thread(target=binance_spot.close_spot_position_by_pct, args=(client, spot_balances, ticker, close_pct, twap_duration), name=f"{connection_name}_{ticker}_{side}_{close_pct}pct_{twap_duration}min_twap").start()
else:
print(f"No coins available to sell for {ticker}")
print("\n")
elif mode == 99:
print("\n")
print("Reconnecting client and refreshing tickers")
client = binance_spot.auth()
usdt_tickers = binance_spot.get_spot_tickers(client)
print("\n")
def bybit_cli():
client = bybit.auth()
usdt_tickers = bybit.get_usdt_tickers(client)
connection_name = "Bybit_USDT_PERPS"
print("Connected to >> Bybit USDT perps")
exit = False
while not exit:
print("What do you want to do:"
"\n 1 >> display positions"
"\n 2 >> open position"
"\n 3 >> close position"
"\n 4 >> modify positions"
"\n 0 >> exit - bybit usdt perps"
"\n 99 >> restart client and refresh tickers"
"\n 999 >> check current running processes")
mode = int(input("input number >>> "))
if mode == 0:
exit = True
print("Bybit USDT perps - closing")
elif mode == 999:
print("\n")
get_all_running_threads()
print("\n")
elif mode == 1:
open_positions = bybit.get_open_positions(client)
bybit.display_positions(open_positions)
elif mode == 2:
print("\n")
print("Open position mode selected")
print("Select order type:"
"\n 1 >> twap"
"\n 2 >> market order"
"\n 3 >> pair trade 1/1 weighted")
order_mode = int(input("input number >>> "))
if order_mode == 1:
print("twap mode selected")
ticker = bybit.select_ticker(usdt_tickers)
order_amount = bybit.select_order_amount() # number or orders
position_size = bybit.select_usdt_size() # usd amount
twap_duration = bybit.select_duration() # minutes
side = bybit.select_side() # buy/sell
twap_thread = Thread(target=bybit.basic_twap, args=(client, ticker, order_amount, position_size, twap_duration, side), name=f"{connection_name}_{ticker}_{side}_{position_size}_twap{twap_duration}min").start()
elif order_mode == 2:
print("market order mode selected")
ticker = bybit.select_ticker(usdt_tickers)
side = bybit.select_side() # buy/sell
position_size = bybit.select_usdt_size() # usd amount
market_order_thread = Thread(target=bybit.market_order, args=(client, ticker, side, position_size), name=f"{connection_name}_{ticker}_{side}_{position_size}_market").start()
elif order_mode == 3:
print("pair trade mode selected [using twap]\n")
position_size = bybit.select_usdt_size() # usd amount
order_amount = bybit.select_order_amount() # number or orders
twap_duration = bybit.select_duration() # minutes
print("\nSelect Long leg of the pair trade")
ticker_long = bybit.select_ticker(usdt_tickers)
print("\nSelect Short leg of the pair trade")
ticker_short = bybit.select_ticker(usdt_tickers)
side_short = "Sell"
side_long = "Buy"
twap_thread = Thread(target=bybit.basic_twap, args=(client, ticker_long, order_amount, position_size, twap_duration, side_long), name=f"{connection_name}_{ticker_long}_{side_long}_{position_size}_twap{twap_duration}min").start()
twap_thread = Thread(target=bybit.basic_twap, args=(client, ticker_short, order_amount, position_size, twap_duration, side_short), name=f"{connection_name}_{ticker_short}_{side_short}_{position_size}_twap{twap_duration}min").start()
print("\n")
elif mode == 3:
print("\n")
print("Close position mode selected")
print("Select order type:"
"\n 1 >> twap"
"\n 2 >> market order")
order_mode = int(input("input number >>> "))
if order_mode == 1:
print("twap mode selected")
positions = bybit.get_open_positions(client=client)
close_id, ticker, side = bybit.select_close_id(positions=positions)
order_amount = bybit.select_order_amount() # number or orders
twap_duration = bybit.select_duration() # minutes (client, positions, close_id, order_amount:int, twap_duration:int)
twap_close_thread = Thread(target=bybit.basic_twap_close, args=(client, positions, close_id, order_amount, twap_duration), name=f"{connection_name}_CLOSE_{ticker}_{side}_twap{twap_duration}min").start()
elif order_mode == 2:
print("market order mode selected")
positions = bybit.get_open_positions(client=client)
close_id, ticker, side = bybit.select_close_id(positions=positions)
market_close_thread = Thread(target=bybit.market_close, args=(client, positions, close_id), name=f"{connection_name}_CLOSE_{ticker}_{side}_market").start()
print("\n")
elif mode == 4:
print("\n")
print("Modify mode selected")
bybit.set_position_sl_tp(client)
print("\n")
elif mode == 99:
print("Reconnecting client and refreshing tickers")
client = bybit.auth()
usdt_tickers = bybit.get_usdt_tickers(client)
print("\n")
def main():
exit = False
while not exit:
print("\n")
print("Select exchange:"
"\n 1 >> Binance SPOT"
"\n 2 >> Bybit USDT perps"
"\n 999 >> check current running processes"
"\n 0 >> exit terminal")
mode = int(input("input number >>> "))
if mode == 0:
exit = True
print("\n")
print("Terminal closing")
elif mode == 999:
print("\n")
get_all_running_threads()
print("\n")
elif mode == 1:
print("\n")
binance_cli()
elif mode == 2:
print("\n")
bybit_cli()
if __name__ == "__main__":
main()