Need help to check if pending order is fulfilled using magic number to do something

 
if ((OrderMagicNumber()==0)==(OrderType()==OP_BUY)) 
{ 
   OrderSend(NULL,OP_SELLSTOP,ls3,tpSell,10,slSell,tpSellStop,NULL,1); 
}

I want my EA to only do the OrderSend if the OrderMagicNumber 0 (a pending order) is fulfilled. I can't seem to make this work.


I also tried this approach but it also failed.

if (checkOpenOrders()==true)
 {
   if (countOpenOrders() == 2) //I did 1 open order and 1 pending order. If 2 open orders are counted, the OrderSend is to be done. This doesnt work, I dont know why
   {
      OrderSend(NULL,OP_SELLSTOP,ls3,tpSell,10,slSell,tpSellStop,NULL,1);
   }

/--------------

int countOpenOrders()
{
   int countOP = 0;
   for (int x = OrdersTotal()-1; x >=0; x--)
   {
      if( OrderSelect(x, SELECT_BY_POS, MODE_TRADES) && OrderType() == OP_SELL || OrderType()==OP_BUY)
      {
         countOP++;
      }
   }
   return( countOP );
}


bool checkOpenOrders()
{
   for (int z = OrdersTotal()-1; z >=0; z--)
   {  
      if( OrderSelect(z, SELECT_BY_POS, MODE_TRADES) && OrderType() == OP_SELL || OrderType()==OP_BUY)
      return(true); 
   } 
      return(false); //no open orders for mag#
} 

I'm a beginner. I've been working on this for weeks and I'm desperate for help. Any help will be greatly appreciated!!

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
 

What's not working, exactly? Is the order not being placed or is the order not being executed?

Before making the call to OrderSend, do a ResetLastError, make the call, then Print out GetLastError. If the error value is greater than 0, check the  error codes to help you debug it.

If you're trying to do a reversal, double check the parameters for OrderSend.

Right off the bat, I see that you're using NULL as a parameter where you shouldn't, since the docs don't specify NULL as a valid parameter value. It may work, but you should use a valid parameter value, instead.

Make sure to double check your lot size is >= SYMBOL_VOLUME_MIN, <= SYMBOL_VOLUME_MAX, and a valid lot step using SYMBOL_VOLUME_STEP. You can get those values using SymbolInfoDouble.

Make sure your Entry price, TP Price, and SL price are properly normalized. If not, your order won't go through. From the OrderSend section of the docs:

Calculated or unnormalized price cannot be applied.

Or it could be that you're trying to set a take profit on the open order. In which case, you should modify the open order to include the TP using OrderModify

EDIT: OrderSend returns a ticket number if the order was successfully placed, or -1 if it fails. In your code, you're not checking for the return value of OrderSend. It's always wise to do so and have some contingency code, whether it be to retry later on or Print out an error message.
OrderModify - Trade Functions - MQL4 Reference
OrderModify - Trade Functions - MQL4 Reference
  • docs.mql4.com
OrderModify - Trade Functions - MQL4 Reference
 
  1.  if( OrderSelect(z, SELECT_BY_POS, MODE_TRADES) && OrderType() == OP_SELL || OrderType()==OP_BUY)
    Never mix and's and or's.
     A && B  || C
    Do you mean
    (A && B) || C
    Or
     A && (B || C)

  2. Your code
    bool checkOpenOrders()
    {
       for (int z = OrdersTotal()-1; z >=0; z--)
       {  
          if( OrderSelect(z, SELECT_BY_POS, MODE_TRADES) && OrderType() == OP_SELL || OrderType()==OP_BUY)
          return(true); 
       } 
          return(false); //no open orders for mag#
    } 
    Simplified
    bool checkOpenOrders()
    {
       return countOpenOrders() > 0;
    } 
  3.       OrderSend(NULL,OP_SELLSTOP,ls3,tpSell,10,slSell,tpSellStop,NULL,1);

    Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum (2020.07.25)