Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1720

 
MakarFX #:
The error is not in the code of searching for the penultimate, but most likely, in the output of information in the text.

We have never said anything about an error in the code. I will repeat once again, this code works when a new maximum order is opened and when this does not happen, the Expert Advisor considers that the penultimate order = the last one. That is all

 
MakarFX #:
The error is not in the penultimate search code, but most likely in the output of the information to text

Makar, why doesn't the highlighted condition work?

//+----------------------------------------------------------------------------+
//| Расчет тикета предпоследнего ордера в сетке                                 |
//+----------------------------------------------------------------------------+
int GetTicketPenultimateOrder()
  {
   penultimate_ticket = 0;
     {
      for(int cnt = OrdersTotal() - 1; cnt >= 0; cnt--)
        {
         if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
           {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
              {
               if(OrderType() == OP_BUY || OrderType() == OP_SELL)
                 {
                  if(OrderTicket() > penultimate_ticket)
                     if(penultimate_ticket < GetTicketMaxOrder())
                        penultimate_ticket = OrderTicket();

                 }
              }
           }
        }
     }
   return(penultimate_ticket);
  }
 
EVGENII SHELIPOV #:

Makar, tell me why the highlighted condition doesn't work.

In this line you find the maximum ticket

if(OrderTicket() > penultimate_ticket)

and in this line you're comparing the maximum to the maximum.

if(penultimate_ticket < GetTicketMaxOrder())
therefore always false
 
EVGENII SHELIPOV #:

We have never said anything about an error in the code. I will repeat once again, this code works when a new maximum order is opened and when this does not happen, the Expert Advisor considers that the penultimate order = the last one. That is all

This only happens if the EA is restarted

 
MakarFX #:

This is only if the EA is restarted

No Makar, look here, I put the variable of the penultimate order and the function of the last order on the monitor

ObjectCreate("signal400",OBJ_LABEL,0,0,0,0,0);
   ObjectSet("signal400",OBJPROP_XDISTANCE,100);
   ObjectSet("signal400",OBJPROP_YDISTANCE,740);
   ObjectSetText("signal400",DoubleToString(FindPenultimateLots(),2),14,"Tahoma", clrRed);
   ObjectCreate("signal401",OBJ_LABEL,0,0,0,0,0);
   ObjectSet("signal401",OBJPROP_XDISTANCE,200);
   ObjectSet("signal401",OBJPROP_YDISTANCE,740);
   ObjectSetText("signal401",DoubleToString(prev_ticket,0),14,"Tahoma", clrRed);
 
MakarFX #:

On this line you find the maximum ticket

and in this line you compare the maximum with the maximum.

therefore always false

Well, how do you make it so that all the tickets scanned by this function

(OrderTicket()

to remove a ticket equal to

GetTicketMaxOrder()
 
EVGENII SHELIPOV #:

Makar, tell me why the highlighted condition doesn't work

//+----------------------------------------------------------------------------+
//| Расчет тикета предпоследнего ордера в сетке                                 |
//+----------------------------------------------------------------------------+
int GetTicketPenultimateOrder()
  {
   penultimate_ticket = 0;
     {
      for(int cnt = OrdersTotal() - 1; cnt >= 0; cnt--)
        {
         if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
           {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
              {
               if(OrderType() == OP_BUY || OrderType() == OP_SELL)
                 {
                  if(OrderTicket() > penultimate_ticket)
                     if(penultimate_ticket < GetTicketMaxOrder())
                        penultimate_ticket = OrderTicket();

                 }
              }
           }
        }
     }
   return(penultimate_ticket);
  }

Eugene, where does the ticket variable change? Or does the whole loop work with one order?

 
Alexey Viktorov #:

Eugene, where does the ticket variable change? Or does the whole loop work with one order?

I have already written to him
 
EVGENII SHELIPOV #:

No Makar, look here, I put the variable of the penultimate order and the function of the last order on the monitor

prev_ticket is reset when the EA is restarted
 
MakarFX #:

Once again... I gave you a working code!

Run it in the tester and press the buttons.

To see it, you have to make the orders close and this EA does not do that. And the whole problem happens after the max order is closed

Reason: