Download MetaTrader 5

How to add comment on close positions?

To add comments, please log in or register
arcull
121
arcull 2016.11.13 21:13 

Hi everyone. I' programming my own EA which opens a lot of positions on the same instrument. So far so good, but now I have trouble identifying which pairs belong together, I mean which closing position belongs to which opening position. I can add a comment when opening a position, but I can't find a way to add comment on a closing one. Do you know if this is possible at all. Much thanks in advance,

Best regards, Arcull

Carl Schreiber
6667
Carl Schreiber 2016.11.13 21:22  

The ticket number is the individual reference of the broker for each position and the so called MagicNumber is your reference.

If you use them they will solve your problem.

arcull
121
arcull 2016.11.13 22:16  

Thanks Carl, I know that, I have no problem doing that in code, however I need that info on historical entries. Because I'm evaluating EA's work, and want to check manually if it did everything as it was meant. So I need to add that info somehow on closed entry. Is there a way to do that?

Thanks again, best regards

Carl Schreiber
6667
Carl Schreiber 2016.11.13 23:08  
arcull:

Thanks Carl, I know that, I have no problem doing that in code, however I need that info on historical entries. Because I'm evaluating EA's work, and want to check manually if it did everything as it was meant. So I need to add that info somehow on closed entry. Is there a way to do that?

Thanks again, best regards

Print them out incl. ticket and magic number into a csv-file and load all in Excel? Or you give each position an individual magic-number and write it as well in the comment of the order - but this is in live trading not save, as the broker is able to change the order comment.
Osama Shaban
1464
Osama Shaban 2016.11.14 07:02  

It is impossible to add comment to the orders at closing time of it. Comments can be added/included only at the openings of the orders.

If you want to collect some data for the order, like, what was the RSI and EMA values at the moment of order open, you may put these

values in the comment itself. Otherwise, no other-way can be done to modify the comment.

You may generate a new function that traces only closed orders and send the required info to it, but this could make things more complicated.

arcull
121
arcull 2016.11.14 11:25  
Osama Shaban:

It is impossible to add comment to the orders at closing time of it. Comments can be added/included only at the openings of the orders.

If you want to collect some data for the order, like, what was the RSI and EMA values at the moment of order open, you may put these

values in the comment itself. Otherwise, no other-way can be done to modify the comment.

You may generate a new function that traces only closed orders and send the required info to it, but this could make things more complicated.

Thanks Osama. What about the comments that are filled on stop losses, I can see there something like "SL:1.1253". Are these comments filled by the broker's server? It would be great for my case to add comments on closing position, otherwise I have to do some other type of logging in a text file for instance.

Thank you for your help.

arcull
121
arcull 2016.11.14 11:28  
Carl Schreiber:
Print them out incl. ticket and magic number into a csv-file and load all in Excel? Or you give each position an individual magic-number and write it as well in the comment of the order - but this is in live trading not save, as the broker is able to change the order comment.
Thanks Carl, yes that would be my best alternative, however as mentioned, data may differ a bit, because of slippages etc...
Nguyen Nga
7144
Nguyen Nga 2016.11.14 17:41  
arcull:

Hi everyone. I' programming my own EA which opens a lot of positions on the same instrument. So far so good, but now I have trouble identifying which pairs belong together, I mean which closing position belongs to which opening position. I can add a comment when opening a position, but I can't find a way to add comment on a closing one. Do you know if this is possible at all. Much thanks in advance,

Best regards, Arcull

if MT5, you can add comment to close position.

other you can get Position ID from Deal ticket.

arcull
121
arcull 2016.11.15 13:49  
Nguyen Nga:

if MT5, you can add comment to close position.

other you can get Position ID from Deal ticket.

Hi Nguyen, I'm using MT5, please tell me how do I add comment on close position. Please paste the relevant part of MQL5 code.

Much thanks for your help.

Nguyen Nga
7144
Nguyen Nga 2016.11.15 14:41  

Hi

i get code below from CTrade, i add code about comment. you can comment anything when EA close position.

but can not if postion hit TP/SL. 

 

   m_request.comment  = PositionGetString(POSITION_COMMENT); // "Comment anything";

 

bool CTrade::PositionClose(const ulong ticket,const ulong deviation)
  {
//--- check stopped
   if(IsStopped(__FUNCTION__))
      return(false);
//--- check position existence
   if(!PositionSelectByTicket(ticket))
      return(false);
   string symbol=PositionGetString(POSITION_SYMBOL);
//--- clean
   ClearStructures();
//--- check
   if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
     {
      //--- prepare request for close BUY position
      m_request.type =ORDER_TYPE_SELL;
      m_request.price=SymbolInfoDouble(symbol,SYMBOL_BID);
     }
   else
     {
      //--- prepare request for close SELL position
      m_request.type =ORDER_TYPE_BUY;
      m_request.price=SymbolInfoDouble(symbol,SYMBOL_ASK);
     }
//--- setting request
   m_request.action   =TRADE_ACTION_DEAL;
   m_request.position =ticket;
   m_request.symbol   =symbol;
   m_request.volume   =PositionGetDouble(POSITION_VOLUME);
   m_request.magic    =m_magic;
   m_request.deviation=(deviation==ULONG_MAX) ? m_deviation : deviation;

   m_request.comment  = PositionGetString(POSITION_COMMENT); // "Comment anything";


//--- close position
   PrintFormat("PositionClose #%I64d %s %.2f",ticket,EnumToString((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)),m_request.volume);
   return(OrderSend(m_request,m_result));
  }
arcull
121
arcull 2016.11.17 18:58  
Nguyen Nga:

Hi

i get code below from CTrade, i add code about comment. you can comment anything when EA close position.

but can not if postion hit TP/SL. 

 

   m_request.comment  = PositionGetString(POSITION_COMMENT); // "Comment anything";

 

bool CTrade::PositionClose(const ulong ticket,const ulong deviation)
  {
//--- check stopped
   if(IsStopped(__FUNCTION__))
      return(false);
//--- check position existence
   if(!PositionSelectByTicket(ticket))
      return(false);
   string symbol=PositionGetString(POSITION_SYMBOL);
//--- clean
   ClearStructures();
//--- check
   if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
     {
      //--- prepare request for close BUY position
      m_request.type =ORDER_TYPE_SELL;
      m_request.price=SymbolInfoDouble(symbol,SYMBOL_BID);
     }
   else
     {
      //--- prepare request for close SELL position
      m_request.type =ORDER_TYPE_BUY;
      m_request.price=SymbolInfoDouble(symbol,SYMBOL_ASK);
     }
//--- setting request
   m_request.action   =TRADE_ACTION_DEAL;
   m_request.position =ticket;
   m_request.symbol   =symbol;
   m_request.volume   =PositionGetDouble(POSITION_VOLUME);
   m_request.magic    =m_magic;
   m_request.deviation=(deviation==ULONG_MAX) ? m_deviation : deviation;

   m_request.comment  = PositionGetString(POSITION_COMMENT); // "Comment anything";


//--- close position
   PrintFormat("PositionClose #%I64d %s %.2f",ticket,EnumToString((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)),m_request.volume);
   return(OrderSend(m_request,m_result));
  }
Hi Nguyen and sorry for late reply. This is exactly what I need, I've tested and it works ok. However I will extend the CTrade class and reimplement the PositionClose methods, rather then changing the class definition directly. Thank you once again for your help. I owe you a beer ;)
12
To add comments, please log in or register