-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpybit-Bot.py
executable file
·227 lines (186 loc) · 7.47 KB
/
pybit-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
#!/usr/bin/env python3
# TODO after setting leverage set all pairs to "Order by Value"
from os import name
from time import sleep
import json
import requests
import asyncio
from pybit.unified_trading import WebSocket
from pybit.unified_trading import HTTP
from botSettings import *
import ccxt
from pprint import pprint
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
blacklist = BLACKLIST.split(",")
whitelist = []
def handle_message(message):
if checkIfTradable(message):
print("Placing order ...")
placeOrder(message)
else:
print("Liquidated volume to low !")
ws = WebSocket(
testnet=False,
channel_type="linear"
)
session = HTTP(
testnet=False,
api_key=API_KEY,
api_secret=API_SECRET,
)
exchange_id = 'bybit'
exchange_class = getattr(ccxt, exchange_id)
exchange = exchange_class({
'apiKey': API_KEY,
'secret': API_SECRET,
})
#exchange.set_sandbox_mode(True) # activates testnet mode
#exchange.options['defaultType'] = 'swap'
markets = exchange.load_markets()
#just a ccxt test call
ret = exchange.fetchBalance ()
print(ret['USDT'])
def get_symbols():
print("Fetching USDT symbols ...")
response = requests.get("https://api.bybit.com/v2/public/symbols")
response.raise_for_status()
data = response.json()
usdt_symbols = [symbol for symbol in data["result"] if symbol["quote_currency"] == "USDT"]
print("Done !")
return usdt_symbols
def get_ticker_info(symbol_name):
response = requests.get(f"https://api.bybit.com/v2/public/tickers?symbol={symbol_name}")
response.raise_for_status()
return response.json()["result"][0]
def min_order_value(current_price, min_order_size):
return current_price * min_order_size
def getCoinsToTrade(usdt_symbols):
line = f'Only coins with a price under {TRADE_COIN_MAX_ORDER_VALUE} USDT will be traded!'
print(line)
print("Fetching USDT symbols to trade ...")
liquidationCandidates = []
for symbol in usdt_symbols:
ticker_info = get_ticker_info(symbol["name"])
current_price = float(ticker_info["last_price"])
min_order_size = float(symbol["lot_size_filter"]["min_trading_qty"])
min_order_value_result = min_order_value(current_price, min_order_size)
if min_order_value_result < TRADE_COIN_MAX_ORDER_VALUE:
line = f'{symbol["name"]}, {current_price}, {min_order_size}, {min_order_value_result:.6f}'
print(line)
liquidationCandidates.append(symbol["name"])
print("Done !")
return liquidationCandidates
async def subsribeLiquidations(symbol_list):
for symbol in symbol_list:
line = f'Subscribing to liquidation stream for {symbol}'
print(line)
try:
ws.liquidation_stream(symbol, handle_message)
except:
print("Error subscribing to pair.")
print("Done !")
def checkIfTradable(liquidation_message):
#print(liquidation_message)
size = float(liquidation_message["data"]["size"])
price = float(liquidation_message["data"]["price"])
pair = liquidation_message["data"]["symbol"]
volume = size * price
line = f'Got liquidatino for {liquidation_message["data"]["symbol"]}; Side {liquidation_message["data"]["side"]}; Liquidated volume {str(volume)}'
print (line)
if (volume > MIN_LIQUIDATION_VOLUME) and (pair in whitelist):
line = f'Got pair {liquidation_message["data"]["symbol"]}; Side {liquidation_message["data"]["side"]}; Liquidated volume {str(volume)}'
print(OKGREEN + line + ENDC)
return True
else:
return False
def placeOrder(liquidation_message):
liquidated_pair = liquidation_message["data"]["symbol"]
if liquidated_pair in blacklist:
line = f'Pair {liquidated_pair} is on blacklist!'
print(line)
return
else:
liquidated_pair_price = liquidation_message["data"]["price"]
liquidated_side = liquidation_message["data"]["side"]
orderSize_percentage = float(getWalletBalance()) * float(PERCENT_ORDER_SIZE)
order_cost = orderSize_percentage * float(liquidated_pair_price)
order_pair_ccxt = liquidated_pair[ : liquidated_pair.find("USDT")] + "/USDT:USDT"
#order = exchange.createMarketBuyOrder(order_pair_ccxt, order_cost)
#print(order)
if liquidated_side == 'Sell':
order_side = "sell"
#order = exchange.createMarketSellOrder(order_pair_ccxt, order_cost)
else:
order_side = "buy"
#order = exchange.createMarketBuyOrder(order_pair_ccxt, order_cost)
line = f'liquidated_price={liquidated_pair_price}\nliquidated_side = {liquidated_side}\nBalance = {getWalletBalance()}\norderSize_percentage = {orderSize_percentage}\norder_cost = {order_cost}'
print(line)
print("Placing order not implemented yet!")
#print (order)
# print(session.place_order(
# category="linear",
# symbol=order_pair,
# side=order_side,
# orderType="Market",
# qty=str(orderSize),
# ))
order = exchange.createOrder (order_pair_ccxt, 'market', order_side, 1, None, {'qty': 1})
print(order)
def getWalletBalance():
walletInfo = session.get_wallet_balance(accountType="CONTRACT")
return float(walletInfo["result"]["list"][0]["coin"][1]["walletBalance"])
async def main():
walletInfo = session.get_wallet_balance(accountType="CONTRACT")
walletBalance = walletInfo["result"]["list"][0]["coin"][1]["walletBalance"]
equity = walletInfo["result"]["list"][0]["coin"][1]["equity"]
totalPositionIM = walletInfo["result"]["list"][0]["coin"][1]["totalPositionIM"]
unrealisedPnl = walletInfo["result"]["list"][0]["coin"][1]["unrealisedPnl"]
cumRealisedPnl = walletInfo["result"]["list"][0]["coin"][1]["cumRealisedPnl"]
line = f'Balance: {walletBalance}\nEquity: {equity}\nuPnL: {unrealisedPnl}\ncum PnL:{cumRealisedPnl}'
print(line)
usdt_symbols = get_symbols()
global whitelist
whitelist = getCoinsToTrade(usdt_symbols)
# set position mode and leverage
for element in whitelist:
symbol = element[ : element.find("USDT")] + "/USDT:USDT"
line = f'Setting MarginMode for {symbol}'
print(line)
try:
result = exchange.setMarginMode(MARGIN_MODE, symbol, params = {"buyLeverage": int(LEVERAGE), "sellLeverage": int(LEVERAGE)})
line = f'{OKGREEN}OK!{ENDC} :: {result}'
print(line)
except Exception as e:
line = f'{FAIL}Failed!{ENDC} :: {e}'
print(line)
line = f'Setting leverage for {symbol}'
print (line)
try:
result = exchange.setLeverage(LEVERAGE, symbol, params = {})
line = f'{OKGREEN}OK!{ENDC} :: {result}'
print(line)
except Exception as e:
line = f'{FAIL}Failed!{ENDC} :: {e}'
print(line)
line = f'Setting position mode to One-way for {symbol}'
print (line)
try:
result = exchange.set_position_mode(hedged = False, symbol = symbol)
line = f'{OKGREEN}OK!{ENDC} :: {result}'
print(line)
except Exception as e:
line = f'{FAIL}Failed!{ENDC} :: {e}'
print(line)
await subsribeLiquidations(whitelist)
while True:
sleep(1)
if __name__ == '__main__':
asyncio.run(main())