CopyBuffer Only Allows Count of 3 - Array Out Of Range Error

 

Hi, 

I am trying to use a MovingAverage[10] (or anything over 3) in the Copybuffer:

   double MABigOne[];

   ArraySetAsSeries(MABigOne, true);

   MovingAverageBigOne = iMA(Symbol(), PERIOD_M1, 200, 0, MODE_SMA, PRICE_CLOSE);

   SetIndexBuffer(MovingAverageBigOne, MABigOne, INDICATOR_DATA); 

   CopyBuffer(MovingAverageBigOne, 0, 0, 10, MABigOne); //Attempted changing 3 to 10 or 20 but still same result
   Slope = ((MABigOne[10]-MABigOne[0])/10);

The error is when calling MABigOne[10] (Or any other value above 3).

I have read the documentation and having the same issue for over a year however do not understand it, I am sure there is a simple way to get this to work. 

I have tried using this concept in several other EA's but never resolved it, so simply use 3 candles only for systems.

Error shown below

2021.12.29 08:30:35.652 Core 1 2021.08.13 00:00:00   array out of range in 'BoosterPlan.mq5' (111,23)


There are several of these articles in the forums all directing to documentation (great but I do not understand the actual fix). If anyone can point out the changes it would be greatly appreciated and will likely save this topic being brought up over and over.


 
Global Finance Trading:

Hi, 

I am trying to use a MovingAverage[10] (or anything over 3) in the Copybuffer:

The error is when calling MABigOne[10] (Or any other value above 3).

I have read the documentation and having the same issue for over a year however do not understand it, I am sure there is a simple way to get this to work. 

I have tried using this concept in several other EA's but never resolved it, so simply use 3 candles only for systems.

Error shown below

2021.12.29 08:30:35.652 Core 1 2021.08.13 00:00:00   array out of range in 'BoosterPlan.mq5' (111,23)


There are several of these articles in the forums all directing to documentation (great but I do not understand the actual fix). If anyone can point out the changes it would be greatly appreciated and will likely save this topic being brought up over and over.


Change 

MABigOne[10]

to

MABigOne[9]
 
Array indexing starts at zero. When you copy 10 values, you get 10 values from MABigOne[0] to MABigOne[9].
 
Global Finance Trading :


Correct your gross mistake - you create an indicator handle at every tick! Remember: according to the MQL5 style, the indicator hand MUST be created ONCE and you MUST do it in OnInit!

 
Global Finance Trading:

Hi, 

I am trying to use a MovingAverage[10] (or anything over 3) in the Copybuffer:
...

2021.12.29 08:30:35.652 Core 1 2021.08.13 00:00:00   array out of range in 'BoosterPlan.mq5' (111,23)

There are several of these articles in the forums all directing to documentation (great but I do not understand the actual fix). If anyone can point out the changes it would be greatly appreciated and will likely save this topic being brought up over and over.


Just look at the MACD example on your pc: ...\MQL5\Experts\Examples\MACD\MACD Sample.mq5 how it is done there. MACD is an indicator of several MAs.
 
Global Finance Trading:

 

I am trying to use a MovingAverage[10] (or anything over 3) in the Copybuffer:

The error is when calling MABigOne[10] (Or any other value above 3).


When defining an Array[10] you are basically saying 'this array is supposed to have ten fields.' That is 0-9 which means when you are trying to access this array with the index 10 it will give you an out of range error because you would need 11 fields to be able to work with indizes 0-10. Not sure why it shouldn't work with more than three.

In Copybuffer the count is also a number of array fields and not the maximum index you can use. The latter is 9. The CopyBuffer function also automatically resizes the array according to the count number before filling it with the values.
 
pennyhunter #:

When defining an Array[10] you are basically saying 'this array is supposed to have ten fields.' That is 0-9 which means when you are trying to access this array with the index 10 it will give you an out of range error because you would need 11 fields to be able to work with indizes 0-10. Not sure why it shouldn't work with more than three.

In Copybuffer the count is also a number of array fields and not the maximum index you can use. The latter is 9. The CopyBuffer function also automatically resizes the array according to the count number before filling it with the values.

Hi pennyhunter,

Thank you for your comment, I changed my array to one less than the copy buffer and it works above 3 now. Not sure why 3 was working as 3 in copybuffer and 3 in the array call though. 

The code seems to be working now, using the bigger array numbers. Thanks again! Very helpful.

 
Vladimir Karputov #:

Correct your gross mistake - you create an indicator handle at every tick! Remember: according to the MQL5 style, the indicator hand MUST be created ONCE and you MUST do it in OnInit!

Thank you Vladimir,

I have not put the indicators in an OnInit, with the copybuffer and array calls in the OnTick. It seems to be working as it should.

Will this make my code run smoother/faster/easier? 

What is the purpose of this as it was working as planned when running OnTick?

Your help has been much appreciated!

 
Global Finance Trading #:

Thank you Vladimir,

I have not put the indicators in an OnInit, with the copybuffer and array calls in the OnTick. It seems to be working as it should.

Will this make my code run smoother/faster/easier? 

What is the purpose of this as it was working as planned when running OnTick?

Your help has been much appreciated!

Can you show, what you changed in your code please 
Reason: