Misunderstanding of Optimize Exclude

The following code does not seem to work - e.g. results of Optimize will include a case where lowspread = -2 and highspread also = -2, or where lowspread = -2 and highspread = -3.

I’d appreciate a workaround or knowing what I’m doing wrong. Thank you.

lowspread = Optimize("lowspread",-3,-15,15,1);
highspread = Optimize("highspread",-1,-15,15,1);
Exclude = lowspread >= highspread;

Moderator comment: original subject of this topic was “Possible bug of optimize exclude”

The code works fine.

No, it will NOT include these combinations. Here is a screenshot (AmiBroker 6.20)

I think you misunderstood what Exclude does.

Exclude statement does not mean that your code is not run, because how would AB know that Exclude is assigned True if the code was not run. It must be run to find out.
What Exclude does is to skip backtest step, but the formula is run. That is what exclude does.

Hint: please don't assume 'bugs' - it offends me. I don't leave bugs in programs. More often than not your finding is incorrect and/or simply user mistake/misunderstanding or just an assumption made without consulting manual

3 Likes

Tomasz, thank you for your quick response.

Please understand that I did not assume a bug. I used the title "POSSIBLE bug" to reflect my assumption that no program as complex as AB could possibly be totally bug-free all the time. But knowing now that they are banned, rest assured that I will never use the words "possible bug" in any future post on any AB forum.

Further, my post was made only AFTER I checked the following information. Perhaps it is now obsolete, but even upon my now reading it a second time, my interpretation leaves me with my same results expectation.

http://www.amibroker.com/kb/2015/02/05/using-exclude-statement-to-skip-unwanted-optimization-steps/

In any case, the following two screenshots show, first, my code fragment in the include file that is being used, and second, the optimize results I get. I'm using AB 6.20.1.

Any suggestions as to what I'm doing incorrectly?

If you followed the Knowledge Base exactly, without inventing your own ways it would work perfectly. But you decided otherwise.

You are doing not one but two mistakes.

  1. Optimize() calls must not be called conditionally, because the condition may not be met if Optimization setup phase is run (it is run BEFORE backtest).
  2. If you put the assignment like the above inside function, Exclude variable used first time without ‘global’ declaration inside the function/procedure is a LOCAL, not global variable.

So your code modifies LOCAL “exclude” variable that has NOTHING to do with global Exclude variable.

Solution: use the code in global scope (outside the function) - as shown in the Knowledge Base, or declare Exclude as global.

Manual:

http://www.amibroker.com/guide/keyword/global.html

Your chance to find a bug in AB in less than 0.1% (actual statistics from support channel).

2 Likes

Adding Exclude as global in procedure solved problem