Repository navigation
Expand file tree
/
Copy pathprobit.py
More file actions
558 lines (519 loc) · 22.6 KB
/
Copy pathprobit.py
File metadata and controls
558 lines (519 loc) · 22.6 KB
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
# -*- coding: utf-8 -*-
# PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
# https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code
import ccxt.async_support
from ccxt.async_support.base.ws.cache import ArrayCache, ArrayCacheBySymbolById
from ccxt.base.types import Balances, Int, Order, OrderBook, Str, Ticker, Trade
from ccxt.async_support.base.ws.client import Client
from typing import List
from ccxt.base.errors import ExchangeError
from ccxt.base.errors import NotSupported
class probit(ccxt.async_support.probit):
def describe(self):
return self.deep_extend(super(probit, self).describe(), {
'has': {
'ws': True,
'watchBalance': True,
'watchTicker': True,
'watchTickers': False,
'watchTrades': True,
'watchTradesForSymbols': False,
'watchMyTrades': True,
'watchOrders': True,
'watchOrderBook': True,
'watchOHLCV': False,
},
'urls': {
'api': {
'ws': 'wss://api.probit.com/api/exchange/v1/ws',
},
'test': {
'ws': 'wss://demo-api.probit.com/api/exchange/v1/ws',
},
},
'options': {
'watchOrderBook': {
'filter': 'order_books_l2',
'interval': 100, # or 500
},
},
'streaming': {
},
})
async def watch_balance(self, params={}) -> Balances:
"""
watch balance and get the amount of funds available for trading or funds locked in orders
https://docs-en.probit.com/reference/balance-1
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a `balance structure <https://docs.ccxt.com/#/?id=balance-structure>`
"""
await self.authenticate(params)
messageHash = 'balance'
return await self.subscribe_private(messageHash, 'balance', params)
def handle_balance(self, client: Client, message):
#
# {
# "channel": "balance",
# "reset": False,
# "data": {
# "USDT": {
# "available": "15",
# "total": "15"
# }
# }
# }
#
messageHash = 'balance'
self.parse_ws_balance(message)
client.resolve(self.balance, messageHash)
def parse_ws_balance(self, message):
#
# {
# "channel": "balance",
# "reset": False,
# "data": {
# "USDT": {
# "available": "15",
# "total": "15"
# }
# }
# }
#
reset = self.safe_bool(message, 'reset', False)
data = self.safe_value(message, 'data', {})
currencyIds = list(data.keys())
if reset:
self.balance = {}
for i in range(0, len(currencyIds)):
currencyId = currencyIds[i]
entry = data[currencyId]
code = self.safe_currency_code(currencyId)
account = self.account()
account['free'] = self.safe_string(entry, 'available')
account['total'] = self.safe_string(entry, 'total')
self.balance[code] = account
self.balance = self.safe_balance(self.balance)
async def watch_ticker(self, symbol: str, params={}) -> Ticker:
"""
watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market
https://docs-en.probit.com/reference/marketdata
:param str symbol: unified symbol of the market to fetch the ticker for
:param dict [params]: extra parameters specific to the exchange API endpoint
:param int [params.interval]: Unit time to synchronize market information(ms). Available units: 100, 500
:returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
"""
channel = 'ticker'
return await self.subscribe_public('watchTicker', symbol, 'ticker', channel, params)
def handle_ticker(self, client: Client, message):
#
# {
# "channel": "marketdata",
# "market_id": "BTC-USDT",
# "status": "ok",
# "lag": 0,
# "ticker": {
# "time": "2022-07-21T14:18:04.000Z",
# "last": "22591.3",
# "low": "22500.1",
# "high": "39790.7",
# "change": "-1224",
# "base_volume": "1002.32005445",
# "quote_volume": "23304489.385351021"
# },
# "reset": True
# }
#
marketId = self.safe_string(message, 'market_id')
symbol = self.safe_symbol(marketId)
ticker = self.safe_value(message, 'ticker', {})
market = self.safe_market(marketId)
parsedTicker = self.parse_ticker(ticker, market)
messageHash = 'ticker:' + symbol
self.tickers[symbol] = parsedTicker
client.resolve(parsedTicker, messageHash)
async def watch_trades(self, symbol: str, since: Int = None, limit: Int = None, params={}) -> List[Trade]:
"""
get the list of most recent trades for a particular symbol
https://docs-en.probit.com/reference/trade_history
:param str symbol: unified symbol of the market to fetch trades for
:param int [since]: timestamp in ms of the earliest trade to fetch
:param int [limit]: the maximum amount of trades to fetch
:param dict [params]: extra parameters specific to the exchange API endpoint
:param int [params.interval]: Unit time to synchronize market information(ms). Available units: 100, 500
:returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
"""
channel = 'recent_trades'
symbol = self.safe_symbol(symbol)
trades = await self.subscribe_public('watchTrades', symbol, 'trades', channel, params)
if self.newUpdates:
limit = trades.getLimit(symbol, limit)
return self.filter_by_symbol_since_limit(trades, symbol, since, limit, True)
def handle_trades(self, client: Client, message):
#
# {
# "channel": "marketdata",
# "market_id": "BTC-USDT",
# "status": "ok",
# "lag": 0,
# "recent_trades": [
# {
# "id": "BTC-USDT:8010233",
# "price": "22701.4",
# "quantity": "0.011011",
# "time": "2022-07-21T13:40:40.983Z",
# "side": "buy",
# "tick_direction": "up"
# }
# ...
# ]
# "reset": True
# }
#
marketId = self.safe_string(message, 'market_id')
symbol = self.safe_symbol(marketId)
market = self.safe_market(marketId)
trades = self.safe_value(message, 'recent_trades', [])
if self.safe_bool(message, 'reset', False):
return # see comment in handleMessage
messageHash = 'trades:' + symbol
stored = self.safe_value(self.trades, symbol)
if stored is None:
limit = self.safe_integer(self.options, 'tradesLimit', 1000)
stored = ArrayCache(limit)
self.trades[symbol] = stored
for i in range(0, len(trades)):
trade = trades[i]
parsed = self.parse_trade(trade, market)
stored.append(parsed)
self.trades[symbol] = stored
client.resolve(self.trades[symbol], messageHash)
async def watch_my_trades(self, symbol: Str = None, since: Int = None, limit: Int = None, params={}) -> List[Trade]:
"""
get the list of trades associated with the user
https://docs-en.probit.com/reference/trade_history
:param str symbol: unified symbol of the market to fetch trades for
:param int [since]: timestamp in ms of the earliest trade to fetch
:param int [limit]: the maximum amount of trades to fetch
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
"""
await self.load_markets()
await self.authenticate(params)
messageHash = 'trades'
if symbol is not None:
symbol = self.safe_symbol(symbol)
messageHash = messageHash + ':' + symbol
trades = await self.subscribe_private(messageHash, 'trade_history', params)
if self.newUpdates:
limit = trades.getLimit(symbol, limit)
return self.filter_by_symbol_since_limit(trades, symbol, since, limit, True)
def handle_my_trades(self, client: Client, message):
#
# {
# "channel": "trade_history",
# "reset": False,
# "data": [{
# "id": "BTC-USDT:8010722",
# "order_id": "4124999207",
# "side": "buy",
# "fee_amount": "0.0134999868096",
# "fee_currency_id": "USDT",
# "status": "settled",
# "price": "23136.7",
# "quantity": "0.00032416",
# "cost": "7.499992672",
# "time": "2022-07-21T17:09:33.056Z",
# "market_id": "BTC-USDT"
# }]
# }
#
rawTrades = self.safe_value(message, 'data', [])
length = len(rawTrades)
if length == 0:
return
if self.safe_bool(message, 'reset', False):
return # see comment in handleMessage
messageHash = 'trades'
stored = self.myTrades
if stored is None:
limit = self.safe_integer(self.options, 'tradesLimit', 1000)
stored = ArrayCacheBySymbolById(limit)
self.myTrades = stored
trades = self.parse_trades(rawTrades)
tradeSymbols: dict = {}
for j in range(0, len(trades)):
trade = trades[j]
# don't include 'executed' state, because it's just blanket state of the trade, emited before actual trade event
if self.safe_string(trade['info'], 'status') == 'executed':
continue
tradeSymbols[trade['symbol']] = True
stored.append(trade)
unique = list(tradeSymbols.keys())
uniqueLength = len(unique)
if uniqueLength == 0:
return
for i in range(0, len(unique)):
symbol = unique[i]
symbolSpecificMessageHash = messageHash + ':' + symbol
client.resolve(stored, symbolSpecificMessageHash)
client.resolve(stored, messageHash)
async def watch_orders(self, symbol: Str = None, since: Int = None, limit: Int = None, params={}) -> List[Order]:
"""
watches information on an order made by the user
https://docs-en.probit.com/reference/open_order
:param str symbol: unified symbol of the market the order was made in
:param int [since]: timestamp in ms of the earliest order to watch
:param int [limit]: the maximum amount of orders to watch
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.channel]: choose what channel to use. Can open_order or order_history.
:returns dict: An `order structure <https://docs.ccxt.com/#/?id=order-structure>`
"""
await self.authenticate(params)
messageHash = 'orders'
if symbol is not None:
symbol = self.safe_symbol(symbol)
messageHash = messageHash + ':' + symbol
orders = await self.subscribe_private(messageHash, 'open_order', params)
if self.newUpdates:
limit = orders.getLimit(symbol, limit)
return self.filter_by_symbol_since_limit(orders, symbol, since, limit, True)
def handle_orders(self, client: Client, message):
#
# {
# "channel": "order_history",
# "reset": True,
# "data": [{
# "id": "4124999207",
# "user_id": "633dc56a-621b-4680-8a4e-85a823499b6d",
# "market_id": "BTC-USDT",
# "type": "market",
# "side": "buy",
# "limit_price": "0",
# "time_in_force": "ioc",
# "filled_cost": "7.499992672",
# "filled_quantity": "0.00032416",
# "open_quantity": "0",
# "status": "filled",
# "time": "2022-07-21T17:09:33.056Z",
# "client_order_id": '',
# "cost": "7.5"
# },
# ...
# ]
# }
#
rawOrders = self.safe_value(message, 'data', [])
length = len(rawOrders)
if length == 0:
return
messageHash = 'orders'
reset = self.safe_bool(message, 'reset', False)
stored = self.orders
if stored is None or reset:
limit = self.safe_integer(self.options, 'ordersLimit', 1000)
stored = ArrayCacheBySymbolById(limit)
self.orders = stored
orderSymbols: dict = {}
for i in range(0, len(rawOrders)):
rawOrder = rawOrders[i]
order = self.parse_order(rawOrder)
orderSymbols[order['symbol']] = True
stored.append(order)
unique = list(orderSymbols.keys())
for i in range(0, len(unique)):
symbol = unique[i]
symbolSpecificMessageHash = messageHash + ':' + symbol
client.resolve(stored, symbolSpecificMessageHash)
client.resolve(stored, messageHash)
async def watch_order_book(self, symbol: str, limit: Int = None, params={}) -> OrderBook:
"""
watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data
https://docs-en.probit.com/reference/marketdata
:param str symbol: unified symbol of the market to fetch the order book for
:param int [limit]: the maximum amount of order book entries to return
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
"""
channel = None
channel, params = self.handle_option_and_params(params, 'watchOrderBook', 'filter', 'order_books')
orderbook = await self.subscribe_public('watchOrderBook', symbol, 'orderbook', channel, params)
return orderbook.limit()
async def subscribe_private(self, messageHash, channel, params):
url = self.urls['api']['ws']
subscribe: dict = {
'type': 'subscribe',
'channel': channel,
}
request = self.extend(subscribe, params)
subscribeHash = messageHash
return await self.watch(url, messageHash, request, subscribeHash)
async def subscribe_public(self, methodName: str, symbol: str, dataType, filter, params={}):
await self.load_markets()
market = self.market(symbol)
symbol = market['symbol']
url = self.urls['api']['ws']
client = self.client(url)
subscribeHash = 'marketdata:' + symbol
messageHash = dataType + ':' + symbol
filters = {}
if subscribeHash in client.subscriptions:
# already subscribed
filters = client.subscriptions[subscribeHash]
if not (filter in filters):
# resubscribe
del client.subscriptions[subscribeHash]
filters[filter] = True
keys = list(filters.keys())
interval = None
interval, params = self.handle_option_and_params(params, methodName, 'interval', 100)
request: dict = {
'type': 'subscribe',
'channel': 'marketdata',
'market_id': market['id'],
'filter': keys,
'interval': interval,
}
request = self.extend(request, params)
return await self.watch(url, messageHash, request, subscribeHash, filters)
def handle_order_book(self, client: Client, message, orderBook):
#
# {
# "channel": "marketdata",
# "market_id": "BTC-USDT",
# "status": "ok",
# "lag": 0,
# "order_books": [
# {side: "buy", price: '1420.7', quantity: "0.057"},
# ...
# ],
# "reset": True
# }
#
marketId = self.safe_string(message, 'market_id')
symbol = self.safe_symbol(marketId)
dataBySide = self.group_by(orderBook, 'side')
messageHash = 'orderbook:' + symbol
# orderbook = self.safe_value(self.orderbooks, symbol)
if not (symbol in self.orderbooks):
self.orderbooks[symbol] = self.order_book({})
orderbook = self.orderbooks[symbol]
reset = self.safe_bool(message, 'reset', False)
if reset:
snapshot = self.parse_order_book(dataBySide, symbol, None, 'buy', 'sell', 'price', 'quantity')
orderbook.reset(snapshot)
else:
self.handle_delta(orderbook, dataBySide)
client.resolve(orderbook, messageHash)
def handle_bid_asks(self, bookSide, bidAsks):
for i in range(0, len(bidAsks)):
bidAsk = bidAsks[i]
parsed = self.parse_bid_ask(bidAsk, 'price', 'quantity')
bookSide.storeArray(parsed)
def handle_delta(self, orderbook, delta):
storedBids = orderbook['bids']
storedAsks = orderbook['asks']
asks = self.safe_value(delta, 'sell', [])
bids = self.safe_value(delta, 'buy', [])
self.handle_bid_asks(storedBids, bids)
self.handle_bid_asks(storedAsks, asks)
def handle_error_message(self, client: Client, message):
#
# {
# "errorCode": "INVALID_ARGUMENT",
# "message": '',
# "details": {
# "interval": "invalid"
# }
# }
#
code = self.safe_string(message, 'errorCode')
errMessage = self.safe_string(message, 'message', '')
details = self.safe_value(message, 'details')
feedback = self.id + ' ' + code + ' ' + errMessage + ' ' + self.json(details)
if 'exact' in self.exceptions:
self.throw_exactly_matched_exception(self.exceptions['exact'], code, feedback)
if 'broad' in self.exceptions:
self.throw_broadly_matched_exception(self.exceptions['broad'], errMessage, feedback)
raise ExchangeError(feedback)
def handle_authenticate(self, client: Client, message):
#
# {type: "authorization", result: "ok"}
#
result = self.safe_string(message, 'result')
future = client.subscriptions['authenticated']
if result == 'ok':
messageHash = 'authenticated'
client.resolve(message, messageHash)
else:
future.reject(message)
del client.subscriptions['authenticated']
def handle_market_data(self, client: Client, message):
ticker = self.safe_value(message, 'ticker')
if ticker is not None:
self.handle_ticker(client, message)
trades = self.safe_value(message, 'recent_trades', [])
tradesLength = len(trades)
if tradesLength:
self.handle_trades(client, message)
orderBook = self.safe_value_n(message, ['order_books', 'order_books_l1', 'order_books_l2', 'order_books_l3', 'order_books_l4'], [])
orderBookLength = len(orderBook)
if orderBookLength:
self.handle_order_book(client, message, orderBook)
def handle_message(self, client: Client, message):
#
# {
# "errorCode": "INVALID_ARGUMENT",
# "message": '',
# "details": {
# "interval": "invalid"
# }
# }
#
# Note about 'reset' field
# 'reset': True field - it happens once after initial subscription, which just returns old items by the moment of subscription(like "fetchMyTrades" does)
#
errorCode = self.safe_string(message, 'errorCode')
if errorCode is not None:
self.handle_error_message(client, message)
return
type = self.safe_string(message, 'type')
if type == 'authorization':
self.handle_authenticate(client, message)
return
handlers: dict = {
'marketdata': self.handle_market_data,
'balance': self.handle_balance,
'trade_history': self.handle_my_trades,
'open_order': self.handle_orders,
'order_history': self.handle_orders,
}
channel = self.safe_string(message, 'channel')
handler = self.safe_value(handlers, channel)
if handler is not None:
handler(client, message)
return
error = NotSupported(self.id + ' handleMessage: unknown message: ' + self.json(message))
client.reject(error)
async def authenticate(self, params={}):
url = self.urls['api']['ws']
client = self.client(url)
messageHash = 'authenticated'
expires = self.safe_integer(self.options, 'expires', 0)
future = self.safe_value(client.subscriptions, messageHash)
if (future is None) or (self.milliseconds() > expires):
response = await self.sign_in()
#
# {
# "access_token": "0ttDv/2hTTn3bLi8GP1gKaneiEQ6+0hOBenPrxNQt2s=",
# "token_type": "bearer",
# "expires_in": 900
# }
#
accessToken = self.safe_string(response, 'access_token')
request: dict = {
'type': 'authorization',
'token': accessToken,
}
future = await self.watch(url, messageHash, self.extend(request, params), messageHash)
client.subscriptions[messageHash] = future
return future