How to close a position automatically in MQL5 after x bars?

 

Hi all, 

 

I want to code an EA in MQL5 which closes a position after x bars (let´s say after 6 bars or after 6 days in D1 chart) regardless of whether it's a win or a loss. Normally the position is already closed 1-5 bars after entry because of a trailing stop but sometimes not. Therefore I would need such an additional code. Is someone able to help me?

 

Thank you in advance. 

MQL5.community - User Memo
MQL5.community - User Memo
  • 2010.02.25
  • MetaQuotes Software Corp.
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
 
Jekkt:

Hi all, 

 

I want to code an EA in MQL5 which closes a position after x bars (let´s say after 6 bars or after 6 days in D1 chart) regardless of whether it's a win or a loss. Normally the position is already closed 1-5 bars after entry because of a trailing stop but sometimes not. Therefore I would need such an additional code. Is someone able to help me?

 

Thank you in advance. 

Use PositionGetInteger to obtain open time of your position.
 

you need to do a position select

see my code https://www.mql5.com/en/forum/10956

can be easily modified to suit your needs 

Getting current date without time
Getting current date without time
  • www.mql5.com
day); HistorySelect(StringToTime(CurrDate), TimeCurrent()); are there any simpler ways of getting the current date only?
 
Thank you both. I solved the problem with your help.
 
Jekkt:
Thank you both. I solved the problem with your help.

Can you please share the lines with this specific code? I need support also.

 
Rafael Anderle Lisboa #:

Can you please share the lines with this specific code? I need support also.

trying to achieve the same thing as well.

Reason: