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

 
Alain Verleyen:

I would like to get the server time according to GMT.

This is what you get when you run the script. There is a special display of two values: TimeGMT and TimeServerGMT, so you can check that they coincide.

If they are the same, there is no error, otherwise there is an error.

 
fxsaber:

You get this when you run the script. Two values are specially output there: TimeGMT and TimeServerGMT, so that you can check that they match.

If they coincide, there is no error, otherwise there is a mistake.

Got it.
 

Forum on trading, automated trading systems and testing trading strategies

Libraries: TypeToBytes

fxsaber, 2018.03.31 09:24

// Кроссплатформенный пример передачи произвольных данных через пользовательское событие

#include <fxsaber\HistoryTicks\Data_String.mqh> // https://www.mql5.com/ru/code/20298

// Печать произвольных данных
template <typename T>
bool MyPrint( const T &Value )
{
  T Array[1];
  
  Array[0] = Value;
  
  ArrayPrint(Array, _Digits, NULL, 0, WHOLE_ARRAY, ARRAYPRINT_HEADER | ARRAYPRINT_ALIGN);
  
  return(true);
}

void OnChartEvent( const int id, const long &lparam, const double&, const string &sparam )
{
  // Распечатали полученные данные
  if ((id == CHARTEVENT_CUSTOM) &&
      (lparam ?  MyPrint(DATA_STRING::FromString<MqlDateTime>(sparam)) // Получили MqlDateTime
              : !MyPrint(DATA_STRING::FromString<MqlTick>(sparam))))   // Получили MqlTick
    ExpertRemove(); // Вышли из примера
}

void OnInit()
{
  MqlTick Tick;  
  MqlDateTime DateTime;
  
  // Заполнили значения
  SymbolInfoTick(_Symbol, Tick);  
  TimeCurrent(DateTime);

  // Передали
  EventChartCustom(0, 0, 0, 0, DATA_STRING::ToString(Tick));     // Передали MqlTick
  EventChartCustom(0, 0, 1, 0, DATA_STRING::ToString(DateTime)); // Передали MqlDateTime
}
 
fxsaber:

Sparam has a length limit. Have you taken that into account?

 
Andrey Barinov:

The sparam has a length limit.

Yes, 128 bytes.

const bool Init = EventChartCustom(0, 0, 0, 0, NULL);

void OnChartEvent( const int id, const long&, const double&, const string &sparam )
{
  static int PrevLength = -1;
  
  if (id == CHARTEVENT_CUSTOM)
  {
    const int Length = StringLen(sparam);

    bool Res = (Length != PrevLength);
    
    if (Res)
    {
      string Str;
            
      StringInit(Str, Length + 1, 1);
      
      Res = EventChartCustom(0, 0, 0, 0, Str);
    }
    
    if (!Res)
    {
      Print(PrevLength);
      
      ExpertRemove();
    }
      
    PrevLength = Length;      
  }
}

Did you take that into account?

No, you can see that from it. The example of sparam is given as one of the options for using string storing.

You can make the code more complicated (send it in chunks), but then it will lose its clarity.

 
fxsaber:

I'm afraid that without a concrete example of a design, it will not work.

It gives out such a picture:


  Print( "TimeServer: ",TimeGMT()-TimeServerGMTOffset() );
  Print( "TimeServerGMT: ",TimeServerGMT() );
  Print( "TimeCurrent: ",TimeCurrent() );
  Print( "TimeLocal: ",TimeLocal() );
  Print( "TimeGMT: ",TimeGMT() );
  Print( "TimeGMTOffset: ",TimeServerGMTOffset() );
  Print( "" );

And here's the mt4 installer withGMT+2 work shift

Was expecting to seeTimeServer=2018.04.01 05:54:26

Files:
tw4setup.zip  522 kb
 
Vitaly Muzichenko:

It gives out a picture like this:

Just in case, tell me the name of the trading server.

TradersWay-Demo.

It works correctly.

2018.04.01 08:36:08.858 TimeServerGMT EURUSDi,M1: TimeServerGMT() = 2018.03.30 19:59:59

Maybe you do not have it

// Работает для FOREX-символов, когда M1-история доступна за ближайшую неделю
 
fxsaber:

Just in case, let me know the name of the Trading Server.

ZZY TradersWay-Demo.

It works correctly.

Maybe you do not have it.

Probably not. I probably do not have it in any terminal, I never switch timeframe below M15

Okay, I'll look into it some more.
 
Vitaly Muzichenko:

Probably not. I probably do not have it in any terminal, I never switch the time frame below M15

Do it this way

datetime GetBarTime( const datetime time, const bool NextBar = false, string Symb = NULL, const ENUM_TIMEFRAMES TimeFrame = PERIOD_M15 )
 
fxsaber:

Do this.

I did it, it didn't look like nonsense. Confused by the fact that the shift should probably be -720



P.S. Again, I know that the shift +2 hours is written on the website, but the robot does not know that.

If you run the Expert Advisor on a chart like this, it is unlikely that we will get the required output result: "Learn the current time server TimeServer()" I have not got it right, maybe I am doing something wrong.

Maybe the developers should implement the function in the terminal.