-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:
finally:
self.store_report()
+ @retry(RequestTimeout, tries=5)
def move_balances(self):
needed_in_margin = {}
moving_to_margin = {}
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()