OnOrderClose Function?

 

I'm looking for some way for MQL4 to be notified that an order has been closed, at what price it was closed at, and at what price the order was opend at.

For example, a buy order was initiated at 1.56.  The take profit is set for 1.565.  The price reaches 1.565, the order closes.  Automatically, apiece of code Print()s the message, "Order X was closed at 1.565," and a limit buy order is sent in with a price of 1.56.

Thanks posters.

 

See sample:



//+------------------------------------------------------------------+
//|                                                 OrdersAlerts.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net/ru/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/ru/"
 
 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool CheckSummChanged(int &ControlSumm)
   {
   bool res;
   int currSumm;
//----
   for(int i=0;i<OrdersTotal();i++)
      {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) currSumm+=(OrderType()+1)*OrderTicket();
      }
//----
   if (currSumm!=ControlSumm) 
      {
      ControlSumm=currSumm;
      res=true;
      }
   return(res);   
   }
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
  int prevcheckSumm;
//----
   while (!IsStopped())
      {
      if (CheckSummChanged(prevcheckSumm)) Alert("Orders are changed!");
      Sleep(1000);
      }   
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
yeah, that's a good idea, Rosh. Rosh created a checksum of all open orders and check if this checksum changes.
 
But then how do I get the order information?  Ordertype, Orderopenprice, Orderopentime?
 
Maybe this could be done by reading the transaction history every, say, 200 milliseconds?  Can MQL4 do that?
 
inkexit:
Maybe this could be done by reading the transaction history every, say, 200 milliseconds? Can MQL4 do that?

See function Sleep for endless loop and notice that this function is not allowed for an indicator.

 

I was thinking of something like this:

int init()
{
   OrderSelect(0,SELECT_BY_POS,MODE_HISTORY);
   LastClosedTradePrice=OrderOpenPrice(); 
}
 
 
int start()
{
 
OrderSelect(0,SELECT_BY_POS,MODE_HISTORY);
if(OrderOpenPrice()!=LastClosedTradePrice)
{
   Print("OpenPrice:", OrderOpenPrice(),
         "  OpenTime:", OrderOpenTime(), 
         "  ClosePrice:", OrderClosePrice(),
         "  CloseTime:", OrderCloseTime());
   LastClosedTradePrice = OrderOpenPrice();
}  
 
return;
}
But this isn't working.  Anybody know why?  Perhaps I can't call OrderSelect from init()?  Or the most recently closed trade isn't position 0?
 

Well, test it.

For example, in init() print the ticket number and see what you get.

 
It selects the oldest order for some reason.  I need a way to find out how many orders there are in the terminal so I can reference the newest one.
 
I suppose I could sort through all the orders and look for the biggest number from Time(), but then I would still need to know how many closed orders are in the terminal to set up the For Loop.  Wish there was some kind of OrdersTotal for closed orders.
 
Reason: