Help with selecting and proccesing opened orders

 
Hello, I'm
trying to do the following:

EA checks all open orders one by one separately: OrderProfit and OrderOpenTime and number of open market orders in terminal (for example 10), ie if orders are less than 10, then EA stops closing orders regardless of earnings and other variables. Or put another way, after each closed order EA to recalculate the number of orders and depending of their numbers to close or leave it open. Now it does not calculate correctly. Can you tell me what is wrong?

void CheckOpenOrders()
{
    double profit = 0;
    double order_OpenTime=0;
    double shift_open=0;
    for (int i=OrdersTotal()-1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (OrderSymbol() == Symbol() && OrderMagicNumber() == 0)
            {
                profit = OrderProfit();
                order_OpenTime=OrderOpenTime();
                shift_open=NormalizeDouble(iBarShift(NULL,0,order_OpenTime,false),0);
                        
     
            }
        }
        else
        {
            Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
        }
    }
    
    
    if (profit>0  )//&& TOTALOPENORDERS>10 this works fine, the problem is with atributes of each order. I use extern variable for number of open orders.
    {
        CloseSellOrders();
    }  
    if (profit>0 )
    {  
        CloseBuyOrders();//&& TOTALOPENORDERS>10 this works fine, the problem is with atributes of each order. I use extern variable for number of open orders.
        
    }
}

void CloseSellOrders()
{
    int orderstotal = OrdersTotal();
    int orders = 0;
    int ordticket[30][2];
    for (int i = 0; i < orderstotal; i++)
    {
        OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
        if (OrderType() != OP_SELL || OrderSymbol() != Symbol() || OrderMagicNumber() != 0)
        {
            continue;
        }
        ordticket[orders][0] = OrderOpenTime();
        ordticket[orders][1] = OrderTicket();
        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(),50, CLR_NONE);
            if (ret == false)
            Print("OrderClose() error - ", ErrorDescription(GetLastError()));
        }
    }
    
}

void CloseBuyOrders()
{
    int orderstotal = OrdersTotal();
    int orders = 0;
    int ordticket[30][2];
    for (int i = 0; i < orderstotal; i++)
    {
        OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
        if (OrderType() != OP_BUY || OrderSymbol() != Symbol() || OrderMagicNumber() != 0)
        {
            continue;
        }
        ordticket[orders][0] = OrderOpenTime();
        ordticket[orders][1] = OrderTicket();
        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(), 50, CLR_NONE);
            if (ret == false)
            Print("OrderClose() error - ", ErrorDescription(GetLastError()));
        }
    }
    
}

 

First obvious problem: Loops and Closing or Deleting Orders you MUST count down when closing or deleting orders.

When you count the orders you check the Symbol and Magic Number, so lets assume you have 5 orders that match the Symbol and Magic Number and 5 that do not . . . you will then go and attempt to close the first 5, not the correct 5.

 

Few error I see in your code:

double order_OpenTime // should be datetime order_OpenTime

double shift_open     // should be int shift_open

shift_open=NormalizeDouble(iBarShift(NULL,0,order_OpenTime,false),0);  // why you need NormalizeDouble here..???? Double not required. Keep in int.

if orders are less than 10, => I dont see this check anywhere in your code...

The for loop in CheckOpenOrders() will give you values (profit, order_OpenTime, shift_open) of last selected order only. See how the for loop works...If you want to close based on profit, then include it inside for loop and not outside.

   if (profit>0  )//&& TOTALOPENORDERS>10 this works fine, the problem is with atributes of each order. I use extern variable for number of open orders.
    {
        CloseSellOrders();
    }  
    if (profit>0 )
    {  
        CloseBuyOrders();//&& TOTALOPENORDERS>10 this works fine, the problem is with atributes of each order. I use extern variable for number of open orders.
    }
// Both if check conditions are same then why 2 if.??
 
RaptorUK:

First obvious problem: Loops and Closing or Deleting Orders you MUST count down when closing or deleting orders.

When you count the orders you check the Symbol and Magic Number, so lets assume you have 5 orders that match the Symbol and Magic Number and 5 that do not . . . you will then go and attempt to close the first 5, not the correct 5.


Yes, you are correct for loops and magic numbers, but here I speak for one symbol traded and magic number = 0. The problem is that if you put:

profit<0 and forget about OrdersTotal all orders with profit< 0 will be closed?! Why is that?

 
dineshydv:

Few error I see in your code:

if orders are less than 10, => I dont see this check anywhere in your code...

The for loop in CheckOpenOrders() will give you values (profit, order_OpenTime, shift_open) of last selected order only. See how the for loop works...If you want to close based on profit, then include it inside for loop and not outside.

You mean this? :

 if (profit>0  )//&& TOTALOPENORDERS>10 this works fine, the problem is with atributes of each order. I use extern variable for number of open orders.
    {
        bool ret = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 50, CLR_NONE);
 
BINFA:


Yes, you are correct for loops and magic numbers, but here I speak for one symbol traded and magic number = 0. The problem is that if you put:

profit<0 and forget about OrdersTotal all orders with profit< 0 will be closed?! Why is that?


It's not clear what you are actually trying to do . . . but CloseSellOrders() and CloseBuyOrders() both will close more than one single order . . . if you just want to close a single order you don't need these functions.

Reason: