Practical Example of Walk Forward Analysis for Systematic Trading

Caio Agrizzi
6 min readJul 11, 2021

--

In this article I will show you an example of Walk Forward Optimization or Walk Forward Analysis with an existing and profitable trading strategy.

Intro

First of all, it’s important to say: no strategy will be full disclosed in this article. The main objective here is to prove that if you have a barely profitable strategy, you can improve it by using WFA.

The Strategy shown here is used to trade future contracts of Ibovespa, the main stock index of the Brazilian Stock Exchange - B3. It's a day trading strategy that trade on trends using graphical indicators as entry signals.

The Base Case

Let’s say you have a trading strategy and you want to know which input gives you the best return. For example, what moving average period should I use for a moving average crossing signal strategy. In this case you would normally go to your trading software and use the optimization tool that is available there.

Let’s do the same! I will optimize my strategy using genetic algorithm with modified optimal f method as my fitness function.

The Optimal F system of money management was devised by Ralph Vince, and he’s written several books about this and other money management issues. The idea is that you determine the ideal fraction of your money to allocate per trade based on past performance. If your Optimal F is 18%, then each trade should be 18 percent of your account — no more, no less. The system is similar to the fixed fraction and fixed ratio methods, but with a few differences.

The modified method I’m using will take 20% of the optimal f and use as money management, a more conservative approach as optimal f may lead to strategy with few number of trades, reducing the statistical significance. The fitness value will be the final balance using 20% of the optimal f.

Optimization performed from 12/04/2017 to 06/08/2021
Backtest performed from 12/04/2017 to 06/08/2021

The backtest was performed using the best solution without reinvestment of capital and minimum lot. No transaction costs, taxes and spread were considered also. We can approximate 5 points of spread per trade, which per contract represents 1$ per trade. Considering this, the net profit would’ve been R$ 8572,00 which is still very good. The traded security is very liquid and with minimum spread most of the time.

Daily volume of traded contracts

In sequence we will validate our strategy using WFA

What is Walk Forward ?

Forward Testing or Walk Forward testing is what you do to ensure that you optimization is not overfitted. You simply perform a backtest on a period that you optimization has never seen. In this case, a forward period.

If your strategy doesn’t perform well on the out of sample period, It’s probably overfitted!

A case of overfitted strategy

What is Walk Forward Analysis?

Walk Forward Optimization or Walk Forward Analysis happens when multiples Forward Testings are combined.

Walk Forward Analysis

Most of systematic traders ensure that WFA is the de facto method for optmizing and evaluating trading strategies. I consider myself included.

It is known that a very robust and reliable strategy are the ones that were optimized on a very long period of time and the outcome backtest is profitable. The very problem with this is that it is hard to find a strategy that performs well over a long period of time. This is due to two main reasons: the first is that trading strategies tends to fade away with time as many traders start to find them and the second is that market conditions tends to change over time — volatility, liquidity, new players etc.

WFA solve this issue by optimizing the strategy for short periods but keeping a very long final period when we combine the out of sample test of all Forward Analysis.

Testing 5:1

Let’s try doing WFA using 84% in sample and 16% out of sample. 252 days as in sample period and 50 as out of sample. We can differ each out of sample test by its different color.

At this time we will optimize only 3 parameters that are related to entry signal and exit signal. The smaller optimization period in this step require less optimized parameters to overcome overfitting at each WF.

The fitness parameter we will use in this step is annualized net profit because optimal f is only good when we have a large set of trades. We can also see the difference in balance of the base case and the cumulated out of sample tests looking at the green line at the bottom of the chart.

252:50 WFA analysis

The result shows that the strategy is validated because we optimized it with in sample data and tested with out of sample data and the result in terms of final balance was good, not considering other aspects of a good optimization as rare events, costs, spreads and slippage.

We can also do a deeper interpretation of the result. The chart shows that there is a tendency to change the inputs values after the Corona Crash. Indeed there was a break in correlation of many financial securities and big changes to market price action after Corona Crash.

Multiple 5:1 WFA

We can find below the same analysis for 200, 150, 100 and 50 in sample trading days.

200:40

Reducing the period of in sample to 200 we got a better result in terms of annualised net profit. Looking at the spread we noticed again that market conditions changed after the corona crash.

150:30

By reducing more the in sample period we begin to find the effects of lower number of statistical samples. The strategy showed an improvement in the final balance but with bigger drawdowns. This indicates that a better evaluation of the compounded out of sample should be with a function that consider the drawdown effect, for example optimal f or CAGR/mean DD.

After years of systematic trading I learned to look and interpret what the backtest chart shows us and not only to look at the final number. I always try to find explanation for the events of the chart to get more comfortable when in live trading. I really recommend systematic traders to do this as most of them are only concerned with the numbers.

--

--

Caio Agrizzi

Passionate quantitative trader with 3+ years of experience and proven track record