Warrior_EA/Expert/README.md
Jason Binet Lafontaine a228d1bde7 feat(trade): implement trade safety checks per Article 2555 and resource limits
Add freeze-level checks, no-change modification skipping, entry price routing, and per-tick/memory budget monitoring. Override trade actions (Open, Close, Reverse, TrailingStop, TrailingOrder) to validate at the final gate before sending orders.
2026-07-26 23:08:32 -04:00

75 lines
6 KiB
Markdown

# Expert/ Directory Documentation
## Market-submission compliance (article 2555)
Every runtime check from
[The Checks a Trading Robot Must Pass Before Publication in the Market](https://www.mql5.com/en/articles/2555)
is implemented in `System/TradeChecks.mqh` (free functions, `TC*` prefix) and applied by the classes
in this folder. Where each rule lives:
| # | Rule | Implemented in |
|---|------|----------------|
| 1 | Catch/fix errors via the tester | Development practice; every rejection below logs a throttled reason |
| 2 | Insufficient funds | `TCCheckMoneyForTrade` / `TCFitVolumeToFreeMargin`, applied by `CExpertMoneyCustom::ValidateLotForTrade` |
| 3 | Invalid volumes | `TCCheckVolumeValue` / `TCNormalizeVolume`, same call site |
| 4 | Pending-order count limit | `TCIsNewOrderAllowed`, checked in `CExpertSignalCustom::OpenParams` and again in `CExpertCustom::OpenLong/OpenShort` |
| 5 | Per-symbol lot limit | `TCSymbolVolumeAllowed` / `TCApplySymbolVolumeLimit` |
| 6 | `SYMBOL_TRADE_STOPS_LEVEL` | `TCCheckStops` / `TCAdjustStops` / `TCCheckPendingPrice`, applied when the setup is shaped and again immediately before the order is sent |
| 7 | `SYMBOL_TRADE_FREEZE_LEVEL` | `TCFreezeOkForPosition` / `TCFreezeOkForOrder`, gating close, reverse, trailing, order modify and order delete in `CExpertCustom` |
| 8 | Insufficient quote history | `TCHasEnoughHistory`, in `CExpertCustom::Refresh` and `CExpertSignalCustom::OpenParams` |
| 9 | Array out of range | `TCIndexOk`, plus the existing index guards (`iLowest`/`iHighest` return values, `Direction()`'s bounded loops) |
| 10 | Zero divide | `TCSafeDivide`, plus explicit zero-denominator guards on every volume-step / loss division |
| 11 | Modification with no changes | `TCPositionModifyIsMeaningful` / `TCOrderModifyIsMeaningful`, in the `TrailingStop*` / `TrailingOrder*` overrides |
| 12 | No DLL imports | Build-time: `WARRIOR_MARKET_BUILD` compiles out the `#import` blocks in `AI/NeuronDirectML.mqh` |
| 13 | No external `iCustom` | Build-time: `#resource` block in `Warrior_EA.mq5` + `WARRIOR_CI()` in `Variables/IndicatorResources.mqh` |
| 14 | Invalid function parameters | `TCSymbolIsTradeable`, plus the existing `ValidationSettings()` range checks |
| 15 | Access violation | No runtime check exists by definition; the NULL-pointer and index guards above are what prevent it |
| 16 | CPU / memory consumption | `TCWarnIfSlow` on each `OnTick` pass (`EXPERT_TICK_BUDGET_US`) and `TCWarnIfMemoryAbove` on each timer pass (`EXPERT_MEMORY_SOFT_LIMIT_MB`) |
All rejections report through `TCLog()`, which throttles per condition so a state that persists for
many ticks writes one journal line per minute instead of thousands.
## ExpertCustom.mqh
- **Purpose:** Defines the `CExpertCustom` class, a base class for custom expert advisors in the EA framework.
- **Inheritance:** Inherits from `CExpert` (not shown here, likely a core MQL5/MetaQuotes class or defined elsewhere in the project).
- **Key Features:**
- Provides virtual event handlers for MQL5 events: `OnTick`, `OnTimer`, `OnChartEvent`, etc., allowing derived classes to implement custom trading logic.
- Contains methods for initializing trading objects, handling series data, and managing trading events.
- Designed for extensibility: users can derive their own expert logic by subclassing and overriding the provided virtual methods.
- **Modernization Note:**
- This class is a good candidate for further abstraction to support AI/ML-driven strategies. Consider introducing interfaces or abstract base classes for signal generation, money management, and risk control, allowing plug-and-play of traditional and AI/ML modules.
- Ensure all event handlers are unit-testable and decoupled from hard-coded logic.
## ExpertMoneyCustom.mqh
- **Purpose:** Implements a custom money management class (`CExpertMoneyCustom`) for the EA, extending `CExpertMoney`.
- **Key Features:**
- `CheckAndCorrectVolumeValue`: Ensures trade volume is within broker constraints (min/max/step), adjusting and describing corrections.
- `CheckAndAdjustMoneyForTrade`: Dynamically adjusts lot size to fit available margin, reducing lots if margin is insufficient, and handles errors gracefully.
- **Modernization Note:**
- This class is a good candidate for AI/ML-driven position sizing. Consider abstracting the logic to allow for ML-based risk and lot size optimization.
- Ensure all adjustments are logged for transparency and auditability.
## ExpertSignalAIBase.mqh
- **Purpose:** Provides a base class (`CExpertSignalAIBase`) for AI/ML-driven signal generation, extending `CExpertSignalCustom`.
- **Key Features:**
- Integrates with the neural network/AI subsystem (`Network.mqh`).
- Manages a wide range of indicator objects (Open, Close, High, Low, Volumes, AD, ADX, MACD, etc.) for feature extraction.
- Supports dynamic configuration of neural network topology, training, and feature selection.
- Implements methods for indicator initialization, data buffering, training, and topology persistence.
- **Modernization Note:**
- This is the main integration point for advanced ML/AI logic. Ensure all feature selection and training parameters are externally configurable.
- Refactor to support plug-and-play feature pipelines and automated hyperparameter optimization.
## ExpertSignalCustom.mqh
- **Purpose:** Implements a custom signal logic class (`CExpertSignalCustom`) for the EA, extending `CExpertSignal`.
- **Key Features:**
- Adds database-driven signal tracking, pattern recognition, and trade record management.
- Supports dynamic filter addition, ATR-based entry/exit logic, and advanced signal buffering.
- Implements robust duplicate detection, trade status management, and event-driven signal processing.
- **Modernization Note:**
- This class is central to integrating traditional and AI/ML signals. Refactor to decouple hard-coded logic and support dynamic, testable signal pipelines.
- Ensure all database operations are abstracted for testability and future migration to more advanced data stores.
---
(Other files in Expert/ will be documented as they are processed.)