From 749351ba25cb079c01d0c7f16fe83f34ea459491 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20Sim=C3=B3?= Date: Fri, 13 Dec 2024 14:19:55 +0100 Subject: [PATCH 1/4] - fix: [ERROR] Stop loss amount is higher than balance. Balance --- elena_basics/strategies/common_sl_budget.py | 22 +++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/elena_basics/strategies/common_sl_budget.py b/elena_basics/strategies/common_sl_budget.py index a3cc409..8b341b5 100644 --- a/elena_basics/strategies/common_sl_budget.py +++ b/elena_basics/strategies/common_sl_budget.py @@ -179,6 +179,8 @@ def manage_trailing_stop_losses(self, data: pd.DataFrame, estimated_close_price: if grouped_amount_canceled_orders_and_new_trades >= self.limit_min_amount(): # verify balance, it needs to be checked after any cancellation + base_free = 0 + balance = self.get_balance() if not balance: self._logger.error("Cannot get balance") @@ -187,6 +189,26 @@ def manage_trailing_stop_losses(self, data: pd.DataFrame, estimated_close_price: base_symbol = self.pair.base base_free = balance.currencies[base_symbol].free + # in some cases balance is not available after some time + retry = 0 + retry_limit = 5 + + while total_amount_canceled_orders > base_free and retry < retry_limit: + self._logger.warning(f"Orders for {total_amount_canceled_orders} were cancelled but balance is {base_free}. Retrying...") + time.sleep(5) + balance = self.get_balance() + if not balance: + self._logger.error("Cannot get balance") + return + + base_symbol = self.pair.base + base_free = balance.currencies[base_symbol].free + retry = retry + 1 + + if retry >= retry_limit: + self._logger.error(f"Orders for {total_amount_canceled_orders} were cancelled but balance is {base_free}, after retrying {retry_limit} times.") + return + max_sell = min(grouped_amount_canceled_orders_and_new_trades, base_free) if max_sell < grouped_amount_canceled_orders_and_new_trades: sale_diff = grouped_amount_canceled_orders_and_new_trades - max_sell From 8dc58b794be9dfe079139a00f38db827fbd0077c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20Sim=C3=B3?= Date: Fri, 13 Dec 2024 14:22:17 +0100 Subject: [PATCH 2/4] - fix: [ERROR] Stop loss amount is higher than balance. Balance --- CHANGELOG.md | 3 +++ elena_basics/strategies/common_sl_budget.py | 2 -- setup.cfg | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 15dc9ed..f435041 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 0.1.9 +- fix: [ERROR] Stop loss amount is higher than balance. Balance... + # 0.1.9 - fix: [ERROR] Error creating stop loss: binance Stop price would trigger immediately diff --git a/elena_basics/strategies/common_sl_budget.py b/elena_basics/strategies/common_sl_budget.py index 8b341b5..13324fb 100644 --- a/elena_basics/strategies/common_sl_budget.py +++ b/elena_basics/strategies/common_sl_budget.py @@ -179,8 +179,6 @@ def manage_trailing_stop_losses(self, data: pd.DataFrame, estimated_close_price: if grouped_amount_canceled_orders_and_new_trades >= self.limit_min_amount(): # verify balance, it needs to be checked after any cancellation - base_free = 0 - balance = self.get_balance() if not balance: self._logger.error("Cannot get balance") diff --git a/setup.cfg b/setup.cfg index 35f2725..54b3298 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = elena_basics -version = 0.1.9 +version = 0.1.10 author = "L Software Foundation" author_email = "fransimo@gmail.com, pjover@gmail.com" description = Sample project to link L + CCTX + Backtesting.py From f50a1905fdf6df29a7632d7bb16ece304d1f37bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20Sim=C3=B3?= Date: Fri, 13 Dec 2024 14:22:47 +0100 Subject: [PATCH 3/4] - fix: [ERROR] Stop loss amount is higher than balance. Balance --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f435041..e806947 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# 0.1.9 +# 0.1.10 - fix: [ERROR] Stop loss amount is higher than balance. Balance... # 0.1.9 From 2158dd446ba62584f9ed95307a09cf3c4ccbf3aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20Sim=C3=B3?= Date: Fri, 13 Dec 2024 14:24:25 +0100 Subject: [PATCH 4/4] - fix: [ERROR] Stop loss amount is higher than balance. Balance --- elena_basics/strategies/common_sl_budget.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/elena_basics/strategies/common_sl_budget.py b/elena_basics/strategies/common_sl_budget.py index 13324fb..bd9c4d1 100644 --- a/elena_basics/strategies/common_sl_budget.py +++ b/elena_basics/strategies/common_sl_budget.py @@ -205,7 +205,7 @@ def manage_trailing_stop_losses(self, data: pd.DataFrame, estimated_close_price: if retry >= retry_limit: self._logger.error(f"Orders for {total_amount_canceled_orders} were cancelled but balance is {base_free}, after retrying {retry_limit} times.") - return + return # this return will cause to reprocess the bot in the next cycle but leaves any amount without stop loss max_sell = min(grouped_amount_canceled_orders_and_new_trades, base_free) if max_sell < grouped_amount_canceled_orders_and_new_trades: