X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=ccxt_wrapper.py;fp=ccxt_wrapper.py;h=c500659ae99ce82ed85587b7d9b15579cc178c1d;hb=c7c1e0b26821fdd5622f81fb456f1028d4c9ab09;hp=d37c306882aaae72fa7c56470eada65f77ef5fa1;hpb=472787b6360221588423d03fe3e73d92c09a7c9d;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/ccxt_wrapper.py b/ccxt_wrapper.py index d37c306..c500659 100644 --- a/ccxt_wrapper.py +++ b/ccxt_wrapper.py @@ -1,12 +1,42 @@ from ccxt import * import decimal import time +from retry.api import retry_call +import re def _cw_exchange_sum(self, *args): return sum([arg for arg in args if isinstance(arg, (float, int, decimal.Decimal))]) Exchange.sum = _cw_exchange_sum class poloniexE(poloniex): + RETRIABLE_CALLS = [ + re.compile(r"^return"), + re.compile(r"^cancel"), + re.compile(r"^closeMarginPosition$"), + re.compile(r"^getMarginPosition$"), + ] + + def request(self, path, api='public', method='GET', params={}, headers=None, body=None): + """ + Wrapped to allow retry of non-posting requests" + """ + + origin_request = super(poloniexE, self).request + kwargs = { + "api": api, + "method": method, + "params": params, + "headers": headers, + "body": body + } + + retriable = any(re.match(call, path) for call in self.RETRIABLE_CALLS) + if api == "public" or method == "GET" or retriable: + return retry_call(origin_request, fargs=[path], fkwargs=kwargs, + tries=10, delay=1, exceptions=(RequestTimeout,)) + else: + return origin_request(path, **kwargs) + @staticmethod def nanoseconds(): return int(time.time() * 1000000000)