Well Cloud you are right my n loop to avoid spikes would not function in case of multiple orders but that can be removed or I can incorporate a magic number in the EA.
I have another question and I need your help.
If I want to use OrderCloseBy function to close an existing order and open an opposite order how do I determine the Unique number of the opposite order ticket? If I give it a 0 value would the system allocate its own ticket number.
In order to fix your loop so that it will close multiple orders properly, you must change your loop from an incrementing loop to a decrementing loop.
In other words, make it start at "total number of orders minus one" and stop at "zero".
The reason is that every time you delete an order, the pool is re-indexed, so you will miss some orders in your loop.
OrderCloseBy() is used to simultaneously close two orders which oppose each other on the same symbol, saving on one spread. If there is a difference in lot size between the two opposing orders, then a new order for the difference will be opened.
So you can either store the two ticket numbers when you open the opposing orders or interrogate the orders pool looking for them afterward (checking Symbol() and OrderType() for example).
You can also interrogate the orders pool in the same way to find the ticket number of the 'remainder' order, should one have been created.