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

 
Alain Verleyen:
Not true either 

Clickity click

Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
  • book.mql4.com
Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
 
// Before OnInit () I defined the next two functions :

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

  

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


double OC [20] ;
int Ticket[20];

// In function OnTick() I put this code :

if ( TypeOrder == 1 /*&& OrdersTotal() == 0*/) // TypeOrder is an extern variable
{
   if (OrdersTotal() == 0 ) // with this condition here I get true value of the two functions and false value of OrderClosePrice() , but when I remove it and I remove /* and */ in the precedant line I get true value of OrderClosePrice() and false vaue of  TotalOpenOrder() and ClosePendingOrder().
   {    
   if (OC[0] == 0)
      {
         Ticket0 = OrderSend(Symbol(),OP_BUYSTOP,Lot,Entry,0,StopLossBuy,TakeProfitBuy,NULL); // All variabls are defined previously.
         Result = OrderSelect(Ticket0,SELECT_BY_TICKET);
         OCP[0]= OrderClosePrice();
         Ticket[0] = OrderTicket();
      }
   }
  if (Ticket[0] > 0 && OC[0]> 0)
  {        
     for ( int i =0; i<5 ; i++)
  {
      if (Ticket[i]>0 && Ticket[i+1]== 0 && OC[i]>0 && OC[i+1]==0)
   {
      Result = OrderSelect(Ticket[i],SELECT_BY_TICKET);
       if (OrderType() == OP_BUY || OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP)
       {
         if ( OrderClosePrice() == OrderTakeProfit() )
            {            
               Ticket[i+1] = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,E1,0,StopLossBuy,TakeProfitBuy,NULL); // E1 E2 E3 are defined previously
               Ticket[i+1] = OrderSend(Symbol(),OP_BUYSTOP,LotSize,E2,0,StopLossBuy,TakeProfitBuy,NULL);
               Ticket[i+1] = OrderSend(Symbol(),OP_SELLSTOP,LotSize,E3,0,StopLossSell,TakeProfitSell,NULL);
            }
         else if ( OrderClosePrice() == OrderStopLoss() )
            {                
               Ticket[i+1] = OrderSend(Symbol(),OP_SELLLIMIT,LotSize,EP1,0,StopLossSell,TakeProfitSell,NULL); //EP1 EP2 EP3 are defined previously
               Ticket[i+1] = OrderSend(Symbol(),OP_SELLSTOP,LotSize,EP2,0,StopLossSell,TakeProfitSell,NULL);
               Ticket[i+1] = OrderSend(Symbol(),OP_BUYSTOP,LotSize,EP3,0,StopLossBuy,TakeProfitBuy,NULL);
                        
            }
       }
       else if ( OrderType() == OP_SELL || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP)
       {
         if ( OrderClosePrice() == OrderTakeProfit() )
            {
              
               Ticket[i+1] = OrderSend(Symbol(),OP_SELLLIMIT,LotSize,EE1,0,StopLossSell,TakeProfitSell,NULL); //EE1 EE2 EE3 are defined previously.
               Ticket[i+1] = OrderSend(Symbol(),OP_SELLSTOP,LotSize,EE2,0,StopLossSell,TakeProfitSell,NULL);
               Ticket[i+1] = OrderSend(Symbol(),OP_BUYSTOP,LotSize,EE3,0,StopLossBuy,TakeProfitBuy,NULL);                
            }
         else if ( OrderClosePrice() == OrderStopLoss()  )
            {                
               Ticket[i+1] = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,EEP1,0,StopLossBuy,TakeProfitBuy,NULL); //EEP1 EEP2 EEP3 are defined previously.
               Ticket[i+1] = OrderSend(Symbol(),OP_BUYSTOP,LotSize,EEP2,0,StopLossBuy,TakeProfitBuy,NULL);
               Ticket[i+1] = OrderSend(Symbol(),OP_SELLSTOP,LotSize,EEP3,0,StopLossSell,TakeProfitSell,NULL);
                        
            }        
       }
   }
   if (OC[i+1]==0 && Ticket[i+1]>0)
     {    
      if (TotalOpenOrders() == 1)  
      {      
         ClosePendingOrders();        
         Result = OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
         Ticket[i+1] = OrderTicket();
         OC[i+1] = OrderClosePrice();                  
       }        
     }
    if (OC[i+1] == 0 && Ticket[i+1] == 0) break;
  }
}    
}



I hope to be clear 
 
honest_knave:

Clickity click

The statement of Keith is general : "It (TP) has to be above the entry price for any Buy order."

A Buy order can have a TP below the entry price when it's in loss. Though maybe I should have read the topic more carefully as I see now he is probably talking about a new order.

PS: what is "clickity click" ?
 
Alain Verleyen: PS: what is "clickity click" ?
Maybe related to Chickity China the Chinese Chicken - YouTube?

 
Alain Verleyen:

PS: what is "clickity click" ?

Could mean many things, as whroeder1 has suggested.

Sadly, the true intention was rather more banal: I could not be bothered to type Requirements and Limitations in Making Trades as the description of the clicky

PS the content wasn't aimed at you, it was merely intended as supplemental information to the side discussion.

 

 
honest_knave:

Could mean many things, as whroeder1 has suggested.

Sadly, the true intention was rather more banal: I could not be bothered to type Requirements and Limitations in Making Trades as the description of the clicky

PS the content wasn't aimed at you, it was merely intended as supplemental information to the side discussion.

 

So clickity click means lazy actually 
 
Alain Verleyen:

The statement of Keith is general : "It (TP) has to be above the entry price for any Buy order."

A Buy order can have a TP below the entry price when it's in loss. Though maybe I should have read the topic more carefully as I see now he is probably talking about a new order.

Yes, I was referring to a new order.
 
Yassine:
// Before OnInit () I defined the next two functions :

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

  

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


double OC [20] ;
int Ticket[20];

// In function OnTick() I put this code :

if ( TypeOrder == 1 /*&& OrdersTotal() == 0*/) // TypeOrder is an extern variable
{
   if (OrdersTotal() == 0 ) // with this condition here I get true value of the two functions and false value of OrderClosePrice() , but when I remove it and I remove /* and */ in the precedant line I get true value of OrderClosePrice() and false vaue of  TotalOpenOrder() and ClosePendingOrder().
   {    
   if (OC[0] == 0)
      {
         Ticket0 = OrderSend(Symbol(),OP_BUYSTOP,Lot,Entry,0,StopLossBuy,TakeProfitBuy,NULL); // All variabls are defined previously.
         Result = OrderSelect(Ticket0,SELECT_BY_TICKET);
         OCP[0]= OrderClosePrice();
         Ticket[0] = OrderTicket();
      }
   }
  if (Ticket[0] > 0 && OC[0]> 0)
  {        
     for ( int i =0; i<5 ; i++)
  {
      if (Ticket[i]>0 && Ticket[i+1]== 0 && OC[i]>0 && OC[i+1]==0)
   {
      Result = OrderSelect(Ticket[i],SELECT_BY_TICKET);
       if (OrderType() == OP_BUY || OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP)
       {
         if ( OrderClosePrice() == OrderTakeProfit() )
            {            
               Ticket[i+1] = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,E1,0,StopLossBuy,TakeProfitBuy,NULL); // E1 E2 E3 are defined previously
               Ticket[i+1] = OrderSend(Symbol(),OP_BUYSTOP,LotSize,E2,0,StopLossBuy,TakeProfitBuy,NULL);
               Ticket[i+1] = OrderSend(Symbol(),OP_SELLSTOP,LotSize,E3,0,StopLossSell,TakeProfitSell,NULL);
            }
         else if ( OrderClosePrice() == OrderStopLoss() )
            {                
               Ticket[i+1] = OrderSend(Symbol(),OP_SELLLIMIT,LotSize,EP1,0,StopLossSell,TakeProfitSell,NULL); //EP1 EP2 EP3 are defined previously
               Ticket[i+1] = OrderSend(Symbol(),OP_SELLSTOP,LotSize,EP2,0,StopLossSell,TakeProfitSell,NULL);
               Ticket[i+1] = OrderSend(Symbol(),OP_BUYSTOP,LotSize,EP3,0,StopLossBuy,TakeProfitBuy,NULL);
                        
            }
       }
       else if ( OrderType() == OP_SELL || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP)
       {
         if ( OrderClosePrice() == OrderTakeProfit() )
            {
              
               Ticket[i+1] = OrderSend(Symbol(),OP_SELLLIMIT,LotSize,EE1,0,StopLossSell,TakeProfitSell,NULL); //EE1 EE2 EE3 are defined previously.
               Ticket[i+1] = OrderSend(Symbol(),OP_SELLSTOP,LotSize,EE2,0,StopLossSell,TakeProfitSell,NULL);
               Ticket[i+1] = OrderSend(Symbol(),OP_BUYSTOP,LotSize,EE3,0,StopLossBuy,TakeProfitBuy,NULL);                
            }
         else if ( OrderClosePrice() == OrderStopLoss()  )
            {                
               Ticket[i+1] = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,EEP1,0,StopLossBuy,TakeProfitBuy,NULL); //EEP1 EEP2 EEP3 are defined previously.
               Ticket[i+1] = OrderSend(Symbol(),OP_BUYSTOP,LotSize,EEP2,0,StopLossBuy,TakeProfitBuy,NULL);
               Ticket[i+1] = OrderSend(Symbol(),OP_SELLSTOP,LotSize,EEP3,0,StopLossSell,TakeProfitSell,NULL);
                        
            }        
       }
   }
   if (OC[i+1]==0 && Ticket[i+1]>0)
     {    
      if (TotalOpenOrders() == 1)  
      {      
         ClosePendingOrders();        
         Result = OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
         Ticket[i+1] = OrderTicket();
         OC[i+1] = OrderClosePrice();                  
       }        
     }
    if (OC[i+1] == 0 && Ticket[i+1] == 0) break;
  }
}    
}



I hope to be clear 

You're still not checking your return values. You assign them into variables (which suppresses the warnings in the compiler) but you aren't checking them.

And as we can't see how you have set your lotsize, entry price, TP, SL etc we don't know if the order functions are being correctly executed.

 

Thank you very much honest_knave I tryed to check all return values. 

Also the EntryPrice and Lotsize , are extern variables. For the TP and SL I set them as functions befor OnInit() :

double TakeProfitBuy( double Entry ) { double TP = Entry + (50*pip); return(TP);}
double TakeProfitSell( double Entry ) { double SL = Entry - (50*pip); return(SL);}
double StopLossBuy ( double Entry ) { double SL = Entry - (50*pip); return(SL); }
double StopLossSell ( double Entry ) { double TP = Entry + (50*pip); return(TP);}

 With the pip is : 

int OnInit()
  {
//---
   TickSize = MarketInfo(Symbol(),MODE_TICKSIZE);
   if(TickSize == 0.00001 || Point==0.001)
    pip = TickSize*10;
    else pip = TickSize;  
//---
   return(INIT_SUCCEEDED);
  } 

 Befor OnInit() I defined this functions too : 

  int TotalOpenOrders()
   {
   int OrderCount = 0;
    for (int i = OrdersTotal()-1; i >= 0; i--)
       {
           if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
           {
               if (OrderType() == OP_SELL || OrderType() == OP_BUY)
               OrderCount++;
           }    
       }
   return (OrderCount);
   }

  void ClosePendingOrders()
   {
   for(int i=OrdersTotal()-1;i>=0;i--)
    {
     if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
     {
         if (OrderType() == OP_SELL || OrderType() == OP_BUY)
         continue;
     }
     if(OrderDelete(OrderTicket())) return;
    }
   }

 In OnTick() I put this code : 


double OC [20] ;
int Ticket[20];

// In function OnTick() I put this code :

if ( TypeOrder == 1 /*&& OrdersTotal() == 0*/) // TypeOrder is an extern variable
{
   if (OrdersTotal() == 0 )
   {    
   if (OC[0] == 0)
      {
         Ticket0 = OrderSend(Symbol(),OP_BUYSTOP,Lot,Entry,0,StopLossBuy(Entry),TakeProfitBuy(Entry),NULL); // All variabls are defined previously.
         Result = OrderSelect(Ticket0,SELECT_BY_TICKET);
         OCP[0]= OrderClosePrice();
         Ticket[0] = OrderTicket();
      }
   }
  if (Ticket[0] > 0 && OC[0]> 0)
  {        
     for ( int i =0; i<5 ; i++)
  {
      if (Ticket[i] > 0 && Ticket[i+1] > 0 && OC[i] > 0 && OC[i+1] > 0) continue;
      else if (Ticket[i]>0 && Ticket[i+1]== 0 && OC[i]>0 && OC[i+1]==0)
   {
      if (OrderSelect(Ticket[i],SELECT_BY_TICKET))
       if (OrderType() == OP_BUY || OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP)
       {
         if ( OrderClosePrice() == OrderTakeProfit() )
            {            
               Ticket[i+1] = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,E1,0,StopLossBuy(E1),TakeProfitBuy(E1),NULL); // E1 E2 E3 are defined previously
               Ticket[i+2] = OrderSend(Symbol(),OP_BUYSTOP,LotSize,E2,0,StopLossBuy(E2),TakeProfitBuy(E2),NULL);
               Ticket[i+3] = OrderSend(Symbol(),OP_SELLSTOP,LotSize,E3,0,StopLossSell(E3),TakeProfitSell(E3),NULL);
            }
         else if ( OrderClosePrice() == OrderStopLoss() )
            {                
               Ticket[i+1] = OrderSend(Symbol(),OP_SELLLIMIT,LotSize,EP1,0,StopLossSell(EP1),TakeProfitSell(EP1),NULL); //EP1 EP2 EP3 are defined previously
               Ticket[i+2] = OrderSend(Symbol(),OP_SELLSTOP,LotSize,EP2,0,StopLossSell(EP2),TakeProfitSell(EP2),NULL);
               Ticket[i+3] = OrderSend(Symbol(),OP_BUYSTOP,LotSize,EP3,0,StopLossBuy(EP3),TakeProfitBuy(EP3),NULL);
                        
            }
       }
       else if ( OrderType() == OP_SELL || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP)
       {
         if ( OrderClosePrice() == OrderTakeProfit() )
            {
              
               Ticket[i+1] = OrderSend(Symbol(),OP_SELLLIMIT,LotSize,EE1,0,StopLossSell(EE1),TakeProfitSell(EE1),NULL); //EE1 EE2 EE3 are defined previously.
               Ticket[i+2] = OrderSend(Symbol(),OP_SELLSTOP,LotSize,EE2,0,StopLossSell(EE2),TakeProfitSell(EE2),NULL);
               Ticket[i+3] = OrderSend(Symbol(),OP_BUYSTOP,LotSize,EE3,0,StopLossBuy(EE3),TakeProfitBuy(EE3),NULL);                
            }
         else if ( OrderClosePrice() == OrderStopLoss()  )
            {                
               Ticket[i+1] = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,EEP1,0,StopLossBuy(EEP1),TakeProfitBuy(EEP1),NULL); //EEP1 EEP2 EEP3 are defined previously.
               Ticket[i+2] = OrderSend(Symbol(),OP_BUYSTOP,LotSize,EEP2,0,StopLossBu(EEP2),TakeProfitBuy(EEP2),NULL);
               Ticket[i+3] = OrderSend(Symbol(),OP_SELLSTOP,LotSize,EEP3,0,StopLossSell(EEP3),TakeProfitSell(EEP3),NULL);
                        
            }        
       }
   }
   if (Ticket[i+1]<0 && Ticket[i+2]<0 && Ticket[i+3] < 0) Comment("Error !!");
   else if (Ticket[i+1] > 0 && Ticket[i+2] > 0 && Ticket[i+3] > 0)
     {    
      if (TotalOpenOrders() == 1)      
         ClosePendingOrders();
         if (OrdersTotal() == 1)
            if (OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
             {
            Ticket[i+1] = OrderTicket();
            OC[i+1] = OrderClosePrice(); // The Problem is when i execute the expert, i finde a false value of this OrderClosePrice(), it return the price  of                                                       execution of the order while all other values [OrderTicket() , OrderType(), OrderStopLos() , OrderLoot()..... ] are true, I cheked them thrut a Comment
            Ticket[i+2] = 0 ;
            Ticket[i+3] = 0;
            break;          
       }        
     }
    if (Ticket[i] == 0 && Ticket[i+1] == 0 && OC[i] == 0 && OC[i+1] == 0) break;
  }
}    
}


 when i execut this code  on the terminal, i find all values are right and orders are exactly in theire places also when one is opened others pending orders are closed but the only problem is the return value of OrderClosePrice() of the opend order which return the open price of this Order

Reason: