373 lines
11 KiB
Markdown
373 lines
11 KiB
Markdown
# Base EMA Reversal Indicator - User Guide
|
||
|
||
**Version**: 1.0
|
||
**Date**: 2025-01-22
|
||
**File**: Base_EMA_Reversal.mq5
|
||
|
||
---
|
||
|
||
## 📋 Overview
|
||
|
||
The Base EMA Reversal Indicator identifies potential reversal opportunities using a multi-stage state machine approach with EMA confluence. The indicator tracks both buy and sell opportunities through base formation, EMA confirmation, and pullback detection.
|
||
|
||
---
|
||
|
||
## 🎯 Strategy Logic
|
||
|
||
### Core Concept
|
||
When price touches EMA (50/100/200), TWO possible outcomes:
|
||
1. **Pullback**: Price revisits previous high/low → SIGNAL
|
||
2. **Breakthrough**: Price makes new high/low → Continue watching
|
||
|
||
### State Machine (States 0-3)
|
||
|
||
#### Buy Side
|
||
- **State 0**: Startup/Reset (only at initialization)
|
||
- **State 1**: Finding Base - Track lowest price continuously
|
||
- **State 2**: EMA Touched - Price closed ABOVE EMA 50/100/200
|
||
- **State 3**: Decision/Signal - Within 2 bars of EMA touch
|
||
- Pullback to BaseLow (within 50 points) → **BUY SIGNAL**
|
||
- Cross EMA below again → Cancel, back to State 1
|
||
|
||
#### Sell Side (Mirror of Buy Side)
|
||
- **State 0**: Startup/Reset (only at initialization)
|
||
- **State 1**: Finding Base - Track highest price continuously
|
||
- **State 2**: EMA Touched - Price closed BELOW EMA 50/100/200
|
||
- **State 3**: Decision/Signal - Within 2 bars of EMA touch
|
||
- Pullback to BaseHigh (within 50 points) → **SELL SIGNAL**
|
||
- Cross EMA above again → Cancel, back to State 1
|
||
|
||
### TP Selection Logic
|
||
- **Buy Signals**: Find LOWEST valid EMA ABOVE price from [300, 400, 500, 600]
|
||
- **Sell Signals**: Find HIGHEST valid EMA BELOW price from [300, 400, 500, 600]
|
||
- **ATR Fallback**: If no valid EMA position, use ATR × 2
|
||
|
||
---
|
||
|
||
## 📊 Buffer Structure (8 Buffers)
|
||
|
||
| Index | Buffer Name | Purpose | Value Type | When Populated |
|
||
|-------|-------------|---------|------------|----------------|
|
||
| 0 | BUY_SIGNAL | Buy entry price | Price | Signal bars only |
|
||
| 1 | SELL_SIGNAL | Sell entry price | Price | Signal bars only |
|
||
| 2 | BUY_SL | Buy stop loss | Price (BaseLow) | Signal bars only |
|
||
| 3 | SELL_SL | Sell stop loss | Price (BaseHigh) | Signal bars only |
|
||
| 4 | BUY_TP | Buy take profit | Price (EMA or ATR) | Signal bars only |
|
||
| 5 | SELL_TP | Sell take profit | Price (EMA or ATR) | Signal bars only |
|
||
| 6 | BUY_STATE | Buy state tracker | 0-3 | Every bar |
|
||
| 7 | SELL_STATE | Sell state tracker | 0-3 | Every bar |
|
||
|
||
**Important**: Signal buffers (0-5) = EMPTY_VALUE when not applicable. EA will check for non-EMPTY_VALUE to detect signals.
|
||
|
||
---
|
||
|
||
## ⚙️ Input Parameters
|
||
|
||
### EMA Settings
|
||
| Parameter | Default | Description |
|
||
|-----------|----------|-------------|
|
||
| EMA 50 Period | 50 | Fast EMA (confirmation) |
|
||
| EMA 100 Period | 100 | Medium-fast EMA (confirmation) |
|
||
| EMA 200 Period | 200 | Medium EMA (confirmation) |
|
||
| EMA 300 Period | 300 | TP1 target |
|
||
| EMA 400 Period | 400 | TP2 target |
|
||
| EMA 500 Period | 500 | TP3 target |
|
||
| EMA 600 Period | 600 | TP4 target |
|
||
|
||
### Setup Settings
|
||
| Parameter | Default | Description |
|
||
|-----------|----------|-------------|
|
||
| Lookback Period | 100 | Bars for initial base detection |
|
||
| Base Threshold | 50 | Pullback tolerance (points) |
|
||
| Pullback Bars | 2 | Max bars to wait for pullback |
|
||
| Skip Bars | 50 | Bars skipped at startup |
|
||
|
||
### ATR Settings
|
||
| Parameter | Default | Description |
|
||
|-----------|----------|-------------|
|
||
| ATR Period | 14 | ATR period for TP fallback |
|
||
|
||
### Display Settings
|
||
| Parameter | Default | Description |
|
||
|-----------|----------|-------------|
|
||
| Show Stage Labels | true | Display state labels on chart |
|
||
|
||
---
|
||
|
||
## 🚀 Installation
|
||
|
||
### Step 1: Copy Files to MetaTrader 5
|
||
|
||
1. Navigate to your MetaTrader 5 data folder:
|
||
- **Windows**: `C:\Users\{Username}\AppData\Roaming\MetaQuotes\Terminal\{TerminalID}\MQL5\`
|
||
- **Mac**: Open MetaTrader 5 → File → Open Data Folder
|
||
|
||
2. Copy `Base_EMA_Reversal.mq5` to: `MQL5/Indicators/`
|
||
|
||
### Step 2: Compile the Indicator
|
||
|
||
1. Open MetaEditor 5 (press F4 in MetaTrader 5)
|
||
2. File → Open → Select `Base_EMA_Reversal.mq5`
|
||
3. Press F7 or click "Compile"
|
||
4. Verify **0 errors** in "Errors" tab (warnings are OK)
|
||
5. Close MetaEditor 5
|
||
|
||
### Step 3: Install Indicator on Chart
|
||
|
||
1. In MetaTrader 5, right-click on "Navigator" → "Refresh"
|
||
2. Drag `Base EMA Reversal` from Navigator → Indicators to EURUSD H1 chart
|
||
3. Configure parameters if needed
|
||
4. Click OK
|
||
|
||
### Step 4: Enable Visual Elements
|
||
|
||
Ensure you can see:
|
||
- ✅ **7 EMA lines** (Red, Orange, Yellow, Green, Blue, Purple, Magenta)
|
||
- ✅ **Stage labels** "B:X S:Y" on each bar (color-coded)
|
||
- ✅ **Buy signals** (Lime ↑ arrows)
|
||
- ✅ **Sell signals** (Red ↓ arrows)
|
||
- ✅ **SL markers** (Gray dots)
|
||
- ✅ **TP markers** (Blue +)
|
||
|
||
---
|
||
|
||
## 🎨 Visual Elements
|
||
|
||
### EMA Lines (7 EMAs) - NOW VISIBLE!
|
||
- **EMA 50**: Red (Plot 7)
|
||
- **EMA 100**: Orange (Plot 8)
|
||
- **EMA 200**: Yellow (Plot 9)
|
||
- **EMA 300**: Green (Plot 10)
|
||
- **EMA 400**: Blue (Plot 11)
|
||
- **EMA 500**: Purple (Plot 12)
|
||
- **EMA 600**: Magenta (Plot 13)
|
||
|
||
### Signal Arrows
|
||
- **Buy Signal**: Arrow code 233 (↑), Lime color
|
||
- **Sell Signal**: Arrow code 234 (↓), Red color
|
||
|
||
### Signal Labels (Only on Signal Bars - Large Font)
|
||
- **Buy Signal Labels**: "BUY SIGNAL" - Lime color, Font Size 14
|
||
- **Sell Signal Labels**: "SELL SIGNAL" - Red color, Font Size 14
|
||
- **No labels on non-signal bars** (reduced clutter)
|
||
|
||
### SL/TP Markers
|
||
- **SL**: Gray dot (code 159)
|
||
- **TP**: Blue plus sign (code 160)
|
||
|
||
---
|
||
|
||
## 🔧 Integration with Universal Buffer Reader EA
|
||
|
||
### EA Configuration
|
||
|
||
Configure the Universal Buffer Reader EA with these parameters:
|
||
|
||
| EA Parameter | Value |
|
||
|-------------|--------|
|
||
| IndicatorName | "Base_EMA_Reversal" |
|
||
| BuySignalBuffer | 0 |
|
||
| SellSignalBuffer | 1 |
|
||
| BuySLBuffer | 2 |
|
||
| SellSLBuffer | 3 |
|
||
| SellSLBuffer | 6 |
|
||
| BuyTPBuffer | 4 |
|
||
| SellTPBuffer | 5 |
|
||
|
||
### How EA Reads Signals
|
||
|
||
The EA's `SignalDetector.mqh` checks:
|
||
```mql5
|
||
bool has_buy = (buy_signal != EMPTY_VALUE && buy_signal != 0);
|
||
bool has_sell = (sell_signal != EMPTY_VALUE && sell_signal != 0);
|
||
```
|
||
|
||
Our indicator:
|
||
- Sets **signal price** when signal occurs at bar close
|
||
- Sets **EMPTY_VALUE** on all other bars
|
||
- EA will only execute trades when buffers have valid values
|
||
|
||
---
|
||
|
||
## 🧪 Testing Recommendations
|
||
|
||
### 1. Demo Account Testing (REQUIRED)
|
||
|
||
**Always test on a demo account first!**
|
||
|
||
- Test for at least 1-2 weeks
|
||
- Monitor all trades closely
|
||
- Check all features are working:
|
||
- EMA touch detection
|
||
- Pullback identification
|
||
- Signal generation
|
||
- SL/TP levels
|
||
- State transitions
|
||
- ATR fallback
|
||
|
||
### 2. Visual Verification
|
||
|
||
On EURUSD H1 chart:
|
||
1. Check EMA lines are smooth and correct colors
|
||
2. Verify stage labels change colors as states progress
|
||
3. Watch for signal arrows at appropriate locations
|
||
4. Monitor Experts tab for signal print messages
|
||
|
||
### 3. Backtesting
|
||
|
||
1. Open Strategy Tester (press F4 or View → Strategy Tester)
|
||
2. Select `Base EMA Reversal`
|
||
3. Select symbol (EURUSD) and timeframe (H1)
|
||
4. Select test period (last 6 months recommended)
|
||
5. Click "Start"
|
||
6. Review results:
|
||
- Signal frequency
|
||
- Win rate
|
||
- Risk/reward ratio
|
||
- ATR fallback usage
|
||
|
||
### 4. Forward Testing
|
||
|
||
After successful backtesting:
|
||
- Test on demo account with live data
|
||
- Monitor for 2-4 weeks
|
||
- Compare results with backtest
|
||
- Adjust parameters if needed
|
||
|
||
---
|
||
|
||
## 📊 Monitoring and Analysis
|
||
|
||
### Experts Tab
|
||
Watch for these messages:
|
||
- `BUY SIGNAL at [timestamp] Price: [price] SL: [sl] TP: [tp]`
|
||
- `SELL SIGNAL at [timestamp] Price: [price] SL: [sl] TP: [tp]`
|
||
- `Initialization: BaseLow = [value], BaseHigh = [value]`
|
||
- `BaseLow recalculated: [value] after buy signal`
|
||
- `BaseHigh recalculated: [value] after sell signal`
|
||
|
||
### Signal Quality Indicators
|
||
Monitor:
|
||
- **State Transitions**: Should flow smoothly 1 → 2 → 3 → 1
|
||
- **Timeout Frequency**: How often signals cancel due to timeout?
|
||
- **Pullback Success Rate**: What % of EMA touches result in signals?
|
||
- **ATR Fallback Usage**: How often is TP calculated via ATR?
|
||
|
||
---
|
||
|
||
## ⚠️ Common Issues and Solutions
|
||
|
||
### Issue: No Signals Appearing
|
||
|
||
**Symptoms**: Indicator loads but no arrows appear
|
||
|
||
**Solutions**:
|
||
1. Check minimum data requirement (need 600+ bars)
|
||
2. Verify EMA touch detection is working (watch Experts tab)
|
||
3. Confirm base levels are updating (check print messages)
|
||
4. Increase Lookback Period parameter
|
||
|
||
### Issue: Too Many False Signals
|
||
|
||
**Symptoms**: Signals appearing frequently but price doesn't reverse
|
||
|
||
**Solutions**:
|
||
1. Increase Base Threshold (from 50 to 100+)
|
||
2. Reduce Pullback Bars (from 2 to 1)
|
||
3. Add additional confirmation logic (e.g., close above EMA for 2 bars)
|
||
|
||
### Issue: Stage Labels Too Cluttered
|
||
|
||
**Symptoms**: Chart difficult to read due to too many labels
|
||
|
||
**Solutions**:
|
||
1. Set `Show Stage Labels = false` to disable
|
||
2. Use smaller timeframe (H4 instead of M15)
|
||
3. Filter by state (only show State 2-3 labels)
|
||
|
||
### Issue: EA Not Executing Trades
|
||
|
||
**Symptoms**: Universal Buffer Reader EA sees indicator but no trades
|
||
|
||
**Solutions**:
|
||
1. Verify EA parameters match buffer indices (0-5)
|
||
2. Check AutoTrading is enabled (green button)
|
||
3. Verify EA has permission to trade
|
||
4. Enable debug prints in EA to see signal detection
|
||
|
||
---
|
||
|
||
## 🔒 Risk Management
|
||
|
||
### Important Considerations
|
||
|
||
1. **No Stop Loss Warning**: If SL hits immediately, BaseLow/High may be too close to entry
|
||
2. **TP Verification**: If EMA TP seems unrealistic, ATR fallback will activate
|
||
3. **Pullback Window**: 2 bars is narrow - may miss valid setups
|
||
4. **Base Threshold**: 50 points may be too tight for volatile symbols
|
||
|
||
### Recommended Settings by Symbol
|
||
|
||
| Symbol | Base Threshold | Pullback Bars |
|
||
|--------|---------------|----------------|
|
||
| EURUSD (Majors) | 30-50 | 2 |
|
||
| GBPUSD (Volatile) | 50-80 | 2-3 |
|
||
| XAUUSD (Gold) | 100-200 | 3 |
|
||
| Crypto (BTC/ETH) | 200-500 | 5 |
|
||
|
||
---
|
||
|
||
## 📝 Code Summary
|
||
|
||
### File Statistics
|
||
- **Lines**: 641
|
||
- **Functions**: 7 (OnInit, OnDeinit, OnCalculate, CalculateBuyTP, CalculateSellTP, RecalculateBaseBuy, RecalculateBaseSell, UpdateStageLabels)
|
||
- **State Variables**: 9 (buy_state, sell_state, BaseLow, BaseHigh, last_signal_time, buy_ema_touched, sell_ema_touched, ema_touch_bar, buy_touch_ema_count, sell_touch_ema_count)
|
||
- **Indicator Handles**: 8 (7 EMAs + 1 ATR)
|
||
|
||
### Key Logic Patterns
|
||
- Array indexing: Series (index 0 = current bar)
|
||
- State machine: Event-driven (EMA touch → Decision → Reset)
|
||
- Buffer clearing: EMPTY_VALUE on non-signal bars
|
||
- Object management: Unique prefix "EMA_Stage_"
|
||
|
||
---
|
||
|
||
## 🎓 Next Steps
|
||
|
||
After initial testing:
|
||
|
||
1. ✅ **Optimize Parameters**: Use Strategy Tester to find optimal values
|
||
2. ✅ **Add Filters**: Consider time filters, trend filters
|
||
3. ✅ **Multi-Timeframe**: Test on H4, D1 timeframes
|
||
4. ✅ **Performance Tuning**: Reduce object count for faster chart
|
||
5. ✅ **Backtest Extensively**: 6-12 months of data minimum
|
||
|
||
---
|
||
|
||
## 📞 Getting Help
|
||
|
||
### Debug Mode
|
||
If experiencing issues, enable `InpShowStageLabels = false` to reduce visual clutter and check Experts tab for print messages.
|
||
|
||
### Print Messages
|
||
Key messages to watch:
|
||
- Initialization messages
|
||
- Signal generation messages
|
||
- Base recalculation messages
|
||
- Any error messages
|
||
|
||
### Log Analysis
|
||
Review Strategy Tester logs to:
|
||
- Count total signals
|
||
- Analyze win/loss ratio
|
||
- Check SL/TP effectiveness
|
||
- Identify best performing timeframes
|
||
|
||
---
|
||
|
||
**Last Updated**: 2025-01-22
|
||
**Version**: 1.0
|
||
|
||
**Happy Trading! 📈**
|