October 23


Forgetting This Backtesting Setting Will Cost You!

By Jeff Swanson

October 23, 2023

Algorithmic Trading, TradeStation

Ever get fooled by a backtest when testing a trailing stop?

You backtest your trailing stop, and everything looks excellent. Still, you lose money on the live market because the trailing stop is not working correctly?

Well, that happens all the time, and in this article, I will tell you why it happens and how to fix it.

Algorithmic traders often grapple with the nuances of trailing stops, especially when discrepancies arise between backtested and live trading results. This article aims to dissect the mechanics behind these differences, offering insights into the complexities of both environments.

The Backtesting Conundrum

In backtesting, trailing stops are typically evaluated using historical data points, including the Open, High, Low, and Close (OHLC) of bars—whether they be minute, hourly, or daily bars. The backtesting engine, be it TradeStation's EasyLanguage or MultiCharts, has to make certain assumptions.

For instance, it needs to decide the sequence in which the high and low of a bar occurred. Did the low form before the high, or vice versa? This sequencing is crucial because it directly impacts how your trailing stop is triggered.

However, it's essential to recognize that backtesting is a simplified representation of the market. One of its most significant limitations is the inability to account for intra-bar price action. This means the engine can't simulate the real-time fluctuations within a single bar. Watch a 10-minute bar form in real-time. After the opening tick, the market moves up, then down, before going back up again. This second-by-second movement is unknown to the backtesting engine. So, the backtesting engine assumes how the bar was formed, making your backtest results an approximation at best.

My Backtests Don't Match Live Trading!

The divergence in behavior between backtesting and live trading can result in substantial discrepancies in your trading results. Suppose you have noticed a difference between your backtest and live results. In that case, the culprit is likely the live trading environment's sensitivity to intra-bar price action, something your backtesting engine couldn't simulate.

The other possibility is curve fitting (overfitting) your strategy to the historical data. This is also a common problem. While it's not the topic of this article, you can read more about curve fitting and how to avoid it here.

Best Practices When Backtesting With Trailing Stops

So, how do you mitigate this discrepancy? One practical approach is enabling the "Look Inside Bar Backtesting" (LIBBT) feature in platforms like TradeStation and MultiCharts. LIBBT allows the backtesting engine to dissect each bar into smaller segments, providing a more granular view of price action.

The Look Inside Bar Backtesting (LIBBT) feature is designed to resolve a common issue in backtesting with intraday data, where a strategy might execute orders based on the open, high, low, or close of a bar without considering the sequence of prices within the bar. 

LIBBT Setting

Access by right-clicking on your chart then slecting
Edit Strategies->Properties For All.

By enabling LIBBT, you can instruct TradeStation to "look inside" each bar better to estimate the sequence prices within the bar.

For example, if you have a 15-minute bar, enabling the LIBBT feature can give you a one-minute resolution for each bar. Put another way, the backtesting engine will evaluate your trailing stop on a minute-by-minute basis within a single bar. This results in a more accurate simulation of your trailing stop's behavior in a live trading environment, reducing the gap between backtested and actual results.

Set this feature if your strategy relies on order types that could be influenced by the sequence of prices within a bar. Trailing stops are the obvious choice. Also, if your strategy uses profit targets and a hard stop. The backtesting engine must estimate which one might be hit first; thus, using LIBBT becomes critical.

You don't have to use the LIBBT setting if your strategy only uses "next bar at open" to enter and exit trades. Likewise, the LIBBT setting is unnecessary if your strategy uses "next bar at open" with a single hard stop loss.

By understanding these nuances and employing features like LIBBT, you can significantly improve the reliability of your trading strategies, ensuring that your backtested results are more aligned with live trading outcomes.

Jeff Swanson

About the author

Jeff has built and traded automated trading systems for the futures markets since 2008. He is the creator of the online courses System Development Master Class and Alpha Compass. Jeff is also the founder of EasyLanguage Mastery - a website and mission to empower the EasyLanguage trader with the proper knowledge and tools to become a profitable trader.

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    Learn To Code & Build Strategies
    Using EasyLanguage.