Skip to main content

Whether you’re a novice or pro in trading system development, Backtesting and Optimization are crucial concepts. They form the core of Algorithmic system development. However, many struggle with interpreting the results, leading to losses even when backtests were profitable.

Walk Forward Optimization, an essential method, determines the robustness of your trading system. Similar to standard Optimization, it seeks the best parameters for your system. However, in Walk Forward Optimization, the system is optimized for a specific data period to find optimal parameters. These parameters are then tested on forward data. If they perform well in both sets, the system is considered reliable, preventing overfitting.

For in-depth Algorithmic Trading system development, explore our article:

Build your own Algorithmic Trading Systems -Part 1

Before backtesting or optimization, set up historical data, divided into:

  • In-Sample Data: Historical data for initial testing and optimization.
  • Out-of-Sample Data: Data not part of the in-sample set, ensuring unbiased system testing.

Develop your trading system using in-sample data and then apply out-of-sample data to compare and test results. See the entire process visually below:

Walk Forward Optimization

Performing walk forward optimization manually can be time-consuming. Amibroker offers built-in functionality for this. We’ll guide you through the process:

Walk Forward Optimization in Amibroker

Let’s take a simple EMA Crossover strategy with default EMA periods of 50 and 20 for optimization on NSE:Nifty data spanning around 16 years (2000 to 2016).

				
					//------------------------------------------------------
//
//  Formula Name:    Walk Forward Testing
//  Website:         https://zerobrokerageclub.com/
//------------------------------------------------------

_SECTION_BEGIN("Walk Forward Testing");

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} – {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));


//Initial Parameters

SetTradeDelays( 1, 1, 1, 1 );
SetOption( "InitialEquity", 200000);
SetOption("FuturesMode" ,True);
SetOption("MinShares",1);
SetOption("CommissionMode",2);
SetOption("CommissionAmount",100);
SetOption("AccountMargin",10);
SetOption("RefreshWhenCompleted",True);
SetPositionSize(150,spsShares);
SetOption( "AllowPositionShrinking", True );
BuyPrice=Open;
SellPrice=Open;
ShortPrice=Open;
CoverPrice=Open;

//Parameters

MALength1 = Optimize("MALength1",20,5,200,5);
MALength2 = Optimize("MALength2",50,5,200,5);

//Buy-Sell Logic

Buy = Cross(ema( C, MALength1 ),ema( C, MALength2 ));
Sell =Cross( ema( C, MALength2 ), ema( C, MALength1 )) ;

Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
Short = Sell;
Cover = Buy;


_SECTION_END();

				
			

In your AFL code, the ‘Optimize’ function instructs Amibroker to optimize variables. Its signature is:

optimize( “Description“, default, min, max, step );

In Walk Forward Optimization, access backtester settings and click ‘Walk Forward.’ The settings are usually set based on your data, but you can adjust them as needed.

Start and End dates mark the initial period, which moves forward by Step until it reaches the Last date. You can anchor the Start date or use the current date.

Select “Easy Mode” for simplified setup, ensuring that the out-of-sample segment immediately follows the in-sample segment with matching lengths.

In Advanced mode, you have full control but must ensure valid WF procedure.

The “Optimization target” field defines the metric used for sorting results and finding the best one, e.g., CAR/MDD.

Once Walk-Forward settings are defined, go to Automatic Analysis, select “Walk Forward Optimization,” and run the sequence of optimizations and backtests. Results appear in the “Walk Forward” document.

Here’s a sample Walk Forward Optimization result:

How to interpret Walk Forward Optimization results?

Walk Forward Optimization helps evaluate your system’s reliability. If parameters perform well in both in-sample and out-of-sample data, the system is likely robust.

Walk forward testing assesses optimized system performance:

  • Is it realistic? A system is realistic if it fits the entire test data, indicating real-time market characteristics.
  • Is it overfitting? Overfitting occurs when a system fits only chance characteristics, making it unreliable.

If results for out-of-sample years are promising, continue the walk-forward process in real time to find parameters for real trading. This approach also helps your system adapt to changing market conditions over time.

For detailed Walk Forward Optimization results for this Moving Average trading system, check this link:

Walk Forward Testing Results

Additionally, perform Monte Carlo Analysis to assess your trading system’s robustness. Learn more:

Mastering Monte Carlo Analysis in Amibroker Step by Step

2 Comments

Leave a Reply