# PHASE 2 NOTES: Miroscope_EA_P2.mq5

Built: 2026-09-22. Source: `ea/Miroscope_EA_P2.mq5` (649 lines, 24,262 bytes).
Python port + execution proof: `ea/test_ea_logic.py` -> `ea/TEST-RESULTS-P2.md`.

## What the EA does

- **Lane 0 (default): D1 bias traded on H1. Lane 1: H4 bias traded on M15.**
  Bias = closed-bar close vs SMA(50) on the bias TF. Setup direction must agree.
- **1-2-3 detector** ported from Scanner P1 geometry (bar1/b2/b3 on trigger TF),
  with the P1 gate stack: pullback zone (|close - SMA20| <= 0.5*ATR), RSI veto
  (buy rejected RSI>65, sell rejected RSI<35), bar-2 size filter.
- **Setup objects** carry entry / SL / TP1 / TP2 / invalidation / scale-in level:
  BUY: entry = entry-bar open; SL = b2 low - 0.5*ATR; risk = entry - SL;
  TP1 = entry + 1.5*risk ("15 off"); TP2 = entry + 3*risk ("5 running" runner);
  invalidation = b1 low (premise broken below here); scale-in at entry - 0.5*risk.
  SELL mirrored. Max 1 active setup per direction.
- **Scale-in:** second limit order at the scale level, sized for 40% of setup risk
  against (scaleLevel -> SL); first entry carries 60%. Combined worst case = 1R.
- **Partial-plus-runner:** at TP1 close 75% of position, move SL to breakeven + 0.1R;
  runner exits at TP2. Protective SL/TP2 rest on the broker from entry.
- **Pre-entry invalidation** kills the pending (and deletes its broker order) at 0 cost.
  **Expiry:** pending killed after 12 trigger bars. **Session cutoff** (21:00 server):
  pendings cancelled, opens flattened. **Daily loss limit** (-3%): no new setups.
- **On-chart display:** mode line, bias line, per-setup entry/SL/TP1/TP2/invalidation
  lines and status labels, all prefixed MS_EA_.

## The hard gate (trade execution OFF by default)

`bool TradingAllowed() { return(InpLiveTrading && InpAllowOrders); }`
Both inputs default **false**. All 12 order call sites (BuyLimit x2, SellLimit x2,
OrderDelete x3, PositionClose, PositionClosePartial, PositionModify x2) were
audited to sit inside a `TradingAllowed()` branch or behind an early return.
With either input false the EA tracks setups as labeled on-chart signals only.

## Manual audit (no compiler here)

- Brace/paren balance: 62/62, 360/360. 25 functions, zero duplicate definitions.
- Every trade.* call verified guarded (see above). No OrderSend/CTrade raw calls
  outside CTrade; no hidden execution path.
- NOT compiled. "Compiles clean" is NOT claimed. MetaEditor compile check required
  before this file goes on any terminal, live or demo.

## Simulation results (ported logic, seed 20260922)

- T1: 12/12 injected patterns detected at the expected bar; all setup values
  (entry/SL/TP1/TP2/invalidation/scale/risk) exact vs independent recomputation.
- T2: 0 setups created against the bias (4 bullish shapes, bias down).
- T3: pre-entry invalidation kill -> INVALIDATED, 0.0R.
- T4: untouched setup -> EXPIRED after 12 bars, 0.0R.
- T5: fill -> scale-in -> TP1 partial (75%) -> runner TP2 -> CLOSED at **+3.025R**
  (matches hand computation: 1.8R partial + 1.225R runner).
- T6: fill -> scale-in -> straight to SL -> CLOSED at **-1.0R** combined,
  proving correct risk allocation across entries.
- T7: lane engine end-to-end, 46 setups lane 0 / 84 lane 1, 100% direction/bias agreement.

## Test-harness bugs caught and fixed (not EA bugs)

- P&L accumulator summed fill/scale-in event prices; fixed to sum only pnl events.
- Scripted price paths anchored to the wrong bar; anchored to entry; dip placed
  deterministically between scale level and invalidation.
