-
Notifications
You must be signed in to change notification settings - Fork 0
/
exchange.py
1056 lines (904 loc) · 45.1 KB
/
exchange.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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
__version__ = '1.18.152'
# -----------------------------------------------------------------------------
import asyncio
import concurrent
import socket
import time
import math
import random
import certifi
import aiohttp
import ssl
import sys
import yarl
import re
import json
import base64
import zlib
# -----------------------------------------------------------------------------
from ccxt.async_support.base.throttle import throttle
# -----------------------------------------------------------------------------
from ccxt.base.errors import ExchangeError
from ccxt.base.errors import AuthenticationError
from ccxt.base.errors import ExchangeNotAvailable
from ccxt.base.errors import RequestTimeout
from ccxt.base.errors import NotSupported
from ccxt.base.errors import NetworkError
# -----------------------------------------------------------------------------
from ccxt.base.exchange import Exchange as BaseExchange
from ccxt.async_support.websocket.websocket_connection import WebsocketConnection
from ccxt.async_support.websocket.pusher_light_connection import PusherLightConnection
from ccxt.async_support.websocket.socketio_light_connection import SocketIoLightConnection
from pyee import EventEmitter
# -----------------------------------------------------------------------------
__all__ = [
'BaseExchange',
'Exchange',
]
# -----------------------------------------------------------------------------
class Exchange(BaseExchange, EventEmitter):
def __init__(self, config={}):
if 'asyncio_loop' in config:
self.asyncio_loop = config['asyncio_loop']
self.asyncio_loop = self.asyncio_loop or asyncio.get_event_loop()
self.own_session = 'session' not in config
# async connection initialization
self.wsconf = {}
self.websocketContexts = {}
self.websocketDelayedConnections = {}
self.wsproxy = None
self.cafile = config.get('cafile', certifi.where())
self.open()
super(Exchange, self).__init__(config)
# snake renaming methods
if 'methodmap' in self.wsconf:
def camel2snake(name):
return name[0].lower() + re.sub(r'(?!^)[A-Z]', lambda x: '_' + x.group(0).lower(), name[1:])
for m in self.wsconf['methodmap']:
self.wsconf['methodmap'][m] = camel2snake(self.wsconf['methodmap'][m])
self.init_rest_rate_limiter()
def init_rest_rate_limiter(self):
self.throttle = throttle(self.extend({
'loop': self.asyncio_loop,
}, self.tokenBucket))
def __del__(self):
if self.session is not None:
self.logger.warning(self.id + " requires to release all resources with an explicit call to the .close() coroutine. If you are creating the exchange instance from within your async coroutine, add exchange.close() to your code into a place when you're done with the exchange and don't need the exchange instance anymore (at the end of your async coroutine).")
if sys.version_info >= (3, 5):
async def __aenter__(self):
self.open()
return self
async def __aexit__(self, exc_type, exc, tb):
await self.close()
def open(self):
if self.own_session and self.session is None:
# Create our SSL context object with our CA cert file
context = ssl.create_default_context(cafile=self.cafile)
# Pass this SSL context to aiohttp and create a TCPConnector
connector = aiohttp.TCPConnector(ssl=context, loop=self.asyncio_loop)
self.session = aiohttp.ClientSession(loop=self.asyncio_loop, connector=connector, trust_env=self.aiohttp_trust_env)
async def close(self):
if self.session is not None:
if self.own_session:
await self.session.close()
self.session = None
async def wait_for_token(self):
while self.rateLimitTokens <= 1:
# if self.verbose:
# print('Waiting for tokens: Exchange: {0}'.format(self.id))
self.add_new_tokens()
seconds_delays = [0.001, 0.005, 0.022, 0.106, 0.5]
delay = random.choice(seconds_delays)
await asyncio.sleep(delay)
self.rateLimitTokens -= 1
def add_new_tokens(self):
# if self.verbose:
# print('Adding new tokens: Exchange: {0}'.format(self.id))
now = time.monotonic()
time_since_update = now - self.rateLimitUpdateTime
new_tokens = math.floor((0.8 * 1000.0 * time_since_update) / self.rateLimit)
if new_tokens > 1:
self.rateLimitTokens = min(self.rateLimitTokens + new_tokens, self.rateLimitMaxTokens)
self.rateLimitUpdateTime = now
async def fetch2(self, path, api='public', method='GET', params={}, headers=None, body=None):
"""A better wrapper over request for deferred signing"""
if self.enableRateLimit:
await self.throttle()
self.lastRestRequestTimestamp = self.milliseconds()
request = self.sign(path, api, method, params, headers, body)
return await self.fetch(request['url'], request['method'], request['headers'], request['body'])
async def fetch(self, url, method='GET', headers=None, body=None):
"""Perform a HTTP request and return decoded JSON data"""
request_headers = self.prepare_request_headers(headers)
url = self.proxy + url
if self.verbose:
print("\nRequest:", method, url, headers, body)
self.logger.debug("%s %s, Request: %s %s", method, url, headers, body)
encoded_body = body.encode() if body else None
session_method = getattr(self.session, method.lower())
response = None
http_response = None
json_response = None
try:
async with session_method(yarl.URL(url, encoded=True),
data=encoded_body,
headers=request_headers,
timeout=(self.timeout / 1000),
proxy=self.aiohttp_proxy) as response:
http_response = await response.text()
json_response = self.parse_json(http_response) if self.is_json_encoded_object(http_response) else None
headers = response.headers
if self.enableLastHttpResponse:
self.last_http_response = http_response
if self.enableLastResponseHeaders:
self.last_response_headers = headers
if self.enableLastJsonResponse:
self.last_json_response = json_response
if self.verbose:
print("\nResponse:", method, url, response.status, headers, http_response)
self.logger.debug("%s %s, Response: %s %s %s", method, url, response.status, headers, http_response)
except socket.gaierror as e:
self.raise_error(ExchangeNotAvailable, url, method, e, None)
except concurrent.futures._base.TimeoutError as e:
self.raise_error(RequestTimeout, method, url, e, None)
except aiohttp.client_exceptions.ClientConnectionError as e:
self.raise_error(ExchangeNotAvailable, url, method, e, None)
except aiohttp.client_exceptions.ClientError as e: # base exception class
self.raise_error(ExchangeError, url, method, e, None)
self.handle_errors(response.status, response.reason, url, method, headers, http_response, json_response)
self.handle_rest_response(http_response, json_response, url, method, headers, body)
if json_response is not None:
return json_response
return http_response
async def load_markets(self, reload=False, params={}):
if not reload:
if self.markets:
if not self.markets_by_id:
return self.set_markets(self.markets)
return self.markets
markets = await self.fetch_markets(params)
currencies = None
if self.has['fetchCurrencies']:
currencies = await self.fetch_currencies()
return self.set_markets(markets, currencies)
async def fetch_fees(self):
trading = {}
funding = {}
try:
trading = await self.fetch_trading_fees()
except AuthenticationError:
pass
except AttributeError:
pass
try:
funding = await self.fetch_funding_fees()
except AuthenticationError:
pass
except AttributeError:
pass
return {
'trading': trading,
'funding': funding,
}
async def load_fees(self):
await self.load_markets()
self.populate_fees()
if not (self.has['fetchTradingFees'] or self.has['fetchFundingFees']):
return self.fees
fetched_fees = await self.fetch_fees()
if fetched_fees['funding']:
self.fees['funding']['fee_loaded'] = True
if fetched_fees['trading']:
self.fees['trading']['fee_loaded'] = True
self.fees = self.deep_extend(self.fees, fetched_fees)
return self.fees
async def fetch_markets(self, params={}):
# markets are returned as a list
# currencies are returned as a dict
# this is for historical reasons
# and may be changed for consistency later
return self.to_array(self.markets)
async def fetch_currencies(self, params={}):
# markets are returned as a list
# currencies are returned as a dict
# this is for historical reasons
# and may be changed for consistency later
return self.currencies
async def fetch_order_status(self, id, symbol=None, params={}):
order = await self.fetch_order(id, symbol, params)
return order['status']
async def fetch_partial_balance(self, part, params={}):
balance = await self.fetch_balance(params)
return balance[part]
async def fetch_l2_order_book(self, symbol, limit=None, params={}):
orderbook = await self.fetch_order_book(symbol, limit, params)
return self.extend(orderbook, {
'bids': self.sort_by(self.aggregate(orderbook['bids']), 0, True),
'asks': self.sort_by(self.aggregate(orderbook['asks']), 0),
})
async def perform_order_book_request(self, market, limit=None, params={}):
raise NotSupported(self.id + ' performOrderBookRequest not supported yet')
async def fetch_order_book(self, symbol, limit=None, params={}):
await self.load_markets()
market = self.market(symbol)
orderbook = await self.perform_order_book_request(market, limit, params)
return self.parse_order_book(orderbook, market, limit, params)
async def fetch_ohlcv(self, symbol, timeframe='1m', since=None, limit=None, params={}):
if not self.has['fetchTrades']:
self.raise_error(NotSupported, details='fetch_ohlcv() not implemented yet')
await self.load_markets()
trades = await self.fetch_trades(symbol, since, limit, params)
return self.build_ohlcv(trades, timeframe, since, limit)
async def fetchOHLCV(self, symbol, timeframe='1m', since=None, limit=None, params={}):
return await self.fetch_ohlcv(symbol, timeframe, since, limit, params)
async def fetch_full_tickers(self, symbols=None, params={}):
return await self.fetch_tickers(symbols, params)
async def edit_order(self, id, symbol, *args):
if not self.enableRateLimit:
self.raise_error(ExchangeError, details='updateOrder() requires enableRateLimit = true')
await self.cancel_order(id, symbol)
return await self.create_order(symbol, *args)
async def load_trading_limits(self, symbols=None, reload=False, params={}):
if self.has['fetchTradingLimits']:
if reload or not('limitsLoaded' in list(self.options.keys())):
response = await self.fetch_trading_limits(symbols)
for i in range(0, len(symbols)):
symbol = symbols[i]
self.markets[symbol] = self.deep_extend(self.markets[symbol], response[symbol])
self.options['limitsLoaded'] = self.milliseconds()
return self.markets
# websocket methods
def parse_bids_asks2(self, bidasks, price_key=0, amount_key=1):
result = []
if len(bidasks):
if type(bidasks[0]) is list:
for bidask in bidasks:
result.append(self.parse_bid_ask(bidask, price_key, amount_key))
elif type(bidasks[0]) is dict:
for bidask in bidasks:
if (price_key in bidask) and (amount_key in bidask):
result.append(self.parse_bid_ask(bidask, price_key, amount_key))
else:
self.raise_error(ExchangeError, details='unrecognized bidask format: ' + str(bidasks[0]))
return result
def searchIndexToInsertOrUpdate(self, value, orderedArray, key, descending=False):
direction = -1 if descending else 1
def compare(a, b):
return -direction if (a < b) else direction if (a > b) else 0
i = 0
for i in range(len(orderedArray)):
if compare(orderedArray[i][key], value) >= 0:
return i
# return i
return len(orderedArray)
def updateBidAsk(self, bidAsk, currentBidsAsks, bids=False):
# insert or replace ordered
index = self.searchIndexToInsertOrUpdate(bidAsk[0], currentBidsAsks, 0, bids)
if ((index < len(currentBidsAsks)) and (currentBidsAsks[index][0] == bidAsk[0])):
# found
if (bidAsk[1] == 0):
# remove
print('Reached removal update')
del currentBidsAsks[index]
else:
# update
currentBidsAsks[index] = bidAsk
else:
if (bidAsk[1] != 0):
# insert
currentBidsAsks.insert(index, bidAsk)
def updateBidAskDiff(self, bidAsk, currentBidsAsks, bids=False):
# insert or replace ordered
index = self.searchIndexToInsertOrUpdate(bidAsk[0], currentBidsAsks, 0, bids)
if ((index < len(currentBidsAsks)) and (currentBidsAsks[index][0] == bidAsk[0])):
# found
nextValue = currentBidsAsks[index][1] + bidAsk[1]
if (nextValue == 0):
# remove
del currentBidsAsks[index]
else:
# update
currentBidsAsks[index][1] = nextValue
else:
if (bidAsk[1] != 0):
# insert
currentBidsAsks.insert(index, bidAsk)
def mergeOrderBookDelta(self, currentOrderBook, orderbook, timestamp=None, bids_key='bids', asks_key='asks', price_key=0, amount_key=1):
bids = self.parse_bids_asks2(orderbook[bids_key], price_key, amount_key) if (bids_key in orderbook) and isinstance(orderbook[bids_key], list) else []
asks = self.parse_bids_asks2(orderbook[asks_key], price_key, amount_key) if (asks_key in orderbook) and isinstance(orderbook[asks_key], list) else []
for bid in bids:
self.updateBidAsk(bid, currentOrderBook['bids'], True)
for ask in asks:
self.updateBidAsk(ask, currentOrderBook['asks'], False)
currentOrderBook['timestamp'] = timestamp
currentOrderBook['datetime'] = self.iso8601(timestamp) if timestamp is not None else None
return currentOrderBook
def mergeOrderBookDeltaDiff(self, currentOrderBook, orderbook, timestamp=None, bids_key='bids', asks_key='asks', price_key=0, amount_key=1):
bids = self.parse_bids_asks2(orderbook[bids_key], price_key, amount_key) if (bids_key in orderbook) and isinstance(orderbook[bids_key], list) else []
asks = self.parse_bids_asks2(orderbook[asks_key], price_key, amount_key) if (asks_key in orderbook) and isinstance(orderbook[asks_key], list) else []
for bid in bids:
self.updateBidAskDiff(bid, currentOrderBook['bids'], True)
for ask in asks:
self.updateBidAskDiff(ask, currentOrderBook['asks'], False)
currentOrderBook['timestamp'] = timestamp
currentOrderBook['datetime'] = self.iso8601(timestamp) if timestamp is not None else None
return currentOrderBook
def _websocketContextGetSubscribedEventSymbols(self, conxid):
ret = []
events = self._contextGetEvents(conxid)
for key in events:
for symbol in events[key]:
symbol_context = events[key][symbol]
if ((symbol_context['subscribed']) or (symbol_context['subscribing'])):
params = symbol_context['params'] if ('params' in symbol_context) else {}
ret.append({
'event': key,
'symbol': symbol,
'params': params,
})
return ret
def _websocketValidEvent(self, event):
return ('events' in self.wsconf) and (event in self.wsconf['events'])
def _websocket_reset_context(self, conxid, conxtpl=None):
if (not (conxid in self.websocketContexts)):
self.websocketContexts[conxid] = {
'_': {},
'conx-tpl': conxtpl,
'events': {},
'conx': None,
}
else:
events = self._contextGetEvents(conxid)
for key in events:
for symbol in events[key]:
symbol_context = events[key][symbol]
symbol_context['subscribed'] = False
symbol_context['subscribing'] = False
symbol_context['data'] = {}
def _contextGetConxTpl(self, conxid):
return self.websocketContexts[conxid]['conx-tpl']
def _contextGetConnection(self, conxid):
if (self.websocketContexts[conxid]['conx'] is None):
return None
return self.websocketContexts[conxid]['conx']['conx']
def _contextGetConnectionInfo(self, conxid):
if (self.websocketContexts[conxid]['conx'] is None):
raise NotSupported("websocket <" + conxid + "> not found in this exchange: " + self.id)
return self.websocketContexts[conxid]['conx']
def _contextIsConnectionReady(self, conxid):
return self.websocketContexts[conxid]['conx']['ready']
def _contextSetConnectionReady(self, conxid, ready):
self.websocketContexts[conxid]['conx']['ready'] = ready
def _contextIsConnectionAuth(self, conxid):
return self.websocketContexts[conxid]['conx']['auth']
def _contextSetConnectionAuth(self, conxid, auth):
self.websocketContexts[conxid]['conx']['auth'] = auth
def _contextSetConnectionInfo(self, conxid, info):
self.websocketContexts[conxid]['conx'] = info
def _contextSet(self, conxid, key, data):
self.websocketContexts[conxid]['_'][key] = data
def _contextGet(self, conxid, key):
if (key not in self.websocketContexts[conxid]['_']):
return None
return self.websocketContexts[conxid]['_'][key]
def _contextGetEvents(self, conxid):
return self.websocketContexts[conxid]['events']
def _contextGetSymbols(self, conxid, event):
return self.websocketContexts[conxid]['events'][event]
def _contextResetEvent(self, conxid, event):
self.websocketContexts[conxid]['events'][event] = {}
def _contextResetSymbol(self, conxid, event, symbol):
self.websocketContexts[conxid]['events'][event][symbol] = {
'subscribed': False,
'subscribing': False,
'data': {},
}
def _contextGetSymbolData(self, conxid, event, symbol):
return self.websocketContexts[conxid]['events'][event][symbol]['data']
def _contextSetSymbolData(self, conxid, event, symbol, data):
self.websocketContexts[conxid]['events'][event][symbol]['data'] = data
def _contextSetSubscribed(self, conxid, event, symbol, subscribed, params={}):
self.websocketContexts[conxid]['events'][event][symbol]['subscribed'] = subscribed
self.websocketContexts[conxid]['events'][event][symbol]['params'] = params
def _contextIsSubscribed(self, conxid, event, symbol):
return (event in self.websocketContexts[conxid]['events']) and \
(symbol in self.websocketContexts[conxid]['events'][event]) and \
self.websocketContexts[conxid]['events'][event][symbol]['subscribed']
def _contextSetSubscribing(self, conxid, event, symbol, subscribing):
self.websocketContexts[conxid]['events'][event][symbol]['subscribing'] = subscribing
def _contextIsSubscribing(self, conxid, event, symbol):
return (event in self.websocketContexts[conxid]['events']) and \
(symbol in self.websocketContexts[conxid]['events'][event]) and \
self.websocketContexts[conxid]['events'][event][symbol]['subscribing']
def _websocketGetConxid4Event(self, event, symbol):
eventConf = self.safe_value(self.wsconf['events'], event)
conxParam = self.safe_value(eventConf, 'conx-param', {
'id': '{id}'
})
return {
'conxid': self.implode_params(conxParam['id'], {
'event': event,
'symbol': symbol,
'id': eventConf['conx-tpl']
}),
'conxtpl': eventConf['conx-tpl']
}
def _websocket_get_action_for_event(self, conxid, event, symbol, subscription=True, subscription_params={}):
# if subscription and still subscribed no action returned
isSubscribed = self._contextIsSubscribed(conxid, event, symbol)
isSubscribing = self._contextIsSubscribing(conxid, event, symbol)
if (subscription and (isSubscribed or isSubscribing)):
return None
# if unsubscription and no subscribed and no subscribing no action returned
if (not subscription and ((not isSubscribed and not isSubscribing))):
return None
# get conexion type for event
event_conf = self.safe_value(self.wsconf['events'], event)
if (event_conf is None):
raise ExchangeError("invalid websocket configuration for event: " + event + " in exchange: " + self.id)
conx_tpl_name = self.safe_string(event_conf, 'conx-tpl', 'default')
conx_tpl = self.safe_value(self.wsconf['conx-tpls'], conx_tpl_name)
if (conx_tpl is None):
raise ExchangeError("tpl websocket conexion: " + conx_tpl_name + " does not exist in exchange: " + self.id)
conxParam = self.safe_value(event_conf, 'conx-param', {
'url': '{baseurl}',
'id': '{id}',
'stream': '{symbol}',
})
params = self.extend({}, conx_tpl, {
'event': event,
'symbol': symbol,
'id': conx_tpl_name,
})
config = self.extend({}, conx_tpl)
for key in conxParam:
config[key] = self.implode_params(conxParam[key], params)
if (not (('id' in config) and ('url' in config) and ('type' in config))):
raise ExchangeError("invalid websocket configuration in exchange: " + self.id)
if (config['type'] == 'signalr'):
return {
'action': 'connect',
'conx-config': config,
'reset-context': 'onconnect',
'conx-tpl': conx_tpl_name,
}
elif (config['type'] == 'ws-io'):
return {
'action': 'connect',
'conx-config': config,
'reset-context': 'onconnect',
'conx-tpl': conx_tpl_name,
}
elif (config['type'] == 'pusher'):
return {
'action': 'connect',
'conx-config': config,
'reset-context': 'onconnect',
'conx-tpl': conx_tpl_name,
}
elif (config['type'] == 'ws'):
return {
'action': 'connect',
'conx-config': config,
'reset-context': 'onconnect',
'conx-tpl': conx_tpl_name,
}
elif (config['type'] == 'ws-s'):
subscribed = self._websocketContextGetSubscribedEventSymbols(config['id'])
if subscription:
subscribed.append({
'event': event,
'symbol': symbol,
})
config['url'] = self._websocket_generate_url_stream(subscribed, config, subscription_params)
return {
'action': 'reconnect',
'conx-config': config,
'reset-context': 'onreconnect',
'conx-tpl': conx_tpl_name,
}
else:
for i in range(len(subscribed)):
element = subscribed[i]
if ((element['event'] == event) and (element['symbol'] == symbol)):
del subscribed[i]
break
if (len(subscribed) == 0):
return {
'action': 'disconnect',
'conx-config': config,
'reset-context': 'always',
'conx-tpl': conx_tpl_name,
}
else:
config['url'] = self._websocket_generate_url_stream(subscribed, config, subscription_params)
return {
'action': 'reconnect',
'conx-config': config,
'reset-context': 'onreconnect',
'conx-tpl': conx_tpl_name,
}
else:
raise NotSupported("invalid websocket connection: " + config['type'] + " for exchange " + self.id)
async def _websocket_ensure_conx_active(self, event, symbol, subscribe, subscription_params={}, delayed=False):
await self.load_markets()
# self.load_markets()
ret = self._websocketGetConxid4Event(event, symbol)
conxid = ret['conxid']
conxtpl = ret['conxtpl']
if (not(conxid in self.websocketContexts)):
self._websocket_reset_context(conxid, conxtpl)
action = self._websocket_get_action_for_event(conxid, event, symbol, subscribe, subscription_params)
if (action is not None):
conx_config = self.safe_value(action, 'conx-config', {})
conx_config['verbose'] = self.verbose
if (not(event in self._contextGetEvents(conxid))):
self._contextResetEvent(conxid, event)
if (not(symbol in self._contextGetSymbols(conxid, event))):
self._contextResetSymbol(conxid, event, symbol)
if (action['action'] == 'reconnect'):
conx = self._contextGetConnection(conxid)
if (conx is not None):
conx.close()
if not delayed:
if (action['reset-context'] == 'onreconnect'):
# self._websocket_reset_context(conxid, conxtpl)
self._contextResetSymbol(conxid, event, symbol)
self._contextSetConnectionInfo(conxid, await self._websocket_initialize(conx_config, conxid))
elif (action['action'] == 'connect'):
conx = self._contextGetConnection(conxid)
if (conx is not None):
if (not conx.isActive()):
conx.close()
self._websocket_reset_context(conxid, conxtpl)
self._contextSetConnectionInfo(conxid, await self._websocket_initialize(conx_config, conxid))
else:
self._websocket_reset_context(conxid, conxtpl)
self._contextSetConnectionInfo(conxid, await self._websocket_initialize(conx_config, conxid))
elif (action['action'] == 'disconnect'):
conx = self._contextGetConnection(conxid)
if (conx is not None):
conx.close()
self._websocket_reset_context(conxid, conxtpl)
if delayed:
# if not subscription in conxid remove from delayed
if conxid in list(self.websocketDelayedConnections.keys()):
del self.websocketDelayedConnections[conxid]
return conxid
if delayed:
if conxid not in list(self.websocketDelayedConnections.keys()):
self.websocketDelayedConnections[conxid] = {
'conxtpl': conxtpl,
'reset': False, # action['action'] != 'connect'
}
else:
await self.websocket_connect(conxid)
return conxid
async def _websocket_connect_delayed(self):
try:
for conxid in list(self.websocketDelayedConnections.keys()):
if self.websocketDelayedConnections[conxid]['reset']:
self._websocket_reset_context(conxid, self.websocketDelayedConnections[conxid]['conxtpl'])
await self.websocket_connect(conxid)
finally:
self.websocketDelayedConnections = {}
async def websocket_connect(self, conxid='default'):
sys.stdout.flush()
websocket_conx_info = self._contextGetConnectionInfo(conxid)
conx_tpl = self._contextGetConxTpl(conxid)
websocket_connection = websocket_conx_info['conx']
await self.load_markets()
# self.load_markets()
if (not websocket_conx_info['ready']):
wait4ready_event = self.safe_string(self.wsconf['conx-tpls'][conx_tpl], 'wait4readyEvent')
if (wait4ready_event is not None):
future = asyncio.Future()
@self.once(wait4ready_event)
def wait4ready_event(success, error=None):
if success:
websocket_conx_info['ready'] = True
future.done() or future.set_result(None)
else:
future.done() or future.set_exception(error)
self.timeout_future(future, 'websocket_connect')
# self.asyncio_loop.run_until_complete(future)
await websocket_connection.connect()
await future
else:
await websocket_connection.connect()
def websocketParseJson(self, raw_data):
return json.loads(raw_data)
def websocketClose(self, conxid='default'):
websocket_conx_info = self._contextGetConnectionInfo(conxid)
websocket_conx_info['conx'].close()
def websocketCloseAll(self):
for c in self.websocketContexts:
self.websocketClose(c)
def websocketCleanContext(self, conxid=None):
if conxid is None:
for conxid in self.websocketContexts:
self._websocket_reset_context(conxid)
else:
self._websocket_reset_context(conxid)
async def websocketRecoverConxid(self, conxid='default', eventSymbols=None):
if eventSymbols is None:
eventSymbols = self._websocketContextGetSubscribedEventSymbols(conxid)
self.websocketClose(conxid)
self._websocket_reset_context(conxid)
await self.websocket_subscribe_all(eventSymbols)
def websocketSend(self, data, conxid='default'):
websocket_conx_info = self._contextGetConnectionInfo(conxid)
if self.verbose:
print("Async send:" + data)
sys.stdout.flush()
websocket_conx_info['conx'].send(data)
def websocketSendJson(self, data, conxid='default'):
websocket_conx_info = self._contextGetConnectionInfo(conxid)
if (self.verbose):
print("Async send:" + json.dumps(data))
sys.stdout.flush()
websocket_conx_info['conx'].sendJson(data)
async def _websocket_initialize(self, websocket_config, conxid='default'):
websocket_connection_info = {
'auth': False,
'ready': False,
'conx': None,
}
websocket_config = await self._websocket_on_init(conxid, websocket_config)
if self.proxies is not None:
websocket_config['proxies'] = self.proxies
if (websocket_config['type'] == 'signalr'):
websocket_connection_info['conx'] = WebsocketConnection(websocket_config, self.timeout, self.asyncio_loop)
elif (websocket_config['type'] == 'ws-io'):
websocket_connection_info['conx'] = SocketIoLightConnection(websocket_config, self.timeout, self.asyncio_loop)
elif (websocket_config['type'] == 'pusher'):
websocket_connection_info['conx'] = PusherLightConnection(websocket_config, self.timeout, self.asyncio_loop)
elif (websocket_config['type'] == 'ws'):
websocket_connection_info['conx'] = WebsocketConnection(websocket_config, self.timeout, self.asyncio_loop)
elif (websocket_config['type'] == 'ws-s'):
websocket_connection_info['conx'] = WebsocketConnection(websocket_config, self.timeout, self.asyncio_loop)
else:
raise NotSupported("invalid async connection: " + websocket_config['type'] + " for exchange " + self.id)
conx = websocket_connection_info['conx']
@conx.on('open')
def websocket_connection_open():
websocket_connection_info['auth'] = False
self._websocket_on_open(conxid, websocket_connection_info['conx'].options)
@conx.on('err')
def websocket_connection_error(error):
websocket_connection_info['auth'] = False
self._websocket_on_error(conxid)
# self._websocket_reset_context(conxid)
self.emit('err', NetworkError(error), conxid)
@conx.on('message')
def websocket_connection_message(msg):
if self.verbose:
print((conxid + '<-' + msg).encode('utf-8'))
sys.stdout.flush()
try:
self._websocket_on_message(conxid, msg)
except Exception as ex:
self.emit('err', ex, conxid)
@conx.on('close')
def websocket_connection_close():
websocket_connection_info['auth'] = False
self._websocket_on_close(conxid)
# self._websocket_reset_context(conxid)
self.emit('close', conxid)
return websocket_connection_info
def timeout_future(self, future, scope):
self.asyncio_loop.call_later(self.timeout / 1000, lambda: future.done() or future.set_exception(TimeoutError("timeout in scope: " + scope)))
def _cloneOrderBook(self, ob, limit=None):
ret = {
'timestamp': ob['timestamp'],
'datetime': ob['datetime'],
'nonce': ob['nonce']
}
if limit is None:
ret['bids'] = ob['bids'][:]
ret['asks'] = ob['asks'][:]
else:
ret['bids'] = ob['bids'][:limit]
ret['asks'] = ob['asks'][:limit]
return ret
def _executeAndCallback(self, contextId, method, params, callback, context={}, this_param=None):
this_param = this_param if (this_param is not None) else self
eself = self
# future = asyncio.Future()
async def t():
try:
ret = await getattr(self, method)(*params)
# ret = getattr(self, method)(*params)
try:
getattr(this_param, callback)(context, None, ret)
except Exception as ex:
eself.emit('err', ExchangeError(eself.id + ': error invoking method ' + callback + ' in _asyncExecute: ' + str(ex)), contextId)
except Exception as ex:
try:
getattr(this_param, callback)(context, ex, None)
except Exception as ex:
eself.emit('err', ExchangeError(eself.id + ': error invoking method ' + callback + ' in _asyncExecute: ' + str(ex)), contextId)
# future.set_result(True)
asyncio.ensure_future(t(), loop=self.asyncio_loop)
# self.asyncio_loop.call_soon(future)
# self.asyncio_loop.call_soon(t)
async def websocket_fetch_order_book(self, symbol, limit=None):
if not self._websocketValidEvent('ob'):
raise ExchangeError('Not valid event ob for exchange ' + self.id)
conxid = await self._websocket_ensure_conx_active('ob', symbol, True)
ob = self._get_current_websocket_orderbook(conxid, symbol, limit)
if (ob is not None):
return ob
future = asyncio.Future()
def wait4orderbook(symbol_r, ob):
if symbol_r == symbol:
self.remove_listener('ob', wait4orderbook)
future.done() or future.set_result(self._get_current_websocket_orderbook(conxid, symbol, limit))
self.on('ob', wait4orderbook)
self.timeout_future(future, 'websocket_fetch_order_book')
return await future
async def websocket_subscribe(self, event, symbol, params={}):
await self.websocket_subscribe_all([{
'event': event,
'symbol': symbol,
'params': params
}])
async def websocket_subscribe_all(self, eventSymbols):
# check all
for eventSymbol in eventSymbols:
if not self._websocketValidEvent(eventSymbol['event']):
raise ExchangeError('Not valid event ' + eventSymbol['event'] + ' for exchange ' + self.id)
conxIds = []
# prepare all conxid
for eventSymbol in eventSymbols:
event = eventSymbol['event']
symbol = eventSymbol['symbol']
params = eventSymbol['params']
conxid = await self._websocket_ensure_conx_active(event, symbol, True, params, True)
conxIds.append(conxid)
self._contextSetSubscribing(conxid, event, symbol, True)
# connect all delayed
await self._websocket_connect_delayed()
for i in range(0, len(eventSymbols)):
conxid = conxIds[i]
event = eventSymbols[i]['event']
symbol = eventSymbols[i]['symbol']
params = eventSymbols[i]['params']
oid = self.nonce() # str(self.nonce()) + '-' + symbol + '-ob-subscribe'
future = asyncio.Future()
oidstr = str(oid)
@self.once(oidstr)
def wait4obsubscribe(success, ex=None):
if success:
self._contextSetSubscribed(conxid, event, symbol, True, params)
self._contextSetSubscribing(conxid, event, symbol, False)
future.done() or future.set_result(conxid)
else:
self._contextSetSubscribed(conxid, event, symbol, False)
self._contextSetSubscribing(conxid, event, symbol, False)
ex = ex if ex is not None else ExchangeError('error subscribing to ' + event + '(' + symbol + ') in ' + self.id)
future.done() or future.set_exception(ex)
self.timeout_future(future, 'websocket_subscribe')
self._websocket_subscribe(conxid, event, symbol, oid, params)
await future
async def websocket_unsubscribe(self, event, symbol, params={}):
await self.websocket_unsubscribe_all([{
'event': event,
'symbol': symbol,
'params': params
}])
async def websocket_unsubscribe_all(self, eventSymbols):
# check all
for eventSymbol in eventSymbols:
if not self._websocketValidEvent(eventSymbol['event']):
raise ExchangeError('Not valid event ' + eventSymbol['event'] + ' for exchange ' + self.id)
try:
for eventSymbol in eventSymbols:
event = eventSymbol['event']
symbol = eventSymbol['symbol']
params = eventSymbol['params']
conxid = await self._websocket_ensure_conx_active(event, symbol, False, params, True)
# ret = self._websocketGetConxid4Event(event, symbol)
# conxid = ret['conxid']
oid = self.nonce() # str(self.nonce()) + '-' + symbol + '-ob-subscribe'
future = asyncio.Future()
oidstr = str(oid)
@self.once(oidstr)
def wait4obunsubscribe(success, ex=None):
if success:
self._contextSetSubscribed(conxid, event, symbol, False)
self._contextSetSubscribing(conxid, event, symbol, False)
future.done() or future.set_result(True)
else:
ex = ex if ex is not None else ExchangeError('error unsubscribing to ' + event + '(' + symbol + ') in ' + self.id)
future.done() or future.set_exception(ex)
self.timeout_future(future, 'websocket_unsubscribe')
self._websocket_unsubscribe(conxid, event, symbol, oid, params)
await future
finally:
await self._websocket_connect_delayed()
async def _websocket_on_init(self, contextId, websocketConexConfig):
return websocketConexConfig
def _websocket_on_open(self, contextId, websocketConexConfig):
pass
def _websocket_on_message(self, contextId, data):
pass
def _websocket_on_close(self, contextId):
pass
def _websocket_on_error(self, contextId):
pass
def _websocketMarketId(self, symbol):
return self.market_id(symbol)
def _websocket_generate_url_stream(self, events, options, subscription_params):
raise NotSupported("You must to implement _websocketGenerateStream method for exchange " + self.id)
def _websocket_subscribe(self, contextId, event, symbol, oid, params={}):
raise NotSupported('subscribe ' + event + '(' + symbol + ') not supported for exchange ' + self.id)
def _websocket_unsubscribe(self, contextId, event, symbol, oid, params={}):
raise NotSupported('unsubscribe ' + event + '(' + symbol + ') not supported for exchange ' + self.id)
def _websocketMethodMap(self, key):
if ('methodmap' not in self.wsconf) or (key not in self.wsconf['methodmap']):
raise ExchangeError(self.id + ': ' + key + ' not found in websocket methodmap')
return self.wsconf['methodmap'][key]
def _setTimeout(self, contextId, mseconds, method, params, this_param=None):
this_param = this_param if (this_param is not None) else self
def f():
try:
getattr(this_param, method)(*params)
except Exception as ex:
self.emit('err', ExchangeError(self.id + ': error invoking method ' + method + ' ' + str(ex)), contextId)
return self.asyncio_loop.call_later(mseconds / 1000, f)
def _cancelTimeout(self, handle):
handle.cancel()
def _setTimer(self, contextId, mseconds, method, params, this_param=None):
this_param = this_param if (this_param is not None) else self
def f():