Please fix this error - OrderDelete error 4108 - unknown ticket 84 for OrderDelete function - MT4

 
         // delete pending order
         if (OrderDelete(ticket))
             return(1); 
         }
 
  1. Why are you repeatedly (for) selecting the same ticket?

    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 / position 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.

  2. Less than or equal to OP_BUY is not checking for an opened position, it is checking for a buy. Less than or equal to OP_SELL is checking for either buy or sell.

  3. There is no need to create pending orders in code.

    1. The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)

      Don't worry about it unless you're scalping M1 or trading news.

    2. Humans can't watch the screen 24/7, so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and just open an order.

  4. 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).

  5. Don't double post! You already had another thread open.

              General rules and best pratices of the Forum. - General - MQL5 programming forum (2017)

    Your question is identical to Close or Delete Pending Order When Buy Order or Sell Order Executed - MQL4 programming forum

 
MR.:

I think 4108 means its already closed (deleted , i had a script doing a while loop for killing pending orders and it run into 4108s), if you encounter it assume the order is closed .

Reason: