diff options
Diffstat (limited to 'ccxt_wrapper.py')
-rw-r--r-- | ccxt_wrapper.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/ccxt_wrapper.py b/ccxt_wrapper.py index d37c306..4ed37d9 100644 --- a/ccxt_wrapper.py +++ b/ccxt_wrapper.py | |||
@@ -1,12 +1,57 @@ | |||
1 | from ccxt import * | 1 | from ccxt import * |
2 | import decimal | 2 | import decimal |
3 | import time | 3 | import time |
4 | from retry.api import retry_call | ||
5 | import re | ||
4 | 6 | ||
5 | def _cw_exchange_sum(self, *args): | 7 | def _cw_exchange_sum(self, *args): |
6 | return sum([arg for arg in args if isinstance(arg, (float, int, decimal.Decimal))]) | 8 | return sum([arg for arg in args if isinstance(arg, (float, int, decimal.Decimal))]) |
7 | Exchange.sum = _cw_exchange_sum | 9 | Exchange.sum = _cw_exchange_sum |
8 | 10 | ||
9 | class poloniexE(poloniex): | 11 | class poloniexE(poloniex): |
12 | RETRIABLE_CALLS = [ | ||
13 | re.compile(r"^return"), | ||
14 | re.compile(r"^cancel"), | ||
15 | re.compile(r"^closeMarginPosition$"), | ||
16 | re.compile(r"^getMarginPosition$"), | ||
17 | ] | ||
18 | |||
19 | def request(self, path, api='public', method='GET', params={}, headers=None, body=None): | ||
20 | """ | ||
21 | Wrapped to allow retry of non-posting requests" | ||
22 | """ | ||
23 | |||
24 | origin_request = super(poloniexE, self).request | ||
25 | kwargs = { | ||
26 | "api": api, | ||
27 | "method": method, | ||
28 | "params": params, | ||
29 | "headers": headers, | ||
30 | "body": body | ||
31 | } | ||
32 | |||
33 | retriable = any(re.match(call, path) for call in self.RETRIABLE_CALLS) | ||
34 | if api == "public" or method == "GET" or retriable: | ||
35 | return retry_call(origin_request, fargs=[path], fkwargs=kwargs, | ||
36 | tries=10, delay=1, exceptions=(RequestTimeout,)) | ||
37 | else: | ||
38 | return origin_request(path, **kwargs) | ||
39 | |||
40 | def __init__(self, *args, **kwargs): | ||
41 | super(poloniexE, self).__init__(*args, **kwargs) | ||
42 | |||
43 | # For requests logging | ||
44 | self.session.origin_request = self.session.request | ||
45 | self.session._parent = self | ||
46 | |||
47 | def request_wrap(self, *args, **kwargs): | ||
48 | r = self.origin_request(*args, **kwargs) | ||
49 | self._parent._market.report.log_http_request(args[0], | ||
50 | args[1], kwargs["data"], kwargs["headers"], r) | ||
51 | return r | ||
52 | self.session.request = request_wrap.__get__(self.session, | ||
53 | self.session.__class__) | ||
54 | |||
10 | @staticmethod | 55 | @staticmethod |
11 | def nanoseconds(): | 56 | def nanoseconds(): |
12 | return int(time.time() * 1000000000) | 57 | return int(time.time() * 1000000000) |