Download MetaTrader 5

EA Keep Disturb Other Pair Order

To add comments, please log in or register
Mohammad Najib Daud
195
Mohammad Najib Daud  

Hi,

I have an EA attached to several pair. The problem is it always disturb each other (closing pending order).

The code  for closing is custom function as below;

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

            }
         else if ((!IsTradeTime(HourStart1, HourStop1) || !IsTradeTime(HourStart2, HourStop2) || !IsTradeTime(HourStart3, HourStop3) || !IsTradeTime(HourStart4, HourStop4))) { 
         CloseAllNow();
         }
      }
   }
  }
}

 

It  disturb only on pending order BUY STOP and SELL STOP. The EA have the same magic number but the code has include filter with current symbol chart but still close other EA order. I dont know what to do.

peter.MT4Web
158
peter.MT4Web  

Do you also check Symbol and MagicNumber in

 

        CloseAllNow();

Mohammad Najib Daud
195
Mohammad Najib Daud  
Yes, it also check.
void CloseAllNow() {
   for (int i = OrdersTotal() - 1; i >= 0; i--) {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == TRUE) {
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderCloseTime() == 0) {
      while (!IsTradeAllowed()) Sleep(100);
         if (OrderType() == OP_BUY) { 
         if (OrderClose(OrderTicket(), OrderLots(), Bid, slp_X(), Blue) == TRUE) {
         if (WriteLog == TRUE) Print ("CloseAllNow Activated",";"," OrderTicket"," #",OrderTicket());
         }
         }
         if (OrderType() == OP_SELL) {
         if (OrderClose(OrderTicket(), OrderLots(), Ask, slp_X(), Red) == TRUE) {
         if (WriteLog == TRUE) Print ("CloseAllNow Activated",";"," OrderTicket"," #",OrderTicket());
         }
         }
         if (OrderType() == OP_BUYSTOP) { 
         if (OrderDelete(OrderTicket()) == TRUE) {
         if (WriteLog == TRUE) Print ("CloseAllNow Activated",";"," OrderTicket"," #",OrderTicket());
         }
         }
         if (OrderType() == OP_SELLSTOP) {
         if (OrderDelete(OrderTicket()) == TRUE) {
         if (WriteLog == TRUE) Print ("CloseAllNow Activated",";"," OrderTicket"," #",OrderTicket());
         }
         }
      }
   }
  }
}
Carl Schreiber
6691
Carl Schreiber  
Add in your Prints the MagicNumber of the closed orders
Mohammad Najib Daud
195
Mohammad Najib Daud  

I have yet edit and test to print MagicNumber since today market is closed. But this is the example from the expert log & journal:

2       03:33:18.442    MetaFx v4.2 GBPUSD,M30: open #75608056 sell stop 0.35 GBPUSD at 1.53102 tp: 1.52752 ok

0       03:58:23.206    '1012597': order #75608056 sell stop 0.35 GBPUSD at 1.53102 activated at price 1.53102                   -(This is Journal Log, Not Expert Log)


2       03:58:23.569    MetaFx v4.2 GBPUSD,M30: delete #75608055 buy stop 0.35 GBPUSD at 1.54193 sl: 0.00000 tp: 1.54543 ok      -(EA then close GU pending order once pending activated)
0       03:58:23.569    MetaFx v4.2 GBPUSD,M30: Pending Order Closed; OrderTicket #75608055


3       03:59:56.812    MetaFx v4.2 GBPUSD,M30: pending order #75608594 cannot be closed
0       03:59:56.812    MetaFx v4.2 GBPUSD,M30: Order Close On LimitTime; OrderTicket #75608594 Time : 22:59:59
0       03:59:56.812    MetaFx v4.2 GBPUSD,M30: Close: OrderClose Error = invalid ticket
2       03:59:57.152    MetaFx v4.2 GBPUSD,M30: delete #75608594 sell stop 0.37 EURUSD at 1.09967 sl: 0.00000 tp: 1.09617 ok      (Why EA on GU close EU pair?)
0       03:59:57.152    MetaFx v4.2 GBPUSD,M30: Pending Order Closed; OrderTicket #75608594


3       03:59:57.152    MetaFx v4.2 GBPUSD,M30: pending order #75608593 cannot be closed
0       03:59:57.152    MetaFx v4.2 GBPUSD,M30: Order Close On LimitTime; OrderTicket #75608593 Time : 23:0:0
0       03:59:57.152    MetaFx v4.2 GBPUSD,M30: Close: OrderClose Error = invalid ticket
2       03:59:57.521    MetaFx v4.2 GBPUSD,M30: delete #75608593 buy stop 0.37 EURUSD at 1.11400 sl: 0.00000 tp: 1.11750 ok       (Why EA on GU close EU pair?)
0       03:59:57.521    MetaFx v4.2 GBPUSD,M30: Pending Order Closed; OrderTicket #75608593


2       03:41:20.162    MetaFx v4.2 USDJPY,M30: open #75608592 sell stop 0.35 USDJPY at 120.227 tp: 119.877 ok
3       03:59:57.522    MetaFx v4.2 GBPUSD,M30: invalid price 1.53136000 for OrderClose function
0       03:59:57.522    MetaFx v4.2 GBPUSD,M30: Order Close On LimitTime; OrderTicket #75608592 Time : 23:0:0
0       03:59:57.522    MetaFx v4.2 GBPUSD,M30: Close: OrderClose Error = invalid price parameter for trade function
2       03:59:57.904    MetaFx v4.2 GBPUSD,M30: delete #75608592 sell stop 0.35 USDJPY at 120.227 sl: 0.000 tp: 119.877 ok         (Why EA on GU close UJ pair?)
0       03:59:57.907    MetaFx v4.2 GBPUSD,M30: Pending Order Closed; OrderTicket #75608592


2       03:59:57.566    MetaFx v4.2 EURUSD,M30: open #75609945 sell stop 0.34 EURUSD at 1.09967 tp: 1.09617 ok
0       03:59:57.566    MetaFx v4.2 EURUSD,M30: Slippage sent to server = 0
3       03:59:58.795    MetaFx v4.2 GBPUSD,M30: pending order #75609945 cannot be closed
0       03:59:58.795    MetaFx v4.2 GBPUSD,M30: Order Close On LimitTime; OrderTicket #75609945 Time : 23:0:1
0       03:59:58.795    MetaFx v4.2 GBPUSD,M30: Close: OrderClose Error = invalid ticket
2       03:59:59.328    MetaFx v4.2 GBPUSD,M30: delete #75609945 sell stop 0.34 EURUSD at 1.09967 sl: 0.00000 tp: 1.09617 ok       (Why EA on GU close EU pair?)
0       03:59:59.328    MetaFx v4.2 GBPUSD,M30: Pending Order Closed; OrderTicket #75609945


2       03:59:57.905    MetaFx v4.2 EURUSD,M30: open #75609980 buy stop 0.34 EURUSD at 1.11400 tp: 1.11750 ok
3       03:59:57.908    MetaFx v4.2 GBPUSD,M30: pending order #75609980 cannot be closed
0       03:59:57.908    MetaFx v4.2 GBPUSD,M30: Order Close On LimitTime; OrderTicket #75609980 Time : 23:0:0
0       03:59:57.908    MetaFx v4.2 GBPUSD,M30: Close: OrderClose Error = invalid ticket
2       03:59:58.791    MetaFx v4.2 GBPUSD,M30: delete #75609980 buy stop 0.34 EURUSD at 1.11400 sl: 0.00000 tp: 1.11750 ok         (Why EA on GU close EU pair?)
0       03:59:58.792    MetaFx v4.2 GBPUSD,M30: Pending Order Closed; OrderTicket #75609980


2       03:59:59.328    MetaFx v4.2 USDJPY,M30: open #75610001 sell stop 0.34 USDJPY at 120.227 tp: 119.877 ok
0       04:00:02.280    MetaFx v4.2 GBPUSD,M30: Order Close On LimitTime; OrderTicket #75610001 Time : 23:0:5
2       04:01:34.536    MetaFx v4.2 GBPUSD,M30: delete #75610001 sell stop 0.34 USDJPY at 120.227 sl: 0.000 tp: 119.877 ok          (Why EA on GU close UJ pair?)
0       04:01:34.536    MetaFx v4.2 GBPUSD,M30: Pending Order Closed; OrderTicket #75610001



3       03:59:59.329    MetaFx v4.2 GBPUSD,M30: pending order #75610002 cannot be closed
0       03:59:59.329    MetaFx v4.2 GBPUSD,M30: Order Close On LimitTime; OrderTicket #75610002 Time : 23:0:2
0       03:59:59.329    MetaFx v4.2 GBPUSD,M30: Close: OrderClose Error = invalid ticket
2       03:59:59.329    MetaFx v4.2 EURUSD,M30: open #75610002 buy stop 0.34 EURUSD at 1.11400 tp: 1.11750 ok
0       03:59:59.352    MetaFx v4.2 EURUSD,M30: Slippage sent to server = 0
2       03:59:59.746    MetaFx v4.2 GBPUSD,M30: delete #75610002 buy stop 0.34 EURUSD at 1.11400 sl: 0.00000 tp: 1.11750 ok         (Why EA on GU close EU pair?)
0       03:59:59.746    MetaFx v4.2 GBPUSD,M30: Pending Order Closed; OrderTicket #75610002



2       03:59:59.745    MetaFx v4.2 EURUSD,M30: open #75610007 sell stop 0.34 EURUSD at 1.09967 tp: 1.09617 ok
3       03:59:59.747    MetaFx v4.2 GBPUSD,M30: pending order #75610007 cannot be closed
0       03:59:59.747    MetaFx v4.2 GBPUSD,M30: Order Close On LimitTime; OrderTicket #75610007 Time : 23:0:2
0       03:59:59.747    MetaFx v4.2 GBPUSD,M30: Close: OrderClose Error = invalid ticket
0       03:59:59.781    MetaFx v4.2 EURUSD,M30: Slippage sent to server = 0
2       04:00:00.416    MetaFx v4.2 GBPUSD,M30: delete #75610007 sell stop 0.34 EURUSD at 1.09967 sl: 0.00000 tp: 1.09617 ok         (Why EA on GU close EU pair?)
0       04:00:00.416    MetaFx v4.2 GBPUSD,M30: Pending Order Closed; OrderTicket #75610007


I have arranged it by OrderTicket so that easy to track the flow.
 

Carl Schreiber
6691
Carl Schreiber  

MetaFX seems to be a MultiAccount Manager - may be  that is the problem?

And if you have the same MagicNumbers for different currencies...?

What you show is only that MetaFX applied to a GBPUSD-M30 chart deletes an EURUSD pending order.

JC
1456
JC  
najibdaud:

I have yet edit and test to print MagicNumber since today market is closed. But this is the example from the expert log & journal:

The CloseAllNow() function looks fine, and the problems reported in this log seem only to be from the CloseLimit() function originally posted, not from CloseAllNow().

What is the IsTradeTime() function? It seems unlikely from context, but if this function has any rules which involve looking at existing open trades, and thus doing an OrderSelect(), that will affect the order which has previously been selected in the loop in CloseLimit().

For example, the following would cause a problem...

bool IsTradeTime(int Hour1, int Hour2)
{
  // Check the time
  if (TimeHour(TimeCurrent()) < Hour1) return false;
  if (TimeHour(TimeCurrent()) > Hour2) return false;

  // And also take this opportunity to block if there are existing trades...
  // Causes a problem if the calling function expects the selected order not to be changed by this function
  for (int i = 0; i < OrdersTotal(); i++) {
    if (OrderSelect(i, SELECT_BY_POS)) {
       etc...
    }
  }
}
Mohammad Najib Daud
195
Mohammad Najib Daud  

gooly, 

It's not a MAM, its a standalone MT4 and EA with attached to multiple pair. The magicnumber is same to all pair. 

 

jjc,

Yes the problem come from code with the first one I posted. The IsTradeTime is custom function for checking trading time. There is no loop in the function.

However there are some function that using OrderSelect for example for order count. Is it a problem if there multiple OrderSelect ? Or is it an MT4 itself have a bug?


It is easy for testing using one pair but not for multiple pair since strategy tester only allowed for one pair.

JC
1456
JC  
najibdaud:

Yes the problem come from code with the first one I posted. The IsTradeTime is custom function for checking trading time. There is no loop in the function.

However there are some function that using OrderSelect for example for order count. Is it a problem if there multiple OrderSelect ? Or is it an MT4 itself have a bug?

The point isn't the loop. The point is whether, after doing each OrderSelect() in CloseLimit(), you are calling another function which does another OrderSelect() and changes the selected order.

The selection by OrderSelect() is not private to the function in which you do OrderSelect(). If affects the whole of the EA. Giving you another example of the same thing, the following will not work as intended:

 

void ExampleFunction()
{
  int myCloseTicket = 12345678;
  ...
  if (OrderSelect(myCloseTicket, SELECT_BY_TICKET)) {
    if (ExampleFunctionWhichChangesOrderSelect()) {

       // Closes 98765432, not 12345678...
       OrderClose(OrderTicket(), OrderLots(), Bid, OrderClosePrice());
    }
  }
}


bool ExampleFunctionWhichChangesOrderSelect()
{
  int testTicket = 98765432;
  if (OrderSelect(testTicket, SELECT_BY_TICKET)) {
     // Some test on this other ticket. Doesn't matter what. The point is that 
     // this function does an OrderSelect(), and the selected order inside the
     // parent function is no longer what it is expected to be
     return (OrderProfit() > 0);
  } else {
     return false;
  }
}
Mohammad Najib Daud
195
Mohammad Najib Daud  
jjc:

The point isn't the loop. The point is whether, after doing each OrderSelect() in CloseLimit(), you are calling another function which does another OrderSelect() and changes the selected order.

The selection by OrderSelect() is not private to the function in which you do OrderSelect(). If affects the whole of the EA. Giving you another example of the same thing, the following will not work as intended:

 

 

I get it. Let me check for redundant looping.  

Mohammad Najib Daud
195
Mohammad Najib Daud  
I have eliminate redundant looping but still it close other pair which is like they don't even checking the symbol.
12
To add comments, please log in or register