Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1466

 
Dmitry Ivanushko #:
if(i+30<=ArraySize(opens))
if(i+30<ArraySize(opens))

Here is the output outside the array

 
if(i+30<ArraySize(opens))

This error is due to inattention. Fixing it does not solve the problem.

How could I create an array of 30 candlestick opening prices to search for highs and lows in it?

 
Dmitry Ivanushko #:
ArrayMaximum
ArrayMaximum

Returns index of the maximum value, not the price.

//+------------------------------------------------------------------+
int indexMax[];
int indexMin[];
int sizeArray = ArraySize(opens);
int size = 0;
int n = 30;

for(int i = n; i < sizeArray; i + n)
  {
   ArrayResize(indexMax, size + 1);
   indexMax[size] = ArrayMaximum(opens, i - n, i);
   indexMin[size] = ArrayMinimum(opens, i - n, i);
   size++;
  }
//+------------------------------------------------------------------+

I haven't checked, but it seems to be the way to get indices of minimums and maximums of candlestick openings (opens).

Although you can also get the price at once, but not the indices.

double PriseMin = opens[ArrayMinimum(opens, i - n, i)];
 
for(int i = n; i < sizeArray; i + n)

Nowhere have I found an example of an mql loop with a step. This simplifies the whole matter.

Only ChatGPT advised me to write it this way:

for(int i = n; i < sizeArray; i += n)

I will experiment further. The code works. Thank you.

The "array out of range" error occurs if you don't do ArrayResize.

 
Dmitry Ivanushko #:

This error is due to inattention. Fixing it does not solve the problem.

How could I create an array of 30 candlestick opening prices to search for highs and lows in it?

What is the problem? Declare an array temp[] and copy 30 elements into it and look for the index of the minimum/maximum value. And if at the end it is less than 30, it will copy how many are left. And in this case, I'd rather use a while() loop.

 
Alexey Viktorov #:
What's the problem? Declare an array temp[] and copy 30 elements into it and look for the index of the minimum/maximum value. And if at the end it is less than 30, it will copy how many are left. And in this case, I'd rather use a while() loop.

Yes, you can do that. I'm not thinking straight, so I'm asking for advice.

 
Dmitry Ivanushko #:

Yeah, you could do that. I'm not thinking straight, so I'm asking for advice.

In a static array the size is set constant, it cannot be changed, in a dynamic array it can be changed, but the size of the array is set! i.e. the size of the array is known and the indexes of elements from 0 to the end. <= is out of bounds just because the indexes of elements from zero, and the index of the last element is less by one unit of the array size.

Everything is in your hands))))

 
Valeriy Yastremskiy #:

In a static array the size is set constant and cannot be changed, in a dynamic array it can be changed, but the size of the array is set! i.e. the size of the array is known and the indexes of the elements are from 0 to the end. <= is out of bounds just because the indexes of elements from zero, and the index of the last element is less by one unit of the array size.

Everything is in your hands))))

What do you mean bythat ? Do you think that to copy some number of elements from one array to another, some temp[], you should set the array size before copying ArrayResize(temp, new_size); ?????

 
Dmitry Ivanushko #:

Yeah, you could do that. I'm not thinking straight, so I'm asking for advice.

You don't even need a temporary array.

int  ArrayMaximum( 
   const void&   array[],             // массив для поиска 
   int           start=0,             // с какого индекса начинаем поиск 
   int           count=WHOLE_ARRAY    // количество проверяемых 
   );

This code

  double open[];
  int copy = CopyOpen(_Symbol, PERIOD_CURRENT, 0, 100, open);
  int arrMaximum = ArrayMaximum(open, 85, 20);

searches for the maximum of 15 remaining elements despite the fact that it is set to search for 20 elements.

So you'd better use the while() loop and not worry about the number of remaining array items.

 
Alexey Viktorov #:

What do you mean bythat ? Do you think that in order to copy some number of elements from one array to another, some temp[], you should set the array size before copying ArrayResize(temp, new_size); ?????

No of course, I meant that the array size is always known or can be known. And it can and should be controlled for index overrun.