How to correct this Code ?

Suy Kok Yik
366
Suy Kok Yik  
void CloseBal()
  {
   if(AccountEquity()<=MinEquity &&  AccountEquity()>= AccountBalance()+ Maxprofit )
     {
      for(int i=0; i<OrdersTotal(); i++)
        {

The maximum profit seem doesn't work

Carl Schreiber
Moderator
11026
Carl Schreiber  
Are you sure that both elements of the if(..) are to be connected by && and not || ?
PainKillerRO
22
PainKillerRO  
Suy Kok Yik:

The maximum profit seem doesn't work

Ok, you are trying to tell the EA to close orders a when in profit && in loss? :)

void CloseBal()
  {
   if(AccountEquity()<=MinEquity || AccountEquity()>= AccountBalance()+ Maxprofit )
     {
      for(int i=0; i<OrdersTotal(); i++)
        {


This should do.
Suy Kok Yik
366
Suy Kok Yik  
PainKillerRO:

Ok, you are trying to tell the EA to close orders a when in profit && in loss? :)


This should do.
Thanks , it working now .💪
Mehmet Bastem
2116
Mehmet Bastem  
Suy Kok Yik:

You can simply close all orders this way.

void CloseBal ()
  {
  if (AccountEquity () <= MinEquity) && AccountBalance () + Mixprofit ()> = AccountEquity ())
    {
   int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    bool rest=OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();

    bool result = false;
     //if(OrderType()==OP_BUY) vOrderClose (OrderTicket(), lot3, MarketInfo(OrderSymbol(), MODE_BID), 10, Red);
     //if(OrderType()==OP_SELL) vOrderClose (OrderTicket(), lot3, MarketInfo(OrderSymbol(), MODE_ASK), 10, Red);
    
    switch(type)
    {
      //Close opened long positions
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE );
                          break;

      //Close pending orders
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
    }
    
    if(result == false)
    {
       if (GetLastError()>0) Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }  
  }
  }
  return;
}
Suy Kok Yik
366
Suy Kok Yik  
Mehmet Bastem:

You can simply close all orders this way.

Great and thanks 
William Roeder
23446
William Roeder  
Mehmet Bastem: You can simply close all orders this way.
  1.   for(int i=total-1;i>=0;i--){
        bool rest=OrderSelect(i, SELECT_BY_POS);
    Check your return codes, per № 2/4.3. Down per № 4.1.

  2.        if (GetLastError()>0) Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
    

    Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum 2012.05.20
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles 25 March 2014

  3. “Simply” simplified:

          case OP_BUY       :
          case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 5, CLR_NONE );
    
  4. 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 (non-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 programming forum
    2. For In First Out (FIFO rules — US brokers,) and you (potentially) process multiple orders per symbol, you must find the earliest order (count up,) close it, and on a successful operation, reprocess all positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11 ACCOUNT_FIFO_CLOSE

    3. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().
Keith Watford
Moderator
26043
Keith Watford  
Suy Kok Yik:

Do not double post!

I will delete your duplicated topic and move the replies here.

Suy Kok Yik
366
Suy Kok Yik  
Keith Watford:

Do not double post!

I will delete your duplicated topic and move the replies here.

I double post due to can't find my previous post.