Loading…

Trading Hours Restrictor

This EA places no orders of its own. What it does is answer one question, continuously and visibly: is right now, in server time, inside the hours you have decided are acceptable to trade? The answer appears as an on-chart Comment and a periodic journal line, so it works as a companion to a manual trader deciding whether to act, or as a reference while watching an automated strategy for whether it should be active. See the notes below for what it would take to have another EA actually honour this window itself. The window is set as a start and end time, and can legitimately cross midnight - a 22:00 to 06:00 overnight session, for example. That is where nearly every trading-hours guard published for this platform gets the logic wrong: the segment after midnight belongs to the previous calendar day's session, so checking "is today's weekday enabled" against the segment after midnight checks the wrong day. A session starting Friday night would report itself closed the instant the clock ticks into Saturday, even though the session is still legitimately running. This EA checks the pre-midnight segment against today's weekday gate and the post-midnight segment against yesterday's, which is the only way to get an overnight window right. InpFlattenAtClose closes every open position, on every symbol, the instant the window transitions from open to closed. It is off by default because that decision - whether a trade in progress should be forced shut purely because the clock reached a boundary, regardless of its own stop loss or target - belongs to the operator, not to a default. Notes and limitations - This EA does not place, modify or block orders placed by another EA or by hand - it has no mechanism to intercept a trade. "Blocking new orders" means visible reporting only: a clear log line and an on-chart Comment every tick the window is closed. A companion EA that wants to actually honour this window would need to read the same window logic itself (or a shared signal this EA does not currently write) before sending its own orders - that cross-EA signalling was deliberately left out, because it would commit every consuming EA to a specific mechanism that is this EA's decision to make, not this project's. - The overnight-window logic is server time only. It does not know your local time zone or your broker's daylight-saving schedule; both shift the server clock relative to the wall clock you experience. - InpFlattenAtClose acts on every symbol on the account, not only the chart's own symbol, because "the window closed" is a decision about the operator's trading hours, not about one instrument. - If AutoTrading is off when InpFlattenAtClose needs to fire, the EA cannot flatten the account. It logs and comments this plainly. - A window with every weekday disabled never opens; the EA logs this at start-up in case it was not the intention.

Expert advisor · MT5 · v1.0 · Free

About Trading Hours Restrictor

Trading Hours Restrictor is an expert advisor (trading robot) for MetaTrader 5. It trades only in the hours you set (time filter). It has 14 settings, such as Start hour, Start minute and End hour. MQL source code included.

Files in the download: TradingHoursGuard.ex5, TradingHoursGuard.mq5, TradingHoursGuard.txt.

Trading robots can lose money. Check every setting and test on a demo account before using real funds.

Settings

SettingDefaultWhat it does
Start hour8Window start hour, server time (0-23)
Start minute0Window start minute (0-59)
End hour17Window end hour, server time (0-23)
End minute0Window end minute (0-59)
Monday enabledtrueTrade on Monday
Tuesday enabledtrueTrade on Tuesday
Wednesday enabledtrueTrade on Wednesday
Thursday enabledtrueTrade on Thursday
Friday enabledtrueTrade on Friday
Flatten at closefalseClose every open position the instant the window ends
Slippage pips3Slippage tolerance when flattening, in pips
Max retries5Close attempts per position before giving up
Retry wait ms500Pause between retry attempts, milliseconds
Status log seconds300How often the status line repeats while blocked

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Experts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
  6. Test it first on a demo account or in the Strategy Tester.

Download the PDF guide

Tags: Time Filter · MT5 Expert Advisor · MQL5 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria