Rotation of strategies in walk forward testing

Hello,

Is it possible to rotate between different strategies based their recent score of metrics in walk forward testing. For example, there are 3 strategies which saved in 3 AFL files, using 12 months as IS and 3 months as OOS. First, those 3 strategies will do optimization for the 12 months as IS, then pick the strategy with the highest score of metrics e.g. highest UPI for OOS testing.

Thank you!

Sure. Everything is possible.

``````strategy = Optimize("strategy", 1, 1, 3, 1 );

if( strategy == 1 )
{
// rules for strategy 1
#include "your_first_code.afl"
}
else
if( strategy == 2 )
{
// rules for strategy 2
#include "your_second_code.afl"
}
else
{
#include "your-thirdcode.afl"
}
``````
8 Likes

Hello,

I have studied the above codes for a while but it seems that i can only do optimization for the last strategy. The codes will skip all the optimization except for the last one. For examples, consider the following 2 strategies which saved in Include file.

``````// Strategy1.afl

Fast1 = Optimize ("Fast1", 5, 5, 20, 1);
Slow1 = Optimize ("Slow1", 25, 25, 100, 5);
FastMA = MA(C, Fast1);
SlowMA = MA(C, Slow1);
Sell = Cross(SlowMA, FastMA);
``````
``````// Strategy2.afl

Fast2 = Optimize ("Fast2", 5, 5, 20, 1);
Slow2 = Optimize ("Slow2", 25, 25, 100, 5);
FastEMA = EMA(C, Fast2);
SlowEMA = EMA(C, Slow2);
Sell = Cross(SlowEMA, FastEMA);
``````

Then I use the rotation codes as below:

``````strategy = Optimize("strategy", 1, 1, 2, 1 );

if( strategy == 1 )
{
// rules for strategy 1
#include <Strategy1.afl>
}
else
if( strategy == 2 )
{
// rules for strategy 2
#include <Strategy2.afl>
}
``````

The optimization will always do for strategy 2 but not strategy 1.

Thank you!!

It runs fine strategy 1 and strategy 2 - you may need to add _TRACE statements to really know what is happening (see How do I debug my AFL).
What is wrong with your code is that you have Optimize() calls INSIDE conditional statements and they have different names.
Don’t do that. All Optimize() calls must be OUTSIDE conditional statements because they need to be
the SAME for all optimization runs (as they are used to create COLUMNS in result list).
http://www.amibroker.com/f?optimize

Hi Tomasz,

If no optimization included in strategy 1 and strategy 2, then it works fine in walk forward.

However, what I want to do is to optimize these 2 strategies in IS period, then see which strategy and which parameters get the highest score and use this strategy with optimized parameters in OOS period. I am still thinking the solutions for it.

Thank you!!

As I wrote you can have Optimization but you need to put those Optimize statements OUTSIDE if() statement.

Alternatively you should make sure that both have SAME NAMES. Instead your code use this:

``````// Strategy1.afl
Fast1 = Optimize ("Param1", 5, 5, 20, 1);
Slow1 = Optimize ("Param2", 25, 25, 100, 5);
FastMA = MA(C, Fast1);
SlowMA = MA(C, Slow1);
Sell = Cross(SlowMA, FastMA);
``````

and

``````// Strategy2.afl
Fast2 = Optimize ("Param1", 5, 5, 20, 1); // SAME Opt NAME !!!
Slow2 = Optimize ("Param2", 25, 25, 100, 5); // SAME Opt NAME !!!
FastEMA = EMA(C, Fast2);
SlowEMA = EMA(C, Slow2);