'ret' - undeclared identifier? what could be the issue?

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

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

 
void BuyOrderLots()
{
    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()));
    }
}
}
 
am getting error 'ret' undeclared identifier after adding #property strict.but need to add it so as to share ea on mql5 website
 
#property strict

forces you to use a better style for your code.

For example you need to declare the variables before they are used.

So just write

bool ret;

at the beginning of the function.

And please use the SRC button when you post code, like I did - do you see the difference?

 
Carl thanks a lot! This code keeps changing the Take profit ,TP, when this is not the code for trailing,l dont want the take profit to be modified once set. Would you identify whats causing this ?
Reason: