Download MetaTrader 5
To add comments, please log in or register
Have you found an interesting application? Publish it in Code Base - millions of traders will appreciate that!
Sherif Hasan
7223
Sherif Hasan 2015.09.25 09:11 
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);
    }
    for (int i = 0; i < orders; i++)
    {
        if (OrderSelect(ordticket[i][1], SELECT_BY_TICKET) == true)
        {
            lots = OrderLots()*Lots/100;
            lots=NormalizeDouble(lots, NDigits);
            if (lots < MarketInfo(Symbol(), MODE_MINLOT))    // make sure lot is not smaller than allowed value
            lots = MarketInfo(Symbol(), MODE_MINLOT);
            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()));
            
        }
    }
Keith Watford
Moderator
7969
Keith Watford 2015.09.25 09:25  
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 

Sherif Hasan
7223
Sherif Hasan 2015.09.25 09:50  

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()));
    }
}

Sherif Hasan
7223
Sherif Hasan 2015.09.27 18:37  
sheriffonline:

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()));
    }
}
Keith Watford
Moderator
7969
Keith Watford 2015.09.27 21:59  
 
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()) 

/
To add comments, please log in or register