-
Notifications
You must be signed in to change notification settings - Fork 22
/
ftxapi.py
312 lines (278 loc) · 11.6 KB
/
ftxapi.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
309
310
311
312
import logbot
import time, hmac
from requests import Request, Session, Response
class Ftx:
def __init__(self, var: dict):
self.ENDPOINT = 'https://ftx.com/api/'
self.session = Session()
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 _request(self, method: str, path: str, **kwargs):
request = Request(method, self.ENDPOINT + path, **kwargs)
self._sign_request(request)
response = self.session.send(request.prepare())
return self._process_response(response)
def _sign_request(self, request: Request):
ts = int(time.time() * 1000)
prepared = request.prepare()
signature_payload = f'{ts}{prepared.method}{prepared.path_url}'.encode()
if prepared.body:
signature_payload += prepared.body
signature = hmac.new(self.api_secret.encode(), signature_payload, 'sha256').hexdigest()
request.headers['FTX-KEY'] = self.api_key
request.headers['FTX-SIGN'] = signature
request.headers['FTX-TS'] = str(ts)
if self.subaccount_name:
request.headers['FTX-SUBACCOUNT'] = self.subaccount_name
def _process_response(self, response: Response):
try:
data = response.json()
except ValueError:
response.raise_for_status()
raise
else:
return data
def _try_request(self, method: str, path: str, params=None):
try:
if params:
req = self._request(method, path, json=params)
else:
req = self._request(method, path)
except Exception as e:
logbot.logs('>>> /!\ An exception occured : {}'.format(e), True)
return {
"success": False,
"error": str(e)[1:-1]
}
if not req['success']:
logbot.logs('>>> /!\ {}'.format(req['error']), True)
return {
"success": False,
"error": req['error']
}
return req
# ================== 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']
# 0/ Get free collateral and calculate position
r = self._try_request('GET', 'account')
if not r['success']:
return r
free_collateral = r['result']['freeCollateral']
logbot.logs('>>> Found free collateral : {}'.format(free_collateral))
size = (free_collateral * self.risk) / abs(payload['price'] - stop_loss)
if (size / (free_collateral / payload['price'])) > 20:
size = (free_collateral / payload['price']) * self.leverage
logbot.logs(f">>> SIZE : {size}, SIDE : {side}, PRICE : {payload['price']}, SL : {stop_loss}, TP : {take_profit}")
# 1/ for safety place market stop loss first
sl_payload = {
"market": ticker,
"side": close_sl_tp_side,
"triggerPrice": stop_loss,
"size": size,
"type": "stop",
"reduceOnly": True,
"retryUntilFilled": True
}
r = self._try_request('POST', 'conditional_orders', sl_payload)
if not r['success']:
return r
orders.append(r['result'])
logbot.logs(">>> Stop loss posted with success")
# 2/ place order
if 'type' in payload.keys():
order_type = payload['type'] # 'market' or 'limit'
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']
order_payload = {
"market": ticker,
"side": side,
"price": exe_price,
"type": order_type,
"size": size,
"reduceOnly": False,
"ioc": False,
"postOnly": False,
"clientId": None
}
r = self._try_request('POST', 'orders', order_payload)
if not r['success']:
r['orders'] = orders
return r
orders.append(r['result'])
logbot.logs(f">>> Order {order_type} posted with success")
# 3/ finally the take profit only if it is not None or 0
if take_profit:
if order_type == 'market':
tp_payload = {
"market": ticker,
"side": close_sl_tp_side,
"price": take_profit,
"type": "limit", # so we avoid paying fees on market take profit
"size": size,
"reduceOnly": True,
"ioc": False,
"postOnly": False,
"clientId": None
}
r = self._try_request('POST', 'orders', tp_payload)
if not r['success']:
r['orders'] = orders
return r
orders.append(r['result'])
logbot.logs(">>> Take profit posted with success")
else: # Limit order type
tp_payload = {
"market": ticker,
"side": close_sl_tp_side,
"triggerPrice": exe_price,
"orderPrice": take_profit,
"size": size,
"type": "stop",
"reduceOnly": True,
}
r = self._try_request('POST', 'conditional_orders', tp_payload)
if not r['success']:
r['orders'] = orders
return r
orders.append(r['result'])
logbot.logs(">>> Take profit posted with success")
# 4/ (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)
if order_type == 'market':
tp_payload = {
"market": ticker,
"side": close_sl_tp_side,
"price": mid_take_profit,
"type": "limit", # so we avoid paying fees on market take profit
"size": mid_size,
"reduceOnly": True,
"ioc": False,
"postOnly": False,
"clientId": None
}
r = self._try_request('POST', 'orders', tp_payload)
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
tp_payload = {
"market": ticker,
"side": close_sl_tp_side,
"triggerPrice": exe_price,
"orderPrice": mid_take_profit,
"size": mid_size,
"type": "stop",
"reduceOnly": True,
}
r = self._try_request('POST', 'conditional_orders', tp_payload)
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('GET', 'positions')
if not r['success']:
return r
logbot.logs(">>> Retrieve positions")
for position in r['result']:
if position['future'] == ticker:
open_size = position['size']
open_side = position['side']
if open_size: # position is open so close it
close_side = 'sell' if open_side == 'buy' else 'buy'
close_order_payload = {
"market": ticker,
"side": close_side,
"price": None,
"type": "market",
"size": open_size,
"reduceOnly": True,
"ioc": False,
"postOnly": False,
"clientId": None
}
r = self._try_request('POST', 'orders', close_order_payload)
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('DELETE', 'orders')
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('GET', 'positions')
if not r['success']:
return r
logbot.logs(">>> Retrieve positions")
orders = []
for position in r['result']:
if position['future'] == ticker:
open_size = position['openSize']
open_side = position['side']
if open_size: # position is still open (and should be)
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
breakeven_sl_payload = {
"market": ticker,
"side": close_side,
"triggerPrice": breakeven_price,
"size": open_size,
"type": "stop",
"reduceOnly": True,
"retryUntilFilled": True
}
r = self._try_request('POST', 'conditional_orders', breakeven_sl_payload)
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
}