OrderClose - Error 4051

 

Hi there,

I manually opened a BUY order and ran the below code:

void OnStart()
  {
//---
   for(int i=OrdersTotal();i>0; i--)
     {
      Print("OrdersTotal: ",OrdersTotal());
      
      if(OrderSelect(i,SELECT_BY_POS)==false) Print("Unable to close order. Error: ",GetLastError());
      
      Print("Ticket: ",OrderTicket());
      Print("Lots ",OrderLots());

      if(OrderType()==0)//BUY
        {
         if(OrderClose(OrderTicket(),OrderLots(),Bid,4,Red)==false) Print("Unable to close order. Error: ",GetLastError());
        }
      else if(OrderType()==1)//SELL
        {
         if(OrderClose(OrderTicket(),OrderLots(),Ask,4,Red)==false) Print("Unable to close order. Error: ",GetLastError());
        }
     }
  }

 It generated the below output:

 

 The trade tab shows an active order of 0.01 lots:

 

 What changes do I need to make to be able to delete active orders please?

Kind regards. 

 
for(int i=OrdersTotal();i>0; i--)

Change to

for(int i=OrdersTotal()-1;i>=0; i--)

 .

 
for(int i = 0; i < OrdersTotal(); i++)

i should be 0 and count upto OrdersTotal()

Just so you know, your OrderSelect(i, SELECT_BY_POS) starts from 0, if you have 1 order, selecting order 1 wont exist because its indexed from 0 like most arrays.

 

Keelan:

for(int i = 0; i < OrdersTotal(); i++)

 i should be 0 and count upto OrdersTotal()


 Absolutely not!!

 When closing orders ALWAYS count down. 

 
In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading) you must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
 

Thanks. 

 
Why? i iterate through my Orders in ascending order and nothings broke yet, dont have to use FIFO or LIFO? or is it an mql thing? 
 
Keelan:
Why? i iterate through my Orders in ascending order and nothings broke yet, dont have to use FIFO or LIFO? or is it an mql thing? 
WHRoeder posted a link that will tell you why
 
Hi im new..

there is a function to selecting a symbol :

SymbolSelect();

i dont know how to use it in order to select for example EURUSD symbol, can you please write exactly the code which select the symbol EURUSD ?

thanks
 
mahmood0:
Hi im new..

there is a function to selecting a symbol :

SymbolSelect();

idont know how to use it in order to select for example EURUSD symbol,can you please write exactly the code which select the symbol EURUSD ?

thanks

I am wasting a lot of time deleting your replicated posts.

Don't double post

You are banned for 1 month and lucky that it is not permanent!

Reason: