EA Keep Disturb Other Pair Order - page 2

 
//----------------------------------------------------
//CLOSING ORDER



   bool CloseOrder;
   int CloseError;
   bool DeleteOrder;
   int DeleteError;

if (CountAll > 0) {   
    for (int i = 0; i < OrdersTotal(); i++) {                                                          //----CHANGED----
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == TRUE) {
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderCloseTime() == 0) {
            if ((IsTradeTime(HourStart1, HourStop1) || IsTradeTime(HourStart2, HourStop2) || IsTradeTime(HourStart3, HourStop3) || IsTradeTime(HourStart4, HourStop4))) {
               while (!IsTradeAllowed()) Sleep(100);
               RefreshRates();
               if (OrderType() == OP_BUY && (TimeCurrent() - OrderOpenTime() >= LimitTime)) {
                  CloseOrder = OrderClose(OrderTicket(), OrderLots(), Bid, slp_X(), Blue);
                  if (WriteLog == TRUE && CloseOrder == TRUE) {
                     Print ("Order Close On LimitTime",";"," OrderTicket"," #",OrderTicket()," Time : ",Hour(),":",Minute(),":",Seconds());
                     return(0);
                  }
                     if (!CloseOrder) {
                        CloseError = GetLastError();
                        if(WriteLog == TRUE) {
                           Print("Close: OrderClose Error = ", ErrorDescription(CloseError));
                           return(0);                                                                    //---ADD-------
                        }
                        if (CloseError == 2 || CloseError == 128 || CloseError == 139) {
                           Alert("Error Execution ", ErrorDescription(CloseError),"!!!"); 
                           PlaySound("alert.wav");
                           CriticalError = TRUE;
                           return(0);                                                                  
                        }
                        if (CloseError == 4 || CloseError == 129) Sleep(1000); return(0);                //---ADD-------
                        if (CloseError == 133) Sleep(3000); return(0); //Trade is Disabled                //---ADD-------
                     } 
               }
               if (OrderType() == OP_SELL && (TimeCurrent() - OrderOpenTime() >= LimitTime)) {
                  CloseOrder = OrderClose(OrderTicket(), OrderLots(), Ask, slp_X(), Red);
                  if (WriteLog == TRUE && CloseOrder == TRUE) {
                     Print ("Order Close On LimitTime",";"," OrderTicket"," #",OrderTicket()," Time : ",Hour(),":",Minute(),":",Seconds());
                     return(0);                                                                           //---ADD-------
                  }   
                     if (!CloseOrder) {
                        CloseError = GetLastError();
                        if(WriteLog == TRUE) {
                           Print("Close: OrderClose Error = ", ErrorDescription(CloseError));
                           return(0);                                                                      //---ADD-------
                        }
                        if (CloseError == 2 || CloseError == 128 || CloseError == 139) {
                           Alert("Error Execution ", ErrorDescription(CloseError),"!!!"); 
                           PlaySound("alert.wav");
                           CriticalError = TRUE;
                           return(0);                                                                  
                        }
                        if (CloseError == 4 || CloseError == 129) Sleep(1000); return(0);                 //---ADD-------
                        if (CloseError == 133) Sleep(3000); return(0); //Trade is Disabled                //---ADD-------
                     } 
               }
               if ((OrderType() == OP_SELLSTOP || OrderType() == OP_BUYSTOP )&& (CountBuy > 0 || CountSell > 0)) {
                  DeleteOrder = OrderDelete(OrderTicket());
                  if (WriteLog == TRUE && DeleteOrder == TRUE) {
                     Print ("Pending Order Closed Due To Activate Order",";"," OrderTicket"," #",OrderTicket());
                     return(0);                                                                            //---ADD-------
                  } 
                     if (!DeleteOrder) {
                        DeleteError = GetLastError();
                        if(WriteLog == TRUE) {
                           Print("Delete: OrderDelete Error = ", ErrorDescription(DeleteError));
                           return(0);                                                                      //---ADD-------
                        }
                        if (DeleteError == 2 || DeleteError == 128 || DeleteError == 139) {
                           Alert("Error Execution ", ErrorDescription(DeleteError),"!!!"); 
                           PlaySound("alert.wav");
                           CriticalError = TRUE;
                           return(0);
                        }
                        if (DeleteError == 4 || DeleteError == 129) Sleep(1000); return(0);                //---ADD-------
                        if (DeleteError == 133) Sleep(3000); return(0); //Trade is Disabled                //---ADD-------
                     } 
               }

            }
      }
   }
  }
}

//------------------

 

I have also changed the looping FOR iteration and put return on every closing but still it keep closing other pair orders. what's wrong with this code? 

 
najibdaud:

 I have also changed the looping FOR iteration and put return on every closing but still it keep closing other pair orders. what's wrong with this code? 

Same question as before: what's the code for IsTradeTime() ? 
 
It just a function for checking trading time. There is no orderselect function
bool IsTradeTime(int OpenHour, int CloseHour) {
////////////////////////////// Check DayOfWeek
   int cur_d = DayOfWeek();
   if(OpenHour == HourStart1 && CloseHour == HourStop1) {
      if (cur_d == 1 && !MondayOn) return(FALSE);
      if (cur_d == 2 && !TuesdayOn) return(FALSE);
      if (cur_d == 3 && !WednesdayOn) return(FALSE);
      if (cur_d == 4 && !ThursdayOn) return(FALSE);
      if (cur_d == 5 && !FridayOn) return(FALSE);
      if (cur_d == 6 && !SaturdayOn) return(FALSE);
      if (cur_d == 0 && !SundayOn) return(FALSE);
      if (!ExtendedSession1) return (FALSE);
   } else if(OpenHour == HourStart2 && CloseHour == HourStop2) {
      if (cur_d == 1 && !MondayOn) return(FALSE);
      if (cur_d == 2 && !TuesdayOn) return(FALSE);
      if (cur_d == 3 && !WednesdayOn) return(FALSE);
      if (cur_d == 4 && !ThursdayOn) return(FALSE);
      if (cur_d == 5 && !FridayOn) return(FALSE);
      if (cur_d == 6 && !SaturdayOn) return(FALSE);
      if (cur_d == 0 && !SundayOn) return(FALSE);
      if (!ExtendedSession2) return (FALSE);
   } else if(OpenHour == HourStart3 && CloseHour == HourStop3) {
      if (cur_d == 1 && !MondayOn) return(FALSE);
      if (cur_d == 2 && !TuesdayOn) return(FALSE);
      if (cur_d == 3 && !WednesdayOn) return(FALSE);
      if (cur_d == 4 && !ThursdayOn) return(FALSE);
      if (cur_d == 5 && !FridayOn) return(FALSE);
      if (cur_d == 6 && !SaturdayOn) return(FALSE);
      if (cur_d == 0 && !SundayOn) return(FALSE);
      if (!ExtendedSession3) return (FALSE);
   } else if(OpenHour == HourStart4 && CloseHour == HourStop4) {
      if (cur_d == 1 && !MondayOn) return(FALSE);
      if (cur_d == 2 && !TuesdayOn) return(FALSE);
      if (cur_d == 3 && !WednesdayOn) return(FALSE);
      if (cur_d == 4 && !ThursdayOn) return(FALSE);
      if (cur_d == 5 && !FridayOn) return(FALSE);
      if (cur_d == 6 && !SaturdayOn) return(FALSE);
      if (cur_d == 0 && !SundayOn) return(FALSE);
      if (!ExtendedSession4) return(FALSE);
   }
////////////////////////////// Check DayOfWeek
   bool result = FALSE;
   RefreshRates();
   int CurrentHour = Hour();
   if (OpenHour > 23 || OpenHour < 0 ) OpenHour = 0;
   if (CloseHour > 23 || CloseHour < 0 ) CloseHour = 0;
   if (OpenHour < CloseHour && (Hour() >= OpenHour && Hour() < CloseHour)) result = TRUE;
   if (OpenHour > CloseHour && (Hour() >= OpenHour || Hour() < CloseHour)) result = TRUE;
   if (DayOfWeek() != TradeDayOfWeek && TradeDayOfWeek != 0) result = FALSE;
   if (NewsFilter == TRUE && NewsHandling() == TRUE) result = FALSE;
return(result);
}
 
najibdaud:
It just a function for checking trading time. There is no orderselect function
And what is NewsHandling() ?
 
jjc:
And what is NewsHandling() ?
It just a function for News filtering. No Orderselect function or looping. Just true / false.
 
In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading) you must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
 

Hi WHRoeder,

Thank you for your advice. Looking for the example, does it a must for function OrdersTotal() pass to the variable?

int PositionIndex;    

int TotalNumberOfOrders;   

TotalNumberOfOrders = OrdersTotal();                                                // Does this line a must?

for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --)  
   {
   if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;   
   
   if( OrderMagicNumber() == MagicNo       
      && OrderSymbol() == Symbol()         
      && ( OrderType() == OP_BUY           
      ||   OrderType() == OP_SELL ) )      
   
      if ( ! OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), Slippage ) )               
         Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );   
      
   } //  end of For loop

 

Or can I do like this ?  OrdersTotal included in the loop without pass it to the variable at first. Does it make a different?

 

int PositionIndex;    

for(PositionIndex = OrdersTotal() - 1; PositionIndex >= 0 ; PositionIndex --)   // Can I use like this instead?
   {
   if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;   
   
   if( OrderMagicNumber() == MagicNo       
      && OrderSymbol() == Symbol()         
      && ( OrderType() == OP_BUY           
      ||   OrderType() == OP_SELL ) )      
   
      if ( ! OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), Slippage ) )               
         Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );   
      
   } //  end of For loop
 
najibdaud:

gooly, 

The magicnumber is same to all pair. 

So how could one EA determine whether its a position of its own or from another EA?

The comment is not fail safe a the broker can change it!

 
gooly:

So how could one EA determine whether its a position of its own or from another EA?

The comment is not fail safe a the broker can change it!

 

It should determine by pair symbol whether same magic number or not. Even though same magic number but the pair is different as example from this Loops and Closing or Deleting Orders - MQL4 forum

 

But I dont know what is wrong with my code. 

 
najibdaud:

It should determine by pair symbol whether same magic number or not. Even though same magic number but the pair is different as example from this Loops and Closing or Deleting Orders - MQL4 forum

But I dont know what is wrong with my code. 

So any of your EAs will close all Orders of the same Symbol() no matter whether it has opened it itself or it was opened by another EA.

Normally each EA has its own MagicNumber so it can detect its own Orders and does not disturb other EAs!