aboutsummaryrefslogtreecommitdiff
path: root/store.py
diff options
context:
space:
mode:
Diffstat (limited to 'store.py')
-rw-r--r--store.py37
1 files changed, 35 insertions, 2 deletions
diff --git a/store.py b/store.py
index cd0bf7b..76cfec8 100644
--- a/store.py
+++ b/store.py
@@ -7,6 +7,7 @@ import datetime
7import inspect 7import inspect
8from json import JSONDecodeError 8from json import JSONDecodeError
9from simplejson.errors import JSONDecodeError as SimpleJSONDecodeError 9from simplejson.errors import JSONDecodeError as SimpleJSONDecodeError
10import dbs
10 11
11__all__ = ["Portfolio", "BalanceStore", "ReportStore", "TradeStore"] 12__all__ = ["Portfolio", "BalanceStore", "ReportStore", "TradeStore"]
12 13
@@ -302,13 +303,16 @@ class BalanceStore:
302 compute_value, type) 303 compute_value, type)
303 return amounts 304 return amounts
304 305
305 def fetch_balances(self, tag=None, log_tickers=False, 306 def fetch_balances(self, tag=None, add_portfolio=False, log_tickers=False,
306 ticker_currency="BTC", ticker_compute_value="average", ticker_type="total"): 307 ticker_currency="BTC", ticker_compute_value="average", ticker_type="total"):
307 all_balances = self.market.ccxt.fetch_all_balances() 308 all_balances = self.market.ccxt.fetch_all_balances()
308 for currency, balance in all_balances.items(): 309 for currency, balance in all_balances.items():
309 if balance["exchange_total"] != 0 or balance["margin_total"] != 0 or \ 310 if balance["exchange_total"] != 0 or balance["margin_total"] != 0 or \
310 currency in self.all: 311 currency in self.all:
311 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, {}))
312 if log_tickers: 316 if log_tickers:
313 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)
314 self.market.report.log_balances(tag=tag, 318 self.market.report.log_balances(tag=tag,
@@ -508,7 +512,9 @@ class Portfolio:
508 cls.get_cryptoportfolio(refetch=True) 512 cls.get_cryptoportfolio(refetch=True)
509 513
510 @classmethod 514 @classmethod
511 def repartition(cls, liquidity="medium"): 515 def repartition(cls, liquidity="medium", from_cache=False):
516 if from_cache:
517 cls.retrieve_cryptoportfolio()
512 cls.get_cryptoportfolio() 518 cls.get_cryptoportfolio()
513 liquidities = cls.liquidities.get(liquidity) 519 liquidities = cls.liquidities.get(liquidity)
514 return liquidities[cls.last_date.get()] 520 return liquidities[cls.last_date.get()]
@@ -530,12 +536,39 @@ class Portfolio:
530 try: 536 try:
531 cls.data.set(r.json(parse_int=D, parse_float=D)) 537 cls.data.set(r.json(parse_int=D, parse_float=D))
532 cls.parse_cryptoportfolio() 538 cls.parse_cryptoportfolio()
539 cls.store_cryptoportfolio()
533 except (JSONDecodeError, SimpleJSONDecodeError): 540 except (JSONDecodeError, SimpleJSONDecodeError):
534 cls.data.set(None) 541 cls.data.set(None)
535 cls.last_date.set(None) 542 cls.last_date.set(None)
536 cls.liquidities.set({}) 543 cls.liquidities.set({})
537 544
538 @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
560 def store_cryptoportfolio(cls):
561 if dbs.redis_connected():
562 hash_ = {}
563 for liquidity, repartitions in cls.liquidities.items():
564 hash_[liquidity] = repartitions[cls.last_date.get()]
565 dump = json.dumps(hash_)
566 key = "/cryptoportfolio/repartition/latest"
567 dbs.redis.set(key, dump)
568 key = "/cryptoportfolio/repartition/date"
569 dbs.redis.set(key, cls.last_date.date().isoformat())
570
571 @classmethod
539 def parse_cryptoportfolio(cls): 572 def parse_cryptoportfolio(cls):
540 def filter_weights(weight_hash): 573 def filter_weights(weight_hash):
541 if weight_hash[1][0] == 0: 574 if weight_hash[1][0] == 0: