Download MetaTrader 5

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

To add comments, please log in or register
Yassine
32
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 :)

Kaleem Haider
166022
Kaleem Haider  
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.
Yassine
32
Yassine  
Kaleem Haider:
you should always use the SRC button when posting the code.
Thank you Kaleem Haider :) it's done
Osama Shaban
1589
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.

honest_knave
Moderator
2293
honest_knave  
You should check function return values and print error codes when necessary. 
Keith Watford
Moderator
10541
Keith Watford  
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.
Yassine
32
Yassine  
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.
Yassine
32
Yassine  
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.
honest_knave
Moderator
2293
honest_knave  
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();

       }

}


 

honest_knave
Moderator
2293
honest_knave  

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.

Alain Verleyen
Moderator
33803
Alain Verleyen  
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 
12
To add comments, please log in or register