`OnCalculate` runs on every tick. The two most recently closed bars (index `rates_total-2` and `rates_total-3`) are compared. A bullish signal is latched when the fast EMA crosses above the slow EMA between those two bars. A bearish signal is latched on the reverse cross. Each new signal resets the drawn state and triggers a calibration refresh.
### Calibration
`CalcAvgCandleMetrics` reads the last `AvgLookback` closed bars using `CopyOpen`, `CopyHigh`, `CopyLow`, `CopyClose` starting at shift 1 (skipping the live forming bar). It computes:
-`g_AvgBody` — mean of `|close - open|`
-`g_AvgUpperWick` — mean of `high - max(open, close)`
-`g_AvgLowerWick` — mean of `min(open, close) - low`
-`g_AvgRange` — mean of `high - low`
All values are floored at 1 pip to prevent collapse in flat markets. The function runs once at `OnInit` and again on each new crossover signal.
### Prediction Engine
`GeneratePrediction` builds the `PredictedCandle[]` array. Each candle body size is computed as:
where `momentumScale` maps the EMA slope to the range `[0.5, 1.5]`, the sine-squared term pulses sizes across the projection, `0.93^i` is the exponential decay, and jitter adds ±8% noise. Every `CounterCandleFreq` bars a counter-trend candle is injected at 25–45% of the trend body size. Wick lengths use the `avgWick / avgBody` ratio with ±25% jitter and a 20% floor.
### Rendering
`DrawAllCandles` places each synthetic candle one bar-width into the future relative to the anchor, inset by `CandleGapFraction` on each side for visible spacing. Each candle is drawn as:
-`OBJ_RECTANGLE` — filled body
-`OBJ_RECTANGLE` — body outline (unfilled)
-`OBJ_TREND` × 2 — upper and lower wicks, centred on the full bar slot
Supporting objects include a dashed `OBJ_VLINE` separator at the anchor, an `OBJ_TEXT` zone label, and an `OBJ_TEXT` invalidation notice.
### Invalidation
On every tick, the live bar's close is compared against the signal price. If a bullish projection's live price drops more than `InvalidationPips` below the signal price, or a bearish projection's live price rises more than `InvalidationPips` above it, all objects are deleted, an invalidation label is drawn, and the engine waits silently for the next valid cross.
### Interactive Removal
`OnCalculate` detects when `rates_total` increases by one, meaning a new real bar has closed. At that point the object group for `FutureCandle_0` is deleted and the remaining projected candles shift down by one index. The projection is always anchored exactly one bar ahead of the current live bar.
| Counter-trend retrace range | 25% – 45% of body |
| Wick minimum floor | 20% of body height |
| Timer interval | 3 seconds (manual anchor) |
| Chart objects per candle | 4 |
---
## Installation
1. Copy `ForwardSimEngine.mq5` to your MetaTrader 5 data folder:
```
MQL5/Indicators/ForwardSimEngine.mq5
```
2. Open MetaEditor (F4 in MT5) and compile the file (F7).
3. In the MT5 Navigator panel under Custom Indicators, drag the indicator onto a chart.
4. Recommended timeframe: **M15**.
5. Configure inputs and click OK.
6. Open the Experts tab in the terminal to confirm the `[INIT]` and `[AVG]` log lines appear.
---
## Tuning Notes
- **`AvgLookback`** — increase to 100+ for a smoother long-run average. Lower to 20–30 to react faster to recent volatility shifts such as news events.
- **`InvalidationPips`** — increase on wide-spread instruments (XAUUSD, GBPJPY) to avoid premature removal. Lower on tight instruments (EURUSD, USDJPY) for stricter invalidation.
- **`CandleGapFraction`** — 0.08 works well on M15. Increase to 0.12–0.15 on higher timeframes where bar width appears wider on screen.
- **`CounterCandleFreq`** — set to 3 for more frequent pullbacks. Set to 6–8 for a cleaner directional sequence.
- **`FutureBars`** — keep at or below 60. Above that, the exponential decay reduces later candles to near the floor size and the projection becomes visually flat.
- **Manual anchor** — set `AutoAnchor = false` and draw an `OBJ_VLINE` named `FSE_Anchor` on any past bar to replay the projection from that point.
---
## Limitations
- The projection is a structured visual scenario, not a price forecast. It does not use volume, order flow, or market microstructure data.
- Signal detection is limited to EMA crossovers on closed bars. Intra-bar crosses are ignored until the bar closes.
- The invalidation threshold is fixed in pips. It does not adapt dynamically to ATR or recent volatility.
- The indicator does not place, manage, or close any trades. It is a visualization tool only.
- Wick and body calibration depends on the quality and quantity of history available on the active symbol and timeframe.