MQL5 Anti-Pattern Detection Skill
Analyze the MQL5 codebase for general and platform-specific anti-patterns.
Detection Rules
General Anti-Patterns
- Magic numbers: Replace with named constants or enumerations.
- Deep nesting: Refactor complex conditional structures to early returns/guard clauses.
- Dead code: Identify and remove unused variables, unreachable branches, or redundant functions.
- Code duplication: Extract repetitive logic into shared include files (
.mqh) or reusable functions. - Naming issues: Enforce consistent conventions (e.g., camelCase vs. PascalCase) and avoid shadowing global/platform symbols.
MQL5-Specific Anti-Patterns
- Trading Error Handling: Improper handling of
OrderSend,OrderModify, orOrderDelete. Check return values and ensureGetLastError()is consulted for failures. - Inefficient OnTick Logic: Repeated recalculation of values instead of utilizing indicator handles or rates of change.
- Resource Management: Failure to release indicator handles (
IndicatorRelease) or file handles (FileClose) during deinitialization or after use. - Input Validation: Missing boundary checks for
inputvariables that could lead to logic errors (e.g., negative lot size or stop loss). - Blocking Operations: Use of
Sleep()or performing heavy I/O operations inside time-critical functions likeOnTickorOnTimer.
Severity Taxonomy
- Critical: Breaks core functionality, security, or trading safety (e.g., unhandled
OrderSendfailures, resource leaks). - Major: Significant correctness, performance, or quality issues that could lead to bugs under specific conditions (e.g., blocking
OnTick, missing input validation). - Minor: Style, maintainability, or minor optimizations (e.g., magic numbers, deep nesting, naming).
Report Format
For each finding, provide a structured report using the following format:
[Severity Level]
- [ ] File:
path/to/file.mq5 - [ ] Line:
[Line Number] - [ ] Issue: [Description of the anti-pattern]
- [ ] Suggestion: [Concise fix or refactoring advice]
Usage
Verify each finding against the current code before reporting.
Ask for user confirmation before applying any automated fixes.