Download MetaTrader 5

Closing after x days

To add comments, please log in or register
Your broker does not support mobile MetaTrader 5? Ask him to buy them!
northquant
18
northquant 2011.09.13 15:09 

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
Jimmy
490
Jimmy 2011.09.14 07:59  
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

    }

}


northquant
18
northquant 2011.09.15 17:22  
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

    }

}



Jimmy
490
Jimmy 2011.09.16 07:42  
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

    }

}

Sasa Pucko
399
Sasa Pucko 2014.12.26 13:42  
int BarsSinceEntry()
{
   if(PositionSelect(_Symbol)) return((int)round((TimeCurrent()-myposition.Time()))/_Period/60);
   return(-1);
}
To add comments, please log in or register