Backtest more accurately with the Bar Magnifier

May 30, 2022

Premium account holders can now obtain more realistic order fills in their strategy backtests by using The Bar Magnifier option. This tool uses intrabar inspection to obtain deeper granularity on price movement within a bar, allowing for more precise order fills. When selected, Bar Magnifier mode replaces the assumptions the broker emulator must make on price movement with only OHLC values for historical bars.

The intrabar timeframe used with the Bar Magnifier dynamically adjusts with the chart’s timeframe. This table lists the intrabar timeframe used for progressively higher chart timeframes:

Chart timeframe, T Intrabar timeframe used
1S < T < 30S 1S
30S <= T < 5 5S
5 <= T < 30 15S
30 <= T < 60 1
60 <= T < 240 5
240 <= T < D 15
D <= T < W 60
W <= T < 2W 120
T >= 2W D

Table 1. Intrabar timeframes used

Here’s an example of a strategy that uses a stop order without using the Bar Magnifier option:

strategy("bar_magnifier_demo", overlay = true, use_bar_magnifier = false)

if bar_index  == 10381
    strategy.entry("Long", strategy.long, stop = 157.0)
    strategy.exit("Exit", stop = 156.0)

The broker emulator places a stop order on bar #10381 and fills an order with a price of 157.0 on the next bar as soon as the stop = 157.0 condition is met. The broker emulator estimates that inside the bar itself, the price goes from “open to “low, then to “high (triggering the entry), then to “close. After a few bars (11 days for the current timeframe), the condition for exiting the position with the stop price = 156.0 is triggered:

When the Bar magnifier is enabled (parameter use_bar_magnifier = true), exit and entry prices are unchanged; however, the exit from the position occurs inside the same bar in which the entry happened:

strategy("bar_magnifier_demo", overlay = true, use_bar_magnifier = true)

if bar_index  == 10381
    strategy.entry("Long", strategy.long, stop = 157.0)
    strategy.exit("Exit", stop = 156.0)

If we check the lower timeframe chart for the same symbol (a 60-minute chart, according to the intrabar timeframe table) and find the time range corresponding to bar 10382, we can see that on the hourly timeframe, after reaching 157.0 and triggering the entry, the price goes down below 156.0, satisfying the stop = 156.0 condition:

With Bar Magnifier on, the broker emulator gets access to price changes from lower timeframes during backtesting, making its behavior more similar to what would happen during forward testing the strategy for the same time period.

Here is an example of a strategy that uses lower timeframes to achieve more precise fills on limit and stop orders:

 title                  =   "Magnifier On",
 overlay                =   true, 
 calc_on_order_fills    =   true,
 calc_on_every_tick     =   true,
 precision              =   3, 
 default_qty_type       =, 
 currency               =   currency.USD, 
 default_qty_value      =   1000, 
 initial_capital        =   1000,
 use_bar_magnifier      =   true)

trailPoints =, "Trail Points (in ticks)")
trailOffset =, "Trail Offset (in ticks)")
stopSize    =, "Stop Offset (in ticks)")

longCondition = bar_index % 25 == 0 and not (strategy.closedtrades.exit_bar_index(strategy.closedtrades - 1) == bar_index)

if (longCondition)
    strategy.entry("Long", strategy.long)

strategy.exit("Exit", loss = stopSize, trail_points = trailPoints, trail_offset = trailOffset)

With the bar magnifier option on, the strategy results are closer to what they would be in real-time. The profits for our test strategy are 50% worse when it’s on, which is discouraging for the strategy itself, but showcases how important using lower timeframe data could be for getting the more accurate backtesting data:

The Bar magnifier option can be switched by toggling corresponding input in strategy’s “Settings/Properties” window:

After turning the option off, the strategy is recalculated with the old logic, showing us less accurate information about the strategy’s behavior:

To stay informed of new Pine features, keep an eye on the User Manual’s Release notes. The PineCoders account also broadcasts updates from its Squawk Box Telegram channel, Twitter account, and from the “Pine Script™ Q&A” public chat on TradingView.

We hope you find these improvements useful. Please continue sending us your feedback. We build TradingView for our users and we love hearing from you.

Look first Then leap

TradingView is built for you, so make sure you're getting the most of our awesome features
Launch Chart