diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-03-25 19:08:48 +0200 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-03-25 20:28:33 +0200 |
commit | 337c8286cc31d81ffdad06a225996f86c46c46f0 (patch) | |
tree | 37bd365a49fe31d2c6424e37edcba729bc9cb7b0 /market.py | |
parent | 45fffd4963005a1f3957868f9ddb1aa7ec66c0e3 (diff) | |
download | Trader-337c8286cc31d81ffdad06a225996f86c46c46f0.tar.gz Trader-337c8286cc31d81ffdad06a225996f86c46c46f0.tar.zst Trader-337c8286cc31d81ffdad06a225996f86c46c46f0.zip |
Handle timeouts for move_balances
Diffstat (limited to 'market.py')
-rw-r--r-- | market.py | 22 |
1 files changed, 16 insertions, 6 deletions
@@ -1,10 +1,11 @@ | |||
1 | from ccxt import ExchangeError, NotSupported | 1 | from ccxt import ExchangeError, NotSupported, RequestTimeout |
2 | import ccxt_wrapper as ccxt | 2 | import ccxt_wrapper as ccxt |
3 | import time | 3 | import time |
4 | import psycopg2 | 4 | import psycopg2 |
5 | from store import * | 5 | from store import * |
6 | from cachetools.func import ttl_cache | 6 | from cachetools.func import ttl_cache |
7 | from datetime import datetime | 7 | from datetime import datetime |
8 | from retry import retry | ||
8 | import portfolio | 9 | import portfolio |
9 | 10 | ||
10 | class Market: | 11 | class 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() |