how to catch when an order closes

To add comments, please log in or register
danny185
28
danny185  

Hello friends, I have a little question...


I need to know a event to catch when an order closes... Could anyone help me??


Thanks!!

amando
1653
amando  
Check the closetime from the last order and compare it with time current
Jack Thomas
1625
Jack Thomas  
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
  • www.mql5.com
Trading is done by sending orders to open positions using the OrderSend() function, as well as to place, modify or delete pending orders. Each trade order refers to the type of the requested operation. Trading operations are described in the ENUM_TRADE_REQUEST_ACTIONS enumeration...
Marco vd Heijden
Moderator
11474
Marco vd Heijden  
When PositionsTotal() returns one less.
Code2219 or 2319
494
Code2219 or 2319  
for handling positions you can use this : (fast write up)

void OnTrade()
{
    static int previous_open_positions = 0;
    int current_open_positions = PositionsTotal();
    if(current_open_positions != previous_open_positions)
    {
        if(current_open_positions < previous_open_positions)    // A position just got closed
        {
                previous_open_positions = current_open_positions;
                // some operations on last closed position. (or others in history.)
        }
        else                                                    // A position just got opened
        {
                previous_open_positions = current_open_positions;
                // some operations on last opened position. (or all open ones.)
        }
    }
}


OnTrade event triggers on modifies, and orders execution/cancelling too. so the codes filters the calls, to only watch for a change in position lists. tweak that if u want

Enrique Dangeroux
375
Enrique Dangeroux  
+------------------------------------------------------------------+
void OnTradeTransaction (const MqlTradeTransaction &trans,
                         const MqlTradeRequest &request,
                         const MqlTradeResult &result)
  {
  
//--- get transaction type as enumeration value 
   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
   
//--- if transaction is result of addition of the transaction in history
   if(type==TRADE_TRANSACTION_DEAL_ADD)
     {
       long     deal_ticket      = 0;
       long     deal_order       = 0;
       long     deal_time        = 0;
       long     deal_time_msc    = 0;
       long     deal_type        = -1;
       long     deal_entry       = -1;
       long     deal_magic       = 0;
       long     deal_reason      = -1;
       long     deal_position_id = 0;
       double   deal_volume      = 0.0;
       double   deal_price       = 0.0;
       double   deal_commission  = 0.0;
       double   deal_swap        = 0.0;
       double   deal_profit      = 0.0;
       string   deal_symbol      = "";
       string   deal_comment     = "";
       string   deal_external_id = "";
       
       if(HistoryDealSelect(trans.deal))
         {
          deal_ticket            = HistoryDealGetInteger(trans.deal, DEAL_TICKET);
          deal_order             = HistoryDealGetInteger(trans.deal, DEAL_ORDER);
          deal_time              = HistoryDealGetInteger(trans.deal, DEAL_TIME);
          deal_time_msc          = HistoryDealGetInteger(trans.deal, DEAL_TIME_MSC);
          deal_type              = HistoryDealGetInteger(trans.deal, DEAL_TYPE);
          deal_entry             = HistoryDealGetInteger(trans.deal, DEAL_ENTRY);
          deal_magic             = HistoryDealGetInteger(trans.deal, DEAL_MAGIC);
          deal_reason            = HistoryDealGetInteger(trans.deal, DEAL_REASON);
          deal_position_id       = HistoryDealGetInteger(trans.deal, DEAL_POSITION_ID);
  
          deal_volume            = HistoryDealGetDouble(trans.deal, DEAL_VOLUME);
          deal_price             = HistoryDealGetDouble(trans.deal, DEAL_PRICE);
          deal_commission        = HistoryDealGetDouble(trans.deal, DEAL_COMMISSION);
          deal_swap              = HistoryDealGetDouble(trans.deal, DEAL_SWAP);
          deal_profit            = HistoryDealGetDouble(trans.deal, DEAL_PROFIT);
  
          deal_symbol            = HistoryDealGetString(trans.deal, DEAL_SYMBOL);
          deal_comment           = HistoryDealGetString(trans.deal, DEAL_COMMENT);
          deal_external_id       = HistoryDealGetString(trans.deal, DEAL_EXTERNAL_ID);
         }
       else
          return;
          
       if(deal_reason!=-1)
          //DebugBreak();

       if((deal_type==DEAL_TYPE_BUY || deal_type==DEAL_TYPE_SELL) && deal_magic==InpMagic)
         {


         }

       if(deal_symbol==_Symbol && deal_magic==InpMagic)
         {
   

         }

       etc

     }


     
  }  
To add comments, please log in or register