|
need modification help
Hello fellow trader
I need help with this code.
current function is to close first open orders by time and any following orders, mean it could be more than 2 orders closed in the same time.
Question: how do I change it to make it close the FIRST 2 open orders by time ONLY.
here the code.
thanks for help
//+------------------------------------------------------------------+
//| Close Condition Type 2 |
//+------------------------------------------------------------------+
void CheckCloseConditionType2()
{
int Orders[];
int i, j;
ArrayResize(Orders, 0);
int cnt = OrdersTotal();
for (i=0; i < cnt; i++)
{
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol()) continue;
if (OrderMagicNumber() != Magic) continue;
int type = OrderType();
if (type == OP_BUY || type == OP_SELL)
{
int size = ArraySize(Orders);
ArrayResize(Orders, size+1);
Orders[size] = OrderTicket();
}
}
//-----
size = ArraySize(Orders);
for (i=0; i < size; i++)
{
if (!OrderSelect(Orders[i], SELECT_BY_TICKET)) continue;
if (OrderCloseTime() > 0) continue;
datetime tm1 = OrderOpenTime();
for (j=i+1; j < size; j++)
{
if (!OrderSelect(Orders[j], SELECT_BY_TICKET)) continue;
if (OrderCloseTime() > 0) continue;
datetime tm2 = OrderOpenTime();
if (tm1 > tm2)
{
int ticket = Orders[i];
Orders[i] = Orders[j];
Orders[j] = ticket;
}
}
}
//-----
for (i = size-1; i >= 1; i--)
{
double Profit = ArrayGetOrdersProfit(Orders);
if ((Profit >= TotalTakeProfit && TotalTakeProfit > 0) || (Profit >= Profit2Exit && Profit2Exit > 0))
{
Print("[Enter] Close by condition Type2");
string msg1 = "";
string msg2 = "";
double P;
double T.P = 0;
for (j=0; j < size; j++)
{
if (Orders[j] == -1) continue;
if (!OrderSelect(Orders[j], SELECT_BY_TICKET)) continue;
if (OrderCloseTime() > 0) continue;
if (StringLen(msg1) > 0) msg1 = msg1 + " + ";
msg1 = msg1 + "order " +Orders[j];
GetOrderProfit(Orders[j], P);
if (StringLen(msg2) > 0) msg2 = msg2 + " + ";
msg2 = msg2 +DoubleToStr(P, 2);
T.P += P;
}
Print("Close: " + msg1);
Print("Profit: " + msg2 + " = " + DoubleToStr(T.P, 2));
ArrayCloseOrders(Orders);
Print("[Exit] Close by condition Type2");
return;
}
Orders[i] = -1;
}
}
__________________
creativity + common sense + a bit of humor = lots of pipp
|