How to log ClosePosition cause (SL, TP, EA)?

 

I want to implement some way to log my OpenPositions, modifyPositions (SLTP changes) and closePositions (by EA or by SLTP) to generate some metrics.

 

So I read the docs: onTrade and historySelect to implement a code to print all Deals and Orders history. The SLTP operations are in the history, but I'm unable to find a way to show the closePosition cause. There is a way to log this information? If not, there is some other way to get this information?

 

HistorySelect(0, TimeCurrent());
string   name;
uint     total=HistoryDealsTotal();
ulong    ticket=0;
double   price;
double   profit;
double volume;
datetime time;
string   symbol;
long magic;
ENUM_DEAL_TYPE     type;
ENUM_DEAL_ENTRY     entry;
for(int i = 0; i<total;i++){
   if((ticket=HistoryDealGetTicket(i))>0){
      price =HistoryDealGetDouble(ticket,DEAL_PRICE);
      volume = HistoryDealGetDouble(ticket, DEAL_VOLUME);
      time  =(datetime)HistoryDealGetInteger(ticket,DEAL_TIME);
      symbol=HistoryDealGetString(ticket,DEAL_SYMBOL);
      type  =HistoryDealGetInteger(ticket,DEAL_TYPE);
      entry =HistoryDealGetInteger(ticket,DEAL_ENTRY);
      profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
      magic=HistoryDealGetInteger(ticket,DEAL_MAGIC);
      if(price && time && symbol==Symbol()) {
         //--- create price object
         PrintFormat("DEAL>>>>> magic: %d ticket: %d, price: %f, time: %s, symbol: %s, type: %s, entry: %s, profit: %f, volume: %f",
                        magic, ticket, price, TimeToString(time), symbol, EnumToString(type),
                        EnumToString(entry), profit, volume );
      }
   }
}

total=HistoryOrdersTotal();
long price_open, price_current, sl, tp, volume_initial, volume_current;
ENUM_ORDER_TYPE order_type;
ENUM_ORDER_STATE order_state;
for(int i = 0; i<total;i++){
   if((ticket=HistoryOrderGetTicket(i))>0){
      price_open =HistoryOrderGetDouble(ticket,ORDER_PRICE_OPEN);
      price_current =HistoryOrderGetDouble(ticket,ORDER_PRICE_CURRENT);
      sl =HistoryOrderGetDouble(ticket,ORDER_SL);
      tp =HistoryOrderGetDouble(ticket,ORDER_TP);
      volume_initial = HistoryOrderGetDouble(ticket, ORDER_VOLUME_INITIAL);
      volume_current = HistoryOrderGetDouble(ticket, ORDER_VOLUME_CURRENT);
      time  =(datetime)HistoryOrderGetInteger(ticket,ORDER_TIME_DONE);
      symbol=HistoryOrderGetString(ticket,ORDER_SYMBOL);
      order_type  =HistoryOrderGetInteger(ticket,ORDER_TYPE);
      order_state=HistoryOrderGetInteger(ticket, ORDER_STATE);
      magic=HistoryOrderGetInteger(ticket,ORDER_MAGIC);
      if(price && time && symbol==Symbol()) {
         //--- create price object
         PrintFormat("ORDER>>>>> magic: %d ticket: %d, price_open: %f, price_current: %f, sl: %f, tp: %f, volume_initial: %f, volume_current: %f, time: %s, symbol: %s, type: %s, state: %s",
                        magic, ticket, price_open,price_current,sl,tp, volume_initial, volume_current, TimeToString(time), symbol, EnumToString(order_type),
                        EnumToString(order_state) );
      }
   }
}          


 

Documentation on MQL5: Language Basics / Functions / Event Handling Functions
Documentation on MQL5: Language Basics / Functions / Event Handling Functions
  • www.mql5.com
Language Basics / Functions / Event Handling Functions - Reference on algorithmic/automated trading language for MetaTrader 5
 
https://www.mql5.com/en/forum/59186
How to check if an order has been closed for stop loss
How to check if an order has been closed for stop loss
  • www.mql5.com
gives only the current price of the order symbol.