Skip to content
All library documents

A Chart-Based Backtest Engine for Market-Order Signals

Article ProRealCode

Summary

The document explains a lightweight chart indicator that turns user-defined order signals into a visual backtest. Signals specify signed quantities, and the engine applies them at the next bar’s open with a configurable spread. It tracks net position, average entry price, realized and unrealized profit and loss, and supports partial position changes, reversals, fixed stops, trailing stops, and profit targets. An example uses moving-average crossovers to produce different buy and sell quantities.

Chart annotations display cost basis, stop and target levels, fills, and transaction-level profit or loss; optional outputs can show signals, exposure, and unrealized profit and loss. The described execution model uses market orders and does not simulate intrabar fills or slippage beyond the spread setting. Gaps can pass through stop or target levels without intermediate execution. The document presents a rapid strategy exploration aid rather than evidence of profitability, and its accounting depends on leaving the engine’s internal variables intact.

Key ideas

  • Order quantities are supplied as signed signals and filled at the next bar’s open with a configurable spread.
  • The engine tracks position changes, average cost, realized profit and loss, and unrealized profit and loss.
  • It can display fixed stops, trailing stops, profit targets, fills, and transaction results on the chart.
  • The example generates scale-in and scale-out orders from moving-average crossovers.
  • Intrabar fills and slippage beyond the spread are not modeled, and price gaps can affect stop and target behavior.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.