Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 1134

 

Hello. If you don't know beforehand how many elements will be in the array, how do you declare the array so that the array increases with each new element?

An example to make it clearer:

double HighA[]; // ??????????????????

for(int i=0; i<1000; i++)
{
if(A[i]<B[i])
HighA[i]=A[i];
}
Print(">>>>>>>>>>>..ArraySize(HighA)= ",ArraySize(HighA));
 
Nauris Zukas:

Hello. If you don't know beforehand how many elements will be in the array, how do you declare the array so that the array increases with each new element?

An example to make it clearer:

double HighA[]; // ??????????????????

for(int i=0; i<1000; i++)
{
if(A[i]<B[i])
HighA[i]=A[i];
}
Print(">>>>>>>>>>>..ArraySize(HighA)= ",ArraySize(HighA));

You should read the documentation more often.
ArrayResize - Операции с массивами - Справочник MQL4
ArrayResize - Операции с массивами - Справочник MQL4
  • docs.mql4.com
ArrayResize - Операции с массивами - Справочник MQL4
 
Alexey Viktorov:
You should read the documentation more often.

I've read and tried but I get an "array out of range" error. In the example it looked like this:

      double HighA[];
      for(int i=0; i<1000; i++)
        {
         if(A[i]<B[i])
           {
            countHlines++;
            ArrayResize(HighA,countHlines);
            HighA[i]=A[i];
           }
        }
      Print(">>>>>>>>>>>..ArraySize(HighA)= ",ArraySize(HighA));
 
Nauris Zukas:

I've read and tried but I get an "array out of range" error. On the example it looked like this:

      double HighA[];
      for(int i=0; i<1000; i++)
        {
         if(A[i]<B[i])
           {
            countHlines++;
            ArrayResize(HighA,countHlines);
            HighA[i]=A[i];
           }
        }
      Print(">>>>>>>>>>>..ArraySize(HighA)= ",ArraySize(HighA));

When condition A[i] < B[i] is not fulfilled, the array size remains unchanged and the loop counter is incremented. That's why it is out of range.

Try it this way.

      double HighA[];
      for(int i=0; i<1000; i++)
        {
         if(A[i]<B[i])
           {
            int size = ArraySize(HighA);
            ArrayResize(HighA, size+1);
            HighA[size]=A[i];
           }
        }
      Print(">>>>>>>>>>>..ArraySize(HighA)= ",ArraySize(HighA));
 
Alexey Viktorov:

When the A[i] < B[i] condition is not satisfied, the array size remains unchanged and the loop counter is incremented. That's why it is out of bounds.

Try it this way.

      double HighA[];
      for(int i=0; i<1000; i++)
        {
         if(A[i]<B[i])
           {
            int size = ArraySize(HighA);
            ArrayResize(HighA, size+1);
            HighA[size]=A[i];
           }
        }
      Print(">>>>>>>>>>>..ArraySize(HighA)= ",ArraySize(HighA));


Thank you! It all works now !
 
Nauris Zukas:

I've read and tried but I get an "array out of range" error. In the example it looked like this:

      double HighA[];
      for(int i=0; i<1000; i++)
        {
         if(A[i]<B[i])
           {
            countHlines++;
            ArrayResize(HighA,countHlines);
            HighA[i]=A[i];
           }
        }
      Print(">>>>>>>>>>>..ArraySize(HighA)= ",ArraySize(HighA));

Well that would work faster

  int countHlines=0;
  double HighA[];
      for(int i=0; i<1000; i++)
        {
         if(A[i]<B[i])
           {
            countHlines++;
            ArrayResize(HighA,countHlines);
            HighA[countHlines-1]=A[i];
           }
        }
      Print(">>>>>>>>>>>..ArraySize(HighA)= ",ArraySize(HighA));


// Вариант №2
  int countHlines=-1;
  double HighA[];
      for(int i=0; i<1000; i++)
        {
         if(A[i]<B[i])
           {
            countHlines++;
            ArrayResize(HighA,countHlines+1);
            HighA[countHlines]=A[i];
           }
        }
      Print(">>>>>>>>>>>..ArraySize(HighA)= ",ArraySize(HighA));
 
Vitaly Muzichenko:

Well, it will work faster that way

  int countHlines=0;
  double HighA[];
      for(int i=0; i<1000; i++)
        {
         if(A[i]<B[i])
           {
            countHlines++;
            ArrayResize(HighA,countHlines);
            HighA[countHlines-1]=A[i];
           }
        }
      Print(">>>>>>>>>>>..ArraySize(HighA)= ",ArraySize(HighA));


// Вариант №2
  int countHlines=-1;
  double HighA[];
      for(int i=0; i<1000; i++)
        {
         if(A[i]<B[i])
           {
            countHlines++;
            ArrayResize(HighA,countHlines+1);
            HighA[countHlines]=A[i];
           }
        }
      Print(">>>>>>>>>>>..ArraySize(HighA)= ",ArraySize(HighA));
Thank you, I see! It will be faster because of the ArraySize, does it somehow slow it down?
 
Nauris Zukas:
Thank you, I see! It will be faster because of ArraySize, does it slow it down somehow?
Not much, but it's clear and error-proof.
 
Alexey Viktorov:
Not much, but it's clear and error-proof.
Thank you!
 
Hello, I use an Expert Advisor ILAN 2.0, can you advise how to make an automatic stop EA after closing all orders, I need it, say before the release of important news that he brought the transaction to a logical end and no longer trade.