OrderClose - Error 4051

Back to topics list To post a new topic, please log in or register
avatar
77
brad 2015.12.07 09:02
 

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. 

avatar
Moderator
3407
GumRai 2015.12.07 09:29 #
 
for(int i=OrdersTotal();i>0; i--)

Change to

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

 .

avatar
70
Keelan 2015.12.07 12:04 #
 
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.

 

avatar
Moderator
3407
GumRai 2015.12.07 12:17 #
 

Keelan:

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

 i should be 0 and count upto OrdersTotal()


 Absolutely not!!

 When closing orders ALWAYS count down. 

 

avatar
12985
WHRoeder 2015.12.07 14:18 #
 
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
avatar
77
brad 2015.12.09 07:05 #
 

Thanks. 

avatar
70
Keelan 2015.12.09 19:47 #
 
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? 
avatar
Moderator
3407
GumRai 2015.12.09 21:56 #
 
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
avatar
1
mahmood0 2015.12.14 11:40 #
 
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
avatar
Moderator
3407
GumRai 2015.12.14 11:50 #
 
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!

Back to topics list  

To add comments, please log in or register