Coding my first EA. I'm having trouble checking if the a pending order has been activated.

 
// tradeExecuted is a boolFlag to check if pending order has been placed
if(tradeExecuted) {
    // Check if the pending order has been activated
    if (orderResult.order > 0 && OrderSelect(orderResult.order) && OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_BUY) {
            Print("order has been activated.");
            
        } 
}

My MQL5 script places pending orders. I later want to be able to modify them ones activated. I've therefore made a couple of if statements in the "OnTick()" function. My desired result would be that the script prints "Order has been activated." once in the loop but it is not working. 


Can anyone help me to se what i've done wrong? I've provided the code I think. this is the first time I'm posting here so sorry if I'm breaking any community rules. :)

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
  • www.mql5.com
Trading is done by sending orders to open positions using the OrderSend() function, as well as to place, modify or delete pending orders. Each...
 
I'll give a lazy answer first -  if you tick visual mode in the strategy tester, you can see what happens when the bid or ask price hits the pending order. It should turn into a buy or sell position
 
Conor Mcnamara #:
hen the bid or ask price hits the pending ord
Hi Conor, thank you for your reply. I know that but it is unfortunately not helping. I've attached the code now, please se above^^
 

Alright ORDER_TYPE_BUY and ORDER_TYPE_SELL are not pending orders. They might have been in MQL4, but they are instantly executed orders in MQL5. That is why the condition will never work


this will work:

void CheckOrderExistence(){

   for(int i = 0; i < OrdersTotal(); i++){
       ulong order = OrderGetTicket(i);
       if(order > 0 && OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_BUY_STOP){
           Print("Buy stop order created", order);
       }
       else if(order > 0 && OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_SELL_STOP){
           Print("Sell stop order created", order);
       }
       else if(order > 0 && OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_BUY_LIMIT){
           Print("Buy limit order created", order);
       }
       else if(order > 0 && OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_SELL_LIMIT){
           Print("Sell limit order created", order);
       }      
   }  
}
 
Conor Mcnamara #:

Alright ORDER_TYPE_BUY and ORDER_TYPE_SELL are not pending orders. They might have been in MQL4, but they are instantly executed orders in MQL5. That is why the condition will never work


this will work:

Thank you so much! How would you go about to check if these have been triggered? For example; my buy stop is triggered and now it has become a market order that has been filled and is waiting for either Sl or TP. Meaning I want to do a new loop that enables you to change the stoploss and takeprofit for an activeorder as well.
 
Felix Lehmann #:
Thank you so much! How would you go about to check if these have been triggered? For example; my buy stop is triggered and now it has become a market order that has been filled and is waiting for either Sl or TP. Meaning I want to do a new loop that enables you to change the stoploss and takeprofit for an activeorder as well.
When a order is executed it becomes a Position
You can make a loop with PositionTotal()
 
Felix Lehmann #:
Thank you so much! How would you go about to check if these have been triggered? For example; my buy stop is triggered and now it has become a market order that has been filled and is waiting for either Sl or TP. Meaning I want to do a new loop that enables you to change the stoploss and takeprofit for an activeorder as well.

An activated order simply becomes a position. So you'll do that loop:

for (int i = PositionsTotal() - 1; i >= 0; i--){
    ulong posTicket = PositionGetTicket(i);

    if (posTicket > 0) {
       if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL){
          
                // the sell stop order turned into a position, handle it here now
       }                            
    }
}
 
Conor Mcnamara #:

An activated order simply becomes a position. So you'll do that loop:

Thank you so much Conor!!!
 
Rajesh Kumar Nait #:
When a order is executed it becomes a Position
You can make a loop with PositionTotal()
Thank you Rajesh!!