# Help with an Array bounds error

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
Laurent
Posts: 159
Joined: 20 Nov 2010
Location: France
Has thanked: 76 times
Been thanked: 32 times

### Help with an Array bounds error

Ok I have simplified my code, here is the buggy part.

It's the code of a signal, when I want to optimize it (Length from 4 to 100), I got the following error: "Array bounds. Wrong index value: 51"

Inputs:

MAType ( 0 ),
Length ( 20 ),
ConfirmBars ( 1 );

Variables:
MAValue ( 0 ),
NBars ( 0 );

switch (MAType)
begin

// 0 - SMA - Simple Moving Average
case 0:
MAValue = AverageFC(C, Length);

// 1 - SmMA - Smoothed Moving Average
case 1:
MAValue = SmoothedAverage(C, Length);

// 2 - TMA - Triangular Moving Average
case 2:
MAValue = TriAverage(C, Length);

default:
MAValue = AverageFC(C, Length);
end;

condition1 = C > MAValue;
if condition1 then
NBars = NBars + 1
else
NBars = 0;

condition1 = currentBar > ConfirmBars and NBars = ConfirmBars;
if condition1 then
buy ( "LE-Price-Over-MA" ) next bar at market;

Why do I got this error?
(I have MC 7.2)

TJ
Posts: 6518
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 957 times
Been thanked: 1883 times

### Re: Help with an Array bounds error

Laurent wrote:Ok I have simplified my code, here is the buggy part.

It's the code of a signal, when I want to optimize it (Length from 4 to 100), I got the following error: "Array bounds. Wrong index value: 51"

Inputs:

MAType ( 0 ),
Length ( 20 ),
ConfirmBars ( 1 );

Variables:
MAValue ( 0 ),
NBars ( 0 );

switch (MAType)
begin

// 0 - SMA - Simple Moving Average
case 0:
MAValue = AverageFC(C, Length);

// 1 - SmMA - Smoothed Moving Average
case 1:
MAValue = SmoothedAverage(C, Length);

// 2 - TMA - Triangular Moving Average
case 2:
MAValue = TriAverage(C, Length);

default:
MAValue = AverageFC(C, Length);
end;

condition1 = C > MAValue;
if condition1 then
NBars = NBars + 1
else
NBars = 0;

condition1 = currentBar > ConfirmBars and NBars = ConfirmBars;
if condition1 then
buy ( "LE-Price-Over-MA" ) next bar at market;

Why do I got this error?
(I have MC 7.2)

have you tried not using AverageFC?

Laurent
Posts: 159
Joined: 20 Nov 2010
Location: France
Has thanked: 76 times
Been thanked: 32 times

### Re: Help with an Array bounds error

I have the bug with a lot of other function,

What I do not understand is that code works perfectly by removing the switch, and by using if/else if/else...

HMMM? Is it a bug or is it normal?
I really do not understand

Laurent
Posts: 159
Joined: 20 Nov 2010
Location: France
Has thanked: 76 times
Been thanked: 32 times

### Re: Help with an Array bounds error

Ok so,

here the results of my testing:

1.
No error with if/else if/else with AverageFC
Error with switch and AverageFC

2.
No error with if/else if/else with Average
No error with switch and Average

I do not understand why there is a difference for the first case.
Why the switch is interpreted differently of a if/else if/else??

Should I report it?

TJ
Posts: 6518
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 957 times
Been thanked: 1883 times

### Re: Help with an Array bounds error

Laurent wrote:Ok so,

here the results of my testing:

1.
No error with if/else if/else with AverageFC
Error with switch and AverageFC

2.
No error with if/else if/else with Average
No error with switch and Average

I do not understand why there is a difference for the first case.
Why the switch is interpreted differently of a if/else if/else??

Should I report it?

Is the problem with the switch?

Here is the key:

Error with switch and AverageFC
No error with switch and Average

TJ
Posts: 6518
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 957 times
Been thanked: 1883 times

### Re: Help with an Array bounds error

Laurent wrote:Ok so,

here the results of my testing:

1.
No error with if/else if/else with AverageFC
Error with switch and AverageFC

2.
No error with if/else if/else with Average
No error with switch and Average

I do not understand why there is a difference for the first case.
Why the switch is interpreted differently of a if/else if/else??

Should I report it?

have you compared the optimization results between:

1.
No error with if/else if/else with AverageFC

2.
No error with if/else if/else with Average

Laurent
Posts: 159
Joined: 20 Nov 2010
Location: France
Has thanked: 76 times
Been thanked: 32 times

### Re: Help with an Array bounds error

I have compared data between

"switch Average" and "if/else if/else Average", I have the same results.

So I do not understand, switch should work as if/else if/else by the interpreter/compiler.

Laurent
Posts: 159
Joined: 20 Nov 2010
Location: France
Has thanked: 76 times
Been thanked: 32 times

### Re: Help with an Array bounds error

Well, I have done some little functions to try to see where's the bug.

I have restarted my MC and retry... and no more bug.............!!!!!!!!!!!!!!!!!

I'm investigating...