X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=market.py;h=fc6f9f667fb320e469f7fa103d398d2c40d184b2;hb=9eb0de20f243bb78de0bf9118289f01f1ea1f77c;hp=ce418415ad30e30ab6c9e556ee1a3d2b1c4cd94f;hpb=239db61d825e0727d2d351e224e7c8552730e472;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/market.py b/market.py index ce41841..fc6f9f6 100644 --- a/market.py +++ b/market.py @@ -210,18 +210,32 @@ class Market: self.report.log_stage("follow_orders_end") def prepare_trades(self, base_currency="BTC", liquidity="medium", - compute_value="average", repartition=None, only=None): + compute_value="average", repartition=None, only=None, + available_balance_only=False): self.report.log_stage("prepare_trades", base_currency=base_currency, liquidity=liquidity, compute_value=compute_value, only=only, - repartition=repartition) + repartition=repartition, available_balance_only=available_balance_only) values_in_base = self.balances.in_currency(base_currency, compute_value=compute_value) - total_base_value = sum(values_in_base.values()) + if available_balance_only: + balance = self.balances.all.get(base_currency) + if balance is None: + total_base_value = portfolio.Amount(base_currency, 0) + else: + total_base_value = balance.exchange_free + balance.margin_available + else: + total_base_value = sum(values_in_base.values()) new_repartition = self.balances.dispatch_assets(total_base_value, liquidity=liquidity, repartition=repartition) + if available_balance_only: + for currency, amount in values_in_base.items(): + if currency != base_currency: + new_repartition.setdefault(currency, portfolio.Amount(base_currency, 0)) + new_repartition[currency] += amount + self.trades.compute_trades(values_in_base, new_repartition, only=only) def print_tickers(self, base_currency="BTC"): @@ -292,7 +306,7 @@ class Processor: "before": False, "after": True, "fetch_balances": ["begin", "end"], - "prepare_trades": { "only": "acquire" }, + "prepare_trades": { "only": "acquire", "available_balance_only": True }, "prepare_orders": { "only": "acquire", "compute_value": "average" }, "move_balances": {}, "run_orders": {}, @@ -326,7 +340,7 @@ class Processor: "before": False, "after": True, "fetch_balances": ["begin", "end"], - "prepare_trades": {}, + "prepare_trades": { "available_balance_only": True }, "prepare_orders": { "compute_value": "average" }, "move_balances": {}, "run_orders": {}, @@ -377,14 +391,14 @@ class Processor: process_name = "process_{}__{}_{}".format(scenario_name, step["number"], step["name"]) self.market.report.log_stage("{}_begin".format(process_name)) if "begin" in step.get("fetch_balances", []): - self.market.balances.fetch_balances(tag="{}_begin".format(process_name)) + self.market.balances.fetch_balances(tag="{}_begin".format(process_name), log_tickers=True) for action in self.ordered_actions: if action in step: self.run_action(action, step[action], kwargs) if "end" in step.get("fetch_balances", []): - self.market.balances.fetch_balances(tag="{}_end".format(process_name)) + self.market.balances.fetch_balances(tag="{}_end".format(process_name), log_tickers=True) self.market.report.log_stage("{}_end".format(process_name)) def method_arguments(self, action):