Wait/Pause code until limit order becomes open position

 

Hello, I use limit orders. Since it might take some time for these orders to get filled and become an open position, I want to wait/pause code until the limit order has been filled and becomes an open position to then collect the open price and open time etc. for this position. 

#include <Trade\Trade.mqh>

CTrade CT;
CT.SellLimit(Lot_Sell,LAST_PRICE)

while(CT.ResultPrice()==0) // wait...

ulong ticket=CT.ResultOrder();
double price=CT.ResultPrice();

etc...

When I used market orders I was able to receive CT.ResultPrice() and other information and just continue with the code. 

Without the while loop, my limit order is send and after a second or so it gets filled so all fine. However, when I use the while loop it gets stuck there until the strategy tester ends saying the end of the backtest is reached?

Any ideas how to handle this?

 

I found a solution for anyone interested. Probably not the best way to do it but seems to work.

First another while loop to check and wait until the limit order has become an open position:

while (!PositionSelectByTicket(ticket))
{  Sleep(1000); // Wait for 1 second before checking again
    Print("No ticket: ", PositionSelectByTicket(ticket));
}

Then I use the following code to extract the information:

 HistorySelect(start,TimeTradeServer());

 for(int t=0; t<HistoryDealsTotal(); t++)
 {  ulong deal_ticket = HistoryDealGetTicket(t);
    if(HistoryDealGetInteger(deal_ticket,DEAL_TYPE)==DEAL_TYPE_SELL)
    {  if(t==HistoryDealsTotal()-1)
       {  Print(HistoryDealGetDouble(deal_ticket,DEAL_PRICE));
           DebugBreak();
.....


 

There is no need to create pending orders in code.

  1. The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)

    Don't worry about it unless you're scalping M1 or trading news.

  2. Humans can't watch the screen 24/7, so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and just open an order.

 
Hi William, thanks for your time to reply. I am actually scalping M1-M5, so I need limit orders to be filled at specific prices. I did some more testing and for now it seems to work :-)
 
ammer.jens #:

I found a solution for anyone interested. Probably not the best way to do it but seems to work.

First another while loop to check and wait until the limit order has become an open position:

Then I use the following code to extract the information:


Your solution (while..) blocks your EA. Have a look at OnTradeTransaction().

But it is tricky :(

as not always the values of MagicNumber and the IDs of the position order and/or deal are assigned to trans., req., res.  and one act like send an order causes this function to 'pop up' between one and four times :(

Documentation on MQL5: Event Handling / OnTradeTransaction
Documentation on MQL5: Event Handling / OnTradeTransaction
  • www.mql5.com
OnTradeTransaction - Event Handling - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Hi Carl, I already had a look at OnTradeTransaction as well, but it was not clear to me how to extract the needed information. As you said, it seems to be tricky... 

Yes, you are right that the while loop blocks the EA but between sending the limit order and its filling nothing really needs to be done by my EA anyhow. Since, at least, in my case, I have only one position open at a time, so I would not block the checking for other positions stop losses or anything else like that. Maybe I am currently overseeing something but it'll pop up later then ;-)

Enjoy your day!

 
ammer.jens #:

Hi Carl, I already had a look at OnTradeTransaction as well, but it was not clear to me how to extract the needed information. As you said, it seems to be tricky... 

Yes, you are right that the while loop blocks the EA but between sending the limit order and its filling nothing really needs to be done by my EA anyhow. Since, at least, in my case, I have only one position open at a time, so I would not block the checking for other positions stop losses or anything else like that. Maybe I am currently overseeing something but it'll pop up later then ;-)

Enjoy your day!

The right solution is OnTradeTransaction(). You just have to learn how to use it correctly.

There are several articles about it, for example this one : https://www.mql5.com/en/articles/1111

MQL5 Cookbook: Processing of the TradeTransaction Event
MQL5 Cookbook: Processing of the TradeTransaction Event
  • www.mql5.com
This article considers capabilities of the MQL5 language from the point of view of the event-driven programming. The greatest advantage of this approach is that the program can receive information about phased implementation of a trade operation. The article also contains an example of receiving and processing information about ongoing trade operation using the TradeTransaction event handler. In my opinion, such an approach can be used for copying deals from one terminal to another.
Reason: