# How to backtest a trading strategy

Once you’ve created your trading or investment strategy, the next thing you need to do is backtest it. Blindly believing in a strategy can lead to disastrous results. It’s like designing and building a car without putting the car through crash tests. Every trader and investor must backtest his or her strategy. By backtesting, you will be able to see:

- What’s your strategy’s average annual return.
- What’s the normal expected volatility for your strategy.
- What’s the maximum loss that your strategy can incur.

The first question is important because you want to know just how profitable your trading strategy is! There is no point in doing what you’re doing if your trading strategy can barely beat buy-and-hold in the U.S. stock market. Remember, traders face higher taxes than long term investors. Hence a trading strategy must outperform buy-and-hold by a wide margin in order for it to be useful.

The second question is important because you need to know what “normal” losses are based on your strategy. Do not panic or freak out if you are losing money right now. Stick to your trading strategy as long as those losses are within the normal range.

The third question is important because you always want to know what the worst case scenario is. You need to be financially and psychologically ok with the worst case scenario in the event that it does happen.

Backtesting your trading strategy will answer all 3 questions. This way you don’t have to guess how “good or bad” your strategy is. You will have a solid, quantitative answer.

This is also why we suggest traders use a quantitative strategy instead of a discretionary strategy. It is not possible to backtest a discretionary strategy that has no clear and consistent buy, sell, position management, or risk management rules. Backtesting a quantitative strategy allows you see just how effective your strategy is.

A strategy that has worked in the past might not work in the future if the markets change. But a strategy that hasn’t worked in the past has an even smaller probability of working in the future.

So without further ado, here’s how you can backtest your trading strategy once it is complete. We include do’s, don’ts, and things to watch out for.

### You must have buy AND sell dates when backtesting

Every trading strategy must have buy AND sell dates. Some traders forget to include their SELL dates when backtesting their strategy. It doesn’t matter how accurate you catch the market’s tops and bottoms. You will not make a lot of money if you catch the market’s exact tops and bottoms but get out of a profitable position too early.

- Use your trading strategy’s BUY and SELL dates.
- Write down the market’s price on those BUY and SELL dates.
- Subtract the BUY price from your SELL price. Divide your nominal $ profit from the market’s price. This will give you a percentage profit or percentage loss.
- Add up all the profits and subtract all the losses.

Remember that you should make your backtest as real as possible. This means that you must take position size into account.

Multiply your percentage profit or percentage loss on each trade by your position size. This will show you just how big of an impact each trade has on your portfolio. For example, let’s assume that you made a 10% profit on a trade. If your position size is 15% of your entire portfolio, this trade gave you a 1.5% profit on your entire portfolio.

### Include risk management and stop loss when backtesting your trading strategy

Every trading strategy must have an adequate risk management and stop loss system. You cannot allow your losses grow until they destroy your entire portfolio. You must include these stop losses in your backtests.

For example, let’s assume that your trading strategy went long at $10. Your stop loss was at $9. The market fell to $8.5 and then surged to $15. You cannot consider this as a $5 profit when backtesting ($15-$10=$5). This is a $1 loss when backtesting because you would have been forced to cut loss.

Don’t forget to include margin calls if your strategy trades futures. Margin calls should show up as losses in your historical backtesting. Margin calls and forced-stop losses are a very real part of trading. You cannot ignore them in your backtests.

Essentially you should flow through your entire trading strategy when backtesting. Flow through the entire history as if **you were there at that point in time.**

### Include commission costs when backtesting your strategy

Lastly, you need to include the cost of commissions when you calculate just how profitable your strategy is historically. Commission costs can mean the difference between a trade that’s profitable and a trade that isn’t profitable.

Commission costs are especially important to day traders, who place hundreds if not thousands of trades each year. Profits from each day trade tends to be small, so even relatively low commission costs can have a sizeable impact on your trading performance.

In addition, commission costs can have a meaningful impact on your portfolio’s P&L if your trading account is small. This is especially true if you’re using a flat-fee broker. (Flat-fee brokers charge a flat commission per trade, regardless of how shares you have in that trade.)

### You don’t need extensive programming knowledge to backtest a strategy

A trader who uses a rules-based, systematic strategy does not need to have extensive knowledge on programming in order to backtest his or her strategy. You can do so in Excel. Find the BUY and SELL dates, then manually calculate your losses and profits.

This manual backtesting is more laborious but just as effective.

Troy, you will love Python. It’s way easier to learn than C or other languages. You will be able to do far more than you can now on excel. There are a couple of good tutorials on it. The Great Courses have a special course on it which you can get on sale and it’s specifically geared towards trading. Also, Udemy or Lynda make a tutorial on how to use python for your advantage.

PS I spent money on Matlab, do you think its programming is harder than Python? Matlab doesn’t seem to be limited in any way, but I’m unsure. james

I started learning Python a few months ago, and now I’m starting to apply it.

I don’t know much about matlab James. But from what I heard, people prefer Python over Matlab because Python’s syntax is easier to read and learn

Sorry.

Troy what software do you use to backtest strategies and what software to quantify? It seems like you use programming skills.

james

Hi James,

I’m mainly using Excel right now, but will use Python soon.

“But a strategy that hasn’t worked in the past has an even smaller probability of working in the future.”

-why can’t u take all these strategies that won’t work in the future and inversely trade them so they have a higher probability of working?