A little help with a closing position time function, please

 

Hello, all.

I'm having an issue tiwh this part of my code:

            datetime UltimaPosAberta = PositionGetInteger(POSITION_TIME);
            if(UltimaPosAberta <= TimeCurrent() + 300)
              {
               Print("Limite de Tempo da Operação");
               FecharPosicao();
              }  

OR

            if(TimeCurrent() >= PositionGetInteger(POSITION_TIME) - 300)
              {
               Print("Limite de Tempo da Operação");
               FecharPosicao();
              } 


I want all positions to close after 5 minutes, be that with profit or not.

What am I missing?

Thank you!


**Note:

The "FechaPosicao()" function is working fine to close it all.

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
if(TimeCurrent() >= PositionGetInteger(POSITION_TIME) - 300)
if(UltimaPosAberta <= TimeCurrent() + 300)

Current time will always be greater than position time. Your condition is always true.

Don't you want current time greater than position time by 5 minutes? THINK.

 
William Roeder:

Current time will always be greater than position time. Your condition is always true.

Don't you want current time greater than position time by 5 minutes? THINK.

You're right.

I kept reading stuff, changed a little, but still it's not getting "TRUE" for my if

                     long update_time_msc=PositionGetInteger(POSITION_TIME_UPDATE_MSC); 
                     if(TimeToString(update_time_msc/1000+300) <= TimeToString(TimeCurrent()))
                        {
                           Print("Limite de Tempo da Operação");
                              FecharPosicao();
                        }  
 
  1. Why are you converting time to string for comparison? Just compare.
  2. Your original want was 5 minutes after position time. Just do that.
    if(TimeCurrent() - PositionGetInteger(POSITION_TIME) >= 300)
    if(TimeCurrent() - UltimaPosAberta >= 300)

 
William Roeder:
  1. Why are you converting time to string for comparison? Just compare.
  2. Your original want was 5 minutes after position time. Just do that.

I tried it.

long UltimaPosAberta = PositionGetInteger(POSITION_TIME);
	if(TimeCurrent() - UltimaPosAberta >= 300)
        {
        Print("Limite de Tempo da Operação");
        FecharPosicao();
        }  


See that it only closes the position at 17:30 (only because there is another 'if' for that).


if(TimeToString(TimeCurrent(),TIME_MINUTES) == "17:30" && PositionSelect(_Symbol)==true)
     {
      Print("-----> Fim do Tempo Operacional: encerrar posições abertas!");

      FecharPosicao();}
 

I don't know why it was not working, but... somehow it did:


         if((datetime)PositionGetInteger(POSITION_TIME) <= TimeCurrent()-300)
                        {
                           FecharPosicao();
                        } 


Thanks for all the assistence, buddy.

Reason: