Features of the mql4 language, subtleties and techniques - page 8

 
  1. The first call of the indicator through the Expert Advisor using iCustom causes start of OnInit and OnCalculate of the indicator
  2. The indicator does not call its OnCalculate until the next call of iCustom.
  3. Exiting the Expert Advisor calls the indicator OnDeinit.
The iCustom indicators are not complete. And it is impossible to receive indicator buffer values from the manually started indicators, using MQL4 tools.
 
fxsaber:
The iCustom indicators are not complete.

What is the incompleteness? Events are not handled?

 
Andrey Khatimlianskii:

What is the incompleteness? Events aren't being processed?

Yes.

 
#property strict

#define  HOUR 3600
#define  DAY (24 * HOUR)
#define  WEEK 7

datetime GetBarTime( const datetime time, const bool NextBar = false, string Symb = NULL, const ENUM_TIMEFRAMES TimeFrame = PERIOD_M1 )
{
  if (Symb == NULL)
    Symb = _Symbol;
    
  return(iTime(Symb, TimeFrame, iBarShift(Symb, TimeFrame, time) - (NextBar ? 1 : 0)));
}

datetime GetTimeDayOfWeek( const int Shift = 0, const ENUM_DAY_OF_WEEK Day = SUNDAY )
{
  const datetime Res = TimeCurrent() / DAY * DAY;
  
  return(Res - (((WEEK + (TimeDayOfWeek(Res) - Day)) % WEEK) + Shift * WEEK) * DAY);
}

// Аналог по серверному времени - https://www.mql5.com/ru/docs/dateandtime/timegmtoffset
// Работает для FOREX-символов, когда M1-история доступна за ближайшую неделю
int TimeServerGMTOffset( void )
{
  const datetime Sunday = GetTimeDayOfWeek();
  
  return(((int)MathRound((double)MathMin(Sunday - DAY - GetBarTime(Sunday), Sunday + DAY - GetBarTime(Sunday, true)) / HOUR) - 3) * HOUR);
}

// Аналог по серверному времени - https://www.mql5.com/ru/docs/dateandtime/timegmt
// Работает для FOREX-символов, когда M1-история доступна за ближайшую неделю
datetime TimeServerGMT( void )
{
  return(TimeCurrent() + TimeServerGMTOffset());
}


Application

#define  PRINT(A) Print(#A + " = " + (string)(A))

void OnStart()
{  
  PRINT(TimeGMT());
  PRINT(TimeServerGMT());  
}


The approach is good in that it works not only on weekend, Tester and real time, but also on third party data. I.e. GMT quotes taken from other than MT are determined.

 
fxsaber:

What if instead ofTimeCurrent(), the time of the last tick on the instrument, the calculation is not disturbed?

 
Vitaly Muzichenko:

What if instead ofTimeCurrent(), you take the time of the last tick for the instrument, the calculation will not be broken?

It won't. The TimeCurrent affects the algorithm very indirectly. You can take the bar time, etc.

It is simply necessary to calculate the date of the last passed (in the history of bars) Sunday in any way - to have bars before Sunday and after Sunday. You can at least use TimeLocal to do this.

 
It's hard not to like a language that allows you to write such code
double Lots[] = {0, 0, 0, 0, 0, 0};

for (int i = OrdersTotal() - 1; i >= 0; i--)
  if (OrderSelect(i, SELECT_BY_POS))
    Lots[OrderType()] += OrderLots();

I'm sure the developers didn't plan for such things when writing the language. How it turned out that MQL4 is full of such handy things is a mystery.

 
fxsaber:
It's difficult not to like a language that allows you to write such a code

I'm sure the developers didn't plan for such things when writing the language. How it turned out that MQL4 is full of such handy things is a mystery.

There is no mystery. I have been using it for many years in mql5 as well, but we have to explicitly convert position type and order type to int type.

I just declare an array like this

double  Lots[6] = {0.0};
You can also store ticketypes in an array.
 
Alexey Viktorov:

No mystery. I've been using it for many years and in mql5 too, but you have to explicitly pass it to int type.

The meaning will be quite different there.