int orderstotal = OrdersTotal(); int orders = 0; int ordticket[][2]; double lots = 0; for (int i = 0; i < orderstotal; i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if (OrderType() != OP_BUY || OrderSymbol() != Symbol() || OrderMagicNumber() != 9) { continue; } ordticket[orders][0] = OrderOpenTime(); ordticket[orders][1] = OrderTicket(); orders++; } if (orders > 1) { ArrayResize(ordticket,orders); ArraySort(ordticket); }
You don't size your array ordticket before trying to assign values to it.
So nothing in the code following it will work
for (int j =0; j < 10; j++) { Sleep(100); RefreshRates(); bool ret = OrderClose(OrderTicket(), lots, OrderClosePrice(), 4, Red); if (ret) break; } if (ret == false) Print("OrderClose() error - ", ErrorDescription(GetLastError()));
ret is declared inside the for loop, so is local to that loop.
Outside of the loop, it is undeclared
fixed warnings. hope my logic is poor.
actually i want to repeat the OrderClose() till order closed successfully. if requote means it must try closing again and again with small break. what could be the solution?
here is the current code i am using to close the order, but it exit with error when requotes occur!
int orderstotal = OrdersTotal(); int orders = 0; int ordticket[][2]; ArrayResize(ordticket,orderstotal); for (int i = 0; i < orderstotal; i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if (OrderType() != OrderType() || OrderSymbol() != Symbol() || OrderMagicNumber() != OrderId()) { continue; } ordticket[orders][0] = OrderOpenTime(); ordticket[orders][1] = OrderTicket(); orders++; } ArrayResize(ordticket,orders); if (orders > 1) { ArrayResize(ordticket,orders); ArraySort(ordticket); } for (i = 0; i < orders; i++) { if (OrderSelect(ordticket[i][1], SELECT_BY_TICKET) == true) { bool ret = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 4, red); if (ret == false) Print("OrderClose() error - ", ErrorDescription(GetLastError())); } }
fixed warnings. hope my logic is poor.
actually i want to repeat the OrderClose() till order closed successfully. if requote means it must try closing again and again with small break. what could be the solution?
here is the current code i am using to close the order, but it exit with error when requotes occur!
Anyone has the suggestion?
int orderstotal = OrdersTotal(); int orders = 0; int ordticket[][2]; ArrayResize(ordticket,orderstotal); for (int i = 0; i < orderstotal; i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if (OrderType() != OrderType() || OrderSymbol() != Symbol() || OrderMagicNumber() != OrderId()) { continue; } ordticket[orders][0] = OrderOpenTime(); ordticket[orders][1] = OrderTicket(); orders++; } ArrayResize(ordticket,orders); if (orders > 1) { ArrayResize(ordticket,orders); ArraySort(ordticket); } for (i = 0; i < orders; i++) { if (OrderSelect(ordticket[i][1], SELECT_BY_TICKET) == true) { bool ret = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 4, red); if (ret == false) Print("OrderClose() error - ", ErrorDescription(GetLastError())); } }
if (OrderType() != OrderType() || OrderSymbol() != Symbol() || OrderMagicNumber() != OrderId())
????????
for (i = 0; i < orders; i++) { if (OrderSelect(ordticket[i][1], SELECT_BY_TICKET) && OrderCloseTime()==0) { bool ret = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 4, red); if (ret == false) { Print("OrderClose() error - ", ErrorDescription(GetLastError())); i--; } } }
You could simply retry, but have to be aware of getting stuck in an endless loop.
Checking the OrderCloseTime() makes sure that you are not trying to close a trade that may have been closed by SL or TP at the server.
Also add a check for a Pending order as you cannot delete a Pending Order with OrderClose(). That may have been your intention with
if (OrderType() != OrderType())
{
if (MarketInfo(Symbol(), MODE_SPREAD)/PipValue <= MaxSpread)
{
double SL = Ask - StopLoss*PipValue*Point;
if (StopLoss == 0) SL = 0;
double TP = Ask + TakeProfit*PipValue*Point;
if (TakeProfit == 0) TP = 0;
int ticket = -1;
if (true)
ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, MaxSlippage, 0, 0, ExpertName, MagicNumber, 0, Blue);
else
ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, MaxSlippage, SL, TP, ExpertName, MagicNumber, 0, Blue);
if (ticket > -1)
{
if (true)
{
if(OrderSelect(ticket, SELECT_BY_TICKET))
bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, Blue);
if (ret == false)
Print("OrderModify() error - ", ErrorDescription(GetLastError()));
}
}
else
{
Print("OrderSend() error - ", ErrorDescription(GetLastError()));
}
}
}

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use