Urgent.News

What's breaking now, across thousands of outlets.

Tech

Closing an MQL5 position after a fixed number of bars

A time exit is one of the simplest rules an EA can have: if a trade has not reached its stop or target after N bars, close it. It frees margin from trades that are going nowhere, it limits swap on positions that drift, and it is a useful research tool, because replacing the real exits with a plain time exit shows whether the entry has any edge on its own. It is also easy to get slightly wrong.…

A time exit is a straightforward rule for an Expert Advisor (EA) to automatically close a trade after N bars if it has not reached its stop or target. This assists in freeing up margin for trades with no chance of success, reduces swap costs on positions that linger, and serves as a valuable research tool to assess the intrinsic merit of an entry without the influence of real exit conditions.

However, time exits can be implemented incorrectly, such as counting individual ticks instead of bars, misinterpreting weekend hours as bars, or unintentionally closing the same position twice during high-volume trading moments.

This tutorial lays out the implementation of a time exit rule in three parts. The first part involves defining input parameters. The code includes the Trade.mqh library, sets a maximum number of bars (MaxBarsHeld) that the EA will let a position remain open before closing it, and assigns a unique MagicNumber to identify positions opened by this specific EA.

Subsequent steps involve calculating the number of completed bars since a position was opened and integrating the time exit logic into the EA's main loop, which executes once per bar.

Calculating the number of bars since a position opened requires the iBarShift function, which converts a given time into a bar index on the selected timeframe. The BarsHeld function takes the time the position was opened and returns the number of bars since that time. Using false as the third argument for iBarShift ensures that the function returns the index of the completed bar that contains the specified time, rather than a future bar.

A critical consideration when implementing time exits is the handling of partial closes and netting accounts. A partial close does not reset the time exit clock; the timer continues to run for the remaining volume. On a netting account, if an EA adds to an existing position, it retains the original open time for that position while subsequent closes use the current time. Developers must decide which time to use for the time exit rule, especially in complex trading scenarios.

The final step involves periodically checking for all open positions and closing those that have been held for more bars than the specified MaxBarsHeld limit. This check occurs only when a new bar opens, thereby minimizing computational overhead by avoiding unnecessary scans on every tick. The EA maintains the last checked bar's time after each successful close, allowing it to skip redundant checks if no new bar has occurred since the last scan.

To validate the time exit implementation, the tutorial suggests running the EA in the Strategy Tester with a small MaxBarsHeld value (e.g., 3) on a fast timeframe. The tester should confirm that each position closes precisely after the specified number of bars from its opening. Special attention should be paid to trades opened during weekends to ensure they do not prematurely close on Monday.

Comparing the EA's performance using pure time exits with its conventional stop and target levels is also recommended, as the true profitability may be determined by the latter.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Wednesday 4 November →