# Optimize from the paramlist

Hi,
I want to optimize trading system by going through different moving averages. What could be the best way. Right now I am running each moving averages separately in a different Analysis tab. Thanks.
`list = ParamList("Averages","MA|EMA|WMA|DEMA|TSF");`

Hello RaMB0,
I would do it using the switch statement, something like this:

``````averages = Optimize("Averages", 0, 0, 4, 1);
averages_period = 5;
values = 0;

switch (averages)
{
case 0:
{
values = MA(C, averages_period);
break;
}

case 1:
{
values = EMA(C, averages_period);
break;
}
case 2:
{
values = WMA(C, averages_period);
break;
}
case 3:
{
values = DEMA(C, averages_period);
break;
}

//	[...] as many cases as you need ...

default:
{
values = MA(C, averages_period);
break;
}
}
``````

For further information regarding the switch statement look here : LINK

1 Like

Hi RaMbo,

I remember Herman van den Bergen used something like this:

``````function ParamOptimize( description, defaultVal, minv, maxv, step )
{
return Optimize( description, Param( description, defaultVal, minv, maxv, step ), minv, maxv, step );
}

PosQty = ParamOptimize( "Position Qty", 1, 1, 10, 1 );
TrendPeriod = Paramoptimize( "Pd1", 13, 3, 30, 1 );
OpenOffset = paramOptimize( "OS", 0.99, 0.98, 1, 0.001 );
MinVolume = ParamOptimize( "Min. Volume (M)", 0.5, 0.5, 10, 0.5 );
``````
1 Like

No, it's been posted by Amibroker in the manual
https://www.amibroker.com/guide/afl/optimize.html
Look at very last comment at the bottom there.

Hi WoodenShoes,

Thank your for a quick response. I saw this command line in the manual but couldn't understand much. Will give it a second try to see if the outcome is making sense.

Hello 2DD,

This was a very interesting idea. Thanks a ton. I applied similar principle after seeing your codes. I am eager to try your code to see the efficiency.

This is what I wrote for the time.

``````list = Optimize("Moving Averages", 1, 1, 4, 1);

if(list	==	1)
{
ma_custom = MA(src,length_opt);
}

if(list	==	2)
{
ma_custom = EMA(src,length_opt);
}

if(list	==	3)
{
ma_custom = WMA(src,length_opt);
}

if(list	==	4)
{
ma_custom = DEMA(src,length_opt);
}
``````

Hi RaMb0,
well yes of course that's another way to do it. Nice job I guess that the efficiency in term of performance are not so different between the two methods for few cases (as for your needs), in fact according to the user's guide:

" Execution of the statement body begins at the selected statement and proceeds until the end of the body or until a break statement transfers control out of the body."

So it becames as more efficient (in terms of computational time) as greater is the number of cases to be checked.

But to be precise, conceptually the two structures are a little different.
As fas as I know the `switch` statement is more similar to nested `if ... then ... else ` statements. Something like:

``````x = param("type", 1, 0, 3, 1);
if ( x == 1)
{
[code for case 1]
}
else
if (x ==2)
{
[code for case 2]
}
else
if  (x == 3)
{
[code for case 3]
}
else
{
[code for any other possibility, for example the default value, or a not considered case (example x == 0)]
}

``````

For this reason, in my opinion the `switch statement` is more efficient than several `if statement` that test multiple times for the same parameter, also in term of coding because I find easier to modify the condition for several cases without editing every single `if statement` condition and also for improving robustness of the code due to its `default` case that i find useful to handle wrong parameters, outliers and so on ...

1 Like