How to check opened order is long or short?

 

Hi, fellow MT4 users. I have a simple logic in trading which in pseudo would be 

if(no order in the order pool)
  can long or short otherwise wait ;
if(exists opened long order) 
{
  if(situation A meets)
    close order;
}
if(exists opened short order)
{ 
  if(situation B meets)
    close order; 
}

This first if would be easy as if(OrdersTotal()==0) which means no pending or opened orders in the order pool, which is what I need.

But the second if and third are tricky cause I'd like to check "opened" orders are long or short not just orders(which include pending orders). How should I do this?

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Order Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
 
Pak Hong Poon:
use Orderselect
OrderSelect(index or ticket, flag, mode)

MODE_TRADES (default)- order selected from trading pool(opened and pending orders)

To my recollection Orderselect can't exclude pending orders.

 

Use OrderType()

It's either OP_BUY or OP_SELL

See here https://docs.mql4.com/trading/ordertype

And make sure to select the order first by using OrderSelect() function.

OrderType - Trade Functions - MQL4 Reference
OrderType - Trade Functions - MQL4 Reference
  • docs.mql4.com
OrderType - Trade Functions - MQL4 Reference
 
John Voidman:
OrderSelect(index or ticket, flag, mode)

MODE_TRADES (default)- order selected from trading pool(opened and pending orders)

To my recollection Orderselect can't exclude pending orders.



you can choose after you selected the order, that is what "select" means

 
Marco vd Heijden:

Use OrderType()

It's either OP_BUY or OP_SELL

See here https://docs.mql4.com/trading/ordertype

And make sure to select the order first by using OrderSelect() function.

I think this way still could select pending orders and check it's type what I need is just opened orders not pending ones.

 

You can simply reject any order other then OP_BUY and OP_SELL in your code.

Then it will just skip any pending orders.

They carry a different identifier.

OP_BUY - buy order,
OP_SELL - sell order,
OP_BUYLIMIT - buy limit pending order,
OP_BUYSTOP - buy stop pending order,
OP_SELLLIMIT - sell limit pending order,
OP_SELLSTOP - sell stop pending order.
 
Marco vd Heijden:

You can simply reject any order other then OP_BUY and OP_SELL in your code.

Then it will just skip any pending orders.

They carry a different identifier.

Thanks, that's new knowledge for me, much obliged.

Reason: