Last closed order function

 

Hi,

I have this function below which check the status (Profit or Loss) of the last closed order:

string CheckLastOrder()
{
string last;
for(int i=OrdersHistoryTotal()-1;i>=0;i--)
 {
  if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==TRUE)
  {
   if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
    {
       // Loss
       if(OrderType()==OP_BUY && OrderClosePrice()<=OrderOpenPrice()){last="lossBUY";}
       if(OrderType()==OP_SELL && OrderClosePrice()>=OrderOpenPrice()){last="lossSELL";}
       // Profit
       if(OrderType()==OP_BUY && OrderClosePrice()>OrderOpenPrice()){last="profitBUY";}
       if(OrderType()==OP_SELL && OrderClosePrice()<OrderOpenPrice()){last="profitSELL";}
    }
  }
 }
 return(last);
}

But when I use it into an EA (code below) with many orders closed it doesn't work correctly, the "LastOrder" string variable display only the status of the first closed order but not the next closed order, what is wrong into my code?

//+------------------------------------------------------------------+
//| Custom EA start function
//+------------------------------------------------------------------+
int start()
{
// MQL4 code here

string LastOrder = CheckLastOrder();      
      
Comment("\nLast Closed Order : ",LastOrder);

// MQL4 code here
}

//+------------------------------------------------------------------------+
//| Last closed order function
//+------------------------------------------------------------------------+
string CheckLastOrder()
{
string last;
for(int i=OrdersHistoryTotal()-1;i>=0;i--)
 {
  if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==TRUE)
  {
   if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
    {
       // Loss
       if(OrderType()==OP_BUY && OrderClosePrice()<=OrderOpenPrice()){last="lossBUY";}
       if(OrderType()==OP_SELL && OrderClosePrice()>=OrderOpenPrice()){last="lossSELL";}
       // Profit
       if(OrderType()==OP_BUY && OrderClosePrice()>OrderOpenPrice()){last="profitBUY";}
       if(OrderType()==OP_SELL && OrderClosePrice()<OrderOpenPrice()){last="profitSELL";}
    }
  }
 }
 return(last);
}

Thank you for your help.

Documentation on MQL5: Python Integration / order_calc_margin
Documentation on MQL5: Python Integration / order_calc_margin
  • www.mql5.com
order_calc_margin - Python Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
8tango:

Forum on trading, automated trading systems and testing trading strategies

Getting Last Closed Order

William Roeder, 2011.09.28 15:58

  1. The MOMENT you have multiple charts or manual trading, that code will be not be selecting the EA's order but something else. Likewise if you have deposits and withdrawals

    Since the order of the list is NOT documented, i.e. could change in future versions, the paranoid among us go through all orders.

    If you're not paranoid just find the last order and exit the loop.

        for(int pos=OrdersHistoryTotal(); pos >= 0; pos--) if (
            OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY)   // Only orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol()                 // and my pair.
        &&  OrderType()         <= OP_SELL // Avoid cr/bal forum.mql4.com/32363#325360
        ){
            Print(" Last Order Ticket = ",OrderTicket());
            break;
    }
  2. Why do you need to do it more than once? Remember the last OrderCount() in a static. If it has changed find the last history and remember what you need.

Reason: