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
 

Hi, I am having an issue with this ResultOrder() approach.

It works on most terminals (Brokers?) but now I have a case, where 

ResultOrder() returns NOT the position ticket.

Maybe it is the ID of ther order submitted and executed. But this new behaviour breaks the logic.


Is there a way to make sure, that 

ResultOrder() REALLY returns the position ticket number?


Shall we use now OrderGetInteger(ORDER_POSITION_ID) ? 


😟

 
Eugen Funk #:

Hi, I am having an issue with this ResultOrder() approach.

It works on most terminals (Brokers?) but now I have a case, where 

ResultOrder() returns NOT the position ticket.

Maybe it is the ID of ther order submitted and executed. But this new behaviour breaks the logic.


Is there a way to make sure, that 

ResultOrder() REALLY returns the position ticket number?


Shall we use now OrderGetInteger(ORDER_POSITION_ID) ? 


😟

After a trade request is sent, it results on an Order on broker, not a position...

The execution of that order generate the position. 

In MT4 the OrderSend is asynchronous and wait the confirmation from the server (which happens when trade is placed on market)

In MT5 it can be set as sync or async, but even in the async mode, it wait the confirmation which happens at the generation of Order, not position.

The real big difference between MT4 and MT5 is in how they treat trades: in MT4 you have Orders only, In MT5 you have Orders, Deals and Positions.