Skip to content
EE
All work

KeyLevel Hedge EA

Automating a strategy I was already trading by hand

KeyLevel Hedge EA — Automating a strategy I was already trading by hand
Role
Personal project
Year
2026
Status
Shipped

The problem

A MetaTrader 5 Expert Advisor in MQL5 that watches hand-drawn key levels on an H1 chart, waits for a specific one-to-three-candle confirmation, and opens a true hedged straddle — built because I trade this setup manually and kept missing it while away from the screen.

The setup is entirely mechanical once you've drawn the levels: price touches a level, a confirmation pattern completes, you take both sides. The only reason it was ever discretionary is that it requires being in front of a chart at the moment it happens. Every missed trade was a scheduling failure, not an analytical one — which is precisely the kind of problem worth handing to software.

How I built it

01

A state machine per level, not a global signal

Each level runs its own state machine — IDLE → GOT_C1 → GOT_C2 → fire → COOLDOWN — because levels behave independently and a single global 'armed' flag would let one level's activity suppress another's valid setup. Confirmation includes a wick-smaller-than-body continuation test and a re-arm distance, so price grinding along a level doesn't fire repeatedly.

02

Refusing to run in the wrong account mode

A true straddle needs simultaneous long and short positions, which requires a hedging account. On a netting account the second order silently closes the first, producing a flat position and a strategy that appears to do nothing. The EA checks the account mode at initialisation and refuses to start rather than trading incorrectly — failing loudly beats failing profitably-looking.

03

Entries automated, exits deliberately not

Both legs get an independently risk-sized fixed stop and no take-profit. Exits stay manual because the exit is where the discretion actually lives in this strategy — automating the part I execute well and leaving the part I execute by judgement. Alongside that: a spread filter, a magic-number lock allowing one cycle per pair, a master kill switch, and a live on-chart status block.

04

A backtest that lied

An early run produced false-positive straddles that looked like a logic bug. The root cause was picking the wrong EA in the Strategy Tester dropdown — an older, cruder version of the same idea. Worth recording, because the instinct on a bad backtest is to start rewriting the strategy, and the actual fault was in the harness.

Built with

  • MQL5
  • MetaTrader 5
  • Strategy Tester

Outcome

  • Compiles clean against the MQL5 toolchain, with manual level inputs added so it can be backtested
  • Per-level state machines, spread filter, position lock, hedging-mode self-check and kill switch
  • Currently in forward testing on demo — not yet risking live capital