Download MetaTrader 5
To add comments, please log in or register
Deposit to your account via Visa QIWI Wallet
giovi61
11
giovi61 2014.12.27 23:41 
Hi, I am creating an EA but I have a problem.

I created two functions to close all opened positions Buy and Sell , but I do not understand why sometimes not all positions are closed by running this code:


//-------------------------------------------
//  this function close all Sell positions
//-------------------------------------------

void CloseAllSell()
  {
  for(int i=0; i<OrdersTotal(); i++) 
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
         break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
        {
         if(OrderType()==OP_SELL)
           {
               OrderClose( OrderTicket(), OrderLots(), Ask, 5, Red );
           }
        }
     }

  }

//-------------------------------------------
//  this function close all Buy positions
//-------------------------------------------

void CloseAllBuy()
  { 
  for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
         break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
        {
         if(OrderType()==OP_BUY)
           {         
            OrderClose( OrderTicket(), OrderLots(), Bid, 5, Red );
           }

        }
     }

  }

I'm doing something wrong?


Keith Watford
Moderator
8006
Keith Watford 2014.12.28 02:17  

This has been covered many many times, and recently as well.

Do not count up in the loop 

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

 Count down

for(int i=OrdersTotal()-1; i>=0; i--)
whroeder1
13644
whroeder1 2014.12.28 02:42  
  1. Count down Loops and Closing or Deleting Orders - MQL4 forum
  2. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.
giovi61
11
giovi61 2014.12.28 08:49  
I tried this solution but the problem still persists
Keith Watford
Moderator
8006
Keith Watford 2014.12.28 10:32  
giovi61:
I tried this solution but the problem still persists
OrderClose is a boolean function, so check whether it returns true or false. If false, GetLastError and print to find out why it failed
/
To add comments, please log in or register