Ivan
Ivan

Reputation: 7746

How to tell if you are filled in MQL4 order?

I can't for the life of me how to tell if a pending order that has been sent and you have gotten a valid ticket, is filled in MQL4

http://book.mql4.com/trading/index

Is there a callback or, does a script have to continuously poll somehow?

Upvotes: 1

Views: 3475

Answers (2)

user3666197
user3666197

Reputation: 1

No.

Neither the Broker/Terminal ecosystem, nor the MQL4 language provide a callback once a Pending Order meets the Market Price and converts into a Trade.

Yes.

One may opt to either poll the dbPool of records in MT4/Terminal in a rather dumb manner alike a loop

int trades_total = OrdersTotal();

for ( int i = 0; i < trades_total; i++ ) {
      OrderSelect( i, SELECT_BY_POS, MODE_TRADES );
      if (       OrderSymbol() == Symbol()
         && OrderMagicNumber() == Magic
         && (      OrderType() == OP_BUYSTOP
            ||     OrderType() == OP_BUY
            )
         ) { ...

or one may create / store / maintain one's own DMA-alike bag ( array ) of record-numbers ( used as somewhat like pointers ) and associated Order attributes, which can mediate both direct access/modification ( without a prior dbPool OrderSelect() ).

The real-time maintenance an use of such an immense bag-of-records was tested as doable for low intensity HFT with hundreds thousands of active Orders ( which would be impractical to have to get handled via dbPool OrderSelect()/Order*() instrumentation ( the less in Strategy Tester multicriterial optimisation mode ).

Upvotes: 3

Tony Manso
Tony Manso

Reputation: 41

If you have the order ticket, then you can periodically check OrderType(). It will change from BUY_STOP ( OP_BUYSTOP ) to BUY ( OP_BUY ), etc. then you know that your order is filled.

int myTicket;

void OnTick()
{
  // check for order filled
  OrderSelect(myTicket, SELECT_BY_TICKET);
  int type = OrderType();
  if((type == OP_BUY) || (type == OP_SELL))
  {
    // order is filled, do something here.
  }
  else
  {
    // order is not filled yet. keep waiting.
  }
}

Upvotes: 4

Related Questions