• Overview
  • Reviews (2)
  • Comments (13)
  • What's new

EA Protection Filter

EA Protection Filter provides a news filter as well as a stock market crash filter, which can be used in combination with other EAs. Therefore, it serves as an additional protective layer for other EAs that do provide such filters.

During the backtest analysis of my own night scalpers, which already use a stock market crash filter, I noticed that the historic drawdown especially during stock market crash phases like 2007-2008 is reduced significantly by using such a filter. 

This type of protection filter usually only makes sense in combination with scalpers that want to avoid fast markets, so don't use it on impulse/price action strategies! 

Also, there is no guarantee that using this filter will be more profitable compared to the normal trading. 

How it works:

In MT4 an EA is not allowed to modify other EAs for safety reasons. However, it is possible to close and re-open charts, which can be used to turn on/off other EAs. 

The EA will also be able to close existing open positions or pending orders based on the given magic numbers or comments you provide as input. 

Important notes:

- Please test the filter on DEMO account first!

- First arrange all charts and EAs that you want to use in combination with this protection filter. Then save the profile in case something goes wrong: File -> Profile -> Save As...

- Please allow requests to http://fxdata.cc and http://backup.fxdata.cc.

- Attach this EA to a new chart of any symbol after (!) all other EAs are loaded.

- Before you close the MT4 or remove the EA press the button on the chart "Turn filter OFF (opens all charts)" to re-open all charts. Else it will not remember closed charts. 

- It cannot be guaranteed that the other EAs are compatible with this filter. Make sure that the EAs are ok with being removed and initialized possibly multiple times a day ( best ask the developer). 

- Charts can only be filtered by symbol/timeframe, but open trades only by magic/comment. So you have to find a combination that makes sense for your EAs. 


  • includeTimeFrames  - a comma separated list of time frames that should be monitored. For example, if you have non-scalpers on H1 that should not be closed, delete H1 from this list. 
  • excludeSymbols -a  comma separated list of symbols that should be excluded (if you have other EAs that should not use this filter)
  • magicNumbersToCloseOpenPositions - a comma separated list of magic numbers, for which positions should be closed
  • commentsToClosePositionsExact - a comma separated list of comments, for which positions should be closed (exact match)
  • commentsToClosePositionsIncluding - same as the last parameter, but here the comment only has to be included, not an exact match
  • maxSlippagePoints - maximum slippage for OrderClose() function
  • sleepSecondsBetweenChartOpen - to avoid terminal overload
  • filePrefix - name for the saved template files
  • debugMode - to write more information into the Expert log

  • signalSymbol - symbol for the SP500 on your broker
  • maxPercentageChange - the maximum allowed percentage change for the stock filter to trigger
  • onlyTriggerOnNegativeMovement - if false the stock market filter will also trigger on strong positive movement.
  • lookbackHours - period of the stock market movement
  • minimumKeepClosedMinutesStockFilter - minimum time to keep the chart closed after stock market filter triggered

  • useNewsFilter - whether to use the news filter
  • filterCentralBankEvents - whether to filter central bank events
  • filterSpeechesAndTestimonies - whether to filter speeches
  • filterHighImpact - whether to filter other high impact events
  • closeMinutesBeforeEvents - minutes before events when charts and positions will be closed
  • pauseMinutesAfter... - minutes after events for which the charts will be kept closed
  • minimumKeepClosedMinutesNews - minimum time to keep the chart closed after news filter triggered

  • startHourGMTwinter - start hour for the stock market filter. In summer all times will be automatically shifted 1 hour. 
  • endHourGMTwinter - end hour for the stock market filter
  • startHourGMTwinterNews - start hour for the news filter
  • endHourGMTwinterNews - end hour for the news filter
  • liveGMToffset - in case you want to set the GMT offset manually. If liveGMToffset=99 it will be determined automatically. 

Lucy 2019.03.03 09:13 

A filter from a reliable author.

This is what I'm looking for.


Mar. 12 : Masterpiece.

Xu Yaji
Xu Yaji 2018.12.17 09:31 


Version 4.2 2019.04.30
A few parameters have been added:

- filterMediumImpact: If true the EA will also filter medium impact events.

- closeMinutesBeforeBankEvents, closeMinutesBeforeSpeeches and closeMinutesBeforeOtherEvents can now be used to set different time intervals when the positions should be closed before coming news.
Version 4.1 2019.03.20
Correct a visual bug that would show "stock market filter deactivated" while it was active.
Version 3.8 2019.01.11
Added parameter closePositionsBeforeChartsOnMaxPL. If true it will lead to faster close when max open loss/profit is reached (for example during a flash crash), but it could also lead to interactions with still active charts/EAs.

Also changed trading pause so that it will only close after the same hour as the Friday close on the day given by stopTradingMonth and stopTradingDay.
Version 3.7 2019.01.11
- Added handleOpenProfitLossLikeCrashTrigger. If true the EA will close all position when the loss of open positions is larger than maxOpenLoss or when the proft of open positions is larger than maxOpenProfit. If those values are 0 it will not be used. It will handle it the same way as the stock market crash trigger, which means it will open charts again after the given time.
The open Profit/Loss and the number of monitored positions will be shown on the chart as visual feedback.

- Added stopTradingMonth ,stopTradingDay, startTradingMonth and startTradingDay so that users can define a trading pause where all monitored charts and positions will be closed.
Version 3.6 2018.12.29
Changed variable useStockFilterSundayNight to useStockFilterSundayNightAndDec25Jan1. If false, it will now also allow to not use the stock filter on the night of the 25th Dec and 1st Jan (GMT time). It is useful in case a user prefers to always trade the market open even if there was a stock market crash trigger before the weekend/holiday.
Version 3.5 2018.12.23
-Added a parameter handle24and31DecemberLikeFriday to automatically close on Christmas and New Year's Eve similar to Friday close.
Version 3.4 2018.12.08
The last version was a bit inconsistent in the case there the stock market filter triggered Friday (and therefore also Saturday/Sunday). The new version has a new variable useStockFilterSundayNight.
If true it will always use the Friday close time (+ 48 hours + minimumKeepClosedHoursStockFilter hours) as time to open charts again (Monday morning).
If false it will open charts again on Saturday and don't check stock filter until after Monday morning. So it will always allow Sunday night's trading even if there is a gap in stock markets.
Version 3.3 2018.12.06
Added the lengthy variable onlyTryPositionCloseOnceAfterChartCloseError, which needs a bit of explaining.
The problem is if there is an error after trying to close a chart multiple times, then it might be that the chart and other EA is still active. Therefore, we don't want to get into a situation where the other EA opens positions and the filter directly closes them again multiple times. If you have onlyTryPositionCloseOnceAfterChartCloseError=true it will only try to close the position once in case there was an error (only for the symbols that had an error).
However, if the symbol has already closed charts and the position close is not successful the first time, it could then lead to unmonitored positions. But the probability should be small.
Version 3.2 2018.12.04
Changed the stock filter to only add a new bar if the bar time changed to prevent adding the same bar multiple times during holidays.
Version 3.1 2018.11.30
The filter will now use MT4 GMT time to also be able to check for news during the weekend. This means that it is important that your server data is correct. But the filter will give a warning if it detects a difference between my server's GMT time and MT4 GMT time.
Version 3.0 2018.11.30
- The filter will now set global variables for all symbols in market watch. So if the other EA is able to use those for opening and closing of positions, there is no need to add it to the monitored charts.
But if it only check for opening and you also want the positions to be closed, you should include the magic in the filter input and also have at least one chart with the given symbol open.
- The filter will now also close charts during the weekend to prevent trading on market open in case there was a news release before.
Version 2.9 2018.11.24
Added an additional check to retry closing a chart if it did not work on the first attempt.
Version 2.8 2018.11.20
Added a parameter:
waitMinutesBetweenClosePositionAttempts - When news or stock filter triggers the EA will close all positions depending on the given settings. It will then wait at least waitMinutesBetweenClosePositionAttempts minutes until again checking to close positions.
Version 2.7 2018.11.17
Small fix for Friday close issue.
Version 2.6 2018.11.17
Fixed an issue where it would not open charts automatically on Saturday after they were closed on Friday.
Version 2.5 2018.11.15
Changed closeFridaySymbols to closeFridayCurrencies because it requires less input. For example, just set closeFridayCurrencies=GBP to close all charts of symbols containing GBP at the given FridayCloseHourGMTwinter and FridayCloseMinute.
Version 2.4 2018.11.15
Added a parameter closeFridaySymbols, where the user can insert a list of symbols for which charts should be closed on Friday at the given FridayCloseHourGMTwinter and FridayCloseMinute. If the list is empty, no charts will be closed on Friday.
Version 2.3 2018.11.14
- Added sleepSecondsBetweenChartClose to prevent MT4 terminal overload when closing many charts at the same time.

- Added parameters FridayCloseHourGMTwinter and FridayCloseMinute. The EA will close all charts at the given time and close open position to avoid holding positions over the weekend. However, some EAs are specifically designed to trade during the weekend. Set FridayCloseHourGMTwinter=-1 if you want to disable this function.
Version 2.2 2018.11.10
Small fix to make delaySecondsOnInitialization work properly during weekends.
Version 2.1 2018.11.09
Added parameters startHourGMTwinterNewsCharts and endHourGMTwinterNewsCharts.
They allow to set different windows for the news filter:
Between startHourGMTwinterNews and endHourGMTwinterNews the news filter will be active and close positions (but not charts).
Between startHourGMTwinterNewsCharts and endHourGMTwinterNewsCharts the news filter will also close charts (only if also within the normal news filter time window).

Also added a parameter delaySecondsOnInitialization. It will cause the EA to wait a few seconds on normal initialization. For example, after you restart MT4 it makes sure that the EA will read the charts after all EAs on other charts have been initialized. After pressing F7 to change settings it will not cause a delay.
Version 2.0 2018.11.05
Resolved an issue that could prevent loading a template on some MT4 terminals.
Version 1.9 2018.11.05
The new version will not open all charts on pressing F7. Use the chart button "Turn filter OFF" to do that before modifying the EA settings.
Version 1.8 2018.11.05
Added parameter add48hoursIfTriggerWasFriday in case you want to also prevent Sunday/Monday morning trading if there was a stock market crash on Friday. The EA will then automatically add 48 hours to the minimumKeepClosedHoursStockFilter value.

It will also now show the reference time to which the percentage change of SP500 will be calculated. Be aware that on first initialization it will load X bars, so it might be that the reference time has not the same minute compared to the current time.
Version 1.7 2018.11.04
Smal fix for the OnTimer() function so that it will not update during the weekend.
Version 1.6 2018.11.03
Small fix for lookback period.
Version 1.5 2018.11.03
- News is now only checked every minute and the chart also only updates every minute to reduce CPU and memory usage.
- There ois now a parameter onlyCheckStockPriceEveryMinute in case the user also only wants to check stock prices every minute.
Version 1.4 2018.11.03
The stock filter now uses M5 bars for more exact 8h period.
Version 1.3 2018.11.02
- There was a small bug that has been solved now.
- The charts list will now use multiple rows if more than 8 charts are monitored.
Version 1.2 2018.11.02
I decided to again delete openAllChartsAndTurnOffFilter. It is now replaced by a visible button on the chart "Turn filter OFF (opens all charts)", which you should press in case you have closed charts and want to remove the EA without losing those charts.
Version 1.1 2018.11.02
Added a parameter openAllChartsSlowlyAndTurnOffFilter.

MT4 ignores the sleep() function when removing an EA, which means that if you have many closed charts, it will try to open all at once without time in between. This can cause CPU overload and errors in opening the charts.

So if you have many closed charts, first press F7, set openAllChartsAndTurnOffFilter=true and wait until all charts are open. Then the filter will be turned off and you can remove it from the chart.