aboutsummaryrefslogtreecommitdiff
path: root/store.py
diff options
context:
space:
mode:
Diffstat (limited to 'store.py')
-rw-r--r--store.py23
1 files changed, 21 insertions, 2 deletions
diff --git a/store.py b/store.py
index 81828f0..76cfec8 100644
--- a/store.py
+++ b/store.py
@@ -303,13 +303,16 @@ class BalanceStore:
303 compute_value, type) 303 compute_value, type)
304 return amounts 304 return amounts
305 305
306 def fetch_balances(self, tag=None, log_tickers=False, 306 def fetch_balances(self, tag=None, add_portfolio=False, log_tickers=False,
307 ticker_currency="BTC", ticker_compute_value="average", ticker_type="total"): 307 ticker_currency="BTC", ticker_compute_value="average", ticker_type="total"):
308 all_balances = self.market.ccxt.fetch_all_balances() 308 all_balances = self.market.ccxt.fetch_all_balances()
309 for currency, balance in all_balances.items(): 309 for currency, balance in all_balances.items():
310 if balance["exchange_total"] != 0 or balance["margin_total"] != 0 or \ 310 if balance["exchange_total"] != 0 or balance["margin_total"] != 0 or \
311 currency in self.all: 311 currency in self.all:
312 self.all[currency] = portfolio.Balance(currency, balance) 312 self.all[currency] = portfolio.Balance(currency, balance)
313 if add_portfolio:
314 for currency in Portfolio.repartition(from_cache=True):
315 self.all.setdefault(currency, portfolio.Balance(currency, {}))
313 if log_tickers: 316 if log_tickers:
314 tickers = self.in_currency(ticker_currency, compute_value=ticker_compute_value, type=ticker_type) 317 tickers = self.in_currency(ticker_currency, compute_value=ticker_compute_value, type=ticker_type)
315 self.market.report.log_balances(tag=tag, 318 self.market.report.log_balances(tag=tag,
@@ -509,7 +512,9 @@ class Portfolio:
509 cls.get_cryptoportfolio(refetch=True) 512 cls.get_cryptoportfolio(refetch=True)
510 513
511 @classmethod 514 @classmethod
512 def repartition(cls, liquidity="medium"): 515 def repartition(cls, liquidity="medium", from_cache=False):
516 if from_cache:
517 cls.retrieve_cryptoportfolio()
513 cls.get_cryptoportfolio() 518 cls.get_cryptoportfolio()
514 liquidities = cls.liquidities.get(liquidity) 519 liquidities = cls.liquidities.get(liquidity)
515 return liquidities[cls.last_date.get()] 520 return liquidities[cls.last_date.get()]
@@ -538,6 +543,20 @@ class Portfolio:
538 cls.liquidities.set({}) 543 cls.liquidities.set({})
539 544
540 @classmethod 545 @classmethod
546 def retrieve_cryptoportfolio(cls):
547 if dbs.redis_connected():
548 repartition = dbs.redis.get("/cryptoportfolio/repartition/latest")
549 date = dbs.redis.get("/cryptoportfolio/repartition/date")
550 if date is not None and repartition is not None:
551 date = datetime.datetime.strptime(date.decode(), "%Y-%m-%d")
552 repartition = json.loads(repartition, parse_int=D, parse_float=D)
553 repartition = { k: { date: v } for k, v in repartition.items() }
554
555 cls.data.set("")
556 cls.last_date.set(date)
557 cls.liquidities.set(repartition)
558
559 @classmethod
541 def store_cryptoportfolio(cls): 560 def store_cryptoportfolio(cls):
542 if dbs.redis_connected(): 561 if dbs.redis_connected():
543 hash_ = {} 562 hash_ = {}