Invalid ticket for OrderDelete function

 

//if we already have positions, update them if required


      //selecting if we have open positions

      if(OrderSelect(orderID,SELECT_BY_TICKET) == true)

      {

     (code to move stop loss to break even

      }


      //if we have pending orders but market changed and we want to delete them

      else if(OrderSelect(orderID,SELECT_BY_TICKET) == false)

      {

         bool Ans = OrderDelete(orderID,CLR_NONE);

         if(Ans == true)

         {

            Print("Pening order cancelled");

         }

      }


I want to see what I'm doing wrong when coding the OrderDelete function



 
nonamerequired:

//if we already have positions, update them if required


      //selecting if we have open positions

      if(OrderSelect(orderID,SELECT_BY_TICKET) == true)

      {

     (code to move stop loss to break even

      }


      //if we have pending orders but market changed and we want to delete them

      else if(OrderSelect(orderID,SELECT_BY_TICKET) == false)

      {

         bool Ans = OrderDelete(orderID,CLR_NONE);

         if(Ans == true)

         {

            Print("Pening order cancelled");

         }

      }


I want to see what I'm doing wrong when coding the OrderDelete function



Seems this is MT4 code
However you have to share the error number that you are getting
 

nonamerequired:


      else if(OrderSelect(orderID,SELECT_BY_TICKET) == false)

      {

         bool Ans = OrderDelete(orderID,CLR_NONE);

         if(Ans == true)

         {

            Print("Pening order cancelled");

         }

      }


I want to see what I'm doing wrong when coding the OrderDelete function



You are only trying to delete the order if the OrderSelect fails!

Of course it won't delete anything.


Moving this to the MQL4 section.

 
  1. Only post your MT4 questions in MQL4 section, (bottom of the Root page).
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)

  2. Please edit your posts and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  3.       if(OrderSelect(orderID,SELECT_BY_TICKET) == true)
          {
         (code to move stop loss to break even
          }

    You are selecting by ticket. Where do you check if the ticket has already closed?

  4. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover?

    Use a OrderSelect / Position select loop on the first tick, or persistent storage (GV+flush or files) of ticket numbers required.

  5. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool).

Reason: