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

 
fxsaber:

Yes, I have history reading present.

You wrote then that my indicator was slow.
I found the reason for that very thing. When I loaded the account with 30,000 trades.
I managed to reduce the brakes . By a factor of 4. I redid the history reading a bit and optimized the code. But it still has them. The reason is that there are filters and everything is recalculated when I click on one filter.
Although everything is read from arrays.
Everything is OK on accounts with 5000 trades. But it is a problem on large ones.
It concerns not only that indicator.

I have made a clean history reading project.
Maybe, it may be due to the Internet. After all, the entire history is a lot of data.

 

Forum on trading, automated trading systems and strategy testing

Libraries: Easy Canvas

Nikolai Semko, 2020.02.17 05:15

I want to clarify an important point for interested programmers when operating the kanvas in tester mode.
A well-known programmer in this community came to me with this question:

- Why in tester-mode my panel, created on objects is redrawn much faster than drawn on canvas, while in normal mode my panel is drawn at normal speed on canvas?

I have managed to understand the reason for the problem and its solution.

The problem is that redrawing of objects goes together with redrawing of the whole screen, while the screen in the tester is redrawn no more frequently than 30 frames per second.

Objects are ultimately the same canvas (internal), but when you change the properties of the object, the object canvas is not generated (not recalculated), but generated only when the screen is updated (ChartRedraw), which occurs in the tester (and in normal mode, too) no more often than our eyes can distinguish changes, ie no more often than ~ 32 frames per second.

Suppose the panel changes every tick. Then the default canvas will also be redrawn every tick, but redrawing in the tester is still no more frequent than ~30 msec (~30 fps).

In other words, the canvas will be recomputed much more frequently than it is actually displayed on the screen, which results in a disproportionate use of resources.

The solution to this problem would be to ensure that canvas is recalculated and redrawn no more often than every 15-30 milliseconds of computer time, and then there will be no unnecessary cycles of empty recalculation.

For example, like this:

void OnTick()
  {
  static uint lastCalc=0;
  uint cur=GetTickCount();
  if (cur-lastCalc>15) {
    ReDrawMyCanvas();
    lastCalc=cur;
   }
  }

 
If, for example, you want to create a file "EURUSD.txt" (large characters) and already have "eurusd.txt" (small characters) on disk, the file will have the same name as before.
 
fxsaber:
If, for example, I want to create file "EURUSD.txt" (large symbols) and there is already "eurusd.txt" (small symbols) on the disk, the file will have the same name as before.

Moreover, when I save EURUSD TicksLongOpt.set, eurusd tickslongopt.set is saved. I have to rename it afterwards. There are a lot of annoying little things like that.

 
Edgar Akhmadeev:

Moreover, when I save EURUSD TicksLongOpt.set, eurusd tickslongopt.set is saved. I have to rename it afterwards. I have to rename it afterwards. There are many such annoying little things.

I think if I delete it first and then create it, everything will be fine.

 
fxsaber:

I think if you delete first and then create, you'll be fine.

No, I mean creating a new file, specifically when saving from the terminal dialog. If it doesn't work for everyone, then it depends on something else. I have Win7, if anything.

 
Edgar Akhmadeev:

No, I mean creating a new file, specifically when saving from the terminal dialog. If it doesn't work for everyone, then it depends on something else. I have Win7, if anything.

I meant FileOpen and FileSave functions.

 
fxsaber:

I meant FileOpen and FileSave functions.

Yes, I've just realised now that it's out of place, the topic is about language...

 
fxsaber:
Easy sorting an array of structures


Application


Result

It does not work, can you update it ? Sets template declarations are not allowed in local classes TestarrSort.mq5 81 3

// Сортировка массива структур и указателей на объекты по (под-) полю/методу.
#define  ArraySortStruct(ARRAY, FIELD)                                            \
{                                                                                \
  class SORT                                                                     \
  {                                                                              \
  private:                                                                       \
    template <typename T>                                                        \
    static void Swap( T &Array[], const int i, const int j )                     \
    {                                                                            \
      const T Temp = Array[i];                                                   \
                                                                                 \
      Array[i] = Array[j];                                                       \
      Array[j] = Temp;                                                           \
                                                                                 \
      return;                                                                    \
    }                                                                            \
                                                                                 \
    template <typename T>                                                        \
    static int Partition( T &Array[], const int Start, const int End )           \
    {                                                                            \
      int Marker = Start;                                                        \
                                                                                 \          
      for (int i = Start; i <= End; i++)                                         \
        if (Array[i].##FIELD <= Array[End].##FIELD)                              \
        {                                                                        \
          SORT::Swap(Array, i, Marker);                                          \
                                                                                 \
          Marker++;                                                              \
        }                                                                        \
                                                                                 \
       return(Marker - 1);                                                       \
    }                                                                            \
                                                                                 \
    template <typename T>                                                        \
    static void QuickSort( T &Array[], const int Start, const int End )          \
    {                                                                            \
      if (Start < End)                                                           \
      {                                                                          \
        const int Pivot = Partition(Array, Start, End);                          \
                                                                                 \
        SORT::QuickSort(Array, Start, Pivot - 1);                                \
        SORT::QuickSort(Array, Pivot + 1, End);                                  \
      }                                                                          \
                                                                                 \
      return;                                                                    \
    }                                                                            \
                                                                                 \
  public:                                                                        \
    template <typename T>                                                        \ 
    static void Sort( T &Array[], int Count = WHOLE_ARRAY, const int Start = 0 ) \
    {                                                                            \
      if (Count == WHOLE_ARRAY)                                                  \
        Count = ::ArraySize(Array)- Start; ;                                     \
                                                                                 \
      SORT::QuickSort(Array, Start, Start + Count - 1);                          \
                                                                                 \
      return;                                                                    \
    }                                                                            \
  };                                                                             \
                                                                                 \
  SORT::Sort(ARRAY);                                                             \
}                                                                                \


void OnStart()
{
  MqlRates Rates[];
  
  CopyRates(_Symbol, PERIOD_CURRENT, 0, 5, Rates); // Взяли бары
  
  Print("\nБары без сортировки - как получили.");
  ArrayPrint(Rates);
  
  Print("\nСортируем по open-цене.");
  ArraySortStruct(Rates, open);
  ArrayPrint(Rates);

  Print("\nСортируем по high-цене.");
  ArraySortStruct(Rates, high);
  ArrayPrint(Rates);

  Print("\nСортируем по времени.");
  ArraySortStruct(Rates, time);
  ArrayPrint(Rates);
}

 
Vladimir Pastushak:

Not working, can you update ? Writes template declarations are not allowed in local classes TestarrSort.mq5 81 3

Unfortunately, the language has been cut off. This code does not work.

void OnStart()
{
  class A
  {
    template <typename T> // template declarations are not allowed in local classes
    void f() {}
  };
}


But you may try an alternative variant.

template <typename T>                                       
void ArrayReindex( T &Array[], const double &TmpSort[][2] )
{                         
  T TmpArray[];
  
  for (int i = ::ArrayResize(TmpArray, ::ArrayRange(TmpSort, 0)) - 1; i >= 0; i--)
    TmpArray[i] = Array[(int)(TmpSort[i][1] + 0.1)];
    
  ::ArraySwap(Array, TmpArray);
              
  return;     
}             

// Сортировка массива структур и указателей на объекты по (под-) полю/методу.
#define  ArraySortStruct(ARRAY, FIELD)                                      \
{                                                                          \
  double TmpSort[][2];                                                     \
                                                                           \
  for (int i =::ArrayResize(TmpSort, ::ArraySize(ARRAY)) - 1; i >= 0; i--) \
  {                                                                        \
    TmpSort[i][0] = (double)ARRAY[i].FIELD;                                \
    TmpSort[i][1] = i;                                                     \
  }                                                                        \
                                                                           \
  ::ArraySort(TmpSort);                                                    \
  ::ArrayReindex(ARRAY, TmpSort);                                          \
}                                         

void OnStart()
{
  MqlRates Rates[];
  
  CopyRates(_Symbol, PERIOD_CURRENT, 0, 5, Rates); // Взяли бары
  
  Print("\nБары без сортировки - как получили.");
  ArrayPrint(Rates);
  
  Print("\nСортируем по open-цене.");
  ArraySortStruct(Rates, open);
  ArrayPrint(Rates);

  Print("\nСортируем по high-цене.");
  ArraySortStruct(Rates, high);
  ArrayPrint(Rates);

  Print("\nСортируем по времени.");
  ArraySortStruct(Rates, time);
  ArrayPrint(Rates);
}


SZZ Here is another example of usage.

Reason: