-
Notifications
You must be signed in to change notification settings - Fork 62
/
bybitapi.py
308 lines (276 loc) · 13.9 KB
/
bybitapi.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
import logbot
from pybit import HTTP
class ByBit:
def __init__(self, var: dict):
self.ENDPOINT = 'https://api-testnet.bybit.com'
self.subaccount_name = var['subaccount_name']
self.leverage = var['leverage']
self.risk = var['risk']
self.api_key = var['api_key']
self.api_secret = var['api_secret']
# =============== SIGN, POST AND REQUEST ===============
def _try_request(self, method: str, **kwargs):
session = HTTP(self.ENDPOINT, api_key=self.api_key, api_secret=self.api_secret)
try:
if method=='get_wallet_balance':
req = session.get_wallet_balance(coin=kwargs.get('coin'))
elif method=='my_position':
req = session.my_position(symbol=kwargs.get('symbol'))
elif method=='place_active_order':
req = session.place_active_order(symbol=kwargs.get('symbol'),
side=kwargs.get('side'),
order_type=kwargs.get('order_type'),
qty=kwargs.get('qty'),
price=kwargs.get('price', None),
stop_loss=kwargs.get('stop_loss', None),
time_in_force=kwargs.get('time_in_force'),
reduce_only=kwargs.get('reduce_only'),
close_on_trigger=kwargs.get('close_on_trigger'))
elif method=='place_conditional_order':
req = session.place_conditional_order(symbol=kwargs.get('symbol'),
side=kwargs.get('side'),
order_type=kwargs.get('order_type'),
qty=kwargs.get('qty'),
price=kwargs.get('price'),
base_price=kwargs.get('base_price'),
stop_px=kwargs.get('stop_px'),
trigger_by=kwargs.get('trigger_by'),
time_in_force=kwargs.get('time_in_force'),
reduce_only=kwargs.get('reduce_only'),
close_on_trigger=kwargs.get('close_on_trigger'))
elif method=='cancel_all_active_orders':
req = session.cancel_all_active_orders(symbol=kwargs.get('symbol'))
elif method=='cancel_all_conditional_orders':
req = session.cancel_all_conditional_orders(symbol=kwargs.get('symbol'))
elif method=='set_trading_stop':
req = session.set_trading_stop(symbol=kwargs.get('symbol'),
side=kwargs.get('side'), # Side of the open position
stop_loss=kwargs.get('stop_loss'))
elif method=='query_symbol':
req = session.query_symbol()
except Exception as e:
logbot.logs('>>> /!\ An exception occured : {}'.format(e), True)
return {
"success": False,
"error": str(e)
}
if req['ret_code']:
logbot.logs('>>> /!\ {}'.format(req['ret_msg']), True)
return {
"success": False,
"error": req['ret_msg']
}
else:
req['success'] = True
return req
# ================== UTILITY FUNCTIONS ==================
def _rounded_size(self, size, qty_step):
step_size = round(float(size) / qty_step) * qty_step
if isinstance(qty_step, float):
decimal = len(str(qty_step).split('.')[1])
return round(step_size, decimal)
return step_size
# ================== ORDER FUNCTIONS ==================
def entry_position(self, payload: dict, ticker):
# PLACE ORDER
orders = []
side = 'Buy'
close_sl_tp_side = 'Sell'
stop_loss = payload['long SL']
take_profit = payload['long TP']
if payload['action'] == 'sell':
side = 'Sell'
close_sl_tp_side = 'Buy'
stop_loss = payload['short SL']
take_profit = payload['short TP']
r = self._try_request('query_symbol')
r = r['result']
my_item = next((item for item in r if item['name'] == 'BTCUSDT'), None)
qty_step = my_item['lot_size_filter']['qty_step']
# 0/ Get free collateral and calculate position
r = self._try_request('get_wallet_balance', coin="USDT")
if not r['success']:
return r
free_collateral = r['result']['USDT']['available_balance']
logbot.logs('>>> Found free collateral : {}'.format(free_collateral))
size = (free_collateral * self.risk) / abs(payload['price'] - stop_loss)
if (size / (free_collateral / payload['price'])) > self.leverage:
return {
"success" : False,
"error" : "leverage is higher than maximum limit you set"
}
size = self._rounded_size(size, qty_step)
logbot.logs(f">>> SIZE : {size}, SIDE : {side}, PRICE : {payload['price']}, SL : {stop_loss}, TP : {take_profit}")
# 1/ place order with stop loss
if 'type' in payload.keys():
order_type = payload['type'] # 'market' or 'limit'
order_type = order_type.capitalize()
else:
order_type = 'Market' # per defaut market if none is specified
if order_type != 'Market' and order_type != 'Limit':
return {
"success" : False,
"error" : f"order type '{order_type}' is unknown"
}
exe_price = None if order_type == "Market" else payload['price']
r = self._try_request('place_active_order',
symbol=ticker,
side=side,
order_type=order_type,
qty=size,
price=exe_price,
stop_loss=stop_loss,
time_in_force="GoodTillCancel",
reduce_only=False,
close_on_trigger=False)
if not r['success']:
r['orders'] = orders
return r
orders.append(r['result'])
logbot.logs(f">>> Order {order_type} posted with success")
# 2/ place the take profit only if it is not None or 0
if take_profit:
if order_type == 'Market':
r = self._try_request('place_active_order',
symbol=ticker,
side=close_sl_tp_side,
order_type="Limit", # so we avoid paying fees on market take profit
qty=size,
price=take_profit,
time_in_force="GoodTillCancel",
reduce_only=True,
close_on_trigger=False)
if not r['success']:
r['orders'] = orders
return r
orders.append(r['result'])
logbot.logs(">>> Take profit posted with success")
else: # Limit order type
r = self._try_request('place_conditional_order',
symbol=ticker,
side=close_sl_tp_side,
order_type="Limit",
qty=size,
price=take_profit,
base_price=exe_price,
stop_px=exe_price,
trigger_by='LastPrice',
time_in_force="GoodTillCancel",
reduce_only=False, # Do not set to True
close_on_trigger=False)
if not r['success']:
r['orders'] = orders
return r
orders.append(r['result'])
logbot.logs(">>> Take profit posted with success")
# 3/ (optional) place multiples take profits
i = 1
while True:
tp = 'tp' + str(i) + ' Mult'
if tp in payload.keys():
# place limit order
dist = abs(payload['price'] - stop_loss) * payload[tp]
mid_take_profit = (payload['price'] + dist) if side == 'Buy' else (payload['price'] - dist)
mid_size = size * (payload['tp Close'] / 100)
mid_size = self._rounded_size(mid_size, qty_step)
if order_type == 'Market':
r = self._try_request('place_active_order',
symbol=ticker,
side=close_sl_tp_side,
order_type="Limit", # so we avoid paying fees on market take profit
qty=mid_size,
price=mid_take_profit,
time_in_force="GoodTillCancel",
reduce_only=True,
close_on_trigger=False)
if not r['success']:
r['orders'] = orders
return r
orders.append(r['result'])
logbot.logs(f">>> Take profit {i} posted with success at price {mid_take_profit} with size {mid_size}")
else: # Stop limit type
r = self._try_request('place_conditional_order',
symbol=ticker,
side=close_sl_tp_side,
order_type="Limit",
qty=mid_size,
price=mid_take_profit,
base_price=exe_price,
stop_px=exe_price,
trigger_by='LastPrice',
time_in_force="GoodTillCancel",
reduce_only=False, # Do not set to True
close_on_trigger=False)
if not r['success']:
r['orders'] = orders
return r
orders.append(r['result'])
logbot.logs(f">>> Take profit {i} posted with success at price {mid_take_profit} with size {mid_size}")
else:
break
i += 1
return {
"success": True,
"orders": orders
}
def exit_position(self, ticker):
# CLOSE POSITION IF ONE IS ONGOING
r = self._try_request('my_position', symbol=ticker)
if not r['success']:
return r
logbot.logs(">>> Retrieve positions")
for position in r['result']:
open_size = position['size']
if open_size > 0:
open_side = position['side']
close_side = 'Sell' if open_side == 'Buy' else 'Buy'
r = self._try_request('place_active_order',
symbol=ticker,
side=close_side,
order_type="Market",
qty=open_size,
price=None,
time_in_force="GoodTillCancel",
reduce_only=True,
close_on_trigger=False)
if not r['success']:
return r
logbot.logs(">>> Close ongoing position with success")
break
# DELETE ALL OPEN AND CONDITIONAL ORDERS REMAINING
r = self._try_request('cancel_all_active_orders', symbol=ticker)
if not r['success']:
return r
r = self._try_request('cancel_all_conditional_orders', symbol=ticker)
if not r['success']:
return r
logbot.logs(">>> Deleted all open and conditional orders remaining with success")
return {
"success": True
}
def breakeven(self, payload: dict, ticker):
# SET STOP LOSS TO BREAKEVEN
r = self._try_request('my_position', symbol=ticker)
if not r['success']:
return r
logbot.logs(">>> Retrieve positions")
orders = []
for position in r['result']:
open_size = position['size']
if open_size > 0:
open_side = position['side']
# close_side = 'Sell' if open_side == 'Buy' else 'Buy'
breakeven_price = payload['long Breakeven'] if open_side == 'Buy' else payload['short Breakeven']
# place market stop loss at breakeven
r = self._try_request('set_trading_stop',
symbol=ticker,
side=open_side, # Side of the open position
stop_loss=breakeven_price)
if not r['success']:
return r
orders.append(r['result'])
logbot.logs(f">>> Breakeven stop loss posted with success at price {breakeven_price}")
return {
"success": True,
"orders": orders
}