Need help with OrderClose()

 

Hi guys,

Currently I'm developing an EA to close trades when the ADX Main line cross down 40. But the EA didn't close the trades. 

Can you guys give me a hand. Thanks.

int OrderClose()
  {
  int all_trade = OrdersTotal();
  int total_orders = 0;
   
   for(int order = 0; order < OrdersTotal(); order++) 
   {
      if(OrderSelect(order,SELECT_BY_POS,MODE_TRADES)==false) break;
      
      if(OrderMagicNumber() == MagicNumber && OrderSymbol() == _Symbol)
         {
            total_orders++;
         }
   }
   return(total_orders);
     if(OrderType() == OP_BUY)
       {
       if( iADX(_Symbol,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_MAIN,1)>40 && iADX(_Symbol,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_MAIN,0) <= 40)
         {
         bool OrderCloseBuy=OrderClose(OrderTicket(),OrderLots(),Bid, 3, Green);
         }
       }
     if( OrderType() == OP_SELL)
       {
       if( iADX(_Symbol,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_MAIN,1)>40 && iADX(_Symbol,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_MAIN,0) <= 40)
         {
         bool OrderCloseSell=OrderClose(OrderTicket(),OrderLots(),Ask, 3, Green);
         }
       }
   }

 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.
 
int OrderClose()
{
   int all_trade = OrdersTotal();  ////////////////////////////////////////What is this variable for? - It is not used.
   int total_orders = 0;

   for(int order = 0; order < OrdersTotal(); order++)
      {
      if(OrderSelect(order,SELECT_BY_POS,MODE_TRADES)==false) break;

      if(OrderMagicNumber() == MagicNumber && OrderSymbol() == _Symbol)
         {
         total_orders++;
         }
      }
   return(total_orders);   /////////////////////////////////////////////////You return here so following code will not be executed.
   if(OrderType() == OP_BUY)
      {
      if( iADX(_Symbol,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_MAIN,1)>40 && iADX(_Symbol,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_MAIN,0) <= 40)
         {
         bool OrderCloseBuy=OrderClose(OrderTicket(),OrderLots(),Bid, 3, Green);
         }
      }
   if( OrderType() == OP_SELL)
      {
      if( iADX(_Symbol,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_MAIN,1)>40 && iADX(_Symbol,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_MAIN,0) <= 40)
         {
         bool OrderCloseSell=OrderClose(OrderTicket(),OrderLots(),Ask, 3, Green);
         }
      }
}
 

Try something like this (not tested)

void CheckToClose()
{
   bool closeBuys=false,closeSells=false;
   if( iADX(_Symbol,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_MAIN,1)>40 && iADX(_Symbol,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_MAIN,0) <= 40)
      {
      closeBuys=true;
      }
   if( iADX(_Symbol,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_MAIN,1)>40 && iADX(_Symbol,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_MAIN,0) <= 40)
      {
      closeSells=true;
      }

   if(closeBuys || closeSells)
      for(int order = OrdersTotal()-1; order >=0 ; order--)
         {
         if(OrderSelect(order,SELECT_BY_POS,MODE_TRADES)==true)
            if(OrderMagicNumber() == MagicNumber && OrderSymbol() == _Symbol)
               if((OrderType()==OP_BUY && closeBuys) || (OrderType()==OP_SELL && closeSells))
                  {
                  if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(), 3, Green))
                     Print ("ERROR closing ticket #",OrderTicket(),"  code ",GetLastError());
                  }
         }
}
 
Keith Watford:

Try something like this (not tested)

Thanks for your help Keith,
I will post in the right section next time.

Concerning to your code, I have tried it but unfortunately, nothing happened. I don't see any errors here as well, I think that there might be something wrong with the ADX's conditions to close orders

 
Hoang Tran:

Thanks for your help Keith,
I will post in the right section next time.

Concerning to your code, I have tried it but unfortunately, nothing happened. I don't see any errors here as well, I think that there might be something wrong with the ADX's conditions to close orders

I just copied the adx check code.

Looking at it I see now that the same adx condition closes both buys and sells.

It should still work though

 
Hoang Tran:

Do not double post!

I have deleted your other post.

You have had replies here already so no need to post in another topic.

 
Hoang Tran:

Concerning to your code, I have tried it but unfortunately, nothing happened. I don't see any errors here as well, I think that there might be something wrong with the ADX's conditions to close orders

Just a thought.

Show the code where you are actually calling the function.

 

Firstly, where do you actually call the function CheckToClose()  ??

It won't do anything if you don't call it!

Secondly, as you are working with new bars, I would think that you should be checking for a cross of the adx with bars 1 and 2, not 0 and 1.

 

You should follow through your code and see what it is doing.

You should get the value of the adx in CheckToClose.

This is a problem with using global-scope variables unnecessarily.

 
Keith Watford:

You should follow through your code and see what it is doing.

You should get the value of the adx in CheckToClose.

This is a problem with using global-scope variables unnecessarily.

Do you have any suggestions?

I have tried many ways but the result was still the same

Reason: