Deal magic numbers

 

Hello,

I'm testing some systems on MT5 and I found a strange thing about History Deals.

I count profit/loss of history deals but if I filter them with my EA Magic number I can only found DEAL_ENTRY_IN and not DEAL_ENTRY_OUT.

It seems that DEAL_OUT do not have my Magic number.

I searched on internet but I didn't found information about.

Is it a normal behaviour or is it a bug?

 
Fabio Cavalloni :

Hello,

I'm testing some systems on MT5 and I found a strange thing about History Deals.

I count profit/loss of history deals but if I filter them with my EA Magic number I can only found DEAL_ENTRY_IN and not DEAL_ENTRY_OUT .

It seems that DEAL_OUT do not have my Magic number.

I searched on internet but I didn't found information about.

Is it a normal behaviour or is it a bug?

If you opened a position with an adviser (the adviser has a magic number), but then MANUALLY closed the position - then the DEAL_ENTRY_OUT transaction will have magic number "0".

This can be viewed using the script:History Deals and Orders:

History Deals and Orders
History Deals and Orders
  • www.mql5.com
The 'History Deals and Orders' script shows absolutely all properties of deals and orders from the trading history over a given time interval. Data can be displayed in the Experts tab of the terminal, as well as written to a text file (which is saved in [data folder]\MQL5\Files). If the data is to be written to a text file, the...
 

Sorry, I forgot to write that I'm working in the strategy tester.

Orders are opened by the EA (with my magic number) and also closed by the EA (using the CTrade.PositionClose class function) when a certain amount of profit or loss is reached.

 
Fabio Cavalloni :

Sorry, I forgot to write that I'm working in the strategy tester .

Orders are opened by the EA (with my magic number) and also closed by the EA (using the CTrade.PositionClose class function) when a certain amount of profit or loss is reached.

I made an example on purpose - the adviser opens a position and closes it in an hour.


And here is the result:

2019.12.13 23:54:59   |Ticket              |Order               |Time                |Time msc            |Type                |Entry               |Magic               |Reason              |Position ID        
2019.12.13 23:54:59   |2                   |2                   |2019.02.09 00:00:00 |1549670400000       |DEAL_TYPE_BUY       |DEAL_ENTRY_IN       |200                 |DEAL_REASON_EXPERT  |2                  
2019.12.13 23:54:59   |Volume              |Price               |Commission          |Swap                |Profit              |Symbol              |Comment                                  |External ID        
2019.12.13 23:54:59   |1.00                |1.13235             |0.00                |0.00                |0.00                |EURUSD              |                                         |                   
2019.12.13 23:54:59   Order:
2019.12.13 23:54:59   |Ticket              |Time setup          |Type                |State               |Time expiration     |Time done           |Time setup msc      |Time done msc       |Type filling       
2019.12.13 23:54:59   |2                   |2019.02.09 00:00:00 |ORDER_TYPE_BUY      |ORDER_STATE_FILLED  |2019.02.09 00:00:00 |2019.02.09 00:00:00 |1549670400000       |1549670400000       |ORDER_FILLING_FOK  
2019.12.13 23:54:59   |Type time           |Magic               |Reason              |Position id         |Position by id     
2019.12.13 23:54:59   |1970.01.01 00:00:00 |200                 |ORDER_REASON_EXPERT |2                   |0                  
2019.12.13 23:54:59   |Volume initial      |Volume current      |Open price          |sl                  |tp                  |Price current       |Price stoplimit    
2019.12.13 23:54:59   |1.00                |0.00                |1.13235             |0.00000             |0.00000             |1.13217             |0.00000            
2019.12.13 23:54:59   |Symbol              |Comment                                  |External id        
2019.12.13 23:54:59   |EURUSD              |                                         |                   
2019.12.13 23:54:59   
2019.12.13 23:54:59   Deal:
2019.12.13 23:54:59   |Ticket              |Order               |Time                |Time msc            |Type                |Entry               |Magic               |Reason              |Position ID        
2019.12.13 23:54:59   |3                   |3                   |2019.02.11 00:02:00 |1549843320000       |DEAL_TYPE_SELL      |DEAL_ENTRY_OUT      |200                 |DEAL_REASON_EXPERT  |2                  
2019.12.13 23:54:59   |Volume              |Price               |Commission          |Swap                |Profit              |Symbol              |Comment                                  |External ID        
2019.12.13 23:54:59   |1.00                |1.13196             |0.00                |0.00                |-39.00              |EURUSD              |                                         |                   
2019.12.13 23:54:59   Order:
2019.12.13 23:54:59   |Ticket              |Time setup          |Type                |State               |Time expiration     |Time done           |Time setup msc      |Time done msc       |Type filling       
2019.12.13 23:54:59   |3                   |2019.02.11 00:02:00 |ORDER_TYPE_SELL     |ORDER_STATE_FILLED  |2019.02.11 00:02:00 |2019.02.11 00:02:00 |1549843320000       |1549843320000       |ORDER_FILLING_FOK  
2019.12.13 23:54:59   |Type time           |Magic               |Reason              |Position id         |Position by id     
2019.12.13 23:54:59   |1970.01.01 00:00:00 |200                 |ORDER_REASON_EXPERT |2                   |0                  
2019.12.13 23:54:59   |Volume initial      |Volume current      |Open price          |sl                  |tp                  |Price current       |Price stoplimit    
2019.12.13 23:54:59   |1.00                |0.00                |1.13196             |0.00000             |0.00000             |1.13213             |0.00000            
2019.12.13 23:54:59   |Symbol              |Comment                                  |External id        
2019.12.13 23:54:59   |EURUSD              |                                         |                   
2019.12.13 23:54:59   
Files:
1.mq5  25 kb
 

Firstly, thanks as always for your help. I've tried your test EA and I obtained the same result as you, but with my EA the result is different and I don't understand why.


I'm not using the CPositionInfo and CTrade to open orders but only to close them, but from the screenshots it's clear the DEAL_IN has my magic (4444) and DEAL_OUT not

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double closedProfitPeriod()
  {
   ResetLastError();
   datetime thisPeriod = periodStart();
   if(!HistorySelect(thisPeriod,TimeCurrent()))
      GetLastError();

   double profit=0.00;
   for(int cnt= HistoryDealsTotal()-1; cnt>=0; cnt--)
     {
      ulong ticket = HistoryDealGetTicket(cnt);
      Print("Found history deal #"+(string)ticket, " Magic: "+(string)HistoryDealGetInteger(ticket,DEAL_MAGIC));
      if(HistoryDealSelect(ticket) && HistoryDealGetInteger(ticket,DEAL_MAGIC)==magicNumber && HistoryDealGetInteger(ticket,DEAL_TIME)>=thisPeriod && (HistoryDealGetInteger(ticket,DEAL_TYPE)==DEAL_TYPE_BUY || HistoryDealGetInteger(ticket,DEAL_TYPE)==DEAL_TYPE_SELL))
        {
         profit += HistoryDealGetDouble(ticket,DEAL_PROFIT) + HistoryDealGetDouble(ticket,DEAL_COMMISSION) + HistoryDealGetDouble(ticket,DEAL_SWAP);
        }
     }
   return(profit);
  }
 
Fabio Cavalloni :

Firstly, thanks as always for your help. I've tried your test EA and I obtained the same result as you, but with my EA the result is different and I don't understand why.


I'm not using the CPositionInfo and CTrade to open orders but only to close them, but from the screenshots it's clear the DEAL_IN has my magic (4444) and DEAL_OUT not

Show where you initialize the object of the trade class CTrade. Show where you assign the Magic Number to the CTrade class object.

 

I initialize the CTrade object in the top of the code, next to the end of the #include part.

This is the function I use to open trades

ulong Order_Send(string symbol, ENUM_ORDER_TYPE type, double lot, double price, int slippage, double sl, double tp, string comment, long magic, datetime expy, color arrow) {
   ResetLastError();
   MqlTradeRequest request={0};
   MqlTradeResult  result={0};
   ENUM_TRADE_REQUEST_ACTIONS action;
   ulong res;
   if( type==ORDER_TYPE_BUY || type==ORDER_TYPE_SELL ) action = TRADE_ACTION_DEAL; else action = TRADE_ACTION_PENDING;
   request.type_filling = ORDER_FILLING_IOC;
   request.action       = action;
   request.symbol       = symbol;
   request.type         = type; 
   request.volume       = lot;
   request.price        = price;
   request.deviation    = slippage;
   request.sl           = sl;
   request.tp           = tp;
   request.type_time    = ORDER_TIME_SPECIFIED;
   request.expiration   = expy;
   request.magic        = magic;
   request.comment      = comment;
   if( !OrderSend(request,result) ) Print("Order send error: "+(string)GetLastError());;
   if( type==ORDER_TYPE_BUY || type==ORDER_TYPE_SELL ) res = result.deal; else res = result.order;
   return(res);
}

And I close all my orders with a for cycle that call trade.PositionClose(oTicket);

 
Fabio Cavalloni:

I initialize the CTrade object in the top of the code, next to the end of the #include part.

This is the function I use to open trades

And I close all my orders with a for cycle that call trade.PositionClose(oTicket);

Forum on trading, automated trading systems and testing trading strategies

Deal magic numbers

Vladimir Karputov, 2019.12.18 14:34

Show where you initialize the object of the trade class CTrade. Show where you assign the Magic Number to the CTrade class object.


 

I've tried to modify my Order_Send function and using the object of class CTrade, also the DEAL_OUT has the right magic number.

   trade.SetExpertMagicNumber(magic);
   trade.SetTypeFillingBySymbol(symbol);
   trade.SetMarginMode();
   trade.SetDeviationInPoints(10);
   
   bool result = false;
   if( type==ORDER_TYPE_BUY ) result = trade.Buy(lot,symbol,price,sl,tp,comment);
   if( type==ORDER_TYPE_BUY ) result = trade.Sell(lot,symbol,price,sl,tp,comment);
   
   if( type==ORDER_TYPE_BUY_LIMIT )  result = trade.BuyLimit(lot,price,symbol,sl,tp,ORDER_TIME_SPECIFIED,expy,comment);
   if( type==ORDER_TYPE_SELL_LIMIT ) result = trade.SellLimit(lot,price,symbol,sl,tp,ORDER_TIME_SPECIFIED,expy,comment);


I don't understand why but now it work as expected.

 
Fabio Cavalloni :


I don't understand why but now it work as expected.

Because there is a magic line:

trade.SetExpertMagicNumber(magic);
 
Vladimir Karputov:

Because there is a magic line:

In my function there is the line

request.magic        = magic;

I though that was the same thing.

And for the DEAL_IN is it correct, I don't know how this can affect DEAL when are closed (DEAL_OUT).