Please I need help , OrderClosPice() and OrderType() return false values in adversarial cases

 

Hi everyone, it will be great if someone could help me to solve this problem,

I  have this partition in my code :

int T1, T2, T3;

int Ticket = 0;

double OC = 0;

double E1,E2,E3  StopLossSell, StopLossBuy, TakeProfitBuy , TakeProfitSell; // All these variables are defined previously

// in the code I need these two functions :  

void ClosePendingOrders()

        {

        for(int i=OrdersTotal()-1;i>=0;i--)

        {

        bool res=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

        if (OrderType() == OP_SELL || OrderType() == OP_BUY) continue;

        bool r=OrderDelete(OrderTicket());

        }

        }

  int TotalOpenOrders() // For Conting Open Orders.

   {

   int OrderCount = 0;

    for (int i = OrdersTotal()-1; i >= 0; i--)

       {

           bool res = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

           if (OrderType() == OP_SELL || OrderType() == OP_BUY) OrderCount++;

       }

   return (OrderCount);

   }  

if (OrdersTotal() == 0)

{

   if (Ticket == 0 && OC == 0)

       {        

         T1 = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,E1,0,StopLossBuy,TakeProfitBuy,NULL);

        T2 = OrderSend(Symbol(),OP_BUYSTOP,LotSize,E2,0,StopLossBuy,TakeProfitBuy,NULL);

        T3 = OrderSend(Symbol(),OP_SELLSTOP,LotSize,E3,0,StopLossSell,TakeProfitSell,NULL);

if (TotalOpenOrders() == 1) ClosePendingOrders();

bool Result = OrderSelect(0,SELECT_BY_POS,MODE_TRADES);

         Ticket = OrderTicket();

         OC = OrderClosePrice();

       }

}

 The problem is in the two function TotalOpenOrders() and ClosePendingOrders() , they  return a false values if I put the condtion OrersTotal() == 0, but when I remove this condition they return true values but  the OrderClosePrice() return a false value !

and I thik that the problem come from the  OrderSelect, it dosn't return a true value of OrderType() if it's  preceded by the condtion OrdersTotal() == 0 , and it dosn't return a true value of OrderClosePrice()  if  it's not  preceded by the condtion OrdersTotal() == 0.

by the way, in the second cas it return the value of the price of execution of the selected order

 Thanks :)

 
Yassine:

Hi everyone, it will be great if someone could help me to solve this problem,

I  have this partition in my code :


int T1, T2, T3;

int Ticket = 0;

double OC = 0;

double E1,E2,E3  StopLossSell, StopLossBuy, TakeProfitBuy , TakeProfitSell; // All these variables are defined previously

// in the code I need these two functions :  

void ClosePendingOrders()

    {

    for(int i=OrdersTotal()-1;i>=0;i--)

     {

      bool res=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

      if (OrderType() == OP_SELL || OrderType() == OP_BUY) continue;

      bool r=OrderDelete(OrderTicket()); 

     }

    }

  int TotalOpenOrders() // For Conting Open Orders.

   {

   int OrderCount = 0;

    for (int i = OrdersTotal()-1; i >= 0; i--) 

       {

           bool res = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

           if (OrderType() == OP_SELL || OrderType() == OP_BUY) OrderCount++;

       }

   return (OrderCount);

   }  

if (OrdersTotal() == 0)

{

   if (Ticket == 0 && OC == 0)

       {  

   T1 = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,E1,0,StopLossBuy,TakeProfitBuy,NULL);

        T2 = OrderSend(Symbol(),OP_BUYSTOP,LotSize,E2,0,StopLossBuy,TakeProfitBuy,NULL);

        T3 = OrderSend(Symbol(),OP_SELLSTOP,LotSize,E3,0,StopLossSell,TakeProfitSell,NULL);

if (TotalOpenOrders() == 1) ClosePendingOrders();

bool Result = OrderSelect(0,SELECT_BY_POS,MODE_TRADES);

         Ticket = OrderTicket();

         OC = OrderClosePrice(); 

       }

 The problem is in the two function TotalOpenOrders() and ClosePendingOrders() , they  return a false values if I put the condtion OrersTotal() == 0, but when I remove this condition they return true values but  the OrderClosePrice() return a false value !

and I thik that the problem come from the  OrderSelect, it dosn't return a true value of OrderType() if it's  preceded by the condtion OrdersTotal() == 0 , and it dosn't return a true value of OrderClosePrice()  if  it's not  preceded by the condtion OrdersTotal() == 0.

by the way, in the second cas it return the value of the price of execution of the selected order

 Thanks :)

you should always use the SRC button when posting the code.
 
Kaleem Haider:
you should always use the SRC button when posting the code.
Thank you Kaleem Haider :) it's done
 

One of the problems (if not the only problem you have) is in the calculation of the Stop Loss and Take profit value.

Always remember that TP for BuyStop is always higher than the current price while the TP for the BuyLimit

order is lower than the current price. The same for the SL (but in reverse) ...

Use the same logic for the BuyLimit and SellLimit orders as well. 

As I see, you are using same values of StopLossBuy,TakeProfitBuy,StopLossSell,TakeProfitSell

for all types of pending orders which will never work for you.

 
You should check function return values and print error codes when necessary. 
 
Osama Shaban:

One of the problems (if not the only problem you have) is in the calculation of the Stop Loss and Take profit value.

Always remember that TP for BuyStop is always higher than the current price while the TP for the BuyLimit

order is lower than the current price. The same for the SL (but in reverse) ...

Use the same logic for the BuyLimit and SellLimit orders as well. 

As I see, you are using same values of StopLossBuy,TakeProfitBuy,StopLossSell,TakeProfitSell

for all types of pending orders which will never work for you.

That is not necessarily true, the TP can be above the current price. It has to be above the entry price for any Buy order.
 
Osama Shaban:

One of the problems (if not the only problem you have) is in the calculation of the Stop Loss and Take profit value.

Always remember that TP for BuyStop is always higher than the current price while the TP for the BuyLimit

order is lower than the current price. The same for the SL (but in reverse) ...

Use the same logic for the BuyLimit and SellLimit orders as well. 

As I see, you are using same values of StopLossBuy,TakeProfitBuy,StopLossSell,TakeProfitSell

for all types of pending orders which will never work for you.

Thank you Osama, but this is not the problem because the TP should be above the entry price also SL should be under the entry price, and I agree that's a different thing for pending orders.
 
honest_knave:
You should check function return values and print error codes when necessary. 
Thank you honest_knave for your reply, there is no errors in compilation, also functions return values are correct but in the execution they return false values as I mentioned.
 
Yassine:
Thank you honest_knave for your reply, there is no errors in compilation, also functions return values are correct but in the execution they return false values as I mentioned.

Do you have more code that you are not posting? What you have posted does not check the return values, or print error codes if there is a problem.

int T1, T2, T3;

int Ticket = 0;

double OC = 0;

double E1,E2,E3  StopLossSell, StopLossBuy, TakeProfitBuy , TakeProfitSell; // All these variables are defined previously

// in the code I need these two functions :  

void ClosePendingOrders()

        {

        for(int i=OrdersTotal()-1;i>=0;i--)

        {

        bool res=OrderSelect(i,SELECT_BY_POS,MODE_TRADES); // not checked

        if (OrderType() == OP_SELL || OrderType() == OP_BUY) continue;

        bool r=OrderDelete(OrderTicket()); //  not checked

        }

        }

  int TotalOpenOrders() // For Conting Open Orders.

   {

   int OrderCount = 0;

    for (int i = OrdersTotal()-1; i >= 0; i--)

       {

           bool res = OrderSelect(i, SELECT_BY_POS, MODE_TRADES); // not checked

           if (OrderType() == OP_SELL || OrderType() == OP_BUY) OrderCount++;

       }

   return (OrderCount);

   }  

if (OrdersTotal() == 0)

{

   if (Ticket == 0 && OC == 0)

       {        

         T1 = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,E1,0,StopLossBuy,TakeProfitBuy,NULL); // not checked

        T2 = OrderSend(Symbol(),OP_BUYSTOP,LotSize,E2,0,StopLossBuy,TakeProfitBuy,NULL); // not checked

        T3 = OrderSend(Symbol(),OP_SELLSTOP,LotSize,E3,0,StopLossSell,TakeProfitSell,NULL); // not checked

if (TotalOpenOrders() == 1) ClosePendingOrders();

bool Result = OrderSelect(0,SELECT_BY_POS,MODE_TRADES); // not checked

         Ticket = OrderTicket();

         OC = OrderClosePrice();

       }

}


 

 

This whole section doesn't make any sense to me:

if (OrdersTotal() == 0)

{

   if (Ticket == 0 && OC == 0)

       {        

         T1 = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,E1,0,StopLossBuy,TakeProfitBuy,NULL); // not checked

        T2 = OrderSend(Symbol(),OP_BUYSTOP,LotSize,E2,0,StopLossBuy,TakeProfitBuy,NULL); // not checked

        T3 = OrderSend(Symbol(),OP_SELLSTOP,LotSize,E3,0,StopLossSell,TakeProfitSell,NULL); // not checked

if (TotalOpenOrders() == 1) ClosePendingOrders();

bool Result = OrderSelect(0,SELECT_BY_POS,MODE_TRADES); // not checked

         Ticket = OrderTicket();

         OC = OrderClosePrice();

       }

}

 

Breaking it down: 

if (OrdersTotal() == 0)

 The code is only going to run if there are NO orders at all, pending or otherwise. 

 

   if (Ticket == 0 && OC == 0)

       {        

         T1 = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,E1,0,StopLossBuy,TakeProfitBuy,NULL); // not checked

        T2 = OrderSend(Symbol(),OP_BUYSTOP,LotSize,E2,0,StopLossBuy,TakeProfitBuy,NULL); // not checked

        T3 = OrderSend(Symbol(),OP_SELLSTOP,LotSize,E3,0,StopLossSell,TakeProfitSell,NULL); // not checked

You try to set pending orders.

 

if (TotalOpenOrders() == 1) ClosePendingOrders();

ClosePendingOrders() never gets called. How can you have an open order if you tested OrdersTotal()==0 at the start and then only set pending orders?

 

bool Result = OrderSelect(0,SELECT_BY_POS,MODE_TRADES); // not checked

 You are always selecting index 0 in the order pool. You can't presume anything about the indexing of the order pool.

Whether you set pending orders or not, you are going to set ticket and OC to whatever is in index 0 of the order pool.

If the OrderSelect() failed, you still store values in ticket and OC but they are probably wrong values.

 
Keith Watford:
That is not necessarily true, the TP can be above the current price. It has to be above the entry price for any Buy order.
Not true either 
Reason: