diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-05-03 00:23:26 +0200 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-05-03 00:23:26 +0200 |
commit | 56c3a6078a2740d43072dfe30f07b17b442e3cc2 (patch) | |
tree | a534f8d64d247d50929f78c92c74acbe4131a1d2 /store.py | |
parent | 2b1ee8f4d54fa1672510141a71a5817120ac031c (diff) | |
parent | 9b69786341d14fd4327b117a12437fd1650cd965 (diff) | |
download | Trader-56c3a6078a2740d43072dfe30f07b17b442e3cc2.tar.gz Trader-56c3a6078a2740d43072dfe30f07b17b442e3cc2.tar.zst Trader-56c3a6078a2740d43072dfe30f07b17b442e3cc2.zip |
Merge branch 'refactor_db' into dev
Diffstat (limited to 'store.py')
-rw-r--r-- | store.py | 37 |
1 files changed, 35 insertions, 2 deletions
@@ -7,6 +7,7 @@ import datetime | |||
7 | import inspect | 7 | import inspect |
8 | from json import JSONDecodeError | 8 | from json import JSONDecodeError |
9 | from simplejson.errors import JSONDecodeError as SimpleJSONDecodeError | 9 | from simplejson.errors import JSONDecodeError as SimpleJSONDecodeError |
10 | import 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: |