This Python script automates the extraction of historical economic calendar data from the MQL5 website. It utilizes Playwright for browser automation to navigate the dynamic date-picker interface and retrieve event details, including actual, forecast, and previous values, along with importance ratings. The collected data is processed, cleaned, and exported to a CSV file for further analysis.
- Automated Navigation: Programmatically controls the MQL5 economic calendar interface to select specific date ranges.
- Dynamic Content Handling: Waits for JavaScript-rendered content to load before extracting data, ensuring completeness.
- Data Cleaning: Removes currency symbols, normalizes whitespace, and handles HTML entities to ensure data consistency.
- Configurable Date Range: Allows users to specify start and end years and months for data extraction.
- Output Format: Saves the scraped data into a structured CSV file (
mql5_calendar_YYYY_YYYY.csv) with UTF-8 encoding.
- Python 3.7+
- Required libraries:
pandasplaywright
Install the dependencies using pip:
pip install pandas playwrightAfter installing playwright, install the required browser binaries:
playwright install chromium- Clone or download the script
TESTecdata.py. - Run the script from the terminal:
python TESTecdata.py- Follow the interactive prompts to enter the desired date range:
- First year
- Last year
- First month
- Last month
Example input:
Please input the first year you wanna scrape data: 2020
Please input the last year you wanna scrape data: 2026
Please input the first month you wanna scrape data: 1
Please input the last month you wanna scrape data: 1
- The script will launch a Chromium browser instance, navigate to the MQL5 economic calendar, and begin scraping data for the specified period. Progress is logged in the console.
- Upon completion, the data is saved to a CSV file in the current directory.
The resulting CSV file contains the following columns:
| Column | Description |
|---|---|
| Date | Event date (YYYY-MM-DD) |
| Time | Event time |
| Currency | Currency code associated with the event |
| Event | Name of the economic event |
| Actual | Actual released value |
| Forecast | Market forecast value |
| Previous | Previous period's value |
| Importance | Impact level (e.g., High, Medium, Low) |
- Execution Time: Scraping large date ranges may take significant time due to the need for page loads and dynamic content rendering.
- Headless Mode: The browser runs in non-headless mode (
headless=False) by default to facilitate debugging and monitoring. Modify thelaunch()parameter if headless execution is preferred. - Rate Limiting: The script includes deliberate delays to avoid overwhelming the server and to ensure data integrity. Adjust timeouts cautiously if modifying the code.
- Error Handling: Basic error handling is implemented for network issues and unexpected interruptions. Users can stop the process safely using
Ctrl+C.
This project is provided as-is for educational and research purposes. Ensure compliance with MQL5's terms of service when using this scraper.