X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git;a=blobdiff_plain;f=market.py;h=ca65bca7a0196211caedfa515e55b169ee71231d;hp=055967cd355a0c05c37bdd64e2e6c4f494949073;hb=337c8286cc31d81ffdad06a225996f86c46c46f0;hpb=45fffd4963005a1f3957868f9ddb1aa7ec66c0e3 diff --git a/market.py b/market.py index 055967c..ca65bca 100644 --- 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()