]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - market.py
Handle timeouts for move_balances
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / market.py
index 055967cd355a0c05c37bdd64e2e6c4f494949073..ca65bca7a0196211caedfa515e55b169ee71231d 100644 (file)
--- a/market.py
+++ b/market.py
@@ -1,10 +1,11 @@
-from ccxt import ExchangeError, NotSupported
+from ccxt import ExchangeError, NotSupported, RequestTimeout
 import ccxt_wrapper as ccxt
 import time
 import psycopg2
 from store import *
 from cachetools.func import ttl_cache
 from datetime import datetime
+from retry import retry
 import portfolio
 
 class Market:
@@ -88,6 +89,7 @@ class Market:
         finally:
             self.store_report()
 
+    @retry(RequestTimeout, tries=5)
     def move_balances(self):
         needed_in_margin = {} 
         moving_to_margin = {}
@@ -102,13 +104,21 @@ class Market:
             current_balance = self.balances.all[currency].margin_available
             moving_to_margin[currency] = (needed - current_balance)
             delta = moving_to_margin[currency].value
+            action = "Moving {} from exchange to margin".format(moving_to_margin[currency])
+
             if self.debug and delta != 0:
-                self.report.log_debug_action("Moving {} from exchange to margin".format(moving_to_margin[currency]))
+                self.report.log_debug_action(action)
                 continue
-            if delta > 0:
-                self.ccxt.transfer_balance(currency, delta, "exchange", "margin")
-            elif delta < 0:
-                self.ccxt.transfer_balance(currency, -delta, "margin", "exchange")
+            try:
+                if delta > 0:
+                    self.ccxt.transfer_balance(currency, delta, "exchange", "margin")
+                elif delta < 0:
+                    self.ccxt.transfer_balance(currency, -delta, "margin", "exchange")
+            except RequestTimeout as e:
+                self.report.log_error(action, message="Retrying", exception=e)
+                self.report.log_move_balances(needed_in_margin, moving_to_margin)
+                self.balances.fetch_balances()
+                raise e
         self.report.log_move_balances(needed_in_margin, moving_to_margin)
 
         self.balances.fetch_balances()