Deal magic numbers - page 3

 
Fabio Cavalloni :

*** if I do it using MqlTradeRequest and order send I will have this result and magic number will be inexplicably removed from the DEAL_ENTRY_OUT.

***

Understand one simple truth: no one goes anywhere !!! You used a trade request through MqlTradeRequest - assigned a magic number. So far so good. But then you INCORRECTLY close the position.

INCORRECT: this means that YOU DO NOT INITIALIZE a trading class object !!! The trading class object DOES NOT KNOW ABOUT the previous MqlTradeRequest - because it does not have a magic crystal ball.


I will repeat my advice: I urge you to completely switch to trading classes and THROW OUT ALL MqlTradeRequest - this guarantees the unity of the code and eliminates your errors.


Added: proper initialization is and

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   m_trade.SetExpertMagicNumber(InpMagic);
   m_trade.SetMarginMode();
   m_trade.SetTypeFillingBySymbol(Symbol());
   m_trade.SetDeviationInPoints(10);
 
Vladimir Karputov:

Understand one simple truth: no one goes anywhere !!! You used a trade request through MqlTradeRequest - assigned a magic number. So far so good. But then you INCORRECTLY close the position.

INCORRECT: this means that YOU DO NOT INITIALIZE a trading class object !!! The trading class object DOES NOT KNOW ABOUT the previous MqlTradeRequest - because it does not have a magic crystal ball.


I will repeat my advice: I urge you to completely switch to trading classes and THROW OUT ALL MqlTradeRequest - this guarantees the unity of the code and eliminates your errors.

I'm changing my codes to using classes, as you suggests ;)

Anyway, I closed the position with the same exact procedure, that you put in your example EA (for both the CTrade EA and the MqlTradeRequest EA).

   for(int i=PositionsTotal()-1; i>=0; i--)
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.Symbol()==Symbol() && m_position.Magic()==InpMagic)
            if(TimeCurrent()-m_position.Time()>60*60*3)
               m_trade.PositionClose(m_position.Ticket());
 
Fabio Cavalloni:

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 result and magic number will be inexplicably removed from the DEAL_ENTRY_OUT.

If you use the CTrade class object to make trade you will have this result.

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

I don't work with CTrade myself, so I hope I'm getting this right. The following may(!) be the problem:

You open the position with the custom method, but then you close using CTrade::PositionClose(). If you look at this function, it is resetting the magic number in the request in line 430 (I think this could sometimes make sense, if the EA that closes the position is different from the EA that opened it or if the position was manually opened, but closed by an EA, so this EA leaves its "signature" for the ENTRY_OUT deal):

m_request.magic    =m_magic;

However, the only part where m_magic is set is from SetExpertMagicNumber(). If you haven't used this function and then close via PositionClose, m_magic will be zero, so if you use the original PositionClose() function, you should use BOTH your code line

m_trade.SetExpertMagicNumber(InpMagic);

and your magic declaration for the position opening request 

request.magic        = InpMagic;
 
Vladimir Karputov:

Understand one simple truth: no one goes anywhere !!! You used a trade request through MqlTradeRequest - assigned a magic number. So far so good. But then you INCORRECTLY close the position.

INCORRECT: this means that YOU DO NOT INITIALIZE a trading class object !!! The trading class object DOES NOT KNOW ABOUT the previous MqlTradeRequest - because it does not have a magic crystal ball.


I will repeat my advice: I urge you to completely switch to trading classes and THROW OUT ALL MqlTradeRequest - this guarantees the unity of the code and eliminates your errors.


Added: proper initialization is and

I though that SetExpertMagicNumber is nothing else that put "magic" in MqlTradeRequest.magic parameter

 
Chris70:

I don't work with CTrade myself, so I hope I'm getting this right. The following may(!) be the problem:

You open the position with the custom method, but then you close using CTrade::PositionClose(). If you look at this function, it is resetting the magic number in the request in line 430 (I think this could sometimes make sense, if the EA that closes the position is different from the EA that opened it or if the position was manually opened, but closed by an EA, so this EA leaves its "signature" for the ENTRY_OUT deal):

However, the only part where m_magic is set is from SetExpertMagicNumber(). If you haven't used this function and then close via PositionClose, m_magic will be zero, so if you use the original PositionClose() function, you should use BOTH youor code line

and your magic declaration for the position opening request 

Your analysis makes sense.

The position is closed correctly but, due to the fact that I have not initialized m_trade.SetExpertMagicNumber(InpMagic); the request has 0 as m_trade.magic and it produces a DEAL_OUT with magic number = 0.

I thought that when a position get closed its magic number remain the same but is not in this way. Probably mql5 behavious is intended to have the possibility to use differents EA for opening and closing trades (maybe) and be aware of which EA trigger the open and which one triggers the close (analyzing magic numbers of DEAL_IN and DEAL_OUT, that can be differents).

With you and Vladimir help, I finally starting to give me an explaination about the issue.

 
Fabio Cavalloni:

I though that SetExpertMagicNumber is nothing else that put "magic" in MqlTradeRequest.magic parameter

Yes, but closing a position also in involves a request! CTrade can't know your InpMagic input variable for this closing request. Just think of the magic number as the "signature" of the entity that is responsible for a transaction, request or request modification. Usually this is all the same EA, but there are cases where it's not. But after your last post I think you get it now.

 
Chris70:

Yes, but closing a position also in involves a request! CTrade can't know your InpMagic input variable for this closing request.

Yes, probably this is the main important thing that I underestimated, that position close is nothing else that a position open with the opposite type.

 
Fabio Cavalloni :

I though that SetExpertMagicNumber is nothing else that put "magic" in MqlTradeRequest .magic parameter

Yes!

Fabio Cavalloni :

Your analysis makes sense.

The position is closed correctly but, due to the fact that I have not initialized  m_trade.SetExpertMagicNumber(InpMagic);  the request has 0 as m_trade.magic and it produces a DEAL_OUT with magic number = 0.

I thought that when a position get closed its magic number remain the same but is not in this way. Probably mql5 behavious is intended to have the possibility to use differents EA for opening and closing trades (maybe) and be aware of which EA trigger the open and which one triggers the close (analyzing magic numbers of DEAL_IN and DEAL_OUT, that can be differents).

With you and Vladimir help, I finally starting to give me an explaination about the issue.

When you create an object of the CTrade trading class, this object by default has a Magic Number equal to "0" - and this is completely normal and it is correct. After creating the object, you must assign the object Magic Number (this is done after  SetExpertMagicNumber).

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
 

Thanks, I fully understood what happens!

The main issue is that I've not initialized CTrade object with my EA magic number and I used it to close positions. Magic number is present on IN deals because I opened them with my manual function in which I specified magic number.

If I had also closed position with a manual function in which I specified my magic in the MqlTradeRequest the issue not happened, but the best thing is for sure, use classes and initialize correctly them in the OnInit, as you did in your example EA and as I will do in all mine.

Thanks again for the support, to you and to Chris70.