Are they different? for(cnt=0;cnt<total;cnt++) compare to (cnt=o;cnt<=total;cnt++)?

 
for(cnt=0;cnt<total;cnt++) compare to (cnt=0;cnt<=total;cnt++)?

As I was trying to use CloseAll(), I found that the typical one (first one) provided by CodersGuru didnot manage to close all
and one open trade was left unclose.
in addition; any different for: OrderSelect(0, SELECT_BY_POS); vs OrderSelect(cnt,SELECT_BY_POS); ??
Any reply is very appreciated. TQ.
 
both of them are bad because they are going from 0 to orderstotal. if you have, for example 5 trades 0,1,2,3,4 and you start closing with for(cnt=0;cnt<total;cnt++) then you expect that all trades have 'til end the same number of trade but after closing first trade you don't have 1,2,3,4 but 0,1,2,3 and so on. so if you are going from 0 up you should use OrderSelect(0, SELECT_BY_POS); rather than OrderSelect(cnt, SELECT_BY_POS); as it always takes 0 position to close. you can avoid it coming from total back to zero or for(cnt=total;cnt>=0;cnt--) so you get a row of 0, 1,2,3 // 0,1,2 and so on.

At the first moment it looks like the same but consider a situation where you don't want to close all of them but only some of trades. If you are going from 0 up some trades are not gonna be closed although they should.

hope it helps!

zolero

P.S. get use to thinking "from end to beginning" kind of style. there are lots of places where you have to thing that way...
 
Hi pwfx,

I'm writing an article about this subject right now! I'll inform you with the link when I finish it!
 
You can use this code to close all of the orders:

int start()
  {
   if(Bars < 100) {Print("bars less than 100"); return(0);}
   
   double MA = iMA(NULL,0,13,0,MODE_EMA,PRICE_CLOSE,0);
   if (MA == Close[0]) CloseAll()
   
   return(0);
  }
 
void CloseAll()
{
int total = OrdersTotal();
 
   for (int cnt = 0 ; cnt <= total ; cnt++)
   {
      OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
      if (OrderMagicNumber() == MagicNumber)
      {
         if(OrderType()==OP_BUY)
            OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Violet);
         if(OrderType()==OP_SELL) 
            OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Violet);
      }
   }
}
 
zolero wrote:
both of them are bad because they are going from 0 to orderstotal. if you have, for example 5 trades 0,1,2,3,4 and you start closing with for(cnt=0;cnt<total;cnt++) then you expect that all trades have 'til end the same number of trade but after closing first trade you don't have 1,2,3,4 but 0,1,2,3 and so on. so if you are going from 0 up you should use OrderSelect(0, SELECT_BY_POS); rather than OrderSelect(cnt, SELECT_BY_POS); as it always takes 0 position to close. you can avoid it coming from total back to zero or for(cnt=total;cnt>=0;cnt--) so you get a row of 0, 1,2,3 // 0,1,2 and so on.

At the first moment it looks like the same but consider a situation where you don't want to close all of them but only some of trades. If you are going from 0 up some trades are not gonna be closed although they should.

hope it helps!

zolero

P.S. get use to thinking "from end to beginning" kind of style. there are lots of places where you have to thing that way...

Thanks a lot. but I don't quite understand what u meant by "but after closing first trade you don't have 1,2,3,4 but 0,1,2,3 and so on."
and
"you can avoid it coming from total back to zero or for(cnt=total;cnt>=0;cnt--) so you get a row of 0,1,2,3 // 0,1,2 and so on."
What's your suggestion if I want to close all of them?

and how's the code like if I want to close just 3 specific pair out of 5 pairs (with condition the total profit of these 3 pair is more than say 200?
 
pwfx wrote:
zolero wrote:
both of them are bad because they are going from 0 to orderstotal. if you have, for example 5 trades 0,1,2,3,4 and you start closing with for(cnt=0;cnt<total;cnt++) then you expect that all trades have 'til end the same number of trade but after closing first trade you don't have 1,2,3,4 but 0,1,2,3 and so on. so if you are going from 0 up you should use OrderSelect(0, SELECT_BY_POS); rather than OrderSelect(cnt, SELECT_BY_POS); as it always takes 0 position to close. you can avoid it coming from total back to zero or for(cnt=total;cnt>=0;cnt--) so you get a row of 0, 1,2,3 // 0,1,2 and so on.

At the first moment it looks like the same but consider a situation where you don't want to close all of them but only some of trades. If you are going from 0 up some trades are not gonna be closed although they should.

hope it helps!

zolero

P.S. get use to thinking "from end to beginning" kind of style. there are lots of places where you have to thing that way...

Thanks a lot. but I don't quite understand what u meant by "but after closing first trade you don't have 1,2,3,4 but 0,1,2,3 and so on."
and
"you can avoid it coming from total back to zero or for(cnt=total;cnt>=0;cnt--) so you get a row of 0,1,2,3 // 0,1,2 and so on."
What's your suggestion if I want to close all of them?

and how's the code like if I want to close just 3 specific pair out of 5 pairs (with condition the total profit of these 3 pair is more than say 200?


pwfx, You might think of a stack of plates at a cafeteria. If you take one off the top, the next one pops up but if you take one off the bottom (if you could), then the rest fall down leaving and empty place where the top plate was. When closing orders you work off the top of the list going down so that by the time you get to the last one (slot 0) you cover them all. If you try to work from the bottom down, the list would keep readjusting its numbers so that you would always need to take off the zero position.

If you want to close all of them use this script to close only the orders associated with the symbol on this chart (delete the && OrderSymbol()==Symbol() to close all open orders regardless of Symbol

int start()
{
bool flag;

int orderType;
int Error;

for(int cnt=OrdersTotal()-1; cnt>=0; cnt--)
{
if(OrderSelect(cnt,SELECT_BY_POS) && OrderSymbol()==Symbol())
{
Error = 0;
orderType = OrderType();
if(orderType == OP_SELL)
{
flag = OrderClose(OrderTicket(), OrderLots(), Ask, 3);
if (flag == false)
Error = GetLastError();
}

if(orderType == OP_BUY)
{
flag = OrderClose(OrderTicket(), OrderLots(), Bid, 3);
if (flag == false)
Error = GetLastError();
}

if(Error != 0)
{
if (Error == 135)
{
RefreshRates();
if(orderType == OP_SELL)
flag = OrderClose(OrderTicket(), OrderLots(), Ask, 3);

if(orderType == OP_BUY)
flag = OrderClose(OrderTicket(), OrderLots(), Bid, 3);
}
else
Alert("Error closing order ", OrderTicket(), " error ", Error);
}
}
}

return(0);
}

 
ScottB wrote:
pwfx wrote:
Thanks a lot. I gonna try it now. TQ.

pwfx, You might think of a stack of plates at a cafeteria. If you take one off the top, the next one pops up but if you take one off the bottom (if you could), then the rest fall down leaving and empty place where the top plate was. When closing orders you work off the top of the list going down so that by the time you get to the last one (slot 0) you cover them all. If you try to work from the bottom down, the list would keep readjusting its numbers so that you would always need to take off the zero position.

If you want to close all of them use this script to close only the orders associated with the symbol on this chart (delete the && OrderSymbol()==Symbol() to close all open orders regardless of Symbol

int start()
{
bool flag;

int orderType;
int Error;

for(int cnt=OrdersTotal()-1; cnt>=0; cnt--)
{
if(OrderSelect(cnt,SELECT_BY_POS) && OrderSymbol()==Symbol())
{
Error = 0;
orderType = OrderType();
if(orderType == OP_SELL)
{
flag = OrderClose(OrderTicket(), OrderLots(), Ask, 3);
if (flag == false)
Error = GetLastError();
}

if(orderType == OP_BUY)
{
flag = OrderClose(OrderTicket(), OrderLots(), Bid, 3);
if (flag == false)
Error = GetLastError();
}

if(Error != 0)
{
if (Error == 135)
{
RefreshRates();
if(orderType == OP_SELL)
flag = OrderClose(OrderTicket(), OrderLots(), Ask, 3);

if(orderType == OP_BUY)
flag = OrderClose(OrderTicket(), OrderLots(), Bid, 3);
}
else
Alert("Error closing order ", OrderTicket(), " error ", Error);
}
}
}

return(0);
}


Thanks a lot. I gonna try it now. TQ. but is seem a bit long, can't we simplify it without those "Error" alert. What are the functions of "flag" here for?
 
pwfx wrote:
ScottB wrote:
pwfx wrote:
Thanks a lot. I gonna try it now. TQ.

pwfx, You might think of a stack of plates at a cafeteria. If you take one off the top, the next one pops up but if you take one off the bottom (if you could), then the rest fall down leaving and empty place where the top plate was. When closing orders you work off the top of the list going down so that by the time you get to the last one (slot 0) you cover them all. If you try to work from the bottom down, the list would keep readjusting its numbers so that you would always need to take off the zero position.

If you want to close all of them use this script to close only the orders associated with the symbol on this chart (delete the && OrderSymbol()==Symbol() to close all open orders regardless of Symbol

int start()
{
bool flag;

int orderType;
int Error;

for(int cnt=OrdersTotal()-1; cnt>=0; cnt--)
{
if(OrderSelect(cnt,SELECT_BY_POS) && OrderSymbol()==Symbol())
{
Error = 0;
orderType = OrderType();
if(orderType == OP_SELL)
{
flag = OrderClose(OrderTicket(), OrderLots(), Ask, 3);
if (flag == false)
Error = GetLastError();
}

if(orderType == OP_BUY)
{
flag = OrderClose(OrderTicket(), OrderLots(), Bid, 3);
if (flag == false)
Error = GetLastError();
}

if(Error != 0)
{
if (Error == 135)
{
RefreshRates();
if(orderType == OP_SELL)
flag = OrderClose(OrderTicket(), OrderLots(), Ask, 3);

if(orderType == OP_BUY)
flag = OrderClose(OrderTicket(), OrderLots(), Bid, 3);
}
else
Alert("Error closing order ", OrderTicket(), " error ", Error);
}
}
}

return(0);
}


Thanks a lot. I gonna try it now. TQ. but is seem a bit long, can't we simplify it without those "Error" alert. What are the functions of "flag" here for?
You are right, this code can be simplified; it is from a production system that was written very explicitly so other people could maintain it. The OrderClose returns a bool; true if it worked, false if it failed. Many operations reset GetLastError to 0, so I saved the results from GetLastError in the variable Error immediately upon getting a false from the OrderClose.

The flag variable can be eliminated by coding: if (!OrderClose(OrderTicket(), OrderLots(), Bid,3)) Error = GetLastError(); Then process the error condition. I think the flag variable falls under the general heading - it seemed like a good idea at 2 am when I probably wrote it. As with a lot of my code; it can be improved. Thanks for the suggestion; I have been a programmer for over 30 years and yet I learn something new everyday.

Scott
 
In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
  1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
              Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
    For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
  2. and check OrderSelect in case earlier positions were deleted.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.
 
ScottB:

int start()
{

:

When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your (original) post.
          General rules and best pratices of the Forum. - General - MQL5 programming forum
          Messages Editor
Reason: