Backtest Lab Docs

State Machines in Trading Strategies

Trading strategies often involve more than a single set of entry and exit conditions. They may require a sequence of events, where each decision depends on what has happened previously.

State machines provide a natural way to represent such strategies. By dividing a strategy into distinct states and defining the conditions for moving between them, they allow trading logic to follow a structured sequence rather than evaluating every possible condition at once.

States, Conditions, and Actions

In a trading strategy, each state represents a particular stage of the trading process. For example, a strategy might have three states:

  • Wait for Entry: Monitor the market for a buying opportunity.
  • Enter Long: Place a buy order when the entry condition is satisfied.
  • Exit Long: Close the position when the exit condition is satisfied.

As explained in the State Machine Overview, only one state is active at a time, and only its outgoing conditions are evaluated.

Trading strategies introduce another important concept: state actions.

Each state can have actions associated with it. When the strategy transitions into a state, its configured actions are executed immediately upon entering that state.

Common actions include:

  • Trade Entry: Place an order to open a position.
  • Trade Exit: Place an order to close an existing position.
  • Saved Values: Record information, such as the current market price, for use later in the strategy.

Actions are performed when a state is entered, not continuously while it remains active. Once these actions have been executed, the strategy stays in that state and evaluates its outgoing conditions until a transition occurs.

Learn more about State Actions.

Why Use State Machines?

The main advantage of state machines is that they allow trading decisions to depend on the sequence of events, rather than just the current market conditions.

Consider a breakout strategy that follows these steps:

  1. Wait for the price to break above a resistance level.
  2. After the breakout, wait for the price to retest that level.
  3. Enter a long position only if the price holds above the level during the retest.
  4. Once in the trade, monitor for an exit condition.

Each step can be represented by a separate state, with conditions controlling when the strategy advances to the next step.

This approach offers several advantages:

  • Sequence: Conditions are evaluated only when the strategy reaches the appropriate stage, allowing decisions to depend on earlier events.
  • Timing: A strategy can enforce time limits on individual stages, such as requiring a retest within a certain number of candles.
  • Memory: Information captured during an earlier stage can be saved and used in later decisions.

Together, these capabilities make it possible to represent multi-stage trading strategies whose behavior depends on what happened, when it happened, and what information was recorded along the way.

See It in Action

Watch the Backtest Lab Explainer to see how states, conditions, and actions work together in a visual trading strategy.

The animation demonstrates how the active state determines which conditions are evaluated, how a satisfied condition triggers a transition, and how the strategy progresses through its trading logic.