Dividing open positions into groups - page 7

 

In my opinion, is there a faster and more efficient algorithm for adding a new position (and its 6 properties) to the position structure? At me it works like this - I use brute force to make sure the position is really new, get the values of required parameters, increase the array by 1 and write a new element. I've read in the article that enlarge and write can and should be done differently. I haven't found the function specified there in my terminal and I don't know how to fill it.

 
Sergey Voytsekhovsky:

In my opinion, is there a faster and more efficient algorithm for adding a new position (and its 6 properties) to the position structure? At me it works like this - I use brute force to make sure the position is really new, get the values of required parameters, increase the array by 1 and write a new element. I've read in the article that enlarge and write can and should be done differently. I haven't found the function specified there in my terminal and I don't know how to fill it.

Do not try to search for positions to determine "that the position is really new". Find another way. For example in OnTradeTransaction. And try to use ArrayRemove function instead of array overflow

 
Alexey Viktorov:

Do not try to search for positions to determine "that the position is really new". Find another way. For example in OnTradeTransaction. And try to use ArrayRemove function instead of array overflow

Respect, thanks for your prompt support and helpful advice, I'll get started. And how to add to the tail of the array data without +1 to size, formula from ready-made classes terminal throw over ??? Well, now for the general development, so as not to leave gaps in my head, please?

 
Sergey Voytsekhovsky:

And how to add to the tail of the array data without +1 to size, formula from ready-made classes terminal throw up ??? Well, now for general development, so as not to leave gaps in my head, please?

I found the method myself, but how to attach it, I do not understand, not enough experience. It's hard to master a foreign language when you don't even know it exists. This is a digression.

//+------------------------------------------------------------------+
//| Adding an element to the end of the array                        |
//| Добавление элемента в конец массива                              |
//+------------------------------------------------------------------+
bool CArrayInt::Add(const int element)
  {
//--- check/reserve elements of array
//--- проверка / резерв элементов массива
   if(!Reserve(1))
      return(false);
//--- add
   m_data[m_data_total++]=element;
   m_sort_mode=-1;
//--- successful
//--- успешный
   return(true);
  }

How do I screw it on? Where do you put what kind of array you want to grow? What if the array is a structure?

 
Sergey Voytsekhovsky:

Respect, thanks for your prompt support and helpful advice, I'll get started. And how to add to the tail of the array data without +1 to size, formula from ready-made classes terminal throw over ??? Well, now for the general development, so as not to leave gaps in my head, please?

I try not to use ready-made classes. I only use CTrade for creating trading panels. I'm too lazy to rewrite CTrade for my own needs, while panels are complicated for me and I have no desire to understand them. That's why I'm sure that +1 to array and filling of structure elements is not such a delay compared to three loops.
 
Sergey Voytsekhovsky:

I found the method itself, but I can't figure out how to apply it, because I'm not experienced enough. It's hard to master a foreign language when you don't even know it exists. This is a digression.

So how do I screw it on ???

To your array of structures - no way.

 
Sergey Voytsekhovsky:

I found the method itself, but I can't figure out how to apply it, because I'm not experienced enough. It's hard to master a foreign language when you don't even know it exists. This is a digression.

So how do I screw it on ???

And this, if I understand correctly, is more for OOP object arrays.
 
Alexey Viktorov:
And this, if I understand correctly, is more for OOP object arrays.

Not "more", but from-there.

 
Alexey Viktorov:
I try not to use ready-made classes. I use CTrade only for creating trading panels. I'm too lazy to rewrite CTrade for my own needs, while the panels are too complicated for me and I'm reluctant to understand them. That's why I'm sure +1 to an array and filling structure elements is not that much of a delay compared to three loops.

The idea is clear, it is unclear why we should not use ready-proven algorithms, but about "three loops" - very understandable, thank you. When you get high-quality simple solutions, I always wonder why I have not figured it out myself, because it is so obvious. Thank you.

 
Artyom Trishkin:

Not "more", but from-here.

Please tell me, the articlehttps://www.mql5.com/ru/articles/567 has the following code and mentions

Этот класс находятся в файле "CDynamicArray.mqh" приложения. Файл должен располагаться в каталоге "MQL5\Include" каталога данных терминала.
class CDynamicArray
  {
private:
   int               m_ChunkSize;    // Размер блока
   int               m_ReservedSize; // Реальный размер массива
   int               m_Size;         // Количество занятых элементов массива
public:
   double            Element[];      // Собственно массив. Находится в секции public, 
                                     // чтобы в случае необходимости работать с ним напрямую
   //+------------------------------------------------------------------+
   //|   Конструктор                                                    |
   //+------------------------------------------------------------------+
   void CDynamicArray(int ChunkSize=1024)
     {
      m_Size=0;                            // Количество занятых элементов
      m_ChunkSize=ChunkSize;               // Размер блока
      m_ReservedSize=ChunkSize;            // Реальный размер массива
      ArrayResize(Element,m_ReservedSize); // Подготовка массива
     }
   //+------------------------------------------------------------------+
   //|   Функция добавления в конец массива                             |
   //+------------------------------------------------------------------+
   void AddValue(double Value)
     {
      m_Size++; // Увеличение количества занятых элементов
      if(m_Size>m_ReservedSize)
        { // Требуемое количество больше реального размера массива
         m_ReservedSize+=m_ChunkSize; // Рассчитываем новый размер массива
         ArrayResize(Element,m_ReservedSize); // Увеличиваем реальный размер массива
        }
      Element[m_Size-1]=Value; // Добавляем значение
     }
   //+------------------------------------------------------------------+
   //|   Функция получения количества занятых элементов массива         |
   //+------------------------------------------------------------------+
   int Size()
     {
      return(m_Size);
     }
  };
Этот класс находятся в файле "CDynamicArray.mqh" приложения. Файл должен располагаться в каталоге "MQL5\Include" каталога данных терминала.

Сравним быстродействие при последовательном увеличении массива на 1 и при увеличении размера массива блоками:

int n=50000;
   double ar[];
   CDynamicArray da;

//--- Вариант 1 (увеличение размера по 1-му элементу)
   long st=GetTickCount(); // Запомним время начала 
   ArrayResize(ar,0); // Установка нулевого размера массива 
   for(int i=0;i<n;i++)
     {
      ArrayResize(ar,i+1); // Последовательное изменение размера массива
      ar[i]=i;
     }
   Alert("Вариант 1: "+IntegerToString(GetTickCount()-st)+" мс"); // Сообщение о времени, затраченном на первый вариант

//--- Вариант 2 (увеличение размера блоками)
   st=GetTickCount(); // Запомним время начала 
   for(int i=0;i<n;i++)
     {
      da.AddValue(i); // Добавляем элемент
     }
   Alert("Вариант 2: "+IntegerToString(GetTickCount()-st)+" мс"); // Сообщение о времени, затраченном на второй вариант

  }

But I couldn't find it in the standard package. Didn't look hard enough or is the article out of date ???