Deal magic numbers - page 2

 
Fabio Cavalloni :

In my function there is the line

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).

You need to stop tormenting with request, and go completely to work with trading classes.

 

When you create an object of the trade class CTrade, you must inform the object

SetExpertMagicNumber

Sets the expert ID

Documentation on MQL5: Standard Library / Trade Classes / CTrade / SetExpertMagicNumber
Documentation on MQL5: Standard Library / Trade Classes / CTrade / SetExpertMagicNumber
  • www.mql5.com
Standard Library / Trade Classes / CTrade / SetExpertMagicNumber - Reference on algorithmic/automated trading language for MetaTrader 5
 

Yes I'll move in that direction!

I also read the code of Trade.mqh and look at the SetExpertMagicNumber and I didn't found a difference in what I wrote before with my function, but as you said it's time to stop tormenting about this.

I found also some issue with CloseBy function that generate crazy balance spikes on backtest, but before opening a new topic on forum I'll check again and again the code, including classes objects.

Thanks again for your help, Vladimir! 

 
Fabio Cavalloni:

Yes I'll move in that direction!

I also read the code of Trade.mqh and look at the SetExpertMagicNumber and I didn't found a difference in what I wrote before with my function, but as you said it's time to stop tormenting about this.

I found also some issue with CloseBy function that generate crazy balance spikes on backtest, but before opening a new topic on forum I'll check again and again the code, including classes objects.

Thanks again for your help, Vladimir! 

An other time I would suggest you to post ALL the relevant code from the start (just remove the trading logic if you don't want to post it), rather than let people guess you have have done.
 

It's an EA with more than 2000 rows of code, I've tried to post only relevant part about the issue...

For sure It's better to post all information at the beginning but I never though that my order send function (that include the magic number and it works well) causes magic number to be "removed" when positions get closed. It was a very strange issue...

 
Fabio Cavalloni:

It's an EA with more than 2000 rows of code, I've tried to post only relevant part about the issue...

For sure It's better to post all information at the beginning but I never though that my order send function (that include the magic number and it works well) causes magic number to be "removed" when positions get closed. It was a very strange issue...

It's still a strange issue because there is no way to understand the problem with some snippets of code. I would like to know if it is an issue in your code or something else.

I have no idea why you are thinking using MqlTradeRequest to open would remove the magic when closing, that doesn't make sense of course.

The logic would say that if you are using CTrade to close your positions, and there is no magic number on the deal (=0), that's because you didn't set the magic number or did it incorrectly.

 

Magic number is set correctly in both cases (my custom function and using CTrade). When I use my function to open positions, looking at HistoryDeals, DEAL_IN has magic number,  and DEAL_OUT has not.

My function:

ulong Order_Send(string symbol, ENUM_ORDER_TYPE type, double lot, double price, int slippage, double sl, double tp, string comment, ulong 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); 
}

Function using CTrade:

bool Order_Send(string symbol, ENUM_ORDER_TYPE type, double lot, double price, int slippage, double sl, double tp, string comment, ulong magic, datetime expy, color arrow) {
   m_trade.SetExpertMagicNumber(magic);
   m_trade.SetTypeFillingBySymbol(symbol);
   m_trade.SetMarginMode();
   m_trade.SetDeviationInPoints(10);
   
   bool result = false;

   if( type==ORDER_TYPE_BUY )  result = m_trade.Buy(lot,symbol,price,sl,tp,comment);
   if( type==ORDER_TYPE_SELL ) result = m_trade.Sell(lot,symbol,price,sl,tp,comment);
   if( type==ORDER_TYPE_BUY_LIMIT )  result = m_trade.BuyLimit(lot,price,symbol,sl,tp,ORDER_TIME_SPECIFIED,expy,comment);
   if( type==ORDER_TYPE_SELL_LIMIT ) result = m_trade.SellLimit(lot,price,symbol,sl,tp,ORDER_TIME_SPECIFIED,expy,comment);
   
   return(result);
}
 
Fabio Cavalloni:

Magic number is set correctly in both cases (my custom function and using CTrade). When I use my function to open positions, looking at HistoryDeals, DEAL_IN has magic number,  and DEAL_OUT has not.

My function:

Function using CTrade:

I give up. Again you are posting useless snippets of code (which you already posted). We have no idea what the parameters value are, how and when these functions are called, we can't run it and try it. Nobody can help you.
 
Alain Verleyen :
I give up. ***

And I will observe. One day :)

 
Alain Verleyen:
I give up. Again you are posting useless snippets of code (which you already posted). We have no idea what the parameters value are, how and when these functions are called, we can't run it and try it. Nobody can help you.

You are wrong, Vladimir helped me and pushed me in the right direction to troubleshoot the problem and I thanks him. Again, I posted the only important part, no matter of when function of open and close are called, if I do it using MqlTradeRequest and order send I will have this results and magic number will be inexplicably removed from the DEAL_ENTRY_OUT.

KO      0       09:51:24.047    Core 1  2019.12.13 23:56:59   Deal:
OH      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |Ticket              |Order               |Time                |Time msc            |Type                |Entry               |Magic               |Reason              |Position ID        
PE      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |2                   |2                   |2019.01.01 00:00:00 |1546300800100       |DEAL_TYPE_BUY       |DEAL_ENTRY_IN       |200                 |DEAL_REASON_EXPERT  |2                  
CL      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |Volume              |Price               |Commission          |Swap                |Profit              |Symbol              |Comment                                  |External ID        
GN      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |1.00                |1.05039             |-2.75               |0.00                |0.00                |AUDNZD              |                                         |                   
IO      0       09:51:24.047    Core 1  2019.12.13 23:56:59   Order:
QF      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |Ticket              |Time setup          |Type                |State               |Time expiration     |Time done           |Time setup msc      |Time done msc       |Type filling       
HN      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |2                   |2019.01.01 00:00:00 |ORDER_TYPE_BUY      |ORDER_STATE_FILLED  |2019.01.01 00:00:00 |2019.01.01 00:00:00 |1546300800100       |1546300800100       |ORDER_FILLING_IOC  
NL      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |Type time           |Magic               |Reason              |Position id         |Position by id     
FI      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |1970.01.01 00:00:02 |200                 |ORDER_REASON_EXPERT |2                   |0                  
HJ      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |Volume initial      |Volume current      |Open price          |sl                  |tp                  |Price current       |Price stoplimit    
CM      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |1.00                |0.00                |1.05039             |0.00000             |0.00000             |1.05025             |0.00000            
PF      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |Symbol              |Comment                                  |External id        
CH      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |AUDNZD              |                                         |                   
GF      0       09:51:24.047    Core 1  2019.12.13 23:56:59   
CQ      0       09:51:24.047    Core 1  2019.12.13 23:56:59   Deal:
GF      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |Ticket              |Order               |Time                |Time msc            |Type                |Entry               |Magic               |Reason              |Position ID        
CQ      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |3                   |3                   |2019.01.02 00:00:30 |1546387230100       |DEAL_TYPE_SELL      |DEAL_ENTRY_OUT      |0                   |DEAL_REASON_EXPERT  |2                  
KN      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |Volume              |Price               |Commission          |Swap                |Profit              |Symbol              |Comment                                  |External ID        
IQ      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |1.00                |1.04973             |-2.75               |0.00                |-44.32              |AUDNZD              |                                         |                   
QL      0       09:51:24.047    Core 1  2019.12.13 23:56:59   Order:
IS      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |Ticket              |Time setup          |Type                |State               |Time expiration     |Time done           |Time setup msc      |Time done msc       |Type filling       
GS      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |3                   |2019.01.02 00:00:30 |ORDER_TYPE_SELL     |ORDER_STATE_FILLED  |2019.01.02 00:00:30 |2019.01.02 00:00:30 |1546387230100       |1546387230100       |ORDER_FILLING_IOC  
FS      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |Type time           |Magic               |Reason              |Position id         |Position by id     
NL      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |1970.01.01 00:00:00 |0                   |ORDER_REASON_EXPERT |2                   |0                  
PH      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |Volume initial      |Volume current      |Open price          |sl                  |tp                  |Price current       |Price stoplimit    
LO      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |1.00                |0.00                |1.04973             |0.00000             |0.00000             |1.05022             |0.00000            
HK      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |Symbol              |Comment                                  |External id        
KF      0       09:51:24.047    Core 1  2019.12.13 23:56:59   |AUDNZD              |                                         |                   
OI      0       09:51:24.047    Core 1  2019.12.13 23:56:59   

If I use the CTrade class object to make trade I will have this results.

KF      0       09:53:52.468    Core 1  2019.12.13 23:56:59   Deal:
OO      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |Ticket              |Order               |Time                |Time msc            |Type                |Entry               |Magic               |Reason              |Position ID        
LO      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |2                   |2                   |2019.01.01 00:00:00 |1546300800100       |DEAL_TYPE_BUY       |DEAL_ENTRY_IN       |200                 |DEAL_REASON_EXPERT  |2                  
CE      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |Volume              |Price               |Commission          |Swap                |Profit              |Symbol              |Comment                                  |External ID        
KH      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |1.00                |1.05039             |-2.75               |0.00                |0.00                |AUDNZD              |                                         |                   
IE      0       09:53:52.468    Core 1  2019.12.13 23:56:59   Order:
EL      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |Ticket              |Time setup          |Type                |State               |Time expiration     |Time done           |Time setup msc      |Time done msc       |Type filling       
HG      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |2                   |2019.01.01 00:00:00 |ORDER_TYPE_BUY      |ORDER_STATE_FILLED  |2019.01.01 00:00:00 |2019.01.01 00:00:00 |1546300800100       |1546300800100       |ORDER_FILLING_IOC  
RJ      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |Type time           |Magic               |Reason              |Position id         |Position by id     
DG      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |1970.01.01 00:00:00 |200                 |ORDER_REASON_EXPERT |2                   |0                  
LP      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |Volume initial      |Volume current      |Open price          |sl                  |tp                  |Price current       |Price stoplimit    
CD      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |1.00                |0.00                |1.05039             |0.00000             |0.00000             |1.05025             |0.00000            
LP      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |Symbol              |Comment                                  |External id        
GO      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |AUDNZD              |                                         |                   
CP      0       09:53:52.468    Core 1  2019.12.13 23:56:59   
CK      0       09:53:52.468    Core 1  2019.12.13 23:56:59   Deal:
GL      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |Ticket              |Order               |Time                |Time msc            |Type                |Entry               |Magic               |Reason              |Position ID        
IH      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |3                   |3                   |2019.01.02 00:00:30 |1546387230100       |DEAL_TYPE_SELL      |DEAL_ENTRY_OUT      |200                 |DEAL_REASON_EXPERT  |2                  
KH      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |Volume              |Price               |Commission          |Swap                |Profit              |Symbol              |Comment                                  |External ID        
MK      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |1.00                |1.04973             |-2.75               |0.00                |-44.32              |AUDNZD              |                                         |                   
QS      0       09:53:52.468    Core 1  2019.12.13 23:56:59   Order:
MI      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |Ticket              |Time setup          |Type                |State               |Time expiration     |Time done           |Time setup msc      |Time done msc       |Type filling       
GE      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |3                   |2019.01.02 00:00:30 |ORDER_TYPE_SELL     |ORDER_STATE_FILLED  |2019.01.02 00:00:30 |2019.01.02 00:00:30 |1546387230100       |1546387230100       |ORDER_FILLING_IOC  
JH      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |Type time           |Magic               |Reason              |Position id         |Position by id     
LE      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |1970.01.01 00:00:00 |200                 |ORDER_REASON_EXPERT |2                   |0                  
DF      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |Volume initial      |Volume current      |Open price          |sl                  |tp                  |Price current       |Price stoplimit    
LQ      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |1.00                |0.00                |1.04973             |0.00000             |0.00000             |1.05022             |0.00000            
DR      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |Symbol              |Comment                                  |External id        
OL      0       09:53:52.468    Core 1  2019.12.13 23:56:59   |AUDNZD              |                                         |                   
KR      0       09:53:52.468    Core 1  2019.12.13 23:56:59   

I don't know how to explain it again but I attached two EA (1_CTrade is the one made by Vladimir, the other is the same using a custom MqlTradeRequest function). Anyway, also if there are no specific reasons, the problem is solved using CTrade class.

Files:
Reason: