]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - store.py
Store tickers in balance log
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / store.py
index 072d3a2f02ccbc7dd17c1e92b51a786f57d7c6a5..cd0bf7babe2e7240588f4605571365f98096305b 100644 (file)
--- a/store.py
+++ b/store.py
@@ -98,7 +98,8 @@ class ReportStore:
             "args": args,
             })
 
-    def log_balances(self, tag=None):
+    def log_balances(self, tag=None, tickers=None,
+            ticker_currency=None, compute_value=None, type=None):
         self.print_log("[Balance]")
         for currency, balance in self.market.balances.all.items():
             self.print_log("\t{}".format(balance))
@@ -109,11 +110,22 @@ class ReportStore:
                 "balances": self.market.balances.as_json()
                 }
 
+        if tickers is not None:
+            log["tickers"] = self._ticker_hash(tickers, ticker_currency,
+                    compute_value, type)
+
         self.add_log(log.copy())
         self.add_redis_status(log)
 
     def log_tickers(self, amounts, other_currency,
             compute_value, type):
+        log = self._ticker_hash(amounts, other_currency, compute_value,
+                type)
+        log["type"] = "tickers"
+
+        self.add_log(log)
+
+    def _ticker_hash(self, amounts, other_currency, compute_value, type):
         values = {}
         rates = {}
         if callable(compute_value):
@@ -122,8 +134,7 @@ class ReportStore:
         for currency, amount in amounts.items():
             values[currency] = amount.as_json()["value"]
             rates[currency] = amount.rate
-        log = {
-                "type": "tickers",
+        return {
                 "compute_value": compute_value,
                 "balance_type": type,
                 "currency": other_currency,
@@ -132,9 +143,6 @@ class ReportStore:
                 "total": sum(amounts.values()).as_json()["value"]
                 }
 
-        self.add_log(log.copy())
-        self.add_redis_status(log)
-
     def log_dispatch(self, amount, amounts, liquidity, repartition):
         self.add_log({
             "type": "dispatch",
@@ -294,13 +302,20 @@ class BalanceStore:
                 compute_value, type)
         return amounts
 
-    def fetch_balances(self, tag=None):
+    def fetch_balances(self, tag=None, log_tickers=False,
+            ticker_currency="BTC", ticker_compute_value="average", ticker_type="total"):
         all_balances = self.market.ccxt.fetch_all_balances()
         for currency, balance in all_balances.items():
             if balance["exchange_total"] != 0 or balance["margin_total"] != 0 or \
                     currency in self.all:
                 self.all[currency] = portfolio.Balance(currency, balance)
-        self.market.report.log_balances(tag=tag)
+        if log_tickers:
+            tickers = self.in_currency(ticker_currency, compute_value=ticker_compute_value, type=ticker_type)
+            self.market.report.log_balances(tag=tag,
+                    tickers=tickers, ticker_currency=ticker_currency,
+                    compute_value=ticker_compute_value, type=ticker_type)
+        else:
+            self.market.report.log_balances(tag=tag)
 
     def dispatch_assets(self, amount, liquidity="medium", repartition=None):
         if repartition is None: