# MT4 EA Implementation - Quick Summary **Full Plan:** [mt4-ea-implementation-plan.md](./mt4-ea-implementation-plan.md) **Date:** November 4, 2025 --- ## Overview Creating a **Multi-Timeframe Zone Expert Advisor for MT4** that implements the Strategy.md requirements: - H4 zone detection (4 pattern types) - M30 confirmation zones - M15 entry zones - Automated trade execution --- ## Architecture Decision: Monolithic with Parallel Arrays **Why not OOP?** - MT4 has unreliable support for dynamic arrays of structs - Proven approach from `FINAL_H4_ZONES.mq4` uses parallel arrays - Simpler debugging and maintenance for single-file EA **Data Structure:** ```mql4 // H4 Zone Storage (50 max) double h4_ZoneHighs[50]; double h4_ZoneLows[50]; datetime h4_ZoneTimes[50]; bool h4_ZoneIsReversed[50]; string h4_ZoneTypes[50]; bool h4_ZoneMonitoringM30[50]; int h4_M30ChildIndex[50]; int h4_ZoneCount = 0; // M30 Zone Storage (100 max) - similar structure // M15 Zone Storage (200 max) - similar structure + order tickets ``` --- ## 10 Implementation Phases | Phase | Focus | Deliverable | Hours | |-------|-------|-------------|-------| | 1 | Infrastructure | Working skeleton EA | 2 | | 2 | Bar Detection | New bar detection all TFs | 1 | | 3 | Pattern Logic | Port from MQ4 indicator | 3 | | 4 | H4 Zones | Create, reverse, delete | 4 | | 5 | M30 Zones | Monitoring & confirmation | 4 | | 6 | M15 Zones | Entry detection & orders | 6 | | 7 | Order Management | Monitor & lifecycle | 3 | | 8 | Visual Display | Zones + info panel | 3 | | 9 | Error Handling | Validation & logging | 2 | | 10 | Testing | Strategy tester validation | 4-10 | **Total Estimate:** 20-30 hours --- ## Critical Corrections to Strategy ### Order Type Correction **Strategy states:** "Place Buy Limit at zone High" **Problem:** Limit orders are BELOW current price for buys **Solution:** Use **Buy Stop** at High and **Sell Stop** at Low ### Stop Loss Placement **Strategy:** "SL at zone boundary" **Implementation:** SL = zone boundary ± StopLossPips buffer Example: Buy Stop at High, SL at (Low - StopLossPips×Point) --- ## Key Features ### H4 Level ✅ 4 pattern types (Regular/Irregular Buy/Sell) ✅ Zone lifecycle: Create → Break → Reverse → Break → Delete ✅ Dashed rectangles → Solid on reversal ✅ Visible on all timeframes ### M30 Level ✅ Monitors only when price enters H4 zone ✅ Checks only NEW bars after entry ✅ One-break deletion (no reversal) ✅ Links to parent H4 zone ### M15 Level ✅ Monitors only when price enters M30 zone ✅ Places pending order immediately ✅ One trade per M15 zone ✅ Links to parent M30 zone ### Order Management ✅ Buy Stop at M15 High, SL at M15 Low ✅ Sell Stop at M15 Low, SL at M15 High ✅ TP calculated from input pips ✅ Cancels orders when parent zones break --- ## MT4 Compatibility Notes **What we CAN'T use (MQL5 only):** - ❌ Classes and objects - ❌ `CTrade` library - ❌ `MqlRates[]` structures - ❌ `CopyRates()` function - ❌ Position concept **What we WILL use:** - ✅ Parallel arrays - ✅ Global functions - ✅ `iOpen/iClose/iHigh/iLow` - ✅ `OrderSend()` directly - ✅ Order ticket tracking - ✅ Simple structs (for data passing only) --- ## Testing Strategy ### Per-Phase Testing Each phase must pass before moving to next: - Phase 3: Compare pattern detection with original indicator - Phase 4: Test H4 zone lifecycle manually - Phase 5: Verify M30 monitoring triggers correctly - Phase 6: Verify order placement with correct parameters ### Integration Testing - Full cascade: H4 → M30 → M15 → Order - Multiple concurrent zones - Zone break cascades - Reversal handling ### Strategy Tester - 3+ months historical data - Visual mode verification - Multiple scenario testing - Edge case validation --- ## Input Parameters ```mql4 input double LotSize = 0.01; // Fixed lot size input int StopLossPips = 30; // Additional SL buffer input int TakeProfitPips = 60; // TP distance input int MagicNumber = 123456; // EA identifier input int Slippage = 3; // Max slippage input int RecentBarsToCheck = 10; // Pattern scan depth input bool EnableDebugLogs = false; // Console logging ``` --- ## Function Organization ### Main Flow ``` OnTick() ├─► CheckNewBars() → ProcessH4/M30/M15 ├─► CheckH4ZoneEntries() → Start M30 monitoring ├─► CheckM30ZoneEntries() → Start M15 monitoring ├─► MonitorPendingOrders() → Track execution └─► UpdateDisplay() → Refresh visuals ``` ### Pattern Detection (Reusable) ``` IsRegularBuy(ENUM_TIMEFRAMES tf, int index) IsIrregularBuy(ENUM_TIMEFRAMES tf, int index) IsRegularSell(ENUM_TIMEFRAMES tf, int index) IsIrregularSell(ENUM_TIMEFRAMES tf, int index) ``` ### Zone Management (Per Timeframe) ``` H4: Create, Reverse, Delete, Draw M30: Create, Delete, Draw M15: Create, Draw, PlaceOrder ``` --- ## Risk Considerations ### Limitations - Fixed lot size (no dynamic risk management) - Array size limits (50/100/200 per TF) - No trade management (trailing, breakeven) - No session filters ### Safety Features - Magic number isolation - One trade per M15 zone - Input validation - Comprehensive error logging - Order cancellation on parent breaks --- ## Success Criteria **Must Have:** - ✅ All 4 patterns detected correctly - ✅ Full H4 → M30 → M15 cascade works - ✅ Orders placed with correct parameters - ✅ Zone lifecycle managed properly - ✅ Visual display accurate - ✅ No runtime errors in tester **Nice to Have:** - Code comments and documentation - Debug logging system - Info panel with stats - Clean code organization --- ## Next Steps 1. **Review Plan:** Approve implementation approach 2. **Start Phase 1:** Create EA skeleton (2 hours) 3. **Incremental Development:** Complete phases 2-10 sequentially 4. **Continuous Testing:** Validate each phase before proceeding 5. **Final Testing:** Full strategy tester validation 6. **Demo Deployment:** Live testing on demo account --- ## File Location ``` MQL4/ └── Experts/ └── _Thivyam/ └── MultiTimeframeZone/ └── MultiTimeframeZoneEA_MT4.mq4 (single file) ``` --- ## Development Timeline **Week 1:** Phases 1-4 (Infrastructure + H4 zones) **Week 2:** Phases 5-6 (M30/M15 zones + orders) **Week 3:** Phases 7-8 (Management + display) **Week 4:** Phases 9-10 (Testing + refinement) **Minimum Viable Product:** End of Week 2 **Production Ready:** End of Week 4 --- ## Support Documentation - **Full Implementation Plan:** mt4-ea-implementation-plan.md (50+ pages) - **Reference Code:** FINAL_H4_ZONES.mq4 (pattern detection) - **Strategy Rules:** Strategy.md (requirements) - **MQ4 vs MQ5 Analysis:** COMPARISON_MQ4_vs_MQ5.md (architecture insights) --- **Status:** Ready for Development **Risk:** Medium (complex multi-TF logic) **Confidence:** High (proven pattern logic + clear plan) --- **Quick Reference During Development:** **Phase 1-2:** Focus on bar detection infrastructure **Phase 3:** Port pattern detection from indicator **Phase 4:** Build H4 zone lifecycle **Phase 5-6:** Add M30/M15 cascade **Phase 7:** Add order execution **Phase 8-10:** Polish and test **Remember:** Test each phase thoroughly before moving forward! --- **End of Quick Summary**