During close at once a new order get to be open

 

Hi all,

I've this issue occurring during the time that orders take to get closed;

Say that I have 6 orders open and then the conditions to close all are in place, because the orders do not close at same time how can one avoid to get an opening to happen before all those 6 orders get closed ?

Following is the code to close all. 

void CloseAll()
              {
               int OrdType;
               int GLError;
  
                for(int OrderPos = OrdersTotal()-1; OrderPos >= 0; OrderPos--)       
                if(OrderSelect(OrderPos, SELECT_BY_POS, MODE_TRADES)
                   && OrderMagicNumber()== MagicNumber 
                   && OrderSymbol()== Symbol())                                       
                   {//31
                   OrdType = OrderType();
                   if(OrdType == OP_BUY || OrdType==OP_SELL)
                      {//32
                     while(IsTradeContextBusy()) 
                        Sleep(SleepTime);  
                          RefreshRates();
                    
                     if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(),Slippage*pips2points, Yellow))                
                        {
                        GLError = GetLastError();
                        Print("Error Closing/Deleting Order, error # ", GLError, " for ticket: ", OrderTicket());  
                        }
                      }//32               
                    }//31      
                 }//30 

 Thanks in advance for any help provided.

Luis 

 
luisneves:

Hi all,

I've this issue occurring during the time that orders take to get closed;

Say that I have 6 orders open and then the conditions to close all are in place, because the orders do not close at same time how can one avoid to get an opening to happen before all those 6 orders get closed ?

Following is the code to close all. 

 Thanks in advance for any help provided.

Luis 

The EA does one thing at a time,  while it is in the loop where it is closing Orders it cannot at the same time be opening another new order, not with the code shown above . . . .  if this is happening then you must have something else that is opening orders,  it cannot be the done by a single EA.
 

Hi RaptorUK,

Thank you for your prompt attention to my issue.

What is happening at least during last night, was that nevertheless the setting to close by 7th order the EA have opened more 1 and can't see why.

Following is the code that regulates the maximum number of orders;

 

extern int    MaxOrders              =   7; 

 

OTLastTick = OTCurrentTick;                      
  OTCurrentTick = OrdersTotal();                  
    if(OTCurrentTick == 0 && OTLastTick > 0)
      {
      BuyTrigger = Ask + OpenDistance * pips2dbl;
      SellTrigger = Bid - OpenDistance * pips2dbl;
      }             
    if(OTCurrentTick > 0 )Trail();                   
    if(OTLastTick >= 2                                                
      && OTCurrentTick < OTLastTick
      && OTCurrentTick > 0)
      {
      CloseAllOnSL();return;
      }                                                                  
    if(OTCurrentTick > 0)OpenOppositeOrder();
    if(OTCurrentTick >= MaxOrders)CloseAll();//<------------------------------------------------ Close all at Maximum orders defined at external.
    if(Hour() >= CloseMarketOnFriday && DayOfWeek() == 5)CloseAll();
                        
    if(OTCurrentTick == 0)
      {
       BuyAllowed = true;
       SellAllowed = true; 

 Any help here ?

Best regards

Luis 

 
luisneves:
What is happening at least during last night, was that nevertheless the setting to close by 7th order the EA have opened more 1 and can't see why.
  OTCurrentTick = OrdersTotal();
  :
  if(OTCurrentTick >= MaxOrders)CloseAll();//<- Close all at Max
  if(OTCurrentTick == 0){
       BuyAllowed = true;
       SellAllowed = true; 
  1. Once you've reached maxOrders you close all trades.
  2. On the NEXT tick, OrdersTotal == 0 so you set Buy/SellAllowed and open more.
 
luisneves:

Hi RaptorUK,

Thank you for your prompt attention to my issue.

What is happening at least during last night, was that nevertheless the setting to close by 7th order the EA have opened more 1 and can't see why.

Following is the code that regulates the maximum number of orders;

It's hard to say for sure without seeing all the code,  you really need to check what can open a new order and find how this can happen when you already have 7 open orders,  for example . . .

    if(OTCurrentTick > 0)OpenOppositeOrder();   // <-- will OpenOppositeOrder() open a new order if there are already 7 open ?


    if(OTCurrentTick >= MaxOrders)CloseAll();//<------------------------------------------------ Close all at Maximum orders defined at external.

 if OpenOppositeOrder() will open a new order even if there are already 7 orders open then you will have 8 open orders.

 
RaptorUK:

It's hard to say for sure without seeing all the code,  you really need to check what can open a new order and find how this can happen when you already have 7 open orders,  for example . . .

 if OpenOppositeOrder() will open a new order even if there are already 7 orders open then you will have 8 open orders.


Hi RaptorUK;

Thank you and to WHRoeder as well.

Your clue on  OpenOppositeOrder() seems to be the guilty, so I've include this code and will see from there.

if(OTCurrentTick > 0 && OTCurrentTick < MaxOrders) OpenOppositeOrder();

 Thank you once more for your support.

Luis 

Reason: