TP/SL Hit notification [Code help]

 

Hello all! I'd like to add to some EA's a notification function that, well, notifies when a TP or SL has been hit.

I made a kind of BETA version of the actual code but I'm facing some troubles that I'm not sure how to overcome:

   for (int i = 0;i < OrdersHistoryTotal(); i++)
    {
      bool Select_Res = OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
       {
        if(OrderMagicNumber() == Magic && OrderClosePrice() == OrderStopLoss())
         {
          //Send mail code
          }
         else if(OrderMagicNumber() == Magic && OrderClosePrice() == OrderTakeProfit())
        {
       //Send mail code
      }
     }
    }

The very first problem I'm facing, is how can I make the code identify when it has already notified about a TP/SL hit? So I'll not be getting constant notification on old trades that had been closed. The only walk around I can think now is to modify the order comment and add an "...&& OrderComment() == [Modified comment]" on the Magic Number and ClosePrice part but I'm not sure if I can modify the comment of a closed order.

Any ideas?

 

FernandoBorea: The very first problem I'm facing, is how can I make the code identify when it has already notified about a TP/SL hit? So I'll not be getting constant notification on old trades that had been closed.

The only walk around I can think now is to modify the order comment and add an "...&& OrderComment() == [Modified comment]" on the Magic Number and ClosePrice part but I'm not sure if I can modify the comment of a closed order.

  1. if(OrderMagicNumber() == Magic && OrderClosePrice() == OrderStopLoss())
    Doubles are rarely equal. TP/SL becomes a market order. prices will not be equal in general.
              The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum

  2. On the first tick (start up,) find the order with the largest ticket number. That (and lower numbers) have already been notified. Ignore them.
  3. Comments can not be changed.
 
whroeder1:
  1. Doubles are rarely equal. TP/SL becomes a market order. prices will not be equal in general.
              The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum

  2. On the first tick (start up,) find the order with the largest ticket number. That (and lower numbers) have already been notified. Ignore them.
  3. Comments can not be changed.

Thanks! could you further explain me the 2. answer?:)

 

I wrote a question to the service desk some years ago

"...It is not clear to me whether all brokers incorporate the letters sp or tp in the order comment when a trade hits the StopLoss or TakeProfit. Is this at the brokers' discretion or is it automatically done with every broker?

Sometimes, when writing code, it is necessary to check whether a trade was closed by hitting the sp or tp or manually. Because of slippage, it may not always be possible to simply compare the OrderClosePrice() with OrderStopLoss() or OrderTakeProfit().

I think that it would be a really good idea to have an additional function OrderCloseAction() or something like that to return whether the trade was closed by  hitting the sp or tp or manually"

Their reply.......

"Thank you for your suggestion. Unfortunately, we have no plans to add this additional function."

"It is automatically done with every broker. And also the broker can change this value itself."


You could check whether the close price is nearer to the sl or tp.

 
Not a good idea to use comments, brokers can change comments, including complete replacement.