Skip to content

Commit

Permalink
Remove invalid orders lingering
Browse files Browse the repository at this point in the history
  • Loading branch information
donewiththedollar committed Sep 17, 2024
1 parent f9b29de commit eaca2c8
Showing 1 changed file with 49 additions and 26 deletions.
75 changes: 49 additions & 26 deletions directionalscalper/core/strategies/bybit/bybit_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ def __init__(self, exchange, config, manager, symbols_allowed=None):
self.last_processed_signal = {}
self.last_processed_time_long = {} # Dictionary to store the last processed time for long positions
self.last_processed_time_short = {}
self.invalid_long_condition_time = {}
self.invalid_short_condition_time = {}
self.next_long_tp_update = datetime.now() - timedelta(seconds=10)
self.next_short_tp_update = datetime.now() - timedelta(seconds=10)
ConfigInitializer.initialize_config_attributes(self, config)
Expand Down Expand Up @@ -5690,32 +5692,6 @@ def issue_grid_safely(side: str, grid_levels: list, amounts: list):
else:
logging.warning(f"Cannot calculate buffer for {symbol} short position due to invalid conditions: short_pos_price: {short_pos_price}, mfi_signal_short: {mfi_signal_short}")

# # Replace long grid if conditions are met
# if not has_open_long_order:
# if symbol not in self.max_qty_reached_symbol_long:
# logging.info(f"[{symbol}] Replacing long grid orders due to updated buffer or empty grid timeout.")
# buffer_percentage_long = min_buffer_percentage + (max_buffer_percentage - min_buffer_percentage) * (abs(current_price - long_pos_price) / long_pos_price)
# buffer_distance_long = current_price * buffer_percentage_long
# self.clear_grid(symbol, "buy")
# issue_grid_safely('long', grid_levels_long, amounts_long)
# self.last_empty_grid_time[symbol]['long'] = current_time
# logging.info(f"[{symbol}] Recalculated long grid levels with updated buffer: {grid_levels_long}")
# else:
# logging.info(f"{symbol} is in max qty reached symbol long, cannot replace grid")

# # Replace short grid if conditions are met
# if not has_open_short_order:
# if symbol not in self.max_qty_reached_symbol_short:
# logging.info(f"[{symbol}] Replacing short grid orders due to updated buffer or empty grid timeout.")
# buffer_percentage_short = min_buffer_percentage + (max_buffer_percentage - min_buffer_percentage) * (abs(current_price - short_pos_price) / short_pos_price)
# buffer_distance_short = current_price * buffer_percentage_short
# self.clear_grid(symbol, "sell")
# issue_grid_safely('short', grid_levels_short, amounts_short)
# self.last_empty_grid_time[symbol]['short'] = current_time
# logging.info(f"[{symbol}] Recalculated short grid levels with updated buffer: {grid_levels_short}")
# else:
# logging.info(f"{symbol} is in max qty reached symbol short, cannot replace grid")

# Determine if there are open long and short positions based on provided quantities
has_open_long_position = long_pos_qty > 0
has_open_short_position = short_pos_qty > 0
Expand Down Expand Up @@ -6035,6 +6011,53 @@ def issue_grid_safely(side: str, grid_levels: list, amounts: list):
open_orders=open_orders
)

# Clear long grid if conditions are met
if has_open_long_order and (long_pos_price is None or long_pos_price <= 0) and not mfi_signal_long:
if symbol not in self.max_qty_reached_symbol_long:
current_time = time.time()

# Record the time when the invalid condition is first encountered
if symbol not in self.invalid_long_condition_time:
self.invalid_long_condition_time[symbol] = current_time
logging.info(f"Invalid long condition first met for {symbol}. Waiting before clearing grid...")

# If 1 minute has passed since the condition was first encountered
elif current_time - self.invalid_long_condition_time[symbol] >= 60:
self.clear_grid(symbol, "buy")
logging.info(f"Cleared long grid for {symbol} after 1 minute of invalid long_pos_price: {long_pos_price} and no long signal.")
# Reset the tracking time after clearing
del self.invalid_long_condition_time[symbol]
else:
logging.info(f"{symbol} is in max qty reached symbol long, cannot replace grid")
else:
# Reset the tracking if conditions are no longer met
if symbol in self.invalid_long_condition_time:
del self.invalid_long_condition_time[symbol]

# Clear short grid if conditions are met
if has_open_short_order and (short_pos_price is None or short_pos_price <= 0) and not mfi_signal_short:
if symbol not in self.max_qty_reached_symbol_short:
current_time = time.time()

# Record the time when the invalid condition is first encountered
if symbol not in self.invalid_short_condition_time:
self.invalid_short_condition_time[symbol] = current_time
logging.info(f"Invalid short condition first met for {symbol}. Waiting before clearing grid...")

# If 1 minute has passed since the condition was first encountered
elif current_time - self.invalid_short_condition_time[symbol] >= 60:
self.clear_grid(symbol, "sell")
logging.info(f"Cleared short grid for {symbol} after 1 minute of invalid short_pos_price: {short_pos_price} and no short signal.")
# Reset the tracking time after clearing
del self.invalid_short_condition_time[symbol]
else:
logging.info(f"{symbol} is in max qty reached symbol short, cannot replace grid")
else:
# Reset the tracking if conditions are no longer met
if symbol in self.invalid_short_condition_time:
del self.invalid_short_condition_time[symbol]


except Exception as e:
logging.info(f"Error in executing gridstrategy: {e}")
logging.info("Traceback: %s", traceback.format_exc())
Expand Down

0 comments on commit eaca2c8

Please sign in to comment.