@@ -78,7 +78,11 @@ async def create_new_orders(self, symbol, _, state, **kwargs):
7878 details = kwargs [self .CREATE_ORDER_DATA_PARAM ]
7979 dependencies = kwargs .get (self .CREATE_ORDER_DEPENDENCIES_PARAM , None )
8080 if state == trading_enums .EvaluatorStates .NEUTRAL .value :
81- return await self ._rebalance_portfolio (details , dependencies )
81+ try :
82+ self .trading_mode .is_processing_rebalance = True
83+ return await self ._rebalance_portfolio (details , dependencies )
84+ finally :
85+ self .trading_mode .is_processing_rebalance = False
8286 self .logger .error (f"Unknown index state: { state } " )
8387 return []
8488
@@ -409,6 +413,11 @@ async def _check_index_if_necessary(self):
409413 ) >= self .trading_mode .refresh_interval_days * commons_constants .DAYS_TO_SECONDS :
410414 if self .trading_mode .automatically_update_historical_config_on_set_intervals ():
411415 self .trading_mode .update_config_and_user_inputs_if_necessary ()
416+ if self .trading_mode .is_processing_rebalance :
417+ self .logger .info (
418+ f"[{ self .exchange_manager .exchange_name } ] Index is already being rebalanced, skipping index check"
419+ )
420+ return
412421 if len (self .trading_mode .indexed_coins ) < self .MIN_INDEXED_COINS :
413422 self .logger .error (
414423 f"At least { self .MIN_INDEXED_COINS } coin is required to maintain an index. Please "
@@ -717,6 +726,7 @@ async def cancel_traded_pairs_open_orders_if_any(self) -> typing.Optional[common
717726 order
718727 for order in self .exchange_manager .exchange_personal_data .orders_manager .get_open_orders ()
719728 if order .symbol in self .exchange_manager .exchange_config .traded_symbol_pairs
729+ and not isinstance (order , trading_personal_data .MarketOrder ) # market orders can't be cancelled
720730 ]:
721731 self .logger .info (
722732 f"Cancelling { len (symbol_open_orders )} open orders"
@@ -751,7 +761,8 @@ def __init__(self, config, exchange_manager):
751761 self .synchronization_policy : SynchronizationPolicy = SynchronizationPolicy .SELL_REMOVED_INDEX_COINS_AS_SOON_AS_POSSIBLE
752762 self .requires_initializing_appropriate_coins_distribution = False
753763 self .indexed_coins = []
754-
764+ self .is_processing_rebalance = False
765+
755766 def init_user_inputs (self , inputs : dict ) -> None :
756767 """
757768 Called right before starting the tentacle, should define all the tentacle's user inputs unless
0 commit comments