Trades Not Matching Backtest

A quant trader came to me recently with an issue.

He was backtesting and optimizing his strategy using Python, but then writing code in TradingView to execute the trades.

His trades in TradingView weren’t matching his backtest and he couldn’t figure out why.

You often run into thorny situations like this when backtesting, especially when optimizing in one platform and executing trades in another.

Here’s the advice I gave which I think applies to trading data issues more generally:

Compare The Most Recent Trades

Find a recent trade where the backtest doesn’t match. Starting from recent trades is easier since you save yourself scrolling back in your chart to find the applicable data points. Note the symbol and entry date.

Add Debugging Statements to your Code

In the platform that’s easier to work with (in this case it was Python), add plenty of debugging code to log the important bits – OHLC data, volume, any indicator values that apply to your strategy, etc.

Run Backtest on Just The One Symbol with Debugging Enabled

Try to write your debugging statements to a separate file rather than the console. It’s easier to examine the data when it’s in a text file since there are so many tools you can use to search and parse them. (I mostly use Vim for this purpose, but use grep, findstr, sed, or Python too.)

By running the backtest on just one symbol, you end up with less data to have to sift through.

Dig Into the Nitty Gritty

The devil is in the details in situations like this, so you can’t avoid getting your hands dirty. Dive in and figure it out.

You’ll often learn something unrelated that will come in handy in unexpected ways in the future.

Traders HAVE to be detail-oriented and while this process usually isn’t fun, it’s a necessary part of being successful.

The quicker you become comfortable with it the better.