How to implement an OnTrade() event handler for MQL4?

 

I need an OnTrade() event handler for mql4.. any suggestions on how I can code this will be appreciated.. if it has been done please point me to the code (I ran a search and found nothing!)

Thxs in advance guys

 

search google for irc_signal.mq4. It will send messages on new/changed/canceled orders and on new/filled/stopped/closed/modified trades. It has the mechanisms you are looking for. You can extract the needed code and modify it for your needs.

open it in metaeditor and see how the function

bool findChanged()

is implemented. (see below) It will call message functions that send the messages, these are the places you want to change and call your own event handlers.

///// incomplete code, only meant to illustrate what you find inside this download


/**
* cache for all active trades and orders 
* as they were found during the previous tick
*/
int active_ticket[1000];
double active_type[1000];
double active_price[1000];
double active_stoploss[1000];
double active_takeprofit[1000];
bool active_still_active[1000];
int active_total;


int start(){
   if (findChanged()){
      updateActiveOrders();
   }
}



/**
* find newly opened, changed or closed orders
* and send messages for every change. Additionally
* the function will return true if any changes were
* detected, false otherwise. 
*/
bool findChanged(){
   bool changed = false;
   int total = OrdersTotal();
   int ticket, index;
   for(int i=0; i<total; i++){
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      ticket = OrderTicket();
      index = getOrderCacheIndex(ticket);
      if (index == -1){
         // new order
         changed = true;
         messageNewOrder(ticket);
      }else{
         active_still_active[index] = true; // order is still there
         if (OrderOpenPrice() != active_price[index] ||
             OrderStopLoss() != active_stoploss[index] ||
             OrderTakeProfit() != active_takeprofit[index] ||
             OrderType() != active_type[index]){
             // already active order was changed
             changed = true;
             messageChangedOrder(index);
         }
      }
   }
   
   // find closed orders. Orders that are in our cached list 
   // from the last tick but were not seen in the previous step.
   for (index=0; index<active_total; index++){
      if (active_still_active[index] == false){
         // the order must have been closed.
         changed = true;
         messageClosedOrder(active_ticket[index]);
      }
      
      // reset all these temporary flags again for the next tick
      active_still_active[index] = false;
   }
   return(changed);
}

/**
* read in the current state of all open orders 
* and trades so we can track any changes in the next tick
*/ 
void updateActiveOrders(){
   active_total = OrdersTotal();
   for (int i=0; i<active_total; i++){
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      active_ticket[i] = OrderTicket();
      active_type[i] = OrderType();
      active_price[i] = OrderOpenPrice();
      active_stoploss[i] = OrderStopLoss();
      active_takeprofit[i] = OrderTakeProfit();
      active_still_active[i] = false; // filled in the next tick
   }
}
 
Thank you buddy... very useful!!
7bit:

search google for irc_signal.mq4. It will send messages on new/changed/canceled orders and on new/filled/stopped/closed/modified trades. It has the mechanisms you are looking for. You can extract the needed code and modify it for your needs....

 
The loop is based on new ticks? What if the market is very quiet and barely a tick comes in every minutes, will it still able to detect new opening trades once the trade is fired or it has to wait for a new tick to come in?
 
kai197 will it still able to detect new opening trades once the trade is fired or it has to wait for a new tick to come in?
Of course it has to wait, but nothing is happening so who cares
Reason: