Dear Mr. Rosh,To close orders

 

Dear Mr. Rosh

I want to close orders and write a program like below

But the time has come,nothing happened,these orders still do not close anyway

What's wrong with me ?

It's a multi Symbol() ea.

666666 is a lucky numenber in China,but sadly I am unlucky !

Can you check the program ?

Thank you in adavanced !

int Duration=1;

int MagicExpert=666666;

int CheckForClose(int type)
{
if (Duration!=0)
{ //Lenght of time a postion is held

for(int i2=OrdersTotal();i2>=0;i2--)
{
if(OrderSelect(i2,SELECT_BY_POS,MODE_TRADES)==false) continue;
if(OrderMagicNumber()!=MagicExpert) continue;
if (OrderOpenTime()<TimeCurrent()-Duration*60*720)
{//Duration*60*720
if (OrderType()==OP_BUY)
{
// OrderClose(OrderTicket(),OrderLots(), OrderClosePrice(), 3, Yellow);
OrderClose(OrderTicket(), OrderLots(), Bid, 3,Yellow);
return(0);
}
if (OrderType()==OP_SELL)
{
OrderClose(OrderTicket(), OrderLots(), Ask, 3,Violet);
return(0);
}
}// end if(OrderOpenTime()<TimeCurrent()-Duration*60*720)
}// end for(int i2=OrdersTotal();i2>=0;i2--)
} //end duration check
}//end check for close

 
Please format your source properly



int  Duration=1;
 
int MagicExpert=666666;
 
int CheckForClose(int type)
 {
  if (Duration!=0)
   { //Lenght of time a postion is held 
 
    for(int i2=OrdersTotal()-1;i2>=0;i2--)
     {
     if(!OrderSelect(i2,SELECT_BY_POS,MODE_TRADES))        continue;
      if(OrderMagicNumber()!=MagicExpert) continue;
       if (OrderOpenTime()<TimeCurrent()-Duration*60*720)
        {//Duration*60*720 
         if (OrderType()==OP_BUY && type==OP_BUY)
          {
          OrderClose(OrderTicket(),OrderLots(),Bid,3,Yellow);
          }
         if (OrderType()==OP_SELL && type==OP_SELL)
          {
          OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
          }
        }// end if(OrderOpenTime()<TimeCurrent()-Duration*60*720)
     }// end for(int i2=OrdersTotal();i2>=0;i2--)
   }   //end duration check
 }//end check for close
 

Dear Mr. Rosh

Thank you very much !

I understood why you wrote OrdersTotal()-1 , The OrdersTotal() is not cerrect.

But I can't understand the words "(OrderType()==OP_BUY && type==OP_BUY)"

The parameter "type" has no meaning about BUY or SELL

In my program the "type" equal to 1.

The OrderType()==OP_BUY already says the ticket is "BUY"

that's enough !

Why should I must add other thing ?

 
If you want to close only BUY orders you can write
CheckForClose(OP_BUY);
 

So if i want to close BUY and SELL

What should I do ?

 
int  Duration=1;
 
int MagicExpert=666666;
 
int CheckForClose(int type)
 {
  if (Duration!=0)
   { //Lenght of time a postion is held 
 
    for(int i2=OrdersTotal()-1;i2>=0;i2--)
     {
     if(!OrderSelect(i2,SELECT_BY_POS,MODE_TRADES))        continue;
      if(OrderMagicNumber()!=MagicExpert) continue;
       if (OrderOpenTime()<TimeCurrent()-Duration*60*720)
        {//Duration*60*720 
         if (OrderType()==OP_BUY)
          {
          OrderClose(OrderTicket(),OrderLots(),Bid,3,Yellow);
          }
         if (OrderType()==OP_SELL)
          {
          OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
          }
        }// end if(OrderOpenTime()<TimeCurrent()-Duration*60*720)
     }// end for(int i2=OrdersTotal();i2>=0;i2--)
   }   //end duration check
 }//end check for close
I use your program like above
But no orders are closed 
 
Test this version

int  Duration=1;
 
int MagicExpert=666666;
 
int CheckForClose(int type)
 {
  if (Duration!=0)
   { //Lenght of time a postion is held 
 
    for(int i2=OrdersTotal()-1;i2>=0;i2--)
     {
     if(!OrderSelect(i2,SELECT_BY_POS,MODE_TRADES))        continue;
      if(OrderMagicNumber()!=MagicExpert) continue;
       //if (OrderOpenTime()<TimeCurrent()-Duration*60*720)
        {//Duration*60*720 
         if (OrderType()==OP_BUY)
          {
          OrderClose(OrderTicket(),OrderLots(),Bid,3,Yellow);
          }
         if (OrderType()==OP_SELL)
          {
          OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
          }
        }// end if(OrderOpenTime()<TimeCurrent()-Duration*60*720)
     }// end for(int i2=OrdersTotal();i2>=0;i2--)
   }   //end duration check
 }//end check for close
 

I will try your new version

Hope it work

 

Sadly !

It doesn't work !

What's the next ?

 
 
Well, it's a final attempt

int  Duration=1;
bool res; 
int MagicExpert=666666;
 
int CheckForClose(int type)
 {
  if (Duration!=0)
   { //Lenght of time a postion is held 
 
    for(int i2=OrdersTotal()-1;i2>=0;i2--)
     {
     if(!OrderSelect(i2,SELECT_BY_POS,MODE_TRADES))        continue;
      if(OrderMagicNumber()!=MagicExpert) continue;
       //if (OrderOpenTime()<TimeCurrent()-Duration*60*720)
        {//Duration*60*720 
         if (OrderType()==OP_BUY)
          {
          res=OrderClose(OrderTicket(),OrderLots(),Bid,3,Yellow);
          if (res!) Print("Order #",OrderTicket()," Close is failed with error ",GetLastError());
          }
         if (OrderType()==OP_SELL)
          {
          res=OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
          if (res!) Print("Order #",OrderTicket()," Close is failed with error ",GetLastError());
          }
        }// end if(OrderOpenTime()<TimeCurrent()-Duration*60*720)
     }// end for(int i2=OrdersTotal();i2>=0;i2--)
   }   //end duration check
 }//end check for close
See log on "Experts".
原因: