The MT4 EA News Events tool checks for important news and blocks the opening of new trades at times of greater volatility and uncertainty resulting from impactful news.
In addition, it has the function of closing open trades that were profitable before the news release.
The idea of this tool is to protect accounts and close orders in profit that could be stopped at a loss in times of great instability caused by news.
The tool uses the ForexFactory calendar to track the news.
The MT4 EA News Events is intended to:
- Monitor Upcoming News Events: It uses the Forex Factory Calendar (via the
FFCal.mqh
include file) to detect upcoming high-impact news events. - Disable Trading Before News: It disables trading a specified amount before a high-impact news event.
- Close Profitable Orders: Before disabling trading, it attempts to close any profitable orders from specified Expert Advisors.
- Re-enable Trading After News: It re-enables trading a specified amount of time after the news event has passed.
Detailed Explanation
Let’s go through each part of the code to understand how it functions.
1. Include Files and Properties
#include <FFCal.mqh>
:- Includes the
FFCal.mqh
file, which provides functions and variables to access Forex Factory Calendar data. This allows the EA to retrieve upcoming news events and their details.
- Includes the
#property strict
:- Enforces strict compiler checks, helping to catch potential errors and enforce good coding practices.
#property indicator_chart_window
:- Specifies that the EA (though typically used for indicators) will be attached to the main chart window.
2. External Inputs (Parameters)
These are user-configurable parameters:
TimeToNewsDisableTrading
:- The number of minutes before a high-impact news event when trading should be disabled.
- Default value:
60
minutes.
TimeAfterNewsEnableTrading
:- The number of minutes after a high-impact news event when trading should be re-enabled.
- Default value:
60
minutes.
ProfitPointsThreshold
:- The minimum profit threshold in points for orders to be considered for closing.
- Only orders with profit equal to or greater than this threshold will be closed when disabling trading.
- Default value:
10
points.
3. Initialization and De-initialization Functions
OnInit()
:- Called when the EA is first initialized.
- Currently, it contains no code but can be used for setup tasks like initializing variables or refreshing news data.
OnDeinit()
:- Called when the EA is de-initialized or removed from the chart.
- Can be used for cleanup tasks.
4. Main Function:
- Purpose:
- This function is called every time a new tick (price update) is received.
- It checks for upcoming high-impact news events and manages trading accordingly.
- Functionality:
- Retrieve Next News Event Details:
datetime nextNewsTime = getNextNewsTime();
- Retrieves the time of the next upcoming news event.
string newsImpact = getNextNewsImpact();
- Retrieves the impact level (“High”, “Medium”, “Low”) of the next news event.
- Check for High-Impact News:
if(newsImpact == "High")
- Proceeds only if the next news event is of high impact.
- Disable Trading Before News:
- Condition:
(nextNewsTime - TimeCurrent()) <= TimeToNewsDisableTrading * 60
- Checks if the time remaining until the next news event is less than or equal to the specified time to disable trading (converted to seconds).
GlobalVariableGet("DisableTrading") != 1
- Checks if trading is not already disabled.
- Actions:
GlobalVariableSet("DisableTrading", 1);
- Sets a global variable to indicate that trading is disabled.
CloseProfitableOrders();
- Calls the function to close profitable orders.
Print("Trading disabled due to upcoming high-impact news.");
- Logs a message to the Expert Advisor’s journal.
- Condition:
- Re-enable Trading After News:
- Condition:
(TimeCurrent() - nextNewsTime) >= TimeAfterNewsEnableTrading * 60
- Checks if the current time is at least the specified time after the news event (converted to seconds).
GlobalVariableGet("DisableTrading") != 0
- Checks if trading is currently disabled.
- Actions:
GlobalVariableSet("DisableTrading", 0);
- Resets the global variable to indicate that trading is enabled.
Print("Trading re-enabled after news event.");
- Logs a message to the Expert Advisor’s journal.
- Condition:
- Retrieve Next News Event Details:
5. Closing Profitable Orders:
- Purpose:
- Closes profitable orders that meet certain criteria before disabling trading due to an upcoming high-impact news event.
- Functionality:
- Iterate Through All Open Orders:
for(int i = OrdersTotal() - 1; i >= 0; i--)
- Loops through all open orders in reverse order.
- Select Each Order:
OrderSelect(i, SELECT_BY_POS, MODE_TRADES)
- Selects the order at position
i
in the list of open trades.
- Selects the order at position
- Check if Order Belongs to Specified EAs:
int magicNumber = OrderMagicNumber();
- Retrieves the magic number of the order, which is a unique identifier used to distinguish orders placed by different EAs.
if(IsOtherEAMagicNumber(magicNumber))
- Calls a function to check if the order’s magic number matches those specified for other EAs.
- Calculate Order Profit:
double profit = OrderProfit() + OrderSwap() + OrderCommission();
- Calculates the total profit of the order, including swaps and commissions.
- Check Profit Threshold:
if(profit >= ProfitPointsThreshold * Point)
- Checks if the order’s profit is equal to or greater than the specified profit threshold (converted to the appropriate currency value using
Point
).
- Checks if the order’s profit is equal to or greater than the specified profit threshold (converted to the appropriate currency value using
- Attempt to Close the Order:
OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE)
- Attempts to close the order with a slippage of
3
and no specific color for the closing arrow.
- Attempts to close the order with a slippage of
- Logging Results:
- If successful, prints a message indicating the order was closed and the profit.
- If failed, prints an error message with the error code.
- Iterate Through All Open Orders:
6. Checking Magic Numbers:
- Purpose:
- Determines whether a given magic number matches those of other EAs whose orders should be managed by this EA.
- Functionality:
- Define Magic Numbers:
int otherEAMagicNumbers[] = {1111, 2222, 3333};
- An array containing the magic numbers of other EAs. Note: You should replace these values with the actual magic numbers used by your EAs.
- Check for a Match:
- Loops through the array and checks if the provided
magicNumber
matches any in the list.
- Loops through the array and checks if the provided
- Return Result:
- Returns
true
if a match is found. - Returns
false
if no match is found.
- Returns
- Define Magic Numbers:
How the EA Works in Practice
- Setup:
- Attach the EA to a chart in MetaTrader 4.
- Ensure that
FFCal.mqh
is correctly included and that it can access Forex Factory Calendar data. - Configure the external inputs (
TimeToNewsDisableTrading
,TimeAfterNewsEnableTrading
,ProfitPointsThreshold
) as desired. - Replace the placeholder magic numbers in
IsOtherEAMagicNumber()
with the actual magic numbers of your EAs.
- Monitoring News Events:
- The EA continuously monitors for upcoming high-impact news events using functions provided by
FFCal.mqh
.
- The EA continuously monitors for upcoming high-impact news events using functions provided by
- Disabling Trading Before News:
- If a high-impact news event is detected and is within the specified time window before the event, the EA:
- Sets a global variable to indicate trading is disabled.
- Attempts to close profitable orders from specified EAs.
- Logs the action.
- If a high-impact news event is detected and is within the specified time window before the event, the EA:
- Re-enabling Trading After News:
- After the specified time has passed following the news event, the EA:
- Resets the global variable to re-enable trading.
- Logs the action.
- After the specified time has passed following the news event, the EA:
- Closing Profitable Orders:
- Before disabling trading, the EA seeks to minimize exposure by closing orders that are profitable and meet the specified profit threshold.
Advantages of Using This EA
- Risk Management Around News Events:
- Minimize Volatility Risk:
- High-impact news events can cause significant market volatility. By disabling trading before such events, you reduce the risk of large, unexpected market moves affecting your positions.
- Minimize Volatility Risk:
- Automated Trade Management:
- Hands-Free Operation:
- The EA automates the process of monitoring news events and managing trades, saving you time and ensuring consistent execution.
- Hands-Free Operation:
- Selective Order Management:
- Targeted Action:
- By specifying magic numbers, you can control which EAs’ orders are managed, allowing for tailored strategies.
- Targeted Action:
- Profit Protection:
- Secure Gains:
- The EA attempts to close profitable positions before high-impact news, locking in profits that might otherwise be at risk due to volatility.
- Secure Gains:
- Flexibility and Customization:
- Configurable Parameters:
- You can adjust the time windows for disabling and re-enabling trading and set your desired profit threshold.
- Configurable Parameters:
- Global Trading Control:
- Integration with Other EAs:
- By using global variables, the EA can communicate with other EAs to prevent new trades from being opened during high-risk periods.
- Integration with Other EAs:
- Logging and Transparency:
- Informative Outputs:
- The EA logs its actions, providing transparency and aiding in monitoring and troubleshooting.
- Informative Outputs:
Considerations for Use
- Ensure Accurate News Data:
- The EA relies on
FFCal.mqh
for news event data. Ensure that this file is up-to-date and functioning correctly.
- The EA relies on
- Broker Server Time:
- Be aware of any time zone differences between the news event times and your broker’s server time. Adjustments may be necessary.
- Test Before Live Deployment:
- Test the EA on a demo account to ensure it works as expected with your specific setup.
- Adjust Magic Numbers:
- Replace the placeholder magic numbers with those of your actual EAs to ensure correct operation.
- Monitor EA Performance:
- Regularly check the EA’s logs and behavior to ensure it’s performing as intended.
Other information about the MT4 EA News Events
For the MT4 EA News Events to work, the other EAs running on the account must have the disabletrading function active so that it can disable trading by other robots at times when it detects that there is important news and thus prevent trades from being opened at those times.
Our robots have this function, so they are suitable for use with this account protection tool in times of high market volatility.
Where to test the MT4 EA News Events?
You can test the EA in any Broker that offers MT4 Demo Accounts.
4XC – MT4 Demo Account and 100% deposit bónus with the promo link: PROMO LINK
AVA Trade – MT4 Demo Account: LINK FOR DEMO
ROBO FOREX: MT4 Demo Account: LINK FOR DEMO
Download the MT4 EA News Events below and follow a test account
For the MT4 EA News Events to work, three files are required:
NewsEvents_V1.ex4 – this is to be placed in the Experts folder
FFCal.ex4 – this is to be placed in the Indicators folder
FFCal.mqh – this is to be placed in the Include folder
Below, you can find a link to download the three files.
Below, you can find the test account using our other EAs and this Tool:
Other Posts For You:
Break Even Function in Metatrader
Conclusion
This MT4 EA News Events is a valuable tool for traders who use multiple Expert Advisors and wish to manage trading activity around high-impact news events. Automating the process of disabling trading, closing profitable positions, and re-enabling trading after news events helps mitigate risks associated with market volatility during such times.
The EA’s customizable parameters and selective order management provide flexibility, allowing you to tailor its operation to your specific trading strategies and risk tolerance. Its integration with the Forex Factory Calendar ensures access to up-to-date news event information.
Incorporating this EA into your trading routine can enhance your risk management practices and protect your trading capital during potentially turbulent market conditions.
Note: Always comply with your broker’s policies and trading platform guidelines when using automated trading tools. Additionally, consider consulting with a financial advisor or professional trader to align the EA’s settings with your overall trading strategy.