Skip to content

Ensure consistent market exit for a strategy #2876

@cuberone

Description

@cuberone

Feature Request

Problem Statement

When stopping trading strategies based on trading conditions, rather than shutdown, active in-flight or racing orders may persist in the market. This issue arises when using a simple sequence of cancel_all_orders followed by close_all_positions, which does not adequately handle orders that are still in transit or in the process of execution.

Proposed Solution

Implement a robust market exit process for trading strategies to ensure all in-flight orders and open positions are fully resolved before shutdown or restart. The process should be integrated into the existing strategy lifecycle as follows:

a. Any component can publish an exit command targeting a specific strategy.
b. Exit Process:

  1. The strategy invokes self.on_market_exit to initiate the exit sequence.
  2. Execute a consistent, iterative market exit procedure:
  • Wait for in-flight orders status change (configure inflight_check_interval_ms for low latency).
  • Wait until all active orders are confirmed canceled.
  • Ensure all positions are fully closed before proceeding.
  1. Invoke self.after_market_exit to customise the exit process.

c. Incorporate the market exit process into the existing strategy shutdown workflow.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions