OnTradeTransaction Event is triggered 4 times when closing one position - page 2

 

Sorry, I didn't understand exactly what you meant. I thought that you meant to change the magic number directly before closing an already open position (that already has a magic number). In my opinion it's not possible to change a magic number of an open position.

Of course I set a magic number corresponding to the strategy before I open the position. This is essential to my EA:


            if( (TTActive[i_TradeType] == 1) && (TTOrderTypeOpen[i_TradeType] == 1) ) {

               m_trade.SetExpertMagicNumber (i_TradeType);
               
               // ...
               
               if (m_trade.Buy (TTLotsCurrent[i_TradeType], NULL, AskPrice, TTStopLossPrice[i_TradeType], TakeProfitPriceOpenPosition, TTstrTradeType[i_TradeType] )) {
                  BuyOpened = 1;
                  Print("Open real buy position type: ", i_TradeType);
               }

               else {
                  Print("Error opening real buy position: ", i_TradeType, ": ",GetLastError());
               }
            }

Could this be a bug in MT5 / OnTradeTransaction?

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 name, return type, composition of parameters (if there are any) and their types must strictly conform to the description of the event handler function. The event handler of the client terminal...
 
Jorg1:

Sorry, I didn't understand exactly what you meant. I thought that you meant to change the magic number directly before closing an already open position (that already has a magic number). In my opinion it's not possible to change a magic number of an open position.

Of course I set a magic number corresponding to the strategy before I open the position. This is essential to my EA:


Could this be a bug in MT5 / OnTradeTransaction?

Forum on trading, automated trading systems and testing trading strategies

OnTradeTransaction Event is triggered 4 times when closing one position

Vladimir Karputov, 2020.07.09 12:09

You have two strategies. Strategy # 0 has Magic '1556', and strategy # 1 has Magic '7548'.

If you want to close a position that was opened according to Strategy # 0 - you must assign the object m_trade Magic '1556'

m_trade.SetExpertMagicNumber(1556);

, if you want to close the position that was opened according to Strategy # 1 - you must assign the object m_trade Magic ' 7548 '

m_trade.SetExpertMagicNumber(7548);

Did you do it? No, you didn’t do this.


Step 1: m_trade.SetExpertMagicNumber(Strategy #);

Step 2: m_trade.PositionClose(

 

I'm doing it the way you described:

            // Close the position
            m_position.SelectByTicket (TicketNo);
            
            if(m_trade.PositionClose(m_position.Ticket())) {
                //...
            }

I think it's a bug that the magic number when closing the position in OnTradeTransaction is set wrong by MT5.

Bug description: Instead of setting it to the magic number of the transaction that closes the position, it is set to the magic number of the last transaction that opened a position.

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 name, return type, composition of parameters (if there are any) and their types must strictly conform to the description of the event handler function. The event handler of the client terminal...
 
Jorg1:

I'm doing it the way you described:

I think it's a bug that the magic number when closing the position in OnTradeTransaction is set wrong by MT5.

Bug description: Instead of setting it to the magic number of the transaction that closes the position, it is set to the magic number of the last transaction that opened a position.

You haven’t done anything - you are not stubbornly listening to me.  I gave you advice three times - but you are not doing anything.  Do not dare to use the word "bug" - if you do not know how to listen.
 

OK, you are right. Thanks for waking me up.

There is no bug. It works perfect if I set the magic number directly before closing the position.

I ran with my head against the wall several hours then it's harder to listen and to think.