Magic Number reverting to 0 on `OnTradeTransaction` Event handler for no reason whatsoever.

 

I have seriously lost it, my patience has snapped with this thing.

I am so sick of absolutely screaming and going bat shit crazy just because MQL5 can't do something as simple as retrieve a magic number. 

It is not difficult for this thing to comprehend something so simple, but I have never been so angry in my entire as am right now with this thing.

This computer is going to be on the receiving end of an axe if MQL5 does not start going as it's told. 

I hae a programme which utilizes the `OnTradeTransaction` even handler. 

Petty simple, right?

Evidently not.

Clearly! Because that would make my life - just for change, just one - just that little bit more simpler.
Oh no!

The Issue:

I have the same symbols over multiple timeframes, I also have to identify which chart period the EA is referring to - so I declared the `ChartID` variable & I have declared this globally. 

The `ChartID` variable is the magic number, as per the trade request in my MRE. The `ChartID` is unique to the period, as well as the symbol.

Re-producable Example

bool OrderRequest(){

   MqlTradeRequest request;

   MqlTradeResult result;

   MqlTradeCheckResult confirmation;

       

   request.action       = TRADE_ACTION_DEAL;    

   request.symbol       = _Symbol;                     

   request.volume       = 1;                         

   request.price        = xxx; 

   request.sl           = xxx;                       

   request.tp           = xxx;                        

   request.deviation    = 10;                         

   request.type         = xxx;

   request.magic        = UniqueChartIdentifier;           

   request.type_filling = ORDER_FILLING_FOK;       

   request.comment      = string(request.magic); 

   if(!OrderSend(request, result)){

      string custom_subject = "Order Exection Error";

      string custom_message = "There order type could not be determined. Please confirm that you have a compliant order type and try again";

      Print(custom_message);

      return false;

   

   } 

   return true;

}

This is my OnTradeTransaction setup

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

   string   custom_subject;

   string   custom_message;

   int      custom_flags = MB_OK | MB_ICONERROR;

   if(transaction.type != TRADE_TRANSACTION_DEAL_ADD){ 

      return;

   }

   ulong latest_deal_ticket   = transaction.deal; //Deal Ticket

   ulong request_order_ticket = transaction.order; //OrderTicket

   if(!HistoryDealSelect(latest_deal_ticket)){

   

      custom_subject = "History Deal Select Error.";

      custom_message = "There was an error in selecting the deal by deal ticket in the history.";

      

      error_handler(custom_subject, custom_message, custom_flags);

      

      return;

      

   }

   

   ulong request_deal_magic   = HistoryDealGetInteger(latest_deal_ticket,DEAL_MAGIC);

   if(request_deal_magic  != UniqueChartIdentifier){
     

      Print(request_deal_magic);

      Print(request_chart_id);      

      return;  

   }   

   long request_deal_dir = HistoryDealGetInteger(latest_deal_ticket,DEAL_ENTRY); 

   if(request_deal_dir == DEAL_ENTRY_IN){  

      Print("You have made a deal, nothing should happen yet! No enails, NADA!");      

      return;  

   }   

   if(request_deal_dir == DEAL_ENTRY_OUT){
   
      Print("You should see this when exiting the position - but you never see this because the magic number reverts to 0");            

      return;   

   }
       
}



Global Declaration

long     UniqueChartIdentifier   = ChartID();


Now, I am going to ask, very calmly, very quietly and very rational, because I am so fuming right now I'm am going to have a break down. I am so tired of nothing I do ever working, I am so tired of trying so hard, just for MQL5 to decided to throw it back in my face. I am sick sick sick sick to bloody death of it.

These are the logs:

2025.04.25 13:11:20.362 2025.21.0.0 (GBPUSD,H1) Position successfully closed. Order ID: 34414939
2025.04.25 13:11:20.362 2025.21.0.0 (GBPUSD,H1) Deal Magic 0
2025.04.25 13:11:20.362 2025.21.0.0 (GBPUSD,H1) ChartID 128968169024912125


Why does the `request_deal_magic` revert to zero when the deal closes?

One more error message or issue and  it's going to get the axe.

Documentation on MQL5: Language Basics / Functions / Event Handling Functions
Documentation on MQL5: Language Basics / Functions / Event Handling Functions
  • www.mql5.com
The MQL5 language provides processing of some predefined events . Functions for handling these events must be defined in a MQL5 program; function...
 
Paul McGalloway:

How was the trade closed ? If you close it manually then the magic is 0.

You should post the relevant code. Now you posted some code and show the output log of an other code which we can't see.