-
Notifications
You must be signed in to change notification settings - Fork 8
/
spot_api.py
452 lines (399 loc) · 24.2 KB
/
spot_api.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
class SpotApi:
"""
API definition for Spot Trading
"""
async def fetch_markets(self, params=None):
"""
Fetch market information includes precision and limitations of amount, price, and cost
:param params: dict for non-specific parameters
:return: ccxt's market structure https://github.com/ccxt/ccxt/wiki/Manual#market-structure
example: [
{
'id': ' btcusd', // string literal for referencing within an exchange
'symbol': 'BTC/USD', // uppercase string literal of a pair of currencies
'base': 'BTC', // uppercase string, unified base currency code, 3 or more letters
'quote': 'USD', // uppercase string, unified quote currency code, 3 or more letters
'baseId': 'btc', // any string, exchange-specific base currency id
'quoteId': 'usd', // any string, exchange-specific quote currency id
'active': true, // boolean, market status
'taker': 0.002, // Optional, taker fee rate, 0.002 = 0.2%
'maker': 0.0016, // Optional, maker fee rate, 0.0016 = 0.16%
'percentage': true, // Optional, whether the taker and maker fee rate is a multiplier or a fixed flat amount
'tierBased': false, // Optional, whether the fee depends on your trading tier (your trading volume)
'precision': { // number of decimal digits "after the dot"
'price': 8, // Optional, integer or float for TICK_SIZE roundingMode, might be missing if not supplied by the exchange
'amount': 8, // Optional, integer, might be missing if not supplied by the exchange
'cost': 8, // Optional, integer, very few exchanges actually have it
},
'limits': { // value limits when placing orders on this market
'amount': { // Optional
'min': 0.01, // order amount should be > min
'max': 1000, // order amount should be < max
'stepSize': 0.01 // Optional, order amount step size
},
'price': { ... }, // Optional, same min/max limits for the price of the order
'cost': { ... }, // Optional, same limits for order cost = price * amount
},
'info': { ... }, // the original unparsed market info from the exchange
},
... # other pairs
]
"""
raise NotImplementedError()
async def fetch_order_book(self, symbol, since=None, limit=None, fromId=None, direct=None, params=None):
"""
Fetch current order book
:param symbol: symbol in ccxt standard format, example: 'BTC/USDT'
:param since: start time of the orders, should be a Unix timestamp in milliseconds
:param limit: limit number of orders in response
:param fromId: start id of the orders
:param direct: "prev" or "next"
:param params: dict for non-specific parameters
:return: ccxt's order book structure https://github.com/ccxt/ccxt/wiki/Manual#order-book-structure
example:
{
'bids': [
[ price, amount ], // [ Decimal, Decimal ]
[ price, amount ],
...
],
'asks': [
[ price, amount ],
[ price, amount ],
...
],
'timestamp': 1499280391811, // Optional, Unix Timestamp in milliseconds (seconds * 1000)
'datetime': '2017-07-05T18:47:14.692Z', // Optional, ISO8601 datetime string with milliseconds
'nonce': 1499280391811, // Optional, an increasing unique identifier of the orderbook snapshot
'info': { ... }, // the original unparsed order structure as is
}
"""
raise NotImplementedError()
async def create_order(self, symbol, type, side, amount, price, clientOrderId, params=None):
"""
Place a new order
:param symbol: symbol in ccxt standard format, example: 'BTC/USDT'
:param type: 'limit' or 'market', other types aren't unified yet
:param side: 'sell' or 'buy'
:param amount: amount of the order
:param price: price of the order, or None for market order
:param clientOrderId: unique id generated by client side, will be used to check order when the create_order
API didn't give back result clearly (eg. timeout when request)
:param params: dict for non-specific parameters
:return: ccxt order structure https://github.com/ccxt/ccxt/wiki/Manual#order-structure
example:
{
'id': '12345-67890:09876/54321', // string
'clientOrderId': 'abcdef-ghijklmnop-qrstuvwxyz', // a user-defined clientOrderId, if any
'datetime': '2017-08-17 12:42:48.000', // ISO8601 datetime of 'timestamp' with milliseconds
'timestamp': 1502962946216, // order placing/opening Unix timestamp in milliseconds
'lastTradeTimestamp': 1502962956216, // Unix timestamp of the most recent trade on this order
'status': 'open', // 'open', 'closed', 'canceled'
'symbol': 'ETH/BTC', // symbol
'type': 'limit', // 'market', 'limit'
'side': 'buy', // 'buy', 'sell'
'price': 0.06917684, // Decimal price in quote currency (may be empty for market orders)
'average': 0.06917684, // Decimal average filling price
'amount': 1.5, // ordered amount of base currency
'filled': 1.1, // filled amount of base currency
'remaining': 0.4, // remaining amount to fill
'cost': 0.076094524, // 'average' * 'price' (filling price used where available)
'trades': [ ... ], // a list of order trades/executions
'fee': { // fee info, if available
'currency': 'BTC', // which currency the fee is (usually quote)
'cost': 0.0009, // the fee amount in that currency
'rate': 0.002, // the fee rate (if available)
},
'info': { ... }, // the original unparsed order structure as is
}
"""
raise NotImplementedError()
async def fetch_order(self, id, symbol, clientOrderId=None, params=None):
"""
Fetch one single order with order id or client order id
:param id: order id
:param clientOrderId: unique id generated by client side, will be used to check order when the create_order
API didn't give back result clearly (eg. timeout when request)
:param symbol: symbol in ccxt standard format, example: 'BTC/USDT'
:param params: dict for non-specific parameters
:return: ccxt order structure https://github.com/ccxt/ccxt/wiki/Manual#order-structure
example:
{
'id': '12345-67890:09876/54321', // string
'clientOrderId': 'abcdef-ghijklmnop-qrstuvwxyz', // a user-defined clientOrderId, if any
'datetime': '2017-08-17 12:42:48.000', // ISO8601 datetime of 'timestamp' with milliseconds
'timestamp': 1502962946216, // order placing/opening Unix timestamp in milliseconds
'lastTradeTimestamp': 1502962956216, // Unix timestamp of the most recent trade on this order
'status': 'open', // 'open', 'closed', 'canceled'
'symbol': 'ETH/BTC', // symbol
'type': 'limit', // 'market', 'limit'
'side': 'buy', // 'buy', 'sell'
'price': 0.06917684, // Decimal price in quote currency (may be empty for market orders)
'average': 0.06917684, // Decimal average filling price
'amount': 1.5, // ordered amount of base currency
'filled': 1.1, // filled amount of base currency
'remaining': 0.4, // remaining amount to fill
'cost': 0.076094524, // 'average' * 'price' (filling price used where available)
'trades': [ ... ], // a list of order trades/executions
'fee': { // fee info, if available
'currency': 'BTC', // which currency the fee is (usually quote)
'cost': 0.0009, // the fee amount in that currency
'rate': 0.002, // the fee rate (if available)
},
'info': { ... }, // the original unparsed order structure as is
}
"""
raise NotImplementedError()
async def cancel_order(self, id, symbol, clientOrderId=None, params=None):
"""
Cancel one single order by id or client order id
:param id: order id
:param clientOrderId: unique id generated by client side, will be used to check order when the create_order
API doesn't give back result clearly (eg. timeout when request)
:param symbol: symbol in ccxt standard format, example: 'BTC/USDT'
:param params: dict for non-specific parameters
:return: info
example:
{
{
'info': { ... } // the original response
}
}
"""
raise NotImplementedError()
async def fetch_orders(self, symbol, since=None, limit=None, fromId=None, direct=None, params=None):
"""
Fetch all orders
:param symbol: symbol in ccxt standard format, example: 'BTC/USDT'
:param since: start time of the orders, should be a Unix timestamp in milliseconds
:param limit: limit number of orders in response
:param fromId: start id of the orders
:param direct: "prev" or "next"
:param params: dict for non-specific parameters
:return: a list of ccxt order structures https://github.com/ccxt/ccxt/wiki/Manual#order-structure
example:
[
{
'id': '12345-67890:09876/54321', // string
'clientOrderId': 'abcdef-ghijklmnop-qrstuvwxyz', // a user-defined clientOrderId, if any
'datetime': '2017-08-17 12:42:48.000', // ISO8601 datetime of 'timestamp' with milliseconds
'timestamp': 1502962946216, // order placing/opening Unix timestamp in milliseconds
'lastTradeTimestamp': 1502962956216, // Unix timestamp of the most recent trade on this order
'status': 'open', // 'open', 'closed', 'canceled'
'symbol': 'ETH/BTC', // symbol
'type': 'limit', // 'market', 'limit'
'side': 'buy', // 'buy', 'sell'
'price': 0.06917684, // Decimal price in quote currency (may be empty for market orders)
'average': 0.06917684, // Decimal average filling price
'amount': 1.5, // ordered amount of base currency
'filled': 1.1, // filled amount of base currency
'remaining': 0.4, // remaining amount to fill
'cost': 0.076094524, // 'average' * 'price' (filling price used where available)
'trades': [ ... ], // a list of order trades/executions
'fee': { // fee info, if available
'currency': 'BTC', // which currency the fee is (usually quote)
'cost': 0.0009, // the fee amount in that currency
'rate': 0.002, // the fee rate (if available)
},
'info': { ... }, // the original unparsed order structure as is
},
...
]
"""
raise NotImplementedError()
async def fetch_open_orders(self, symbol, since=None, limit=None, fromId=None, direct=None, params=None):
"""
Fetch all orders in open status
:param symbol: symbol in ccxt standard format, example: 'BTC/USDT'
:param since: start time of the orders, should be a Unix timestamp in milliseconds
:param limit: limit number of orders in response
:param fromId: start id of the orders
:param direct: "prev" or "next"
:param params: dict for non-specific parameters
:return: a list of ccxt order structures https://github.com/ccxt/ccxt/wiki/Manual#order-structure
example:
[
{
'id': '12345-67890:09876/54321', // string
'clientOrderId': 'abcdef-ghijklmnop-qrstuvwxyz', // a user-defined clientOrderId, if any
'datetime': '2017-08-17 12:42:48.000', // ISO8601 datetime of 'timestamp' with milliseconds
'timestamp': 1502962946216, // order placing/opening Unix timestamp in milliseconds
'lastTradeTimestamp': 1502962956216, // Unix timestamp of the most recent trade on this order
'status': 'open', // 'open', 'closed', 'canceled'
'symbol': 'ETH/BTC', // symbol
'type': 'limit', // 'market', 'limit'
'side': 'buy', // 'buy', 'sell'
'price': 0.06917684, // Decimal price in quote currency (may be empty for market orders)
'average': 0.06917684, // Decimal average filling price
'amount': 1.5, // ordered amount of base currency
'filled': 1.1, // filled amount of base currency
'remaining': 0.4, // remaining amount to fill
'cost': 0.076094524, // 'average' * 'price' (filling price used where available)
'trades': [ ... ], // a list of order trades/executions
'fee': { // fee info, if available
'currency': 'BTC', // which currency the fee is (usually quote)
'cost': 0.0009, // the fee amount in that currency
'rate': 0.002, // the fee rate (if available)
},
'info': { ... }, // the original unparsed order structure as is
},
...
]
"""
raise NotImplementedError()
async def fetch_closed_orders(self, symbol, since=None, limit=None, fromId=None, direct=None, params=None):
"""
Fetch all orders in closed and canceled status
:param symbol: symbol in ccxt standard format, example: 'BTC/USDT'
:param since: start time of the orders, should be a Unix timestamp in milliseconds
:param limit: limit number of orders in response
:param fromId: start id of the orders
:param direct: "prev" or "next"
:param params: dict for non-specific parameters
:return: a list of ccxt order structures https://github.com/ccxt/ccxt/wiki/Manual#order-structure
example:
[
{
'id': '12345-67890:09876/54321', // string
'clientOrderId': 'abcdef-ghijklmnop-qrstuvwxyz', // a user-defined clientOrderId, if any
'datetime': '2017-08-17 12:42:48.000', // ISO8601 datetime of 'timestamp' with milliseconds
'timestamp': 1502962946216, // order placing/opening Unix timestamp in milliseconds
'lastTradeTimestamp': 1502962956216, // Unix timestamp of the most recent trade on this order
'status': 'open', // 'open', 'closed', 'canceled'
'symbol': 'ETH/BTC', // symbol
'type': 'limit', // 'market', 'limit'
'side': 'buy', // 'buy', 'sell'
'price': 0.06917684, // Decimal price in quote currency (may be empty for market orders)
'average': 0.06917684, // Decimal average filling price
'amount': 1.5, // ordered amount of base currency
'filled': 1.1, // filled amount of base currency
'remaining': 0.4, // remaining amount to fill
'cost': 0.076094524, // 'average' * 'price' (filling price used where available)
'trades': [ ... ], // a list of order trades/executions
'fee': { // fee info, if available
'currency': 'BTC', // which currency the fee is (usually quote)
'cost': 0.0009, // the fee amount in that currency
'rate': 0.002, // the fee rate (if available)
},
'info': { ... }, // the original unparsed order structure as is
},
...
]
"""
raise NotImplementedError()
async def fetch_my_trades(self, symbol, since=None, limit=None, fromId=None, direct=None, params=None):
"""
Fetch recent deals
:param symbol: symbol in ccxt standard format, example: 'BTC/USDT'
:param since: start time of the orders, should be a Unix timestamp in milliseconds
:param limit: limit number of orders in response
:param fromId: start id of the orders
:param direct: "prev" or "next"
:param params: dict for non-specific parameters
:return: a list of ccxt trade structures https://github.com/ccxt/ccxt/wiki/Manual#trade-structure
example:
[
{
'info': { ... }, // the original decoded JSON as is
'id': '12345-67890:09876/54321', // string trade id
'timestamp': 1502962946216, // Unix timestamp in milliseconds
'datetime': '2017-08-17 12:42:48.000', // ISO8601 datetime with milliseconds
'symbol': 'ETH/BTC', // symbol
'order': '12345-67890:09876/54321', // string order id or undefined/None/null
'type': 'limit', // order type, 'market', 'limit' or undefined/None/null
'side': 'buy', // direction of the trade, 'buy' or 'sell'
'takerOrMaker': 'taker', // string, 'taker' or 'maker'
'price': 0.06917684, // Decimal price in quote currency
'amount': 1.5, // amount of base currency
'cost': 0.10376526, // total cost (including fees), `price * amount`
'fee': { // provided by exchange or calculated by ccxt
'cost': 0.0015, // Decimal
'currency': 'ETH', // usually base currency for buys, quote currency for sells
'rate': 0.002, // the fee rate (if available)
},
}
]
"""
raise NotImplementedError()
async def fetch_order_trades(self, id, symbol, since=None, limit=None, params=None):
"""
Fetch recent deals
:param id: order id
:param symbol: symbol in ccxt standard format, example: 'BTC/USDT'
:param since: start time of the orders, should be a Unix timestamp in milliseconds
:param limit: limit number of orders in response
:param params: dict for non-specific parameters
:return: a list of ccxt trade structures https://github.com/ccxt/ccxt/wiki/Manual#trade-structure
example:
[
{
'info': { ... }, // the original decoded JSON as is
'id': '12345-67890:09876/54321', // string trade id
'timestamp': 1502962946216, // Unix timestamp in milliseconds
'datetime': '2017-08-17 12:42:48.000', // ISO8601 datetime with milliseconds
'symbol': 'ETH/BTC', // symbol
'order': '12345-67890:09876/54321', // string order id or undefined/None/null
'type': 'limit', // order type, 'market', 'limit' or undefined/None/null
'side': 'buy', // direction of the trade, 'buy' or 'sell'
'takerOrMaker': 'taker', // string, 'taker' or 'maker'
'price': 0.06917684, // Decimal price in quote currency
'amount': 1.5, // amount of base currency
'cost': 0.10376526, // total cost (including fees), `price * amount`
'fee': { // provided by exchange or calculated by ccxt
'cost': 0.0015, // Decimal
'currency': 'ETH', // usually base currency for buys, quote currency for sells
'rate': 0.002, // the fee rate (if available)
},
}
]
"""
raise NotImplementedError()
async def fetch_balance(self, params=None):
"""
Fetch balances in Perpetual Future Account
:param params: dict for non-specific parameters
:return: ccxt balance structure https://github.com/ccxt/ccxt/wiki/Manual#balance-structure
example:
{
'info': { ... }, // the original untouched non-parsed reply with details
//-------------------------------------------------------------------------
// indexed by availability of funds first, then by currency
'free': { // money, available for trading, by currency
'BTC': 321.00, // Decimals...
'USD': 123.00,
...
},
'used': { ... }, // money on hold, locked, frozen, or pending, by currency
'total': { ... }, // total (free + used), by currency
//-------------------------------------------------------------------------
// indexed by currency first, then by availability of funds
'BTC': { // string, three-letter currency code, uppercase
'free': 321.00 // Decimal, money available for trading
'used': 234.00, // Decimal, money on hold, locked, frozen or pending
'total': 555.00, // Decimal, total balance (free + used)
},
'USD': { // ...
'free': 123.00 // ...
'used': 456.00,
'total': 579.00,
},
...
}
"""
raise NotImplementedError()
async def fetch_trading_fees(self, params=None):
"""
Fetch trading fee rates of the user. This is useful if the fee rates are tier-based
:param params: dict for non-specific parameters
:return: a list of fee rate structure
example:
[
{
'symbol': 'ETH/BTC', // symbol
'taker': 0.002, // taker fee rate, 0.002 = 0.2%
'maker': 0.0016, // maker fee rate, 0.0016 = 0.16%
},
...
]
"""
raise NotImplementedError()