aboutsummaryrefslogtreecommitdiff
path: root/market.py
diff options
context:
space:
mode:
Diffstat (limited to 'market.py')
-rw-r--r--market.py22
1 files changed, 16 insertions, 6 deletions
diff --git a/market.py b/market.py
index 055967c..ca65bca 100644
--- a/market.py
+++ b/market.py
@@ -1,10 +1,11 @@
1from ccxt import ExchangeError, NotSupported 1from ccxt import ExchangeError, NotSupported, RequestTimeout
2import ccxt_wrapper as ccxt 2import ccxt_wrapper as ccxt
3import time 3import time
4import psycopg2 4import psycopg2
5from store import * 5from store import *
6from cachetools.func import ttl_cache 6from cachetools.func import ttl_cache
7from datetime import datetime 7from datetime import datetime
8from retry import retry
8import portfolio 9import portfolio
9 10
10class Market: 11class Market:
@@ -88,6 +89,7 @@ class Market:
88 finally: 89 finally:
89 self.store_report() 90 self.store_report()
90 91
92 @retry(RequestTimeout, tries=5)
91 def move_balances(self): 93 def move_balances(self):
92 needed_in_margin = {} 94 needed_in_margin = {}
93 moving_to_margin = {} 95 moving_to_margin = {}
@@ -102,13 +104,21 @@ class Market:
102 current_balance = self.balances.all[currency].margin_available 104 current_balance = self.balances.all[currency].margin_available
103 moving_to_margin[currency] = (needed - current_balance) 105 moving_to_margin[currency] = (needed - current_balance)
104 delta = moving_to_margin[currency].value 106 delta = moving_to_margin[currency].value
107 action = "Moving {} from exchange to margin".format(moving_to_margin[currency])
108
105 if self.debug and delta != 0: 109 if self.debug and delta != 0:
106 self.report.log_debug_action("Moving {} from exchange to margin".format(moving_to_margin[currency])) 110 self.report.log_debug_action(action)
107 continue 111 continue
108 if delta > 0: 112 try:
109 self.ccxt.transfer_balance(currency, delta, "exchange", "margin") 113 if delta > 0:
110 elif delta < 0: 114 self.ccxt.transfer_balance(currency, delta, "exchange", "margin")
111 self.ccxt.transfer_balance(currency, -delta, "margin", "exchange") 115 elif delta < 0:
116 self.ccxt.transfer_balance(currency, -delta, "margin", "exchange")
117 except RequestTimeout as e:
118 self.report.log_error(action, message="Retrying", exception=e)
119 self.report.log_move_balances(needed_in_margin, moving_to_margin)
120 self.balances.fetch_balances()
121 raise e
112 self.report.log_move_balances(needed_in_margin, moving_to_margin) 122 self.report.log_move_balances(needed_in_margin, moving_to_margin)
113 123
114 self.balances.fetch_balances() 124 self.balances.fetch_balances()