X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=store.py;h=76cfec88f4648e55cd9f1b084204e46f5ad730b4;hb=56c3a6078a2740d43072dfe30f07b17b442e3cc2;hp=81828f0ae3df4a1c60ea5a287a7b4fe531cacf90;hpb=40d0fa279e0745b33676f21cdc8b496ebd301cf8;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/store.py b/store.py index 81828f0..76cfec8 100644 --- a/store.py +++ b/store.py @@ -303,13 +303,16 @@ class BalanceStore: compute_value, type) return amounts - def fetch_balances(self, tag=None, log_tickers=False, + def fetch_balances(self, tag=None, add_portfolio=False, 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) + if add_portfolio: + for currency in Portfolio.repartition(from_cache=True): + self.all.setdefault(currency, portfolio.Balance(currency, {})) if log_tickers: tickers = self.in_currency(ticker_currency, compute_value=ticker_compute_value, type=ticker_type) self.market.report.log_balances(tag=tag, @@ -509,7 +512,9 @@ class Portfolio: cls.get_cryptoportfolio(refetch=True) @classmethod - def repartition(cls, liquidity="medium"): + def repartition(cls, liquidity="medium", from_cache=False): + if from_cache: + cls.retrieve_cryptoportfolio() cls.get_cryptoportfolio() liquidities = cls.liquidities.get(liquidity) return liquidities[cls.last_date.get()] @@ -537,6 +542,20 @@ class Portfolio: cls.last_date.set(None) cls.liquidities.set({}) + @classmethod + def retrieve_cryptoportfolio(cls): + if dbs.redis_connected(): + repartition = dbs.redis.get("/cryptoportfolio/repartition/latest") + date = dbs.redis.get("/cryptoportfolio/repartition/date") + if date is not None and repartition is not None: + date = datetime.datetime.strptime(date.decode(), "%Y-%m-%d") + repartition = json.loads(repartition, parse_int=D, parse_float=D) + repartition = { k: { date: v } for k, v in repartition.items() } + + cls.data.set("") + cls.last_date.set(date) + cls.liquidities.set(repartition) + @classmethod def store_cryptoportfolio(cls): if dbs.redis_connected():