how to know the price of the penultimate active order?

 

Hi, I need to know the price of the penultimate order active.

The code below is the code to know the price or the last order active, but I need to know the price of the previous order. How can I know it?


double retorno_precio_apertura_ult(int NMagb,int NMags)
{
int num3=0;
int ot3=0;
double resultprecioultimaorden;
for (int j=OrdersTotal()-1;j>=0;j--)
    {
    if (OrderSelect(j,SELECT_BY_POS)==true && OrderSymbol()==Symbol() && OrderType()==1 || OrderType()==0 && OrderMagicNumber()==NMags || OrderMagicNumber()==NMagb )
       {
       if(OrderOpenTime()>ot3)
         {
         num3=OrderTicket();
         ot3=OrderOpenTime();
         resultprecioultimaorden=OrderOpenPrice();
         }
        return(resultprecioultimaorden);
       }
    }
}
 
jdcabezas:

Hi, I need to know the price of the penultimate order active.

The code below is the code to know the price or the last order active, but I need to know the price of the previous order. How can I know it?

Why don't you use meaningful variable names . . . ot3 ? it doesn't help the people you are asking for help understand your code . . . what is 1 ? OrderType() == 1 ? what is 0 ?

You code doesn't work . . . it returns before it has gone through all the orders . . . I know you think it works but it doesn't do what it is meant to do. You need to fix it first then you can make it do what you need . . . all you do is create a new variable, OOP_LastButOneOrder, then when you find a newer OrderOpenTime you set OOP_LastButOneOrder = resultprecioultimaorden and then resultprecioultimaorden = OrderOpenPrice() when the loop is complete you can return OOP_LastButOneOrder

Also get into the habit of using brackets ( ) for your bool expressions . . . one day soon what you meant may not be what you get . . . read this thread: https://forum.mql4.com/57476

 
RaptorUK:

. . . all you do is create a new variable, OOP_LastButOneOrder, then when you find a newer OrderOpenTime you set OOP_LastButOneOrder = resultprecioultimaorden and then resultprecioultimaorden = OrderOpenPrice() when the loop is complete you can return OOP_LastButOneOrder

. . .


I followed the same logical path you did, but I found that solution may not work all the time. As I was coding my attempt at the solution, I was confronted with a question: with the understanding that the trading pool and history pool may not be sorted in a particular order (such as by ticket or order close time), what happens when the very first order selected is actually the last order entered--and therefore has the largest (most-recent) OrderOpenTime? In the following code, if the very first order selected is the most recent order entered, then the highlighted portion would execute only once:

double retorno_precio_apertura_ult(int NMagb,int NMags) {
   double LastOrderOpenPrice = 0, PenultimateOrderPrice = 0;
   datetime LastOrderOpenTime = 0;
   for (int j = OrdersTotal() - 1; j >= 0; j--) {
      if (OrderSelect(j, SELECT_BY_POS))
         if (OrderSymbol() == Symbol() && OrderType() <= OP_SELL)
            if (OrderMagicNumber() == NMags || OrderMagicNumber() == NMagb)
               if (OrderOpenTime() > LastOrderOpenTime) {
                  PenultimateOrderPrice = LastOrderOpenPrice;
                  LastOrderOpenPrice = OrderOpenPrice();
                  LastOrderOpenTime = OrderOpenTime();
               }
   }
   return (PenultimateOrderPrice);
}

And because the highlighted portion executes only once (when the very first order selected is the most recent order entered), PenultimateOrderPrice never gets the true (non-zero) LastOrderOpenPrice.

I came up with another solution. It's a little bit more intense, but it solves the above issue.

double GetPenultimateOpenPrice(int NMagb = 0, int NMags = 0) {
   int TradingHistory[][2];   // [x][0] = OrderOpenTime; [x][1] = OrderTicket
   for (int j = OrdersTotal() - 1; j >= 0; j--)
      if (OrderSelect(j, SELECT_BY_POS))
         if (OrderSymbol() == Symbol() && OrderType() <= OP_SELL)
            if (OrderMagicNumber() == NMagb || OrderMagicNumber() == NMags) {
               int TH_index = ArrayRange(TradingHistory, 0) + 1;
               ArrayResize(TradingHistory, TH_index);
               TradingHistory[TH_index-1][0] = OrderOpenTime();
               TradingHistory[TH_index-1][1] = OrderTicket();
            }
   if (ArrayRange(TradingHistory, 0) >= 2) {
      ArraySort(TradingHistory);
      if (OrderSelect(TradingHistory[ArrayRange(TradingHistory, 0)-2][1], SELECT_BY_TICKET))
         return (OrderOpenPrice());
      else
         Print ("Error selecting order. Error #; ", GetLastError());  
   }
   else
      Print ("There currently is not a penultimate open price to retrieve.");
   return (0);
}
 
Thirteen:


I followed the same logical path you did, but I found that solution may not work all the time. As I was coding my attempt at the solution, I was confronted with a question: with the understanding that the trading pool and history pool may not be sorted in a particular order (such as by ticket or order close time), what happens when the very first order selected is actually the last order entered--and therefore has the largest (most-recent) OrderOpenTime? In the following code, if the very first order selected is the most recent order entered, then the highlighted portion would execute only once:

And because the highlighted portion executes only once (when the very first order selected is the most recent order entered), PenultimateOrderPrice never gets the true (non-zero) LastOrderOpenPrice.

I came up with another solution. It's a little bit more intense, but it solves the above issue.

I did have something nagging in my mind . . . but I thought the OP could find that out and solve it, but I guess there is no need now.
Reason: