PositionGetTicket(PositionsTotal()-1);
I would prefer this.
Hello, I am currently using this method to get an order ticket when the order is placed successfully. Is this method the best practice? Is there any other more effective and secure way? Thank you in advance.
Most of the times on a real account the position can not be selected immediately after sending the order.
To solve this synchronously, you have to wait until the order is filled or cancelled.
If you need the order ticket and not the position ticket then you can simply use
ulong ticket=trade.ResultOrder();
This may be the most elegant way
Most of the times on a real account the position can not be selected immediately after sending the order.
To solve this synchronously, you have to wait until the order is filled or cancelled.
If you need the order ticket and not the position ticket then you can simply use
ulong ticket=trade.ResultOrder();
Another option, it quite slow. But this will probably ensure asynchrony in live test
I just test it with a simple script:
#include <Trade/Trade.mqh> //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ CTrade trade; void OnStart() { ulong time1; ulong time2; ulong ticket; ulong timeTemp = GetMicrosecondCount(); for(int i = 100000; i >= 0; --i) { ticket = PositionGetTicket(PositionsTotal()-1); } time1 = timeTemp; timeTemp = GetMicrosecondCount(); for(int i = 100000; i >= 0; --i) { ticket = trade.ResultOrder(); } time2 = timeTemp; //--- Result Print(time1, "ms"); Print(time2, "ms"); } //+------------------------------------------------------------------+
And the result
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello, I am currently using this method to get an order ticket when the order is placed successfully. Is this method the best practice? Is there any other more effective and secure way? Thank you in advance.