Optimization advice please

Hi all,
If anyone has some tips or advice for how to do this it would be greatly appreciated. I haven't found anything on the forum about this.
I'm trying to run an optimization across a set of entry conditions. The entry is selected by the value of one optimization parameter, then the code for each entry is a case in the following switch statement. Each entry has different optimization parameters. Like this

EntrySelection = Optimize( "Entry type", 1, 1, 2, 1 );

//Entry type 1 parameters
ET1param1 = Optimize( "ET1param1", 1, 1, 5, 1 );
ET1param2 = Optimize( "ET1param2", 1, 1, 5, 1 );
//Entry type 2 parameters
ET2param1 = Optimize( "ET2param1", 1, 1, 5, 1 );
ET2param2 = Optimize( "ET2param2", 1, 1, 5, 1 );

switch( EntrySelection )
{
case 1:
    // Entry type 1
    //code using ET1param1 and ET1param2, ET2xxx is not used
    break;

case 2:
    // Entry type 2
    //code using ET2param1 and ET2param2, ET1xxx is not used
    break;

default:
    // default action
    break;
}

How do I avoid running through all combinations of entry 1 parameters when entry 2 is selected? This results in many backtests with identical parameters and same results.
I know I could split this up and run each entry type separately, but I'm interested in doing the lot in one go.
Anyone? Thanks!

If you put optimization parameter definition in the corresponding case block, than it should work is you want.

Hi ab-trader, I've tried this and not all parameters are listed in the optimization result. They don't appear when I select Setup Columns...

I've tried couple options, but looks like within the same formula all parameters need to be run. I would put each entry in a separate formulas and then run all of them using batch mode.

Yes that would work of course, but I had the idea (eventually) to run through all permutations of a set of entries and a set of exits, each with their own optimization parameters. So, would have been nice with a solution to do this in one hit.

As an idea - you can use only one number parameter which codes entry type and corresponding entry parameters. For example, first digit defines your entry type, next digit (or 2) defines 1st parameter for this entry type, next digit (or 2) defines 2nd parameter for this entry type etc.

p = Optimize( "Entry param", 100, 100, 299, 1);
EntrySelection = int(p/100);

switch( EntrySelection )
{
case 1:
    // Entry type 1
    // ET1param1 = function11(p)
    // ET1param2 = function12(p)
    //code using ET1param1 and ET1param2, ET2xxx is not used
    break;

case 2:
    // Entry type 2
    // ET2param1 = function21(p)
    // ET2param2 = function22(p)
    //code using ET2param1 and ET2param2, ET1xxx is not used
    break;

default:
    // default action
    break;
}

Playing with the number of digits and step size can give you quite large variety of the parameters.

Optimize() calls must be on global level and must not be called conditionally because
BEFORE your optimization is run, there is a SETUP phase that executes your code once, reads all optimize statements and calculates how many steps entire optimization would take.
What you can do is to use Exclude to exclude given parameter combinations

Exclude = ( param1 == 5 ); // exclude result when param1 is equal 5

When backtester sees Exclude equal to True it will NOT perform backtest for this combination and assume zero result (gain = 0).

3 Likes

Thanks for the ideas!
@ab-trader: That's a creative idea. If I define a function that exhaustively lists only the combinations I'd like to test I should be able to do it.
@Tomasz: This is the most elegant solution. Here's the code and it works nicely.

EntrySelection 		= Optimize( "Entry type", 3, 3, 4, 1 ); // corresponds to cases 3 & 4 in switch statement

ROCperiods4 		= Optimize( "ROCperiods4", 3, 3, 5, 1 ); // only used in case 3
ROCdownPct4			= Optimize( "ROCdownPct4", -8 , -9, -7, 1 );
ROCperiods3			= Optimize( "ROCperiods3", 2, 2, 4, 1 );
PercentRankperiods3	= Optimize( "PercentRankperiods3", 30, 30, 33, 1 );

Exclude3 = ( ( EntrySelection == 3 ) AND( ( ROCperiods4 > 3 ) OR( ROCdownPct4 > -9 ) ) ); // allows only one 'case 4' parameter combination 
Exclude4 = ( ( EntrySelection == 4 ) AND( ( ROCperiods3 > 2 ) OR( PercentRankperiods3 > 30 ) ) );
Exclude = Exclude3 OR Exclude4;

Thanks muchly!

@Tomasz: When I use this method together with OptimizerSetEngine( "cmae" ); ,
I get very few results, so my assumption here is that I must use Exclude only with exhaustive optimization.
Also there can't be more than one Exclude statement.
Is my understanding correct?

CMA-ES needs continuous search space (without holes). Exclude creates holes and you can't use it with CMA-ES.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.