Closeing Problems, Please help

 

Hello to all
Hope that the syntax is better than before. For the last week, I have been reading the Codersguru.

The program below, although does not have any errors, the compiler will log only one order. But my code will not close it.
The program states --- ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0," ",Blue), I have
not inserted the stoploss or the takeprofit, as you can see. Reason, there is a StopLoss condition already coded as such ---
if(OrderTicket() == OrderOpenPrice() - (ATR*2))
{
OrderClose(OrderTicket(), OrderLots(), 3, Blue)
So there is no need to insert another the StopLoss, no? As far as the TakeProfit
I will code that in when this other problem is solved.
Is it that the way I coded the closeing condition, or is there problems again with the syntax?
Thanks to all for your help, and may my problem help others,
Cheers

-----------------------------------------------------------------+
//|                                         Opening and Closeing.mq4 |
//|                                      Copyright © 2010, Ben banta |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Ben banta"
//+------------------------------------------------------+
//|                   Closing My Way                     |
//+------------------------------------------------------+



double ATR;
double Slippage = 3;
double StopLoss_Buy;
double StopLoss_Sell;
double Lots=0.4;
double MagicNumber;

int ticket;
int order;

//----------------- Closing Sell ---------------

int start() 
  {                                                          
    for(int index = OrdersTotal() -1; index >= 0; index--)                              
  {                                                               
    if (OrderSelect(index, SELECT_BY_TICKET)                    // existing orders
&&  OrderMagicNumber()  == MagicNumber                          // with my MN    
&&  OrderSymbol()       == Symbol())                            // with my symbol
  {                                                                                                                                                    
    if(OrderType() == OP_SELL)
    order = OrderTicket(); 
                       
    if(OrderTicket() == OrderOpenPrice() + (ATR*2))     // ATR*2 above Sell. Hard Stop
  {
      OrderClose(OrderTicket(),OrderLots(),3,Blue);   
      Print("Sell order closed with Hard Stop"); 
     }     
  else
  {
      Print("Error closing Sell Hard Stop", GetLastError()); 
   }   
//---------- Closeing Buy -------------
           
    if(OrderType() == OP_BUY) 
    order = OrderTicket(); 
    
    if(OrderTicket() == OrderOpenPrice() - (ATR*2))     // ATR*2 below Buy. Hard Stop
  {
      OrderClose(OrderTicket(),OrderLots(),3,Red); 
      Print("Buy order closed with Hard Stop");
     }     
  else 
  {
      Print("Error closing Buy Hard Stop", GetLastError());                                      
      return(0); 
    }                                              
  } 
}


//------------ Opening Buy -----------------

if(OrdersTotal() < 1)                                        // Checking for any working orders
   {                                   
     if (Ask > High[iHighest(NULL,0,MODE_HIGH,20, 1)])         // When this condition is true, 
                                                               // with no working orders  
   {                                                                                     
        ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0," ",0,0,Blue);
                                                               // Buy! Useing these parameters  
     if(ticket>0)
   { 
      if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))                                                       
      Print ("Buy Order Was a Big Success :" , OrderOpenPrice());
    }
  else
      Print ("Error Opening Buy Order :" , GetLastError()); 
      return(0);                                                  
  }  
                                                      
//--------------- Opening Sell ----------------
                                                                         
  if (Bid < Low[iLowest(NULL,0,MODE_LOW,20,1)])                //When this condition is true, 
                                                               //with no working orders
    {   
      ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0," ",0,0,Red);  
                                                               //Sell! Useing these parameters
   if(ticket>0)
     {     
       if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))                                                
       Print ("Sell Order Was a Big Success :" , OrderOpenPrice());
     }
    else
       Print ("Error Opening Sell Order :" ,GetLastError());
       return(0);
     }  
    return(0); 
  }
}
 
-----------------------------------------------------------------+
//|                                         Opening and Closeing.mq4 |
//|                                      Copyright © 2010, Ben banta |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Ben banta"
//+------------------------------------------------------+
//|                   Closing My Way                     |
//+------------------------------------------------------+



double ATR;
double Slippage = 3;
double StopLoss_Buy;
double StopLoss_Sell;
double Lots=0.4;
double MagicNumber;

int ticket;
int order;

//----------------- Closing Sell ---------------

int start() 
  {                                                          
    for(int index = OrdersTotal() -1; index >= 0; index--)                              
       {                                                               
        if (
           OrderSelect(index, SELECT_BY_TICKET)                    // existing orders
           &&  OrderMagicNumber()  == MagicNumber                  // with my MN    
           &&  OrderSymbol()       == Symbol()                     // with my symbol
           )                     
           {                                                                                                                                                    
            if(OrderType() == OP_SELL) order = OrderTicket(); 
                       
            if(OrderTicket() == OrderOpenPrice() + (ATR*2))     // ATR*2 above Sell. Hard Stop
              {
               OrderClose(OrderTicket(),OrderLots(),3,Blue);   
               Print("Sell order closed with Hard Stop"); 
              }     
              else
              {
               Print("Error closing Sell Hard Stop", GetLastError()); 
              } //ENDif (OrderTicket() == OrderOpenPrice() + (ATR*2))
                                 
                                                              //---------- Closeing Buy -------------
                     
              if(OrderType() == OP_BUY) order = OrderTicket(); 
               
              if(OrderTicket() == OrderOpenPrice() - (ATR*2))     // ATR*2 below Buy. Hard Stop
                {
                 OrderClose(OrderTicket(),OrderLots(),3,Red); 
                 Print("Buy order closed with Hard Stop");
                }     
                else 
                {
                 Print("Error closing Buy Hard Stop", GetLastError());                                      
                 return(0); 
                }//ENDif (OrderTicket() == OrderOpenPrice() - (ATR*2))                                              
           }//ENDIf ( OrderSelect(index, SELECT_BY_TICKET) etc
       }// END for loop               
                     
                             
                                        //------------ Opening Buy -----------------
                       
    if(OrdersTotal() < 1)                                        // Checking for any working orders
      {                                   
       if (Ask > High[iHighest(NULL,0,MODE_HIGH,20, 1)])         // When this condition is true, 
                                                               // with no working orders  
        {                                                                                     
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0," ",0,0,Blue);
                                                               // Buy! Useing these parameters  
         if(ticket>0)
           { 
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print ("Buy Order Was a Big Success :" , OrderOpenPrice());
           }
           else
           Print ("Error Opening Buy Order :" , GetLastError()); //ENDif (ticket>0)
             
         return(0);                                                  
        }//ENDif (Ask > High[iHighest(NU... etc  
                                                      
                                                             //--------------- Opening Sell ----------------
                                                                         
       if (Bid < Low[iLowest(NULL,0,MODE_LOW,20,1)])                //When this condition is true, 
                                                                    //with no working orders
          {   
           ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0," ",0,0,Red);  
                                                               //Sell! Useing these parameters
           if(ticket>0)
             {     
              if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print ("Sell Order Was a Big Success :" , OrderOpenPrice());
             }//ENDif (ticket>0)
             else
             Print ("Error Opening Sell Order :" ,GetLastError()); 
             //ENDif (OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                                 
           return(0);
          }//ENDif (Bid < Low[iLowest(NULL,0,MODE_LOW,20,1)])  
       return(0); 
      }//ENDif  (OrdersTotal() < 1)
}//ENDint start()
Should make you code blocks easier to read
 
if(OrderTicket() == OrderOpenPrice() - (ATR*2)) 
Order ticket is a very large integer (probably in the billions,) the key to the order.
OOP - 2ATR is a price.
The two will NEVER be equal
if you meant OrderClosePrice() == OOP-2ATR real numbers will almost never compare equals. use instead:
 if( OrderClosePrice() - OrderOpenPrice()-ATR*2 <= 0)//...

OrderSelect(index, SELECT_BY_TICKET)                    // existing orders
           &&  OrderMagicNumber()  == MagicNumber                  // with my MN    
           &&  OrderSymbol()       == Symbol() 
Your orderSend isn't setting a magic number
 
Ickyrus wrote >>
Should make you code blocks easier to read


Yes. You are correct. With each attempt and inquire, I learn a bit more. Thank you for cleaning up. It has just been the last few days that I could put the braces in the proper places. The simplier, the better.
Cheers
 
WHRoeder wrote >>
Order ticket is a very large integer (probably in the billions,) the key to the order.
OOP - 2ATR is a price.
The two will NEVER be equal
if you meant OrderClosePrice() == OOP-2ATR real numbers will almost never compare equals. use instead:

Your orderSend isn't setting a magic number


Hello WHRoeder
Thank you for looking in and giving the suggestions. But I must say I did not understand the OOP -2ATR right away.
OOP = OrderOpenPrice. That went right over my head at first. Your explaination is absolutely correct. I was caught up on the == sign. The OrderClosePrice() might never equal OrderOpenPrice() minus the (ATR*2). Thank you for catching that.
Let me see if I understand your suggestion. I'll restate it here. if(OrderClosePrice()-OOP()-ATR*2 <= 0)
This looks like it reads (I'll use arbitary figures here) OCP = 1.4361, OOP = 1.4321, ATR = 20. So the expression would look like:
if (1.4321 - 1.4361 - (20*2) <= 0), I'm sorry, but I do not understand.
Maybe I can use the same figures to rearrange the expression, to show what I meant. But this did not work either when I poked them into the program.
The new arrangement looks like this.

if (1.4361 >= 1.4321 + (20*2)).

Where the OCP >=OOP + ATR*2, control passes to:
OrderClose (OrderTicket(), OrderLots(), etc.

Your other observation was that the OrderSend is not setting a magic number.
Could I just take this part out of the program: && OrderMagicNumber == MagicNumber,
Thanks again, and if you can see other improvements, I would appriciate.
Cheers
 

Hello Ais
Thank you for your response. Hope you are doing well.
The suggestion was inserted this morning. Sorry to say the compilier does not like what I have coded. I am not sure what statement in the program is incorrect. The suggestions I believe are fine. I will repost the program and flag the area I believe maybe the problem. I'll also state the strategy for closeing.

The sell position will be stopped out above the order open price by Atr*2. Such as, OOP is 1.4321, Atr =20, and OCP = 1.4361.
So I coded the expression as,

if(OrderClosePrice >= OrderOpenPrice(), - (ATR*2)) // condition is met, control passes to....
OrderClose (OrderTicket(), OrderLots(), OrderClosePrice(),Blue) ;

Note: There is no StopLoss(), or TakeProfit() in the OrderSend statement, if that has any relevance.

Thanks again for everyones help
Cheers
double ATR;
double Slippage = 3;
double StopLoss_Buy;
double StopLoss_Sell;
double Lots=0.4;
double MagicNumber;

int ticket;
int order;

//----------------- Closing Sell ---------------

int start() 
  {                                                          
    for(int index = OrdersTotal() -1; index >= 0; index--)                              
  {                                                               
    if (OrderSelect(index, SELECT_BY_TICKET)                  // existing orders
//&&  OrderMagicNumber()  == MagicNumber                      // with my MN    
&&  OrderSymbol()       == Symbol())                          // with my symbol
  {                                                                                                                                                    
    if(OrderType() == OP_SELL) order = OrderTicket(); 
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<                       
    if(OrderClosePrice() >= OrderOpenPrice() + (ATR*2)) // condition to be met,
                                                        // control then passes to...
  {                                                     
      OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Blue); // OrderClose
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      Print("Sell order closed with Hard Stop"); 
     }     
  else
  {
      Print("Error closing Sell Hard Stop", GetLastError()); 
   }   
//---------- Closeing Buy -------------
           
    if(OrderType() == OP_BUY)  order = OrderTicket(); 
    
    if(OrderClosePrice() <= OrderOpenPrice() - (ATR*2))        // ATR*2 below Buy. Hard Stop
  {
      OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Red); 
      Print("Buy order closed with Hard Stop");
     }     
  else 
  {
      Print("Error closing Buy Hard Stop", GetLastError());                                      
      return(0); 
    }                                              
  } 
}


//------------ Opening Buy -----------------

if(OrdersTotal() < 1)                                        // Checking for any working orders
   {                                   
     if (Ask > High[iHighest(NULL,0,MODE_HIGH,20, 1)])         // When this condition is true, 
                                                               // with no working orders  
   {                                                                                     
        ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0," ",0,0,Blue);
                                                               // Buy! Useing these parameters  
     if(ticket>0)
   { 
      if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))                                                       
      Print ("Buy Order Was a Big Success :" , OrderOpenPrice());
    }
  else
      Print ("Error Opening Buy Order :" , GetLastError()); 
      return(0);                                                  
  }  
                                                      
//--------------- Opening Sell ----------------
                                                                         
  if (Bid < Low[iLowest(NULL,0,MODE_LOW,20,1)])                //When this condition is true, 
                                                               //with no working orders
    {   
      ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0," ",0,0,Red);  
                                                               //Sell! Useing these parameters
   if(ticket>0)
     {     
       if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))                                                
       Print ("Sell Order Was a Big Success :" , OrderOpenPrice());
     }
    else
       Print ("Error Opening Sell Order :" ,GetLastError());
       return(0);
     }  
    return(0); 
  }
}
 

Hello Huckleberry,
I am going to slightly restyle your program.
Bye for now

 

Why do "order = OrderTicket(); " and not use thit value in the orderclose() function?
While the chance is small that the selected order has changed would it not be safer to use the positivly identified order value?
-
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Blue);
OrderTicket() will be the currently selected ticket - at this stage in my learning
I can't tell which orderticket is selected when you do the do the OrderClose().

//// Edit
Actually I am not sure "order= OrderTicket()" is the ticket you identified in the preceding If statment.

 
Ais wrote >>

Hello Huckleberry,
I am going to slightly restyle your program.
Bye for now


Thank you so very much. I'll be waiting for your reply.
Cheers
 
Ickyrus wrote >>

Why do "order = OrderTicket(); " and not use thit value in the orderclose() function?
While the chance is small that the selected order has changed would it not be safer to use the positivly identified order value?
-
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Blue);
OrderTicket() will be the currently selected ticket - at this stage in my learning
I can't tell which orderticket is selected when you do the do the OrderClose().

//// Edit
Actually I am not sure "order= OrderTicket()" is the ticket you identified in the preceding If statment.


Hi Ickyrus
Thanks for your observation and suggestion. Your time is valuable.
Your reasoning makes perfect sense. I am not any more certain as you as it comes to selected order. I'll give your suggestion a try.
Thanks again
Cheers
Reason: