]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - ccxt_wrapper.py
Add retry facility for api call timeouts
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / ccxt_wrapper.py
index d37c306882aaae72fa7c56470eada65f77ef5fa1..c500659ae99ce82ed85587b7d9b15579cc178c1d 100644 (file)
@@ -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)