//----------------------------------------------------//CLOSING ORDERbool 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?
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() ?
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
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?
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?
It just a function for checking trading time. There is no orderselect function
And what is NewsHandling() ?
Hi WHRoeder,
Thank you for your advice. Looking for the example, does it a must for function OrdersTotal() pass to the variable?
Or can I do like this ? OrdersTotal included in the loop without pass it to the variable at first. Does it make a different?
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!
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.
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!