]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - market.py
Merge branch 'dev'
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / market.py
index b521ea922b004bfedff4e7f82fe04671b571226c..d7b05ce7abb64486a2aad30be1a2317d763ce287 100644 (file)
--- a/market.py
+++ b/market.py
@@ -1,4 +1,4 @@
-from ccxt import ExchangeError, NotSupported, RequestTimeout, InvalidNonce
+from ccxt import AuthenticationError, ExchangeError, NotSupported, RequestTimeout, InvalidNonce
 import ccxt_wrapper as ccxt
 import time
 import dbs
@@ -88,6 +88,7 @@ class Market:
 
     def process(self, actions, before=False, after=False):
         try:
+            self.ccxt.check_required_credentials()
             for action in actions:
                 if bool(before) is bool(after):
                     self.processor.process(action, steps="all")
@@ -95,6 +96,8 @@ class Market:
                     self.processor.process(action, steps="before")
                 elif after:
                     self.processor.process(action, steps="after")
+        except AuthenticationError:
+            self.report.log_error("market_authentication", message="Impossible to authenticate to market")
         except Exception as e:
             import traceback
             self.report.log_error("market_process", exception=e, message=traceback.format_exc())
@@ -217,23 +220,20 @@ class Market:
                 compute_value=compute_value, only=only,
                 repartition=repartition, available_balance_only=available_balance_only)
 
-        values_in_base = self.balances.in_currency(base_currency,
-                compute_value=compute_value)
         if available_balance_only:
-            balance = self.balances.all.get(base_currency)
-            if balance is None:
-                total_base_value = portfolio.Amount(base_currency, 0)
-            else:
-                total_base_value = balance.exchange_free + balance.margin_available
+            repartition, total_base_value, values_in_base = self.balances.available_balances_for_repartition(
+                    base_currency=base_currency, liquidity=liquidity,
+                    repartition=repartition, compute_value=compute_value)
         else:
+            values_in_base = self.balances.in_currency(base_currency,
+                    compute_value=compute_value)
             total_base_value = sum(values_in_base.values())
         new_repartition = self.balances.dispatch_assets(total_base_value,
                 liquidity=liquidity, repartition=repartition)
         if available_balance_only:
             for currency, amount in values_in_base.items():
-                if currency != base_currency:
-                    new_repartition.setdefault(currency, portfolio.Amount(base_currency, 0))
-                    new_repartition[currency] += amount
+                if currency != base_currency and currency not in new_repartition:
+                    new_repartition[currency] = amount
 
         self.trades.compute_trades(values_in_base, new_repartition, only=only)
 
@@ -257,7 +257,11 @@ class Processor:
                 {
                     "name": "print_balances",
                     "number": 1,
-                    "fetch_balances_begin": { "log_tickers": True, "add_portfolio": True },
+                    "fetch_balances_begin": {
+                        "log_tickers": True,
+                        "add_usdt": True,
+                        "add_portfolio": True
+                        },
                     "print_tickers": { "base_currency": "BTC" },
                     }
                 ],
@@ -288,6 +292,7 @@ class Processor:
                     "fetch_balances_begin": {
                         "checkpoint": "end",
                         "log_tickers": True,
+                        "add_usdt": True,
                         "add_portfolio": True
                         },
                     },
@@ -319,6 +324,7 @@ class Processor:
                     "fetch_balances_begin": {},
                     "fetch_balances_end": {
                         "checkpoint": "begin",
+                        "add_usdt": True,
                         "log_tickers": True
                         },
                     "prepare_trades": { "only": "acquire", "available_balance_only": True },
@@ -338,6 +344,7 @@ class Processor:
                     "fetch_balances_begin": {
                         "checkpoint": "end",
                         "log_tickers": True,
+                        "add_usdt": True,
                         "add_portfolio": True
                         },
                     "fetch_balances_end": {},
@@ -362,6 +369,7 @@ class Processor:
                     "fetch_balances_begin": {},
                     "fetch_balances_end": {
                         "checkpoint": "begin",
+                        "add_usdt": True,
                         "log_tickers": True
                         },
                     "prepare_trades": { "available_balance_only": True },