Inclustion of example EA
This commit is contained in:
@@ -1,55 +1,121 @@
|
||||
# MT5_Python_Strategy_Framework
|
||||
# MyLibs for MetaTrader 5
|
||||
|
||||
This project provides an experimental framework for integrating MetaTrader 5 (MT5) custom indicators and trading logic with Python-based data processing and strategy testing using [Freqtrade](https://www.freqtrade.io/).
|
||||
This is a modular MQL5 codebase containing reusable classes and utilities designed to accelerate EA development, backtesting, and live deployment for MetaTrader 5. It includes complete solutions for trade management, risk control, signal generation, time-based utilities, and more.
|
||||
|
||||
## Features
|
||||
|
||||
- 🧠 **Custom MT5 Libraries**: Modular `.mqh` files to handle position sizing, drawdown control, order management, and utility functions.
|
||||
- 🐍 **Python Scripts**:
|
||||
- `pre_process.py`: Prepares or cleans data before indicator processing.
|
||||
- `process_entry_indicators.py`: Extracts and processes entry signals.
|
||||
- `post_process_test.py`: Analyses backtest output or result data.
|
||||
- 📦 **Freqtrade-Compatible Module**: Python strategies and helpers located in `Python_freqtrade/` for integration with the Freqtrade framework.
|
||||
- 🛠️ **Project Structure Support**: Includes `.idea/` and `.vscode/` folders for JetBrains and VSCode IDE configurations.
|
||||
|
||||
## Project Structure
|
||||
> **Note**: To use this framework, the `MyLibs` folder must be placed inside your MetaTrader 5 **`Include/`** directory:
|
||||
>
|
||||
> ```
|
||||
> MQL5/Include/MyLibs/
|
||||
> ```
|
||||
|
||||
## Folder Structure
|
||||
```
|
||||
MT5_Python_Strategy_Framework/
|
||||
├── My_MQL5_Libs/ # Custom MQL5 include files
|
||||
├── Python_freqtrade/ # Freqtrade strategy components
|
||||
├── pre_process.py # Data pre-processing script
|
||||
├── process_entry_indicators.py # Entry signal extraction logic
|
||||
├── post_process_test.py # Backtest result post-processing
|
||||
├── .idea/, .vscode/ # IDE configs (optional)
|
||||
└── README.md # Project documentation
|
||||
MyLibs/
|
||||
├── BacktestUtils/
|
||||
│ ├── CustomMax.mqh # Defines custom optimization criteria for backtesting
|
||||
│ └── TestDataSplit.mqh # Supports splitting historical data into training/testing segments
|
||||
│
|
||||
├── Indicators/
|
||||
│ ├── AtrBands.mqh # Calculates and visualizes ATR-based dynamic bands
|
||||
│ └── TrendlineAnalyser.mqh # Detects trendline breaks and trend direction changes
|
||||
│
|
||||
├── Orders/
|
||||
│ ├── AdjustPosition.mqh # Manages stop loss adjustments, trailing stops, and breakevens
|
||||
│ ├── CalculatePositionData.mqh # Computes lot sizing and position metrics
|
||||
│ ├── EntryOrders.mqh # Handles logic for opening buy/sell orders
|
||||
│ ├── ExitOrders.mqh # Handles logic for closing trades under various conditions
|
||||
│ ├── OrderTracker.mqh # Tracks open orders and their metadata
|
||||
│ └── StopLogic.mqh # Provides rule-based SL/TP value resolution
|
||||
│
|
||||
├── RiskManagement/
|
||||
│ └── DrawdownControl.mqh # Implements drawdown-based risk controls
|
||||
│
|
||||
├── Strategy/
|
||||
│ └── RangeCalculator.mqh # Measures recent market range to inform signal logic
|
||||
│
|
||||
├── Utils/
|
||||
│ ├── AtrHandleManager.mqh # Efficiently manages and caches ATR indicator handles
|
||||
│ ├── ChartUtils.mqh # Utilities for drawing and annotating charts
|
||||
│ ├── DealingWithTime.mqh # Time conversion and formatting helpers
|
||||
│ ├── Enums.mqh # Enum declarations for inputs and logic flow
|
||||
│ ├── MarketDataUtils.mqh # Simplifies access to indicator buffers and price info
|
||||
│ ├── MultiSymbolSignalTracker.mqh # Tracks per-symbol signal state in multi-asset EAs
|
||||
│ ├── ResourceManager.mqh # Central manager for indicator handle cleanup
|
||||
│ ├── SignalStateTracker.mqh # Tracks signal timing (e.g., how many bars ago a trigger occurred)
|
||||
│ ├── TimeZones.mqh # Handles timezone conversion and time window logic
|
||||
│ └── TradeWindow.mqh # Defines and manages tradable time sessions
|
||||
```
|
||||
|
||||
## Getting Started
|
||||
## Included EA Example
|
||||
|
||||
### Requirements
|
||||
This repository also includes a full EA (`trend_following_ea.mq5`) that demonstrates how to use the MyLibs framework.
|
||||
|
||||
- MetaTrader 5 with access to `terminal64.exe`
|
||||
- Python 3.8+
|
||||
- Optional: Freqtrade installed (`pip install freqtrade`)
|
||||
### Features of the EA
|
||||
|
||||
### Running Scripts
|
||||
- Multi-symbol trading (`AUDNZD`, `EURGBP`, etc.)
|
||||
- Custom indicators and buffer signal handling
|
||||
- Virtual TP runner trades
|
||||
- ATR trailing stops and breakeven logic
|
||||
- Risk split between take-profit and runner entries
|
||||
- Support for data splitting during backtesting
|
||||
- Modular entry and exit strategies
|
||||
- Time-based filtering and session control
|
||||
|
||||
```bash
|
||||
python pre_process.py
|
||||
python process_entry_indicators.py
|
||||
python post_process_test.py
|
||||
```
|
||||
### EA Strategy Logic Overview
|
||||
|
||||
### MT5 Library Usage
|
||||
Signal generation is broken into modular components:
|
||||
|
||||
Place the `.mqh` files from `My_MQL5_Libs/` into your `MQL5/Include` folder to use them in your Expert Advisors or custom indicators.
|
||||
- **Trigger** (e.g., RSI cross)
|
||||
- **Trendline** (direction, breakout, pullback)
|
||||
- **Confirmation** (e.g., CCI alignment)
|
||||
- **Volume** (OBV trend confirmation)
|
||||
- **ATR Band Positioning** (via `AtrBands`)
|
||||
- **Exit Logic** (Stochastic crossover, trendline break)
|
||||
|
||||
## Notes
|
||||
### EA Dependencies
|
||||
|
||||
- This project is a scaffold for connecting MQL5 strategies to Python-based optimisation and analysis tools.
|
||||
- Actual EA logic, data formats, and strategy specifics should be customised to your use case.
|
||||
The EA uses standard MQL5 built-in indicators, but can be easily modified to use custom indicators if desired:
|
||||
|
||||
## License
|
||||
- `iMA` – Moving Average (used for trendline estimation)
|
||||
- `iRSI` – Relative Strength Index (used as a trigger)
|
||||
- `iCCI` – Commodity Channel Index (used for confirmation)
|
||||
- `iOBV` – On-Balance Volume (used for volume filtering)
|
||||
- `iStochastic` – Stochastic Oscillator (used for exit signals)
|
||||
|
||||
**Note:** These indicators were selected for simplicity and demonstration purposes only. They are **not optimized** for trading performance and should be replaced or tuned during real development and testing. The architecture is fully modular and allows for plugging in better-suited or proprietary indicators.
|
||||
|
||||
All indicator handles are dynamically created and released via the `ResourceManager` class, ensuring clean memory handling during runtime and backtests.
|
||||
|
||||
---
|
||||
|
||||
## Installation & Usage
|
||||
|
||||
1. Clone or download this repository.
|
||||
2. Copy the `MyLibs/` directory into your MT5 `Include/` folder:
|
||||
3. Place the example EA file into the `MQL5/Experts/` directory.
|
||||
4. Open **MetaEditor** and compile the EA.
|
||||
5. Attach it to a chart or run backtests.
|
||||
|
||||
---
|
||||
|
||||
## Backtesting & Optimization
|
||||
|
||||
The EA supports advanced backtesting scenarios via:
|
||||
|
||||
- **CustomMax.mqh** — Optimizable custom fitness criteria (e.g., win rate, net profit)
|
||||
- **TestDataSplit.mqh** — Supports train/test splits for more robust model evaluation
|
||||
|
||||
Use `OnTester()` to drive optimization scoring logic.
|
||||
|
||||
---
|
||||
|
||||
|
||||
|
||||
## Maintenance Notes
|
||||
|
||||
- Each helper class is standalone and can be reused or extended.
|
||||
- Use `MultiSymbolSignalTracker` to handle per-symbol logic in multi-asset EAs.
|
||||
- Time-related functions are consolidated in `TimeZones.mqh` and `TradeWindow.mqh`.
|
||||
|
||||
---
|
||||
|
||||
This project is provided for educational and prototyping purposes. Please adapt and extend as needed for production environments.
|
||||
|
||||
Reference in New Issue
Block a user