Broker emulator
Broker emulator is the backtesting module responsible for simulating order execution. It implements the core logic of a broker within the strategy testing environment — handling how and when orders are filled, what costs are applied, and which price model is used. The module is configured through a set of parameters that control each aspect of its behavior.
How it works
The Broker Emulator section is located at the bottom of the strategy settings panel. It groups all parameters that affect how the backtesting engine simulates a broker environment — from fee deduction to the price model used for order fills. Together, these settings determine how closely the simulated execution matches real-world trading conditions.
Parameters
- Commission: The amount paid in trading fees for each trade. Commission is applied on both entries and exits. A value and a calculation mode must be provided. Three modes are available.
- Percent: Imposes a commission equal to the specified percentage of the transacted value. The calculated amount varies with the size of the transaction.
- Fixed: Imposes a fixed monetary amount per order, regardless of the number of contracts or transaction value.
- Per contract: Imposes a commission per contract traded.
- Long leverage / Short leverage: Specifies the margin required for long and short positions — i.e., the percentage of the position that the trader must fund from their own equity.
- Slippage: Specifies the number of ticks to be added to the fill price of market and stop orders. Can be used to account for the spread and simulate realistic execution costs in fast-moving markets.
- Limit order execution: Controls the fill logic for limit orders. Two modes are available.
- Requested price: The order is filled exactly at the requested limit price. Guarantees the order is filled at a certain level.
- Requested price and 1 tick beyond: The order is filled only when the price moves 1 tick beyond the requested level. Assumes liquidity is sufficient only when the price has clearly passed through the limit level.
- Order execution delay: Controls when an order is executed relative to the bar lifecycle.
- None: Orders are executed on the same bar where they are created.
- One tick: Orders are executed on the next tick, assuming a one-tick delay due to market conditions.
- Heikin Ashi mode: Determines which price values are used to execute orders when the chart is set to Heikin Ashi.
- Heikin Ashi bars: Uses Heikin Ashi average prices to execute orders, matching the visual chart setup.
- Standard bars: Uses actual OHLC levels to execute orders for more realistic results.
Each of the parameters specified in the properties of the strategy can be changed by editing the arguments of the strategy() function call in the corresponding Pine Script.
Main use cases
- Matching real broker fees: Set Commission to the fee structure of your actual broker (Percent, Fixed, or Per contract) to make the equity curve reflect real trading costs.
- Simulating leveraged positions: Adjust Long and Short leverage to model margin trading scenarios and observe how leverage amplifies both gains and losses in the backtest.
- Accounting for execution slippage: Set Slippage to a non-zero value for strategies using market or stop orders to simulate the price impact of order execution in fast-moving markets.
- Conservative limit order simulation: Use Requested price and 1 tick beyond to prevent fills at prices the market only touched without clearly trading through — reducing overfitting to ideal fill conditions.
- Preventing look-ahead on fills: Enable Order execution delay → One tick to ensure orders placed on a given bar are not filled until the following tick, which more accurately reflects real execution latency.
- Realistic backtesting on Heikin Ashi charts: Set Heikin Ashi mode to Standard bars to execute orders at actual OHLC prices instead of synthetic Heikin Ashi averages, producing more trustworthy backtest results.