How to close an open position after a certain time?

 

Hi,

I just started coding for MT5. I am able to open a position, close it through take profit and stop loss, but I can't seem to get the code to close the open position if, say, it is after 5.00pm.

Are there any suggestions I may be aware of? Or could you simply let me know how an open position can be closed after a certain time?

Many thanks in advance,

 
manuzrp:

Hi,

I just started coding for MT5. I am able to open a position, close it through take profit and stop loss, but I can't seem to get the code to close the open position if, say, it is after 5.00pm.

Are there any suggestions I may be aware of? Or could you simply let me know how an open position can be closed after a certain time?

Many thanks in advance,

Hi,

You can use this way.
Happy coding :)

 
manuzrp: but I can't seem to get the code to close the open position if, say, it is after 5.00pm.
Why not? You know the current time (and when the position was opened.) What's your problem?
 
manuzrp:

Hi,

I just started coding for MT5. I am able to open a position, close it through take profit and stop loss, but I can't seem to get the code to close the open position if, say, it is after 5.00pm.

Are there any suggestions I may be aware of? Or could you simply let me know how an open position can be closed after a certain time?

Many thanks in advance,

What difficulties?

#include <Trade\Trade.mqh>

...

CTrade trade;
...
MqlDateTime time;
TimeCurrent(time);
if (time.day_of_week==5 && time.hour>=17 && time.min>=15) trade.PositionClose(...);
Reason: