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

 
fxsaber:
template <typename T1, typename T2>
bool More(const T1& first, const T2& second)
{
   return first > second;
}

bool More(const MqlTick& first, const MqlTick& second)
{
   return first.bid > second.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 (More(Min, Array[j]))
      {
        Min = Array[j];
        Pos = j;        
      }
      
    if (Pos != i)
    {
      Array[Pos] = Array[i];
      Array[i] = Min;
    }
  }
}
Anyway, I got in a little off-topic
 
// Возвращает TimeLocal даже в Тестере
datetime GetTimeLocal( void )
{ 
  static const bool IsTester = MQLInfoInteger(MQL_TESTER);
  static uint TickCount = 0;
  static datetime InitTimeLocal = 0;
  
  datetime Res = 0;
  
  if (IsTester)
  {
    if (InitTimeLocal)
      Res = InitTimeLocal + (GetTickCount() - TickCount) / 1000;
    else
    {
      int Array[];    
      const string FileName = __FUNCTION__;  
      
      if (FileSave(FileName, Array))
      {
        TickCount = GetTickCount();
        
        Res = InitTimeLocal = (datetime)FileGetInteger(FileName, FILE_MODIFY_DATE);
      }
    }
  }
  else
    Res = TimeLocal();
    
  return(Res);
}


Application

void OnInit()
{
  Print(GetTimeLocal());  
}

double OnTester()
{
  Print(GetTimeLocal());
  
  return(0);
}


HH Is there an easier way to find out the local time of the computer in the Tester?

 
fxsaber:

Application

HH Is there an easier way to find out the local time in the Tester?

Through a global variable (GlobalVariableTemp, GlobalVariableTime).

 
Stanislav Korotky:

Through a global variable (GlobalVariableTemp, GlobalVariableTime).

It will not work.

 
fxsaber:

It won't work.

It's been working for a long time, and it's working fine.

 
Stanislav Korotky:

It's been working properly for a long time.

Then what am I doing wrong?

datetime GetTimeLocal( const bool FlagDelete = false )
{ 
  static const bool IsTester = MQLInfoInteger(MQL_TESTER);
  static const string Name = __FILE__;

  return(IsTester && GlobalVariableTemp(Name) ? GlobalVariableTime(Name) : TimeLocal());
}

double OnTester()
{
  Print(GetTimeLocal());
  
  return(0);
}
 
fxsaber:

Then what am I doing wrong?

I suspect the problem is your love of concatenation. try if.

 
TheXpert:

I suspect the problem is your love of laconicization. try if.

Tried

datetime GetTimeLocal()
{ 
  static const string Name = __FILE__;

  datetime Res = 0;
  
  if (GlobalVariableTemp(Name))
    Res = GlobalVariableTime(Name);
  
  return(Res);
}

Doesn't work.

 
fxsaber:

Tried

Doesn't work.

It doesn't work because the global variable is emulated with the MT5 Tester. They also use TimeCurrent ().
 
fxsaber:

Tried

It doesn't work.

It works for me, as expected...

PS. My mistake - it works in MT4, but doesn't work in MT5.

Reason: