Can anyone explain why this code is not working !!

 

I am trying to capture in real time if any position is closed and then get its comment. not the last position in history.

whatever i try nothing seems to work,  here is an example 

can anyone give me a clue or it can not be done !


void OnTradeTransaction(const MqlTradeTransaction &trans, const MqlTradeRequest &request, const MqlTradeResult &result)

  {
    
    if (trans.type == TRADE_TRANSACTION_DEAL_ADD)
    {      
        ulong dealTicket = trans.deal;
        if (HistoryDealGetInteger(dealTicket, DEAL_ENTRY) == DEAL_ENTRY_OUT)
        {
            string lastClosedPositionComment = HistoryDealGetString(dealTicket, DEAL_COMMENT);
            Print("Closed Position Comment: ", lastClosedPositionComment);
        }
    }

}
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Mohamed Mostafa Mohamed Sonbol:

I am trying to capture in real time if any position is closed and then get its comment. not the last position in history.

whatever i try nothing seems to work,  here is an example 

can anyone give me a clue or it can not be done !


Before calling HistoryDealGetInteger you have to call HistoryDealSelect.
See also the documentation. 
Matthias 
 
Similar issue.


Ryan L Johnson #:

I suspect that this is a broker-dealer issue.

Broker-dealers can overwrite or altogether delete historic deal comments and magic numbers following stoploss execution.

When a position hits the Stop Loss, why is the magic number removed in MT5? - Stop Loss - Expert Advisors and Automated Trading - MQL5 programming forum


As a universal workaround, you could set a series of GlobalVariables (GV's) following your stoploss executions; setting the GV name (a string) to your symbol, timeframe, and/or whatever, and the GV value (a double) to the magic number. In this way, you'd have a readymade list to read in the terminal. Then you can get their values in a loop somewhat like it's an array.


MAHA #:

How about writing trade details to a file in this format:

    TRADE_POSITION_TICKET:EA_MAGIC_NUMBER
      When the stop-loss is triggered, retrieve the TRADE_POSITION_TICKET. Once the ticket is found, extract the corresponding EA_MAGIC_NUMBER.
       
      MAHA #:
      Similar issue.


      In that thread, the trade exit logic was coded into an EA. It really doesn't matter how you store/save the magic number, as long as the magic number is extracted from its definition the EA code. If you're auto-trading, then this applies.

      If however you're trading manually, then just follow the advice in Post #2.