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

 

Forum on trading, automated trading systems and trading strategies testing

Bugs, bugs, questions

fxsaber, 2018.06.20 23:18

Decided to check how long such situations of phantom orders last, when an order is in the system, but not in the Terminal.

// Советник отслеживает длительность ситуаций, когда ордер отсутствует среди текущих и исторических

#define  TOSTRING(A)  #A + " = " + (string)(A) + "\n"
#define  TOSTRING2(A) #A + " = " + EnumToString(A) + " (" + (string)(A) + ")\n"

bool OrderIsExist( const ulong &OrderTicket )
{
  return(OrderTicket ? OrderSelect(OrderTicket) || HistoryOrderSelect(OrderTicket) : true);
}

void OnTradeTransaction ( const MqlTradeTransaction &Trans, const MqlTradeRequest&, const MqlTradeResult& )
{
  static bool PrevIsExist = true;
  static ulong StartTime = 0;
  static ulong MaxInterval = 0;
  
  const ulong NowTime = GetMicrosecondCount();
  const bool IsExist = OrderIsExist(Trans.order);
    
  if (!IsExist)
  {
    Print(TOSTRING2(Trans.type) + TOSTRING(Trans.order) +
          TOSTRING(OrderSelect(Trans.order)) + TOSTRING(HistoryOrderSelect(Trans.order)));       
  
    if (PrevIsExist) 
      StartTime = NowTime;
  }
  else if (!PrevIsExist)
  {
    const ulong Interval = NowTime - StartTime;
    
    Print(TOSTRING(Interval) + TOSTRING2(Trans.type) + TOSTRING(Trans.order) +
          TOSTRING(OrderSelect(Trans.order)) + TOSTRING(HistoryOrderSelect(Trans.order)));       
    
    if (Interval > MaxInterval)
    {
      MaxInterval = Interval;
      
      Comment(TOSTRING(MaxInterval) + TOSTRING(Trans.order)); // mcs.
    }
  }
          
  PrevIsExist = IsExist;
}


Result

2018.06.21 00:10:31.047 Trans.type = TRADE_TRANSACTION_ORDER_DELETE (2)
2018.06.21 00:10:31.047 Trans.order = 2210967406
2018.06.21 00:10:31.047 OrderSelect(Trans.order) = false
2018.06.21 00:10:31.047 HistoryOrderSelect(Trans.order) = false
2018.06.21 00:10:31.047 
2018.06.21 00:10:31.080 Interval = 32643
2018.06.21 00:10:31.080 Trans.type = TRADE_TRANSACTION_HISTORY_ADD (3)
2018.06.21 00:10:31.080 Trans.order = 2210967406
2018.06.21 00:10:31.080 OrderSelect(Trans.order) = false
2018.06.21 00:10:31.080 HistoryOrderSelect(Trans.order) = true


32 milliseconds an order is there but it is not in the Terminal! Just imagine what consequences this would have if the trading logic was executed in this interval.


It is interesting that phantom orders are most often present only atTRADE_TRANSACTION_ORDER_DELETE and TRADE_TRANSACTION_DEAL_ADD (much less often) transaction types.


Very bad platform nuance.


ZZY The questionable speed of trade operations on the five, unfortunately.

 
The HistorySelect is slow, if you make a history request from the beginning. Therefore it is desirable to do something like this

Forum on trading, automated trading systems and testing trading strategies

OrderCloseTime Expert Advisor MQL5

fxsaber, 2018.07.06 09:27

void LastTimeMQL5( datetime &OpenTime, datetime &CloseTime )
{
  static datetime PrevTime = 0;
  
  if (HistorySelect(PrevTime, INT_MAX)) // HistorySelect(0, INT_MAX) - slow.
  {
    for (int i = HistoryDealsTotal() - 1; i >= 0; i--)
    {
      const ulong Ticket = HistoryDealGetTicket(i);
  
      if (HistoryDealGetInteger(Ticket, DEAL_ENTRY) == DEAL_ENTRY_OUT)
      {
        CloseTime = (datetime)HistoryDealGetInteger(Ticket, DEAL_TIME);

        if (HistorySelectByPosition(HistoryDealGetInteger(Ticket, DEAL_POSITION_ID)))
          OpenTime = (datetime)HistoryDealGetInteger(HistoryDealGetTicket(0), DEAL_TIME);
          
        break;
      }
    }
  }
  
  PrevTime = CloseTime;
}


It is clear that the solution is on the verge of crutch. The other implementations won't be as succinct anymore. A simple way around the brakes generally won't work.

 
fxsaber:
Quick Multisymbol OnTick Implementation

I can't figure it out,

Why is OnTick with the Symbol parameter, and why is OnTick without the parameter?

and why is it again at the bottom of the comment

why not just a basicOnChartEvent?

void OnTick()
{
  OnTick(_Symbol); 
}

void OnChartEvent( const int id, const long &lparam, const double&, const string& )
{
  if (id == CHARTEVENT_CUSTOM)
    OnTick(Symbols[(int)lparam]);
}

// Мультисимвольный OnTick
void OnTick( const string &Symb )
{
}
 
fxsaber we have a cool code druker. I can't figure this thing out
 
Fast528:

why not just through a basicOnChartEvent?

Because it's convenient when the "flies and cutlets" are separate. Write all logic in multisymbol OnTick. Coincidences in names are random...

 
fxsaber:

Because it's convenient to keep the "flies and cutlets" separate. Write all logic in multisymbol OnTick. Coincidences in the names are coincidental...

...you're on a first-name basis until the year 2028.

it's your code, I'll find the full one

Индикатор

#property indicator_chart_window
#property indicator_plots 0

input long Chart = 0; // идентификатор графика-получателя события
input int Index = 0;

int OnCalculate( const int rates_total, const int prev_calculated, const int, const double &[] )
{
  if (prev_calculated)
    EventChartCustom(Chart, 0, Index, 0, NULL);
  
  return(rates_total);
}


Советник

input int AmountSymbols = 1;

const string Symbols[] = {"EURUSD", "GBPUSD", "AUDUSD", "USDJPY", "USDCAD"};

void OnInit()
{
  for (int i = 0; i < AmountSymbols; i++)
    if (Symbols[i] != _Symbol)
      iCustom(Symbols[i], PERIOD_W1, "Spy.ex5", ChartID(), i); // MQL5\Indicators\Spy.ex5
}

void OnTick()
{
  OnTick(_Symbol); 
}

void OnChartEvent( const int id, const long &lparam, const double&, const string& )
{
  if (id == CHARTEVENT_CUSTOM)
    OnTick(Symbols[(int)lparam]);
}

// Мультисимвольный OnTick
void OnTick( const string &Symb )
{
}
 
Fast528:

No one is forcing you to use this version of the multisymbol NewTick event implementation. In this branch, I just put all sorts of things together.

 
fxsaber:

No one is forcing you to use this implementation of a multisymbol NewTick event. In this branch I just put all sorts of things.

I was interested in OnTick, and with parameters, the compiler swallows

 
Fast528:

I was wondering why OnTick????

I don't know myself, random coincidence.

 
fxsaber:

I do not know myself, random coincidence.

Don't tell me, I found a feature, the ICs have not yet corrected the loan of the name of one of the main functions, killed a lot of time reading and searching for it

looking for an undocumented feature.

Reason: