Array out of range

 

I googled and read some pages but I am still struggling with this. Will someone please help me?

double getMovAv(ENUM_TIMEFRAMES timeframe, int maperiod, int mashift, int barshift) {
   double myMovingAverageArray[];
   int movingAverageDefinition = iMA(_Symbol, timeframe, maperiod, mashift, MODE_SMA, PRICE_CLOSE);
   ArraySetAsSeries(myMovingAverageArray, true);
   CopyBuffer(movingAverageDefinition, 0, 0, 3, myMovingAverageArray);
   return myMovingAverageArray[barshift];
}


The goal is to do this:

double MovAv40 = getMovAv(NULL, 40, 0, 2);


I keep getting this error message:

2022.07.19 17:32:40.841    ea (EURUSD,M15)    array out of range in 'ea3.mqh' (268,33)


Will someone please explain this in a language I can understand?

 
whoowl:

I googled and read some pages but I am still struggling with this. Will someone please help me?


The goal is to do this:


I keep getting this error message:


Will someone please explain this in a language I can understand?

What's the code in line 268?

- There aren't enough bars.

- barshift is exceeded by having a higher value rather than Data count to copy.

Btw use 0 or PERIOD_CURRENT or _Period or Period() inside getMovAv as argument.

You didn't check the return value of iMA handle in case of failure. Same for CopyBuffer.
 
_MAHA_ #:
What's the code in line 268?


Line 268 is the last line of the function.

return myMovingAverageArray[barshift];


Char 33 is barshift.


_MAHA_ #:

- There aren't enough bars.

- barshift is exceeded by having a higher value rather than Data count to copy.


I thought about that, but I copy three data frames:


CopyBuffer(movingAverageDefinition, 0, 0, 3, myMovingAverageArray);


And I only ask for data from up to 2, I mean: 0, 1, 2. I only need the three last values. Why aren't 3 enough here?


_MAHA_ #:
You didn't check the return value of iMA handle in case of failure. Same for CopyBuffer.


I don't understand. You posted a link to order_calc_margin. Why do I need that? I always have margin. My lots are small enough for at least 10 orders within the margin and I never have more than one order running.

 
whoowl #:

I don't understand. You posted a link to order_calc_margin. Why do I need that? I always have margin. My lots are small enough for at least 10 orders within the margin and I never have more than one order running.

Maha didn't post a link.

_MAHA_ #:

You didn't check the return value of iMA handle in case of failure. Same for CopyBuffer.

It is an annoying feature of this website that links are added automatically to many words. "check" is one of them.

As you see these automatic links often are not actually related to the intention of the post and can cause confusion.