# How can we optimize automatically for different exit options?

The idea of the script is based on the video: Amibroker Tutorial Video - Using The Case Switch Function In Amibroker Afl - YouTube
This is my try, but it doesn’t work. The exit conditions are only examples and can be replaced with any other exit conditions. What can I do to test different exits with optimization?

``````Buy =  O > C;

ExitCondition1 = C > O;
ExitCondition2 = C > O AND Ref(C>O, -1);
ExitCondition3 =  C > O AND Ref(C>O, -1) AND Ref(C>O, -2);

ExitOpt = Optimize("Optimizer for Exit Rule", 1,1,3,1);
Exit =0;		// Initialize variable to use in switch function below

switch (ExitOpt) // This switches between the different index directions automatically
{
case 1: Exit = ExitCondition1;
break;
case 2: Exit = ExitCondition2;
break;
case 3: Exit = ExitCondition3;
break;
}

Sell = ExitOpt;
``````

Should be

`Sell = Exit;`

But you do not need `switch` statement at all.

``````Buy =  O > C;

ExitCondition1 = C > O;
ExitCondition2 = Sum(ExitCondition1,2) == 2;
ExitCondition3 = Sum(ExitCondition1,3) == 3;

ExitOpt = Optimize("Optimizer for Exit Rule", 1,1,3,1);

Sell = VarGet("ExitCondition"+ExitOpt);
``````

Or even shorter

``````Buy =  O > C;

ExitOpt = Optimize("Optimizer for Exit Rule", 1,1,3,1);

Sell = Sum(C>O,ExitOpt) == ExitOpt;
``````

Thank you very much.

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