X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=store.py;h=5533759a2f766159b6d098f77bffd7fa4eedd34f;hb=f0001495538945af15ae0c28a07e4de4cc18cbf1;hp=76cfec88f4648e55cd9f1b084204e46f5ad730b4;hpb=9b69786341d14fd4327b117a12437fd1650cd965;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/store.py b/store.py index 76cfec8..5533759 100644 --- a/store.py +++ b/store.py @@ -99,7 +99,7 @@ class ReportStore: "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(): @@ -108,6 +108,7 @@ class ReportStore: log = { "type": "balance", "tag": tag, + "checkpoint": checkpoint, "balances": self.market.balances.as_json() } @@ -303,7 +304,8 @@ class BalanceStore: compute_value, type) return amounts - def fetch_balances(self, tag=None, add_portfolio=False, 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(): @@ -313,13 +315,58 @@ class BalanceStore: 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 available_balances_for_repartition(self, + compute_value="average", base_currency="BTC", + liquidity="medium", repartition=None): + if repartition is None: + repartition = Portfolio.repartition(liquidity=liquidity) + base_currency_balance = self.all.get(base_currency) + + if base_currency_balance is None: + total_base_value = portfolio.Amount(base_currency, 0) + else: + total_base_value = base_currency_balance.exchange_free + \ + base_currency_balance.margin_available - \ + base_currency_balance.margin_in_position + + amount_in_position = {} + + # Compute balances already in the target position + for currency, (ptt, trade_type) in repartition.items(): + amount_in_position[currency] = portfolio.Amount(base_currency, 0) + balance = self.all.get(currency) + if currency != base_currency and balance is not None: + if trade_type == "short": + amount = balance.margin_borrowed + else: + amount = balance.exchange_free + balance.exchange_used + amount_in_position[currency] = amount.in_currency(base_currency, + self.market, compute_value=compute_value) + total_base_value += amount_in_position[currency] + + # recursively delete more-than-filled positions from the wanted + # repartition + did_delete = True + while did_delete: + did_delete = False + sum_ratio = sum([v[0] for k, v in repartition.items()]) + current_base_value = total_base_value + for currency, (ptt, trade_type) in repartition.copy().items(): + if amount_in_position[currency] > current_base_value * ptt / sum_ratio: + did_delete = True + del(repartition[currency]) + total_base_value -= amount_in_position[currency] + return repartition, total_base_value, amount_in_position def dispatch_assets(self, amount, liquidity="medium", repartition=None): if repartition is None: @@ -517,7 +564,8 @@ class Portfolio: cls.retrieve_cryptoportfolio() cls.get_cryptoportfolio() liquidities = cls.liquidities.get(liquidity) - return liquidities[cls.last_date.get()] + if liquidities is not None and cls.last_date.get() in liquidities: + return liquidities[cls.last_date.get()].copy() @classmethod def get_cryptoportfolio(cls, refetch=False):