Article-22220-Real-Time-Ent.../README.md

141 lines
5.5 KiB
Markdown

# Article-22220-Real-Time-Entropy-Trading-System
This repository is an article-derived reference project based on the original MQL5 article. It does not claim to reproduce the full original source code unless files are explicitly attached.
## Overview
This project documents a hybrid MQL5 + Python trading system centered on real-time market entropy analysis. The architecture combines:
- an MQL5 Expert Advisor for tick collection, HTTP communication, and trade execution
- a Python feature-engineering and model-training pipeline
- a Flask inference server for live signal generation
The article describes how Shannon entropy, volatility metrics, RSI, and trend features are transformed into an 8-feature input vector for a neural network that outputs directional probability. A volatility regime detector then adjusts confidence thresholds and trading risk parameters.
## Original Article
- **Article ID:** 22220
- **Author:** Hlomohang John Borotho
- **Publication date:** 2026.05.19
- **Category:** Experts
- **URL:** https://www.mql5.com/en/articles/22220
## Repository Purpose
This repository should be treated as a reference or reconstruction project derived from the article content. Its purpose is to preserve the article’s technical structure and described file layout for:
- studying entropy-based tick-level trading workflows
- understanding MQL5-to-Python inference integration via HTTP
- reconstructing the Python training/inference stack and the MQL5 EA
- serving as a base for further experimentation with adaptive risk logic
## Key Concepts
- Shannon entropy on rolling return windows
- volatility entropy from squared returns
- adaptive volatility regime detection
- neural-network directional classification
- feature scaling with `StandardScaler`
- Flask-based local inference API
- MQL5 `WebRequest` communication
- adaptive lot sizing, SL, and TP management
- tick-level execution rather than candle-close logic
## Algorithm / Architecture Summary
1. **Historical data collection**
- Python connects to MetaTrader 5 and downloads historical XAUUSD H1 data.
- Data is saved to CSV for offline training.
2. **Feature engineering**
- Compute log returns from recent prices.
- Derive normalized entropy and volatility entropy using percentile-based bins.
- Compute volatility statistics such as standard deviation, MAD, range, skewness, and kurtosis.
- Estimate trend slope and regression `R²`.
- Build an 8-element feature vector including entropy, return statistics, trend metrics, skewness, and normalized RSI.
3. **Regime detection**
- Maintain entropy history in rolling buffers.
- Classify market state as `LOW_VOLATILITY`, `NORMAL`, `HIGH_VOLATILITY`, or `EXTREME_VOLATILITY`.
- Produce risk multipliers for stop-loss, take-profit, and position sizing.
4. **Model training**
- Use a PyTorch feed-forward network with batch normalization, ReLU, dropout, and sigmoid output.
- Train on rolling-window samples labeled by future return direction over a fixed horizon.
- Save the fitted scaler and best model weights.
5. **Live inference**
- MQL5 EA collects recent tick bid prices and computes RSI.
- EA sends JSON to a local Flask `/predict` endpoint.
- Flask rebuilds features, detects regime, runs model inference, and returns:
- probability
- entropy metrics
- signal
- regime
- volatility multiplier
- confidence
6. **Execution**
- EA validates confidence, cooldown, current exposure, and reversal rules.
- Orders are placed through `CTrade`.
- Open positions may have SL/TP adjusted when the regime changes.
## Mentioned or Attached Files
### Explicitly attached files
The article states that the following files are contained in the archive:
- `GettingHistData.py` — downloads historical XAUUSD H1 data from MetaTrader 5
- `Features.py` — feature engineering and volatility regime detection
- `Model.py` — PyTorch neural network definition
- `Train.py` — training pipeline and scaler/model persistence
- `Server.py` — Flask inference server
- `Entropy.ipynb` — notebook for step-by-step execution
- `Real-Time Entropy.mq5` — MQL5 Expert Advisor
### Files mentioned in the text
- `XAUUSD_H1.csv` — generated dataset used by the training script
- `entropy_model.pth` — saved PyTorch model weights
- `scaler.pkl` — saved feature scaler
## Statistics
- **Article sections described:** 6 main sections
- **Python components described:** 5
- **MQL5 components described:** 1 EA
- **Model input features:** 8
- **Primary rolling window size:** 50
- **Training horizon:** 5
- **Flask endpoint:** `/predict`
- **Health endpoint:** `/health`
## Tags
`MQL5`, `MetaTrader5`, `Python`, `Flask`, `PyTorch`, `Machine-Learning`, `Entropy`, `Volatility-Regime`, `Algorithmic-Trading`, `Expert-Advisor`
## Difficulty
**Advanced**
Requires familiarity with:
- MQL5 EA development
- Python data processing
- PyTorch model training
- REST communication between MetaTrader and local services
- trading/risk-management logic
## Limitations
- The repository is derived from article content and should be treated as a reference reconstruction unless the archive files are actually present.
- Publication date and article category were not available in the processed input.
- No performance claims beyond the article description should be assumed.
- The system depends on a local Flask server and MetaTrader 5 environment; deployment details may vary.
- The article describes code and file roles, but full verification of attached source completeness is not possible from the processed input alone.
## Reference
Original MQL5 article: [https://www.mql5.com/en/articles/22220](https://www.mql5.com/en/articles/22220)