Features of the mql5 language, subtleties and tricks - page 104

 
Andrey Khatimlianskii:

How many instruments in the market review?

61.

 
fxsaber:

In build 1907 the bugs miraculously disappeared. I don't even know whether to roll back to find them, or something in this place was just fixed...

 

Question about building indicators

#property indicator_chart_window

#property indicator_buffers 12

#property indicator_plots   6

#property indicator_type1   DRAW_COLOR_LINE

#property indicator_type2   DRAW_COLOR_LINE

#property indicator_type3   DRAW_COLOR_LINE

#property indicator_type4   DRAW_COLOR_LINE

#property indicator_type5   DRAW_COLOR_LINE

#property indicator_type6   DRAW_COLOR_LINE 


SetIndexBuffer(0,upper_line,INDICATOR_DATA);

   SetIndexBuffer(1,upper_line_color,INDICATOR_COLOR_INDEX);


   PlotIndexSetString(0,PLOT_LABEL,"Upper Line");

   PlotIndexSetInteger(0,PLOT_LINE_STYLE,UpperStyle);

   PlotIndexSetInteger(0,PLOT_LINE_WIDTH,UpperWidth);

   PlotIndexSetInteger(0,PLOT_COLOR_INDEXES,2);

   PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,UpperTrendUp);

   PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,UpperTrendDw);

// ==========

   SetIndexBuffer(2,awera_line,INDICATOR_DATA);

   SetIndexBuffer(3,awera_line_color,INDICATOR_COLOR_INDEX);


   PlotIndexSetString(1,PLOT_LABEL,"Average Line");

   PlotIndexSetInteger(1,PLOT_LINE_STYLE,AverageStyle);

   PlotIndexSetInteger(1,PLOT_LINE_WIDTH,AverageWidth);

   PlotIndexSetInteger(1,PLOT_COLOR_INDEXES,2);

   PlotIndexSetInteger(1,PLOT_LINE_COLOR,0,AverageTrendUp);

   PlotIndexSetInteger(1,PLOT_LINE_COLOR,1,AverageTrendDw);

// ==========

   SetIndexBuffer(4,lower_line,INDICATOR_DATA);

   SetIndexBuffer(5,lower_line_color,INDICATOR_COLOR_INDEX);


   PlotIndexSetString(2,PLOT_LABEL,"Lower Line");

   PlotIndexSetInteger(2,PLOT_LINE_STYLE,LowerStyle);

   PlotIndexSetInteger(2,PLOT_LINE_WIDTH,LowerWidth);

   PlotIndexSetInteger(2,PLOT_COLOR_INDEXES,2);

   PlotIndexSetInteger(2,PLOT_LINE_COLOR,0,LowerTrendUp);

   PlotIndexSetInteger(2,PLOT_LINE_COLOR,1,LowerTrendDw);

How many color buffers can be used?

How are the buffers linked to each other?

#property indicator_buffers 12 // Declare a total of 12 buffers

#property indicator_plots 6 // Graphical constructions

Do I understand correctly, that specifying indicator_type 1 through 6 the indicator will understand that color lines should be drawn for 6 graphical constructions ?

#property indicator_type1 DRAW_COLOR_LINE

#property indicator_type2 DRAW_COLOR_LINE

#property indicator_type3 DRAW_COLOR_LINE

#property indicator_type4 DRAW_COLOR_LINE

#property indicator_type5 DRAW_COLOR_LINE

#property indicator_type6 DRAW_COLOR_LINE


// =====

SetIndexBuffer(0,upper_line,INDICATOR_DATA); // First buffer for data

SetIndexBuffer(1,upper_line_color,INDICATOR_COLOR_INDEX); // Second buffer for color data


PlotIndexSetString(0,PLOT_LABEL, "Upper Line"); // Change the properties of the first buffer

PlotIndexSetInteger(0,PLOT_LINE_STYLE,UpperStyle);// change the properties of the first buffer

PlotIndexSetInteger(0,PLOT_LINE_WIDTH,UpperWidth);// change properties of the first buffer

PlotIndexSetInteger(0,PLOT_COLOR_INDEXES,2);// change properties of the first buffer

PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,UpperTrendUp);//changing the properties of the first buffer

PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,UpperTrendDw);//change properties of the first buffer


SetIndexBuffer(2,awera_line,INDICATOR_DATA); // The third buffer

SetIndexBuffer(3,awera_line_color,INDICATOR_COLOR_INDEX); // The fourth color buffer


PlotIndexSetString(1,PLOT_LABEL, "Average Line"); // Change the buffer properties for SetIndexBuffer(2,awera_line,INDICATOR_DATA);

PlotIndexSetInteger(1,PLOT_LINE_STYLE,AverageStyle); // Change the buffer properties for SetIndexBuffer(2,awera_line,INDICATOR_DATA);

PlotIndexSetInteger(1,PLOT_LINE_WIDTH,AverageWidth); // Change the buffer properties for SetIndexBuffer(2,awera_line,INDICATOR_DATA);

PlotIndexSetInteger(1,PLOT_COLOR_INDEXES,2); // Change the buffer properties for SetIndexBuffer(2,awera_line,INDICATOR_DATA);

PlotIndexSetInteger(1,PLOT_LINE_COLOR,0,AverageTrendUp); // Change the buffer properties for SetIndexBuffer(2,awera_line,INDICATOR_DATA);

PlotIndexSetInteger(1,PLOT_LINE_COLOR,1,AverageTrendDw); // Change the buffer properties for SetIndexBuffer(2,awera_line,INDICATOR_DATA);

 
Vladimir Pastushak:

Question about building indicators

#property indicator_chart_window

#property indicator_buffers 12

#property indicator_plots   6

#property indicator_type1   DRAW_COLOR_LINE

#property indicator_type2   DRAW_COLOR_LINE

#property indicator_type3   DRAW_COLOR_LINE

#property indicator_type4   DRAW_COLOR_LINE

#property indicator_type5   DRAW_COLOR_LINE

#property indicator_type6   DRAW_COLOR_LINE 


SetIndexBuffer(0,upper_line,INDICATOR_DATA);

   SetIndexBuffer(1,upper_line_color,INDICATOR_COLOR_INDEX);


   PlotIndexSetString(0,PLOT_LABEL,"Upper Line");

   PlotIndexSetInteger(0,PLOT_LINE_STYLE,UpperStyle);

   PlotIndexSetInteger(0,PLOT_LINE_WIDTH,UpperWidth);

   PlotIndexSetInteger(0,PLOT_COLOR_INDEXES,2);

   PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,UpperTrendUp);

   PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,UpperTrendDw);

// ==========

   SetIndexBuffer(2,awera_line,INDICATOR_DATA);

   SetIndexBuffer(3,awera_line_color,INDICATOR_COLOR_INDEX);


   PlotIndexSetString(1,PLOT_LABEL,"Average Line");

   PlotIndexSetInteger(1,PLOT_LINE_STYLE,AverageStyle);

   PlotIndexSetInteger(1,PLOT_LINE_WIDTH,AverageWidth);

   PlotIndexSetInteger(1,PLOT_COLOR_INDEXES,2);

   PlotIndexSetInteger(1,PLOT_LINE_COLOR,0,AverageTrendUp);

   PlotIndexSetInteger(1,PLOT_LINE_COLOR,1,AverageTrendDw);

// ==========

   SetIndexBuffer(4,lower_line,INDICATOR_DATA);

   SetIndexBuffer(5,lower_line_color,INDICATOR_COLOR_INDEX);


   PlotIndexSetString(2,PLOT_LABEL,"Lower Line");

   PlotIndexSetInteger(2,PLOT_LINE_STYLE,LowerStyle);

   PlotIndexSetInteger(2,PLOT_LINE_WIDTH,LowerWidth);

   PlotIndexSetInteger(2,PLOT_COLOR_INDEXES,2);

   PlotIndexSetInteger(2,PLOT_LINE_COLOR,0,LowerTrendUp);

   PlotIndexSetInteger(2,PLOT_LINE_COLOR,1,LowerTrendDw);

How many color buffers can be used?

How are the buffers linked to each other?

#property indicator_buffers 12 // Declare a total of 12 buffers

#property indicator_plots 6 // Graphical constructions

Do I understand correctly, that specifying indicator_type 1 through 6 the indicator will understand that color lines should be drawn for 6 graphical constructions ?

#property indicator_type1 DRAW_COLOR_LINE

#property indicator_type2 DRAW_COLOR_LINE

#property indicator_type3 DRAW_COLOR_LINE

#property indicator_type4 DRAW_COLOR_LINE

#property indicator_type5 DRAW_COLOR_LINE

#property indicator_type6 DRAW_COLOR_LINE


// =====

SetIndexBuffer(0,upper_line,INDICATOR_DATA); // First buffer for data

SetIndexBuffer(1,upper_line_color,INDICATOR_COLOR_INDEX); // Second buffer for color data


PlotIndexSetString(0,PLOT_LABEL, "Upper Line"); // Change the properties of the first buffer

PlotIndexSetInteger(0,PLOT_LINE_STYLE,UpperStyle);// change the properties of the first buffer

PlotIndexSetInteger(0,PLOT_LINE_WIDTH,UpperWidth);// change properties of the first buffer

PlotIndexSetInteger(0,PLOT_COLOR_INDEXES,2);// change properties of the first buffer

PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,UpperTrendUp);//changing the properties of the first buffer

PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,UpperTrendDw);//change properties of the first buffer


SetIndexBuffer(2,awera_line,INDICATOR_DATA); // The third buffer

SetIndexBuffer(3,awera_line_color,INDICATOR_COLOR_INDEX); // The fourth color buffer


PlotIndexSetString(1,PLOT_LABEL, "Average Line"); // Change the buffer properties for SetIndexBuffer(2,awera_line,INDICATOR_DATA);

PlotIndexSetInteger(1,PLOT_LINE_STYLE,AverageStyle); // Change the buffer properties for SetIndexBuffer(2,awera_line,INDICATOR_DATA);

PlotIndexSetInteger(1,PLOT_LINE_WIDTH,AverageWidth); // Change the buffer properties for SetIndexBuffer(2,awera_line,INDICATOR_DATA);

PlotIndexSetInteger(1,PLOT_COLOR_INDEXES,2); // Change the buffer properties for SetIndexBuffer(2,awera_line,INDICATOR_DATA);

PlotIndexSetInteger(1,PLOT_LINE_COLOR,0,AverageTrendUp); // Change the buffer properties for SetIndexBuffer(2,awera_line,INDICATOR_DATA);

PlotIndexSetInteger(1,PLOT_LINE_COLOR,1,AverageTrendDw); // Change the buffer properties for SetIndexBuffer(2,awera_line,INDICATOR_DATA);

You'd better create a separate branch for your question. This branch is about specifics, there shouldn't be any discussions here.

To better understand the principle of building, implement the functionality incrementally. You have built one line, check its functionality, add the next one.

Each graphical series (in this case a color line) can have only one color buffer. Each buffer can contain many colors.

 
Andrey Khatimlianskii:

In the 1907 build, the bugs have miraculously disappeared. I don't even know whether to roll back to find them, or something in this place just fixed...

The errors disappeared, but the problems with the event queue did not. After a few hours of work the terminal starts to load the CPU up to 95%. And something tells me that the ticks are skipped.

2018.10.11 23:56:54.069 Terminal        MetaTrader 5 x64 build 1907 started (MetaQuotes Software Corp.)
2018.10.11 23:56:54.139 Terminal        Windows 8.1 (build 9600) x64, IE 11, UAC, Intel Core i5-3570  @ 3.40 GHz, Memory: 11580 / 16346 Mb, Disk: 401 / 499 Gb, GMT+2

@Slava, did something change in ChartEvent in the 19xx builds? Does your problem reproduce itself?

 
Andrey Khatimlianskii:

The errors disappeared, but the problems with the event queue did not. After a few hours the terminal starts to load the CPU at 95%. And something tells me that the ticks are missing.

@Slava, did something change in ChartEvent in the 19xx builds? Is your problem reproduced?

This is how the resource monitoring looks like after 15 hours of terminal's work (the minimum frequency of spy's event sending is 500 ms):

Although CPU load is close to 0 for several hours after startup.

 
Andrey Khatimlianskii:

This is what the resource monitoring looks like after 15 hours of operation of the terminal (the minimum event rate of the spy is 500 ms):

Although CPU load is close to 0 for a few hours after startup.

Bild 1908 - no change.

 

The ability to replace the operators of structures of the basic type allows you to be flexible in some situations.

As an example, below is one technique for custom sorting an array of structures

// Основная структура
struct A
{
  int a;
  int b;
  
  // Правило сортировки
  bool operator > ( const A& Value ) const 
  {
    return(this.a > Value.a);
  }
};

// Вспомогательная структура для изменения правила сортировки
struct B : A
{
  // Перегрузка правила сортировки
  bool operator > ( const A& Value ) const 
  {
    return(this.b > Value.b);
  }
};

// Простая сортировка
template <typename T1, typename T2> // T2 - по какому правилу сортируем
void Sort( T1 &Array[] )
{
  const int Size = ArraySize(Array);
  
  for (int i = 0; i < Size - 1; i++)
  {
    T2 Min = Array[i];
    int Pos = i;
    
    for (int j = i + 1; j < Size; j++)
      if (Min > Array[j])
      {
        Min = Array[j];
        Pos = j;        
      }
      
    if (Pos != i)
    {
      Array[Pos] = Array[i];
      Array[i] = Min;
    }
  }
}

void OnStart()
{
  A Array[3] = {{2, 2}, {3,1}, {1, 3}};
  
  ArrayPrint(Array);
  
  // Сортировка по правилу A
  Sort<A, A>(Array); 
  ArrayPrint(Array);
  
  // Сортировка по правилу B
  Sort<A, B>(Array);  
  ArrayPrint(Array);
}


Result

    [a] [b]
[0]   2   2
[1]   3   1
[2]   1   3
    [a] [b]
[0]   1   3
[1]   2   2
[2]   3   1
    [a] [b]
[0]   3   1
[1]   2   2
[2]   1   3


This is probably an ancient bicycle.

 
fxsaber:

The ability to replace operators of structures of basic type allows flexibility in some situations.

there are a few points. in mql, the < operator must necessarily be a method of class/structure.

This means that it cannot be set for embedded structures, or for immutable ones.

This way can be improved a bit by adding a template comparison function, which will by default call the < operator

UPD: I write < operator everywhere because they usually overload it instead of >, it's not strict, but it's generally accepted
 
TheXpert:

there are a few points. in mql, the < operator must necessarily be a method of the class/structure.

This means that it cannot be set for embedded structures. Or for immutable structures.

// Вспомогательная структура для изменения правила сортировки
struct MQLTICK_BID : MqlTick
{
  // Сортировка по времени
  bool operator <( const MqlTick& Value ) const 
  {
    return(this.bid < Value.bid);
  }
};

// Простая сортировка
template <typename T1, typename T2> // T2 - по какому правилу сортируем
void Sort( T1 &Array[] )
{
  const int Size = ArraySize(Array);
  
  for (int i = 0; i < Size - 1; i++)
  {
    T2 Min = Array[i];
    int Pos = i;
    
    for (int j = i + 1; j < Size; j++)
      if (Min < Array[j])
      {
        Min = Array[j];
        Pos = j;        
      }
      
    if (Pos != i)
    {
      Array[Pos] = Array[i];
      Array[i] = Min;
    }
  }
}

void OnStart()
{
  MqlTick Ticks[];
  
  CopyTicks(_Symbol, Ticks, COPY_TICKS_ALL, 0, 5);
  
  ArrayPrint(Ticks);
  
  // Сортировка по правилу MQLTICK_BID
  Sort<MqlTick, MQLTICK_BID>(Ticks); 
  ArrayPrint(Ticks);
}


Result

                 [time]   [bid]   [ask]  [last] [volume]    [time_msc] [flags] [volume_real]
[0] 2018.10.11 23:59:55 1.09115 1.09354 0.00000        0 1539302395303       6       0.00000
[1] 2018.10.11 23:59:56 1.09132 1.09348 0.00000        0 1539302396037       6       0.00000
[2] 2018.10.11 23:59:56 1.09131 1.09353 0.00000        0 1539302396302       6       0.00000
[3] 2018.10.11 23:59:59 1.09135 1.09354 0.00000        0 1539302399458       6       0.00000
[4] 2018.10.11 23:59:59 1.09139 1.09378 1.09260        0 1539302399989      14       0.00000
                 [time]   [bid]   [ask]  [last] [volume]    [time_msc] [flags] [volume_real]
[0] 2018.10.11 23:59:59 1.09139 1.09378 1.09260        0 1539302399989      14       0.00000
[1] 2018.10.11 23:59:59 1.09135 1.09354 0.00000        0 1539302399458       6       0.00000
[2] 2018.10.11 23:59:56 1.09132 1.09348 0.00000        0 1539302396037       6       0.00000
[3] 2018.10.11 23:59:56 1.09131 1.09353 0.00000        0 1539302396302       6       0.00000
[4] 2018.10.11 23:59:55 1.09115 1.09354 0.00000        0 1539302395303       6       0.00000


This way can be improved a bit by adding a template comparison function, which will by default call the < operator

If you use typedef, then you need to define a sort function not only for structures, but also for standard numeric types. Or I don't understand what we're talking about.

Reason: