From 0c5bb7627281dbd4e64a6a132d36b9883c66f988 Mon Sep 17 00:00:00 2001 From: Guillaume De Saint Martin Date: Sun, 22 Oct 2023 17:14:07 +0200 Subject: [PATCH] [Staggered] fix portfolio optimization with empty portfolio --- .../staggered_orders_trading.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Trading/Mode/staggered_orders_trading_mode/staggered_orders_trading.py b/Trading/Mode/staggered_orders_trading_mode/staggered_orders_trading.py index f27afc1f3..33e56a9a4 100644 --- a/Trading/Mode/staggered_orders_trading_mode/staggered_orders_trading.py +++ b/Trading/Mode/staggered_orders_trading_mode/staggered_orders_trading.py @@ -283,9 +283,13 @@ async def single_exchange_process_optimize_initial_portfolio( ) # 4. buy assets - part_2_orders = await self._buy_assets( - producer, pair_bases, target_asset, converted_quote_amount_per_symbol, tickers - ) + if converted_quote_amount_per_symbol == trading_constants.ZERO: + self.logger.warning(f"No {target_asset} in portfolio after optimization.") + part_2_orders = [] + else: + part_2_orders = await self._buy_assets( + producer, pair_bases, target_asset, converted_quote_amount_per_symbol, tickers + ) return [cancelled_orders, part_1_orders, part_2_orders] @@ -347,12 +351,15 @@ async def _buy_assets(self, producer, pair_bases, common_quote, converted_quote_ def _get_converted_quote_amount_per_symbol(self, portfolio, pair_bases, common_quote) -> decimal.Decimal: trading_pairs_count = len(pair_bases) # need portfolio available to be up-to-date with balancing orders - kept_quote_amount = portfolio.portfolio[common_quote].available / decimal.Decimal(2) try: + kept_quote_amount = portfolio.portfolio[common_quote].available / decimal.Decimal(2) return ( (portfolio.portfolio[common_quote].available - kept_quote_amount) / decimal.Decimal(trading_pairs_count) ) + except KeyError: + # no common_quote in portfolio + return trading_constants.ZERO except (decimal.DivisionByZero, decimal.InvalidOperation): # no pair_bases return trading_constants.ZERO