EA_ZigZAP

 

EA Development Notes

ZigZag Reversal OCO EA: Logic and Inputs

A breakout/reversal straddle strategy for MetaTrader, explained for traders and developers.

Core Idea

This Expert Advisor watches the ZigZag indicator in real time and reacts the moment its swing direction flips —
from a down-leg to an up-leg, or vice versa. Rather than guessing which way price will break, it places a
pending order on both sides simultaneously (a “straddle”), lets the market pick a direction,
and automatically cancels the untouched order once the other one fills. This is the classic
OCO (“One Cancels the Other”) breakout/reversal pattern.

How the Logic Works, Step by Step

  1. Direction detection — On every tick, the EA reads the two most recent ZigZag pivot values,
    including the point still forming on the current (unclosed) candle. Comparing them tells it whether the
    last completed leg was rising or falling.
  2. Flip trigger — When that direction differs from the previous tick’s direction, a reversal
    event fires.
  3. Cleanup — Any of the EA’s own pending orders left over from the previous signal are
    cancelled. Optionally, any open position from the prior signal can also be closed out (see
    CloseOnReverseSignal below).
  4. Straddle placement — A Buy-side and a Sell-side pending order are sent together, offset
    from the current price by a fixed distance.
  5. OCO management — Every tick, the EA checks whether either side has been triggered. The
    instant one becomes a live position, the sibling pending order is deleted.
  6. Trailing stop — Once a position is open, its stop-loss trails the price by a configurable
    distance to lock in gains.

External Inputs

Input Purpose
ExtDepth / ExtDeviation / ExtBackstep Standard ZigZag parameters controlling how sensitive the swing detection is
EntryMode Choose ENTRY_STOP (breakout style — orders placed outside current price) or ENTRY_LIMIT (pullback style — orders placed inside current price)
Lots Trade volume for each side of the straddle
StopDistance Distance (in points) from the current price to each pending order
TakeProfit / StopLoss Profit target and protective stop, in points (0 disables either)
Slippage Maximum allowed slippage on execution
OrderExpireMinutes Optional expiry for pending orders (0 = no expiry)
MagicNumber / EA_Comment Identifiers so the EA only manages its own orders
CloseOnReverseSignal If true, closes any open position from the prior signal as soon as a new reversal fires
TrailingStop / TrailingStep Trailing-stop distance and the minimum price movement required before it updates

Why This Design

Because the reversal is confirmed only after the fact, nobody can know in advance which way price will
actually break once ZigZag flips. Placing both a buy and a sell order removes that guesswork — whichever
direction the market moves, the EA is already positioned, and the losing side is cleaned up automatically.

Available for both MetaTrader 4  and MetaTrader 5 , with matching logic
and inputs across both platforms.
Trading involves risk. This article describes the mechanics of an automated strategy and is not investment advice.

Download EA

 

Leave a Reply

Your email address will not be published. Required fields are marked *