指定
Objective
Develop an MQL5 Expert Advisor (EA) that:
1. Saves historical economic news data** from MetaTrader’s economic calendar into a `.csv` file for backtesting purposes.
2. Implements news event filtering** for both backtesting and live trading, based on user-defined parameters such as event impact level, currency, and pre/post event trade restrictions.
Scope of Work
The EA must perform the following tasks:
1. Historical News Data Retrieval and Storage:
- Retrieve news events from MetaTrader’s built-in calendar, with user-defined filtering options for the news data to be saved (date range, importance level, and currency).
- Save this data to a `.csv` file in the `MQL5/Files` directory.
2. Live Trading News Event Filter:
- Apply news event filters in live trading based on the MetaTrader calendar to prevent trading before and after high-impact news events.
- Users should be able to customize the impact level, currencies, and the pre/post news window for the filter.
3. Backtesting with News Event Filter:
- During backtesting, read the saved news data from the `.csv` file and apply the same news event filters to simulate how the strategy would perform under real-time news conditions
Functional Requirements
1. User Input Parameters
The EA should allow users to customize the following parameters:
1. News Data Retrieval Inputs:
- StartYear** (`int`): The starting year for retrieving historical news data.
- EndYear** (`int`): The ending year for retrieving historical news data.
- Currencies*(`string`): A comma-separated list of currencies (e.g., `USD,EUR,GBP`) to filter which currencies' news data is saved.
- ImportanceLevel (`enum`): A selection of impact levels (`High`, `Medium`, `Low`) for filtering news events during data retrieval.
2. News Event Filter Inputs:
- PreNewsFilterMinutes (`int`): The number of minutes before a news event during which trading should be restricted.
- PostNewsFilterMinutes(`int`): The number of minutes after a news event during which trading should be restricted.
- FilterCurrencies (`string`): Currencies to be filtered during live trading and backtesting (comma-separated, e.g., `USD,EUR`).
- FilterImportance (`enum`): The impact level for news filtering (`High`, `Medium`, `Low`).
3. File Name for Saving/Loading News Data:
- CsvFileName (`string`): Name of the `.csv` file where news data is saved and from which it is read during backtesting.
2. News Data Retrieval and CSV File Writing
The EA should retrieve historical economic calendar data from MetaTrader, filtered by the user inputs. Once the data is retrieved, it should be saved into a `.csv` file in the `MQL5/Files` directory.
Filtering Logic for News Data Retrieval:
- Retrieve the news data using the `CalendarValueHistory` function for historical events.
- Filter the data based on the user-selected currencies, importance level, and the date range defined by `StartYear` and `EndYear`.
3. Reading News Data for Backtesting
During backtesting, the EA should read the news events from the previously saved `.csv` file. The news events should be filtered based on the user-defined parameters (`FilterCurrencies`, `FilterImportance`) and applied to the strategy to simulate how it would perform around news events.
Key Features for Backtesting:
- The same filtering logic used during live trading should be applied during backtesting.
- The strategy should prevent trading before and after news events based on the `PreNewsFilterMinutes` and `PostNewsFilterMinutes` inputs.
4. Live Trading News Event Filter
During live trading, the EA should use MetaTrader’s built-in calendar to filter out trades around news events. The filter should prevent trades from being opened within the specified time window before and after a news event.
Steps for Live News Event Filtering:
1. Retrieve Upcoming News Events:
- Use MetaTrader’s `EconomicCalendarValue` function to get the upcoming news events.
2. **Apply Filter to Trading Logic:
- Prevent trades from being opened within the specified pre/post news time window.
- Filter based on the currencies and importance levels selected by the user.
User-Defined Inputs for Live Trading Filter:
-Currencies to Filter: Users should specify which currencies are relevant for filtering.
- Importance Levels: The EA should only consider news events of the specified importance level (e.g., High).
5. Additional Considerations
1. Error Handling:
- Ensure that the EA handles file read/write errors gracefully (e.g., if the `.csv` file is missing or cannot be opened).
- Implement proper logging for debugging purposes.
2. Optimization:
- Efficiently manage large news data files, particularly if retrieving several years' worth of news events.
3. Performance Monitoring:
- Include detailed logging for when trades are skipped due to news events.