Closing after x days

 

Hi all,

i try to code an expert advisor with an automated position closing after x days. It is constructed an a daily basis and in Tradestation there is a function called "barssinceentry" for this routine. The entry is based on a moving average and the advisor have to save the date to close the position exactly x days after the opening. I tried to code something with mqldatetime but i didn't get through this. Has anyone a solution?

Documentation on MQL5: Standard Constants, Enumerations and Structures / Data Structures / Structure of the Date Type
  • www.mql5.com
Standard Constants, Enumerations and Structures / Data Structures / Structure of the Date Type - Documentation on MQL5
 
northquant:

Hi all,

i try to code an expert advisor with an automated position closing after x days. It is constructed an a daily basis and in Tradestation there is a function called "barssinceentry" for this routine. The entry is based on a moving average and the advisor have to save the date to close the position exactly x days after the opening. I tried to code something with mqldatetime but i didn't get through this. Has anyone a solution?

Running a code like below once a day (OnTrade event) may solve your problem.


#define ONEDAY 86400

if( PositionSelect(symbol) ) {

    long delta = TimeCurrent() - PositionGetInteger(POSITION_TIME);

    if( delta % (x*ONEDAY) ) {

        // close position after x days

    }

}


 
THX! I coded a combination of this with OnTick and day_of_year...
fireflies:
Running a code like below once a day (OnTrade event) may solve your problem.


#define ONEDAY 86400

if( PositionSelect(symbol) ) {

    long delta = TimeCurrent() - PositionGetInteger(POSITION_TIME);

    if( delta % (x*ONEDAY) ) {

        // close position after x days

    }

}



 
northquant:
THX! I coded a combination of this with OnTick and day_of_year...

Glad to hear that. My code is incorrect though. It should be something like below ('bigger than' relation operator instead of 'modulo' operation as it's rarely we'll have it hit exactly upto seconds.)

#define ONEDAY 86400

if( PositionSelect(symbol) ) {

    long delta = TimeCurrent() - PositionGetInteger(POSITION_TIME);

    if( delta > (x*ONEDAY) ) {

        // close position after x days

    }

}

 
int BarsSinceEntry()
{
   if(PositionSelect(_Symbol)) return((int)round((TimeCurrent()-myposition.Time()))/_Period/60);
   return(-1);
}