Can't close position

 

I just started studying mql5 and i want to write a program that open a position at the opening of a bar and close it at the close(in 5M timeframe, so after 5 minutes).

for the opening there are no problems, but running the backtest i notice that it stops after opening the first order; can anyone tell me if there is something wrong in the close function?

void close(datetime open){ //function to close my position at the end of the candle in 5M timeframe

   bool close = true;
   MqlDateTime MyOpen, now;
   TimeToStruct(open,MyOpen);

   while(close){ // until the position is closed the function doesen't end
      TimeToStruct(TimeCurrent(),now);
      if(MyOpen.min == 55){ //i need to check the open minute because minutes are between 0/59
         while(close){
            if(now.min == 0){
               trade.PositionClose(0);
               close = false; // if the position is closed the loop ends
            }      
         }
      }   
      else if(now.min == (MyOpen.min+5)){
         trade.PositionClose(0);
         close = false;
      }      
   }

}
 

here is a better way to do it


check

if (iTime(NULL,0,0) - OrderOpenTime() > 60 * 5)

CloseOrder()


you need to loop through all the opened orders

 
Jean Francois Le Bas:

here is a better way to do it


check

if (iTime(NULL,0,0) - OrderOpenTime() > 60 * 5)

CloseOrder()


you need to loop through all the opened orders


Thank you!
 
Jean Francois Le Bas:

here is a better way to do it


check

if (iTime(NULL,0,0) - OrderOpenTime() > 60 * 5)

CloseOrder()


you need to loop through all the opened orders


sorry if i ask you again, but after writing if(iTime(NULL,0,0) - open > 300) it gives me 2 errors: " '-' - illegal operation use" and " '>' - illegal operation use"
 
edopica:


sorry if i ask you again, but after writing if(iTime(NULL,0,0) - open > 300) it gives me 2 errors: " '-' - illegal operation use" and " '>' - illegal operation use"

These errors could be caused by other lines - show more of your latest code.