How to get the ticket of position opened bt m_trade.buy?

 

Hi, I'm trying to migrate an expert from mql4 to mql5, but I can't get the ticket of the new position that just opened.

In mql4, OrderSend() will return the ticket automiticly, but in mql5, trade.mqh m_trade.Buy only return true or false.

Please help me about how to get the ticket of position that opened by trade.mqh function?

Thanks!

 

Mq4 and mq5 aren't compatible!

Here you can find info what to do:

https://www.mql5.com/en/articles/81
https://www.mql5.com/en/docs/migration

more of it with Google search for: site:mql5.com migration mq4 to mq5

Migrating from MQL4 to MQL5
Migrating from MQL4 to MQL5
  • www.mql5.com
This article is a quick guide to MQL4 language functions, it will help you to migrate your programs from MQL4 to MQL5. For each MQL4 function (except trading functions) the description and MQL5 implementation are presented, it allows you to reduce the conversion time significantly. For convenience, the MQL4 functions are divided into groups, similar to MQL4 Reference.
 
You Wu:

Hi, I'm trying to migrate an expert from mql4 to mql5, but I can't get the ticket of the new position that just opened.

In mql4, OrderSend() will return the ticket automiticly, but in mql5, trade.mqh m_trade.Buy only return true or false.

Please help me about how to get the ticket of position that opened by trade.mqh function?

Thanks!

You can use PositionSelectByTicket(ticket)

 
You Wu:

Hi, I'm trying to migrate an expert from mql4 to mql5, but I can't get the ticket of the new position that just opened.

In mql4, OrderSend() will return the ticket automiticly, but in mql5, trade.mqh m_trade.Buy only return true or false.

Please help me about how to get the ticket of position that opened by trade.mqh function?

Thanks!

#include <Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>

CTrade trade;
CPositionInfo m_position;

//open position
trade.Sell(0.1,NULL,0,0,0,NULL);
//Get position Ticket

ulong ticket = trade.ResultOrder();
 
You Wu:

Please help me about how to get the ticket of position that opened by trade.mqh function?

If you use the search, you can quickly find the answer yourself.

Forum on trading, automated trading systems and testing trading strategies

Position ticket not available

Fernando Carreiro, 2023.02.10 16:48

// The code below is uncheck and will not compile. It is only an sample code.

ulong posTicket = 0;

int OnInit( void ) {
   posTicket = 0;
};

void OnTick( void ) {
   if( posTicket == 0 ) {
           if( trade.Sell( lot_size, _Symbol ) ) {
              posTicket = trade.ResultOrder();
              Print( "Sell order placed with ticket number = ", posTicket );
           } else
              Print( "Sell order failed!" );
        };
};

...

 
Chioma Obunadike #:

Thanks, got it)

 
fxsaber #:

If you use the search, you can quickly find the answer yourself.



...

Thanks!

 
Topic has been moved to the section: Expert Advisors and Automated Trading
Reason: