Any questions from a PROFI to a SUPER PROFI - 1. - page 42

 
swanhearts:

Hello, I was sent a MA indicator with all the levels I need. The problem is that when I hover my mouse over the indicator (each MA), it does not show the period of that MA. How to fix it? There is a constant syntax error when changing the nameIndicatorShortName("MASHKI =)"); Please help.

indicator buffer name is set using SetIndexLabel

 

1. What is the cheapest way to know that the function call is on the same tick as the previous call?

bool tick_already_processed()
{
   return( ??? );
}

void some_func()
{
   if ( tick_already_processed() ) return;

   // recalculate only once on each tick
}

void OnTick()
{
   if ( A ) some_func();
   if ( B ) some_func();
   if ( C ) some_func();
}

Time (TimeCurrent) can stay the same, time in ms from SymbolInfoTick, theoretically, too.

Comparing the bid, ask and ms times is quite expensive.

I do not suggest organizing the control in the Expert Advisor itself, I want to get a universal independent function.


The same question concerns trading operations. How do we know that something has changed in the list of orders/positions (including the open levels, SL and TP) since the last run?

I would need to set some flag in OnTrade (and reset it when done), it's the cheapest way to do it.

But, again, I would like a universal solution.


Who has any ideas?

 

Andrey Khatimlianskii: 

Example https://www.mql5.com/ru/code/16997

Resources.mqh file.

IsNewPeriod function


The basic idea is to use a function with static inside.

You can store it in ID = GetMicroSecondsCount to remember and check ticks

For the same with orders - check magic

I haven't checked the pseudo code below :)

bool IsNewTick(ulong newId) // GetMicrosecondCount() or magic
{
  static ulong id = 0;
  
  if (id != newId) 
  {
    id = newId;
    return true;
  }
  
  return false; 
}
 
Andrey Khatimlianskii:

2. similar question for trades. How do I know if something has changed in the list of orders/positions (including open levels, SL and TP) since the last run?

In A, we need to set some kind of flag in OnTrade (and reset it after execution), it's the cheapest way.

But, again, I would like a universal solution.


Who has any ideas?

Add your conditions (SL/TP etc) here.

Forum on trading, automated trading systems and strategy testing

Organizing an order overflow cycle

fxsaber, 2017.10.18 12:29

struct HISTORY_UNIT
{
  long Ticket;
  int Type;
  double Lots; 
    
  HISTORY_UNIT( void ) : Ticket(::OrderTicket()), Type(::OrderType()), Lots(::OrderLots())
  {
  }

  bool operator !=( const HISTORY_UNIT &Unit ) const
  {
    return((this.Ticket != Unit.Ticket) || (this.Type != Unit.Type) || (this.Lots != Unit.Lots));
  }
      
  bool IsChange( void )
  {
    const HISTORY_UNIT Tmp;
    const bool Res = (this != Tmp);
    
    if (Res)
      this = Tmp;
      
    return(Res);
  }
};

// Возвращает true только в случае, если с последнего вызова произошли торговые изменения
bool IsChange( void )
{
  static HISTORY_UNIT History[];  

  const int Total = OrdersTotal();  
  bool Res = (ArraySize(History) != Total);

  for (int i = 0, j = Res ? ArrayResize(History, 0, Total) : 0; i < Total; i++)      
    if (OrderSelect(i, SELECT_BY_POS))
    {
      if (Res || (Res = History[j].IsChange()))
        ArrayResize(History, j + 1, Total);
      
      j++;
    }
  
  return(Res);
}
 
Andrey Khatimlianskii:

1. What is the cheapest way to know that the function call is on the same tick as the previous call?

Time (TimeCurrent) can stay the same, time in ms from SymbolInfoTick, theoretically too.

Comparison of bid, ask and ms times is too expensive.

I do not suggest organizing the control in the EA itself, I want to get a universal independent function.

The issue here is not cheapness, but reliability of ticks numbering. I do the following things in my EA.

#ifdef __MQL5__
#else // __MQL5__
  // В false-режиме если засекли изменения, то последующие вызовы не делают проверку.  
  static bool IsNotChange( const bool bInit = false )
  {
    static bool IsChange = false;
     
    if (bInit)
      IsChange = false;
      
  #ifdef  HISTORYTICKS_ISCHANGE
    if (bInit)
      HISTORYTICKS::IsChange();
    else if (!IsChange)
      IsChange = HISTORYTICKS::IsChange();
  #endif // HISTORYTICKS_ISCHANGE
    
    return(!IsChange);
  }
#endif // __MQL5__

This is the version of the function for MT4. Numbering through spy indicator.


I am not including the version of the same function for MT5 as unnecessary things should be removed. But it is based on this function.

  // Свежие тики с последнего вызова
  static int GetFreshTicks( MqlTick &Ticks[], const datetime dFrom = 0 )
  {
    static long LastTime = 0;
    static int LastAmount = 0;

    if (dFrom)
    {
     ::Comment("Waiting Ticks from " + ::TimeToString(dFrom, TIME_DATE) + "...");

      LastAmount = 0;
    }
    
    ::ArrayFree(Ticks);
  
    int Size = (dFrom || LastTime) ? ::CopyTicksRange(_Symbol, Ticks, COPY_TICKS_INFO, dFrom ? (long)dFrom * 1000 : LastTime) : 0;
    
    if (Size > LastAmount)
    {
      LastTime = Ticks[Size - 1].time_msc;
      int NewLastAmount = 1;
      
      for (int i = Size - 2; (i >= LastAmount) && (Ticks[i].time_msc == LastTime); i--)
        NewLastAmount++;
        
      if (::ArrayRemove(Ticks, 0, LastAmount))
        Size -= LastAmount;
        
      LastAmount = NewLastAmount;
    }
    else
      Size = ::ArrayResize(Ticks, 0);
    
    return(Size);
  }


If it returns non-zero - new tick.

 
...:

The basic idea is to use a function with static inside.

To remember and reconcile ticks you can store in ID = GetMicroSecondsCount

Thank you!

The whole issue is just this unique ID (tick number), which doesn't exist.

GetMicroSecondsCount won't help, because it will change between calls (that's what it's designed for), while GetTickCount, theoretically, may not change on 2 adjacent ticks.

 
fxsaber:

The problem here is not cheapness, but the reliability of tick numbering. In a combat EA I do it this way.

No, it will definitely be slower than a bid/ask/ms comparison.

What's the problem with reliability? It's only the fact of changing something that matters.

 
fxsaber:

Add your conditions (SL/TP etc.) here.

Immediately remembered this code.

But the point is just to avoid unnecessary enumeration of all orders.

It is clear that we can make this loop a single loop for the entire EA. But in this case we should pass to the function a sign of changes (we may as well not call it).

 
Andrey Khatimlianskii:

No, it will definitely be slower than a bid/ask/ms comparison.

What's the problem with reliability? All that matters is that something changes.

Forum on trading, automated trading systems and strategy testing

Any questions from PROFi to SUPER PROFi - 1.

Andrey Khatimlianskii, 2020.03.05 23:46

Time (TimeCurrent) may remain the same, time in ms from SymbolInfoTick, theoretically, also.

Comparing bid, ask and time in msc is expensive.

Especially relevant for MT4 where time_msc step change is 1000ms.

 
Andrey Khatimlianskii:

But the goal is precisely to avoid unnecessary overlapping of all orders.

It is clear that we can make this cycle a single one for the entire EA. But then we have to pass to the function a sign of changes (we may as well not call it).

I don't understand. If something has changed in the database, the only way to find out is to compare it with the previous database state.

You can compare each corresponding database element. Or you can calculate the hash from them and compare the hashes.