import inspect
from json import JSONDecodeError
from simplejson.errors import JSONDecodeError as SimpleJSONDecodeError
+import dbs
__all__ = ["Portfolio", "BalanceStore", "ReportStore", "TradeStore"]
"args": args,
})
- def log_balances(self, tag=None, tickers=None,
+ def log_balances(self, tag=None, checkpoint=None, tickers=None,
ticker_currency=None, compute_value=None, type=None):
self.print_log("[Balance]")
for currency, balance in self.market.balances.all.items():
log = {
"type": "balance",
"tag": tag,
+ "checkpoint": checkpoint,
"balances": self.market.balances.as_json()
}
compute_value, type)
return amounts
- def fetch_balances(self, tag=None, log_tickers=False,
+ def fetch_balances(self, tag=None, add_portfolio=False,
+ checkpoint=None, log_tickers=False, add_usdt=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 add_usdt:
+ self.all.setdefault("USDT", portfolio.Balance("USDT", {}))
if log_tickers:
tickers = self.in_currency(ticker_currency, compute_value=ticker_compute_value, type=ticker_type)
- self.market.report.log_balances(tag=tag,
+ self.market.report.log_balances(tag=tag, checkpoint=checkpoint,
tickers=tickers, ticker_currency=ticker_currency,
compute_value=ticker_compute_value, type=ticker_type)
else:
- self.market.report.log_balances(tag=tag)
+ self.market.report.log_balances(tag=tag, checkpoint=checkpoint)
def dispatch_assets(self, amount, liquidity="medium", repartition=None):
if repartition is None:
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()]
try:
cls.data.set(r.json(parse_int=D, parse_float=D))
cls.parse_cryptoportfolio()
+ cls.store_cryptoportfolio()
except (JSONDecodeError, SimpleJSONDecodeError):
cls.data.set(None)
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():
+ hash_ = {}
+ for liquidity, repartitions in cls.liquidities.items():
+ hash_[liquidity] = repartitions[cls.last_date.get()]
+ dump = json.dumps(hash_)
+ key = "/cryptoportfolio/repartition/latest"
+ dbs.redis.set(key, dump)
+ key = "/cryptoportfolio/repartition/date"
+ dbs.redis.set(key, cls.last_date.date().isoformat())
+
@classmethod
def parse_cryptoportfolio(cls):
def filter_weights(weight_hash):