This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ftx.py
712 lines (622 loc) · 22.2 KB
/
ftx.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
import asyncio
import pybotters as pb
class FTX(object):
def __init__(self, account_name):
self._ACCOUNT_NAME = account_name
self._EXCHANGE_URL = {
'REST': 'https://ftx.com/api',
'Websocket': 'wss://ftx.com/ws/'
}
if self._ACCOUNT_NAME == 'MAIN':
self._HEADERS_INFORMATION = ''
else:
self._HEADERS_INFORMATION = {'FTX-SUBACCOUNT': self._ACCOUNT_NAME}
async def _requests(self, method=None, path=None, params=None):
async with pb.Client(
apis='apis.json', headers=self._HEADERS_INFORMATION, base_url=self._EXCHANGE_URL['REST']
) as client:
if method == 'get':
resp = await client.get(url=path, params=params)
elif method == 'post':
resp = await client.post(url=path, data=params)
elif method == 'put':
resp = await client.put(url=path, data=params)
elif method == 'delete':
resp = await client.delete(url=path, data=params)
elif method == 'ws':
ws = await client.ws_connect(
url=self._EXCHANGE_URL['Websocket'],
send_json=params,
hdlr_json=self.handler,
)
await ws
else:
raise Exception('No match method')
if not method == 'ws':
data = await resp.json()
return data
# REST API
async def subaccounts_balances(self, nick_name=None):
if nick_name is None:
nick_name = self._ACCOUNT_NAME
path = f'/subaccounts/{nick_name}/balances'
results = await self._requests('get', path)
return results
async def transfer_between_subaccounts(self, coin_name, size, to, from_=None):
"""
送金
:param coin_name: str
:param size: int
:param from_: str
:param to: str
:return: dict
"""
if from_ is None:
from_ = self._ACCOUNT_NAME
path = f'/subaccounts/transfer'
params = {
'coin': coin_name,
'size': size,
'source': from_,
'destination': to
}
results = await self._requests('post', path, params)
return results
async def market(self):
path = f'/markets'
results = await self._requests('get', path)
return results
async def single_market(self, market_name):
"""
:param market_name: str
:return:
"""
path = f'/markets/{market_name}'
results = await self._requests('get', path)
return results
async def orderbook(self, market_name, depth=20):
"""
:param market_name: str
:param depth: int # max 100, default 20
:return: dict
"""
path = f'/markets/{market_name}/orderbook'
params = {
'depth': depth
}
results = await self._requests('get', path, params)
return results
async def trades(self, market_name, limit=20, start_time=None, end_time=None):
"""
:param market_name: str
:param limit: int # max 100, default 20
:param start_time: int
:param end_time: int
:return: dict
"""
path = f'/markets/{market_name}/trades'
params = {
'limit': limit
}
if start_time is not None:
params['start_time'] = start_time
if end_time is not None:
params['end_time'] = end_time
results = await self._requests('get', path, params)
return results
async def historical_prices(self, market_name, resolution, limit, start_time=None, end_time=None):
"""
過去の価格と取引量を取得
:param market_name: str
:param resolution: int # 15, 600, 300, 900, 3600, 14400, 86400
:param limit: int # max 5000
:param start_time: int
:param end_time: int
:return:
"""
path = f'/markets/{market_name}/candles'
params = {
'resolution': resolution,
'limit': limit
}
if start_time is not None:
params['start_time'] = start_time
if end_time is not None:
params['end_time'] = end_time
results = await self._requests('get', path, params)
return results
async def list_all_futures(self):
path = f'/futures'
results = await self._requests('get', path)
return results
async def get_future(self, future_name):
path = f'/futures/{future_name}'
results = await self._requests('get', path)
return results
async def future_stats(self, future_name):
path = f'/futures/{future_name}/stats'
results = await self._requests('get', path)
return results
async def funding_rates(self, start_time=None, end_time=None, future=None):
"""
資金調達率の取得
:param start_time: int
:param end_time: int
:param future: str
:return: dict
"""
path = f'/funding_rates'
params = {}
if future is not None:
params['future'] = future
if start_time is not None:
params['start_time'] = start_time
if end_time is not None:
params['end_time'] = end_time
results = await self._requests('get', path, params)
return results
async def index_weights(self, index_name):
path = f'/indexes/{index_name}/weights'
results = await self._requests('get', path)
return results
async def expired_futures(self):
path = f'/expired_futures'
results = await self._requests('get', path)
return results
async def historical_index(self, index_name, resolution, limit=None, start_time=None, end_time=None):
"""
過去の価格と取引量を取得
:param index_name: str
:param resolution: int # 15, 600, 300, 900, 3600, 14400, 86400
:param limit: inst # max 5000
:param start_time: int
:param end_time: int
:return:
"""
path = f'/indexes/{index_name}/candles'
params = {
'resolution': resolution
}
if limit is not None:
params['limit'] = limit
if start_time is not None:
params['start_time'] = start_time
if end_time is not None:
params['end_time'] = end_time
results = await self._requests('get', path, params)
return results
async def account_information(self):
path = f'/account'
results = await self._requests('get', path)
return results
async def positions(self, show_avg_price=False):
path = f'/positions'
params = {
'showAvgPrice': show_avg_price
}
results = await self._requests('get', path, params)
return results
async def change_leverage(self, leverage):
"""
レバレッジの変更
:param leverage: int # 1, 3, 5, 10, 20, 50, 100
:return: dict
"""
path = f'/account/leverage'
params = {
'leverage': leverage
}
results = await self._requests('post', path, params)
return results
async def coins(self):
path = f'/wallet/coins'
results = await self._requests('get', path)
return results
async def balances(self):
path = f'/wallet/balances'
results = await self._requests('get', path)
return results
async def balances_of_all_accounts(self):
path = f'/wallet/all_balances'
results = await self._requests('get', path)
return results
async def deposit_history(self, limit=None, start_time=None, end_time=None):
path = f'/wallet/deposits'
params = {}
if limit is not None:
params['limit'] = limit
if start_time is not None:
params['start_time'] = start_time
if end_time is not None:
params['end_time'] = end_time
results = await self._requests('get', path, params)
return results
async def withdrawal_history(self, limit=None, start_time=None, end_time=None):
path = f'/wallet/withdrawal'
params = {}
if limit is not None:
params['limit'] = limit
if start_time is not None:
params['start_time'] = start_time
if end_time is not None:
params['end_time'] = end_time
results = await self._requests('get', path, params)
return results
async def open_orders(self, market):
"""
:param market: str
:return: dict
"""
path = f'/orders'
params = {
'market': market
}
results = await self._requests('get', path, params)
return results
async def order_history(self, market, start_time=None, end_time=None, limit=None):
"""
:param market: str
:param start_time: int
:param end_time: int
:param limit: int # max 5000
:return: dict
"""
path = f'/orders/history'
params = {
'market': market
}
if limit is not None:
params['limit'] = limit
if start_time is not None:
params['start_time'] = start_time
if end_time is not None:
params['end_time'] = end_time
results = await self._requests('get', path, params)
return results
async def open_trigger_orders(self, market=None, type_=None):
"""
:param market: str
:param type_: str # stop, trailing_stop, take_profit
:return: dict
"""
path = f'/conditional_orders'
params = {}
if market is not None:
params['market'] = market
if type_ is not None:
params['type'] = type_
results = await self._requests('get', path, params)
return results
async def trigger_order_triggers(self, conditional_order_id):
path = f'/conditional_orders/{conditional_order_id}/triggers'
results = await self._requests('get', path)
return results
async def trigger_order_history(
self, market=None, start_time=None, end_time=None, side=None, type_=None, order_type=None, limit=100
):
"""
:param market: str
:param start_time: int
:param end_time: int
:param side: str # buy, sell
:param type_: str # stop, trailing_stop, take_profit
:param order_type: str # market, limit
:param limit: int # max 100
:return: dict
"""
path = f'/conditional_orders/history'
params = {}
if market is not None:
params['market'] = market
if start_time is not None:
params['start_time'] = start_time
if end_time is not None:
params['end_time'] = end_time
if side is not None:
params['side'] = side
if type_ is not None:
params['type'] = type_
if order_type is not None:
params['orderType'] = order_type
if limit is not None:
params['limit'] = limit
results = await self._requests('get', path, params)
return results
async def place_order(
self, market, side, type_, size, price=None, reduce_only=False, ioc=False, post_only=False, client_id=None
):
path = f'/orders'
params = {
'market': market, # BTC-PERP
'side': side, # buy or sell
'price': price, # Send 'None' for market order
'type': type_, # limit or market
'size': size,
'reduceOnly': reduce_only,
'ioc': ioc,
'postOnly': post_only
}
if type_ == 'market':
params['price'] = None
if client_id is not None:
params['clientId'] = client_id
results = await self._requests('post', path, params)
return results
async def place_trigger_order(
self, market, side, size, type_='stop', reduce_only=None, retry_unit_filled=None,
trigger_price=None, order_price=None, trail_value=None
):
path = f'/conditional_orders'
params = {
'market': market,
'side': side, # buy or sell
'size': size,
'type': type_ # stop, trailingStop or takeProfit
}
if reduce_only is None:
params['reduceOnly'] = False
if type_ == 'stop' or type_ == 'takeProfit':
if trigger_price is None:
raise TypeError(f'trigger_price is None')
else:
params['triggerPrice'] = trigger_price
if order_price is None:
if retry_unit_filled is None:
params['retryUntilFilled'] = True
else:
params['retryUntilFilled'] = False
else:
params['orderPrice'] = order_price
if retry_unit_filled is None:
params['retryUntilFilled'] = True
else:
params['retryUntilFilled'] = False
elif type_ == 'trailingStop':
if trigger_price is None:
raise TypeError('trail_value is None')
else:
params['trailValue'] = trail_value # negative(-) for 'sell', positive(+) for 'buy'
results = await self._requests('post', path, params)
return results
async def order_stats(self, order_id):
path = f'/orders/{order_id}'
results = await self._requests('get', path)
return results
async def order_status_by_client_id(self, client_order_id):
path = f'/orders/by_client_id/{client_order_id}'
results = await self._requests('get', path)
return results
async def cancel_order(self, order_id):
path = f'/orders/{order_id}'
results = await self._requests('delete', path)
return results
async def cancel_order_by_client_id(self, client_order_id):
path = f'/orders/by_client_id/{client_order_id}'
results = await self._requests('delete', path)
return results
async def cancel_open_trigger_order(self, id_):
path = f'/conditional_orders/{id_}'
results = await self._requests('delete', path)
return results
async def cancel_all_orders(self):
path = f'/orders'
results = await self._requests('delete', path)
return results
async def request_quote(self, from_coin, to_coin, size):
"""
通貨の変換
:param from_coin: str
:param to_coin: str
:param size: float
:return: dict
"""
path = f'/otc/quotes'
params = {
'fromCoin': from_coin,
'toCoin': to_coin,
'size': size
}
results = await self._requests('post', path, params)
return results
async def quote_status(self, quote_id, market):
path = f'/otc/quotes/{quote_id}'
params = {
'market': market
}
results = await self._requests('get', path, params)
return results
async def accept_quote(self, quote_id):
path = f'/otc/quotes/{quote_id}/accept'
results = await self._requests('post', path)
return results
async def lending_history(self):
path = f'/spot_margin/history'
results = await self._requests('get', path)
return results
async def borrow_rates(self):
path = f'/spot_margin/borrow_rates'
results = await self._requests('get', path)
return results
async def lending_rates(self):
path = f'/spot_margin/lending_rates'
results = await self._requests('get', path)
return results
async def daily_borrowed_amounts(self):
path = f'/spot_margin/borrow_summary'
results = await self._requests('get', path)
return results
async def market_info(self, market):
path = f'/spot_margin/market_info'
params = {
'market': market
}
results = await self._requests('get', path, params)
return results
async def my_borrow_history(self):
path = f'/spot_margin/borrow_history'
results = await self._requests('get', path)
return results
async def my_lending_history(self):
path = f'/spot_margin/lending_history'
results = await self._requests('get', path)
return results
async def lending_offers(self):
path = f'/spot_margin/offers'
results = await self._requests('get', path)
return results
async def lending_info(self):
path = f'/spot_margin/lending_info'
results = await self._requests('get', path)
return results
async def submit_lending_offer(self):
path = f'/spot_margin/offers'
results = await self._requests('post', path)
return results
async def funding_payments(self, start_time, end_time, future):
path = f'/funding_payments'
params = {
'start_time': start_time,
'end_time': end_time,
'future': future
}
results = await self._requests('get', path, params)
return results
async def list_leverage_tokens(self):
path = f'/lt/tokens'
results = await self._requests('get', path)
return results
async def token_info(self, token_name):
path = f'/lt/{token_name}'
results = await self._requests('get', path)
return results
async def leverage_token_balances(self):
path = f'/lt/balances'
results = await self._requests('get', path)
return results
async def list_leverage_token_creation_requests(self):
path = f'/lt/creations'
results = await self._requests('get', path)
return results
async def request_leverage_token_creation(self, token_name, size):
path = f'/lt/{token_name}/create'
params = {
'size': size
}
results = await self._requests('post', path, params)
return results
async def list_leverage_token_redemption_requests(self):
path = f'/lt/redemptions'
results = await self._requests('get', path)
return results
async def request_leverage_token_redemption(self, token_name, size):
path = f'/lt/{token_name}/redeem'
params = {
'size': size
}
results = await self._requests('post', path, params)
return results
async def option_open_interest(self):
path = f'/options/open_interest/BTC'
results = await self._requests('get', path)
return results
async def account_options_info(self):
path = f'/options/account_info'
results = await self._requests('get', path)
return results
async def option_open_interest_history(self, start_time, end_time, limit):
"""
:param start_time: int
:param end_time: int
:param limit: int # max 200
:return:
"""
path = f'/options/historical_open_interest/BTC'
params = {}
if limit is not None:
params['limit'] = limit
if start_time is not None:
params['start_time'] = start_time
if end_time is not None:
params['end_time'] = end_time
results = await self._requests('get', path, params)
return results
async def option_open_interest_volume(self, start_time, end_time, limit):
"""
:param start_time: int
:param end_time: int
:param limit: int # max 200
:return:
"""
path = f'/options/historical_volumes/BTC'
params = {}
if limit is not None:
params['limit'] = limit
if start_time is not None:
params['start_time'] = start_time
if end_time is not None:
params['end_time'] = end_time
results = await self._requests('get', path, params)
return results
async def stakes(self):
path = f'/staking/stakes'
results = await self._requests('get', path)
return results
async def get_unstake_request(self):
path = f'/staking/unstake_requests'
results = await self._requests('get', path)
return results
async def stake_balances(self):
path = f'/staking/balances'
results = await self._requests('get', path)
return results
async def post_unstake_request(self, coin, size):
path = f'/staking/unstake_requests'
params = {
'coin': coin,
'size': size
}
results = await self._requests('post', path, params)
return results
async def cancel_unstake_request(self, request_id):
path = f'/staking/unstake_requests/{request_id}'
results = await self._requests('delete', path)
return results
async def get_staking_rewards(self):
path = f'/staking/staking_rewards'
results = await self._requests('get', path)
return results
async def stake_request(self, coin, size):
path = f'/srm_stakes/stakes'
params = {
'coin': coin,
'size': size
}
results = await self._requests('post', path, params)
return results
# Websocket API
async def ws(self, subscribe, channel_name, market_name):
"""
:param subscribe: str # subscribe, unsubscribe
:param channel_name: str # orderbook, trades, ticker
:param market_name: str # e.g.)BTC-PERP
:return:
"""
params = {'op': subscribe, 'channel': channel_name, 'market': market_name}
await self._requests(method='ws', params=params)
async def handler(self, msg, *ws):
return msg
async def debug():
ftx = FTX('BOT_04') # Todo: Your sub-account name
# get method
# print(await ftx.trades('ETH-PERP'))
# print(await ftx.account())
# print(await ftx.positions())
# print(await ftx.account_options_info())
# print(await ftx.subaccounts_balances())
# post method
# print(await ftx.change_leverage(1))
# subscribe
# await ftx.ws('unsubscribe', 'ticker', 'BTC-PERP')
await ftx.ws('subscribe', 'trades', 'BTC-PERP')
if __name__ == '__main__':
asyncio.run(debug())