OnTradeTransaction()

 
Hello 

I need to know if an order is stopped by a stoploss or take profit. by the server.

I think it is possible in the event OnTradeTransaction () but I can not find the right syntax and magic ticket  and order ticket = 0? 

Does anyone has an example or a tutorial ? 

thank you for your help
 
zartan:
Hello 

I need to know if an order is stopped by a stoploss or take profit. by the server.

I think it is possible in the event OnTradeTransaction () but I can not find the right syntax and magic ticket  and order ticket = 0? 

Does anyone has an example or a tutorial ? 

thank you for your help
An Order cannot be closed by a StopLoss,  infact an Order cannot be closed . . . it can be deleted.  A Position can be closed by a StopLoss . . .  this thread may help:  https://www.mql5.com/en/forum/11996
 

This nice article may help you too: https://www.mql5.com/en/articles/211

Orders, Positions and Deals in MetaTrader 5
Orders, Positions and Deals in MetaTrader 5
  • 2011.02.01
  • MetaQuotes Software Corp.
  • www.mql5.com
Creating a robust trading robot cannot be done without an understanding of the mechanisms of the MetaTrader 5 trading system. The client terminal receives the information about the positions, orders, and deals from the trading server. To handle this data properly using the MQL5, it's necessary to have a good understanding of the interaction between the MQL5-program and the client terminal.
 

thank's

i use this code but in cannot read the ticket to read ithe closed order n the history

 an idea ?

//+------------------------------------------------------------------+
//|                                           OnTradeTransaction.mqh |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
//+------------------------------------------------------------------+
//| defines                                                          |
//+------------------------------------------------------------------+
// #define MacrosHello   "Hello, world!"
// #define MacrosYear    2010
//+------------------------------------------------------------------+
//| DLL imports                                                      |
//+------------------------------------------------------------------+
// #import "user32.dll"
//   int      SendMessageA(int hWnd,int Msg,int wParam,int lParam);
// #import "my_expert.dll"
//   int      ExpertRecalculate(int wParam,int lParam);
// #import
//+------------------------------------------------------------------+
//| EX5 imports                                                      |
//+------------------------------------------------------------------+
// #import "stdlib.ex5"
//   string ErrorDescription(int error_code);
// #import
//+------------------------------------------------------------------+
if (_Symbol!=trans.symbol) return;

  if (result.retcode == TRADE_RETCODE_DONE) AffGrosMessage("C'est un ordre serveur !!!!!!!!!!!");

//--- get transaction type as enumeration value 
   ENUM_TRADE_TRANSACTION_TYPE type=(ENUM_TRADE_TRANSACTION_TYPE)trans.type;
//--- if the transaction is the request handling result, only its name is displayed
//   if(type==TRADE_TRANSACTION_REQUEST)
//     {
      Print(EnumToString(type));
      //--- display the handled request string name
      Print("------------RequestDescription\r\n",RequestDescription(request));
      //--- display request result description
      Print("------------ResultDescription\r\n",TradeResultDescription(result));
      //--- store the order ticket for its deletion at the next handling in OnTick()
      if(result.order!=0)
        {
         //--- delete this order by its ticket at the next OnTick() call
 //        order_ticket=result.order;
 //        Print(" Pending order ticket ",order_ticket,"\r\n");
        }
     }
//   else // display the full description for transactions of another type
//--- display description of the received transaction in the Journal
//     Print("------------TransactionDescription\r\n",TransactionDescription(trans));
 
//---     
//  }
//+------------------------------------------------------------------+
//| Returns transaction textual description                          |
//+------------------------------------------------------------------+
string TransactionDescription(const MqlTradeTransaction &trans)
  {
//--- 
   string desc=EnumToString(trans.type)+"\r\n";
   desc+="Symbol: "+trans.symbol+"\r\n";
   desc+="Deal ticket: "+(string)trans.deal+"\r\n";
   desc+="Deal type: "+EnumToString(trans.deal_type)+"\r\n";
   desc+="Order ticket: "+(string)trans.order+"\r\n";
   desc+="Order type: "+EnumToString(trans.order_type)+"\r\n";
   desc+="Order state: "+EnumToString(trans.order_state)+"\r\n";
   desc+="Order time type: "+EnumToString(trans.time_type)+"\r\n";
   desc+="Order expiration: "+TimeToString(trans.time_expiration)+"\r\n";
   desc+="Price: "+StringFormat("%G",trans.price)+"\r\n";
   desc+="Price trigger: "+StringFormat("%G",trans.price_trigger)+"\r\n";
   desc+="Stop Loss: "+StringFormat("%G",trans.price_sl)+"\r\n";
   desc+="Take Profit: "+StringFormat("%G",trans.price_tp)+"\r\n";
   desc+="Volume: "+StringFormat("%G",trans.volume)+"\r\n";
//--- return the obtained string
   return desc;
  }
//+------------------------------------------------------------------+
//| Returns the trade request textual description                    |
//+------------------------------------------------------------------+
string RequestDescription(const MqlTradeRequest &request)
  {
//---
   string desc=EnumToString(request.action)+"\r\n";
   desc+="Symbol: "+request.symbol+"\r\n";
   desc+="Magic Number: "+StringFormat("%d",request.magic)+"\r\n";
   desc+="Order ticket: "+(string)request.order+"\r\n";
   desc+="Order type: "+EnumToString(request.type)+"\r\n";
   desc+="Order filling: "+EnumToString(request.type_filling)+"\r\n";
   desc+="Order time type: "+EnumToString(request.type_time)+"\r\n";
   desc+="Order expiration: "+TimeToString(request.expiration)+"\r\n";
   desc+="Price: "+StringFormat("%G",request.price)+"\r\n";
   desc+="Deviation points: "+StringFormat("%G",request.deviation)+"\r\n";
   desc+="Stop Loss: "+StringFormat("%G",request.sl)+"\r\n";
   desc+="Take Profit: "+StringFormat("%G",request.tp)+"\r\n";
   desc+="Stop Limit: "+StringFormat("%G",request.stoplimit)+"\r\n";
   desc+="Volume: "+StringFormat("%G",request.volume)+"\r\n";
   desc+="Comment: "+request.comment+"\r\n";
//--- return the obtained string
   return desc;
  }
//+------------------------------------------------------------------+
//| Returns the textual description of the request handling result   |
//+------------------------------------------------------------------+
string TradeResultDescription(const MqlTradeResult &result)
  {
//---
   string desc="Retcode "+(string)result.retcode+"\r\n";
   desc+="Request ID: "+StringFormat("%d",result.request_id)+"\r\n";
   desc+="Order ticket: "+(string)result.order+"\r\n";
   desc+="Deal ticket: "+(string)result.deal+"\r\n";
   desc+="Volume: "+StringFormat("%G",result.volume)+"\r\n";
   desc+="Price: "+StringFormat("%G",result.price)+"\r\n";
   desc+="Ask: "+StringFormat("%G",result.ask)+"\r\n";
   desc+="Bid: "+StringFormat("%G",result.bid)+"\r\n";
   desc+="Comment: "+result.comment+"\r\n";
//--- return the obtained string
   return desc;
 
zartan:

thank's

i use this code but in cannot read the ticket to read ithe closed order n the history

 an idea ?

Hello,

As RaptorUK already said you, with MT5 only a position can be closed by SL. I suggest you to read again the article which Figurelli gives you the link.

If you need help with your code you have to post code that compiles and has a meaning, what's the meaning of the code you posted ?

There are mainly 2 ways to detect that a position was closed by SL :

  • If you had an open position and it is now closed, check the history of deals to find the closing price of your position. This is the standard approach.
  • Use OnTradeTransaction to detect changes in your position "on the fly". But it's an advanced technique.
 

My code is a example MT5 from doc

it shows the structure MQLTradeTransaction and MQLTradeResult

but i don"t find the ticket of the closed order 

 
i'am interested  by :
the code goes in the OnTradeTransaction event, but in cannot find the wished information (id of order ...etc)
 
zartan:
i'am interested  by :
  • Use OnTradeTransaction to detect changes in your position "on the fly". But it's an advanced technique.
the code goes in the OnTradeTransaction event, but in cannot find the wished information (id of order ...etc)
Show what you have tried please.
 

i think i have understood..

i have to scan order in history with the filter symbol to find the caratéristics of the deal

Reason: