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?
#define ONEDAY 86400
if( PositionSelect(symbol) ) {
long delta = TimeCurrent() - PositionGetInteger(POSITION_TIME);
if( delta % (x*ONEDAY) ) {
// close position after x days
}
}
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...
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
}
}
{
if(PositionSelect(_Symbol)) return((int)round((TimeCurrent()-myposition.Time()))/_Period/60);
return(-1);
}

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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?