Forex
EA

EA: Standard Deviation Breakout Expert Advisor

Last updated 2026-07-19

An Expert Advisor (EA) is different from every other download on this site — an indicator only plots a line or fires an alert, while an EA actually sends orders on your behalf. This free EA trades a breakout strategy built around price making a new N-bar high or low, but it refuses to act on that breakout alone: it only enters once Standard Deviation independently confirms that volatility itself is expanding to a new N-bar high at the same moment. Stop Loss, Take Profit, and position size are then all handled automatically using the same ATR- and risk-based tools taught elsewhere on this site.

How the Strategy Works

The raw entry signal here is the simplest kind of breakout there is: has the just-closed bar's close reached the highest level of the last ConfirmPeriod bars (default 20)? If so, price has broken out to a new short-term high, and in theory that's a bullish signal — buyers have pushed price further than they have in the last 20 bars, and a breakout trader would normally jump on that. The mirrored case — the just-closed bar's close is the lowest of the last ConfirmPeriod bars — is the equivalent bearish signal.

The problem with trading that signal alone is one every breakout trader eventually learns the hard way: plenty of new N-bar highs happen on a single oversized candle or a brief, thin push rather than a genuine shift in how the market is trading, and those breakouts snap back into the range almost as fast as they left it. A quiet, low-volatility market that suddenly prints one large bar is not the same thing as a market that has genuinely started moving with conviction. This EA's answer is to demand a second, independent witness before it believes the breakout: at the same bar where price prints its new N-bar high (or low), the EA also checks whether Standard Deviation — a direct measure of how far recent closes are dispersing from their own moving average — is also higher than its own highest value over the prior ConfirmPeriod bars. Price and Standard Deviation are two completely different calculations built from the same raw closes, so when both independently agree that "something new is happening here," that agreement is meaningful: the breakout is arriving alongside a genuine expansion in how much the market is moving, not just one outsized print riding on top of an otherwise quiet, contracting range. If price makes a new high but Standard Deviation is not itself expanding, the EA treats that as a red flag and does nothing at all — this is the core idea the rest of this lesson builds on.

Stop Loss, Take Profit, and Position Sizing

PriceTake Profit (TP)Stop Loss (SL)BUYStandard Deviation (unbounded, always ≥ 0)StdDev expands
Entry conditions: BUY when price prints a new N-bar high at the same time Standard Deviation also prints a new N-bar high — the mirrored new-low confirmation triggers SELL — SL and TP are placed automatically from ATR

Both Stop Loss and Take Profit are sized from ATR rather than a fixed pip distance, so the stop always reflects how much a pair is actually moving right now instead of a number picked once and left unchanged. Stop Loss distance is ATRMultiplier × ATR (default multiplier 2.0, ATR calculated over ATRPeriod, default 14), and Take Profit is then set at RiskRewardRatio × Stop Loss distance (default 2.0) — so a BUY placed with a 20-pip ATR-based stop automatically gets a 40-pip target, and every trade this EA opens, on any pair or timeframe, carries the same fixed reward-to-risk proportion.

Position size follows the exact worked-example method from Risk Management Basics: RiskPercent (default 1.0%) of account balance is divided by the Stop Loss distance to produce a lot size, so the dollar amount at risk stays roughly constant trade to trade even though the ATR-based stop distance itself changes as volatility changes. A wide stop on a volatile pair yields a smaller lot; a tight stop on a calm pair yields a larger one. The calculated lot is normalized to the broker's minimum, maximum, and step lot size before the order is sent. UseFixedLot is available as an escape hatch for traders who would rather trade a constant lot size regardless of account risk — useful for testing, or for small accounts where percentage-based sizing would otherwise round down to the broker's minimum lot anyway.

Entry Conditions

  • BUY — the just-closed bar's close is the highest close of the last ConfirmPeriod bars, and Standard Deviation at that same bar is higher than its own highest value over the prior ConfirmPeriod bars.
  • SELL — the just-closed bar's close is the lowest close of the last ConfirmPeriod bars, and Standard Deviation at that same bar is higher than its own highest value over the prior ConfirmPeriod bars — the mirrored price condition, paired with the exact same volatility-expansion check.

That last detail is worth pausing on: both BUY and SELL share the identical stdDevLast > highestStdDev test. Standard Deviation has no direction — it only measures how widely price is dispersing, never which way it is dispersing — so there is no "bullish expansion" or "bearish expansion" for it to check separately. Direction comes entirely from which side of the price condition is met (a new high vs. a new low); volatility expansion is simply the shared precondition that has to be true regardless of which way price broke.

Beyond the dual-confirmation requirement itself, three safety mechanics govern every trade this EA can place: it evaluates entry conditions once per new bar only (tracked via the bar's open time), so a single qualifying bar can't trigger repeated trades within the same candle; it opens only one position at a time, checked by counting open trades filtered to this EA's own symbol and magic number before any new entry is considered; and it checks the current spread against MaxSpreadPoints (default 30) before sending an order, skipping the entry entirely if the spread is too wide to trade at a reasonable price. Every order is tagged with MagicNumber (default 20260737), so the EA's position-counting logic — and any future management logic — never touches a trade you opened manually or a trade placed by a different EA running on the same account.

Parameters

  • ConfirmPeriod (default 20) — the lookback window, in bars, used for both the price high/low check and the Standard Deviation expansion check. A shorter window (e.g. 10) reacts to smaller, more frequent breakouts; a longer window (e.g. 40-50) waits for a more significant price move and a more significant volatility shift before considering an entry, at the cost of fewer signals.
  • MAPeriod (default 20) — the period of the moving average that Standard Deviation measures dispersion against. This is Standard Deviation's own internal setting, separate from ConfirmPeriod.
  • MAShift (default 0) — shifts that internal moving average forward or backward on the chart; left at 0 for a standard, unshifted calculation.
  • MAMethod (default SMA) — the moving average type (Simple, Exponential, Smoothed, or Linear Weighted) that Standard Deviation's internal average uses.
  • AppliedPrice (default Close) — the price series Standard Deviation is calculated from.
  • ATRPeriod (default 14) — the lookback used to calculate ATR for Stop Loss sizing.
  • ATRMultiplier (default 2.0) — Stop Loss distance = ATR × this multiplier.
  • RiskRewardRatio (default 2.0) — Take Profit distance = Stop Loss distance × this ratio.
  • UseFixedLot — switches position sizing from risk-based to a constant lot size.
  • FixedLotSize (default 0.01) — the lot size used when UseFixedLot is enabled.
  • RiskPercent (default 1.0) — percentage of account balance risked per trade, used to calculate lot size when UseFixedLot is disabled.
  • MaxSpreadPoints (default 30) — skips new entries while the spread is wider than this, to avoid opening trades at a bad price during illiquid conditions.
  • MagicNumber (default 20260737) — tags every order this EA places so its position-management logic never touches trades opened by hand or by a different EA.
  • EnableTrading — a master on/off switch, useful for pausing the EA without removing it from the chart.

Why Require Volatility Expansion to Confirm the Breakout

This is the single design choice this whole EA is built around, so it's worth spelling out why it exists rather than treating it as a throwaway detail. A price breakout — a new N-bar high or low — only tells you that the most recent close is more extreme than anything in the recent lookback window. It says nothing about whether the market's overall behavior has actually changed, or whether this is just one unusually large bar sitting on top of an otherwise quiet, range-bound stretch. A market that's been contracting for weeks can print a single outsized candle on a news spike or a stop run, tag a new N-bar high, and then settle right back into the same tight range a bar or two later once that one print is absorbed. That pattern is one of the most common ways a naive breakout strategy loses money: it buys the top of a spike that had no real, sustained expansion behind it, and the reversal that follows hits the stop almost immediately.

Standard Deviation solves this because it measures something a price breakout alone cannot: whether the market's dispersion around its own average is genuinely widening, bar after bar, rather than just spiking once. Because Standard Deviation is recalculated fresh on every bar from the same MAPeriod-bar window, a true new high in Standard Deviation means the whole recent stretch of price action has grown more spread out, not just that one candle was unusually large. Requiring Standard Deviation to independently reach its own new high at the exact same bar as the price high is effectively asking two unrelated calculations, built from the same raw data in different ways, to agree before the EA trusts the move. This filters out a large share of the thin, single-candle breakouts that make up most false starts, and lets through the breakouts that arrive alongside a genuine, broader increase in how much the market is moving. The tradeoff is symmetrical to the one covered in this site's On-Balance Volume EA lesson: that EA demands volume confirm a breakout, this one demands volatility confirm it, and both exist to filter out weak, unconvincing breakouts — they simply measure conviction through different lenses, one through participation, the other through the sheer size of price movement itself. Either filter will occasionally sit out a fast, genuine breakout that happened to start from unusually calm conditions, since Standard Deviation, like OBV, can only recognize expansion that has already begun accumulating, not a move that is about to develop. No confirmation filter can promise a confirmed breakout continues; it can only raise the odds that a signal reflects a real shift in market behavior rather than noise.

Installation and Setup

EAs install into a different folder than indicators, and this is the step people miss most often.

  1. Download the file below for your platform.
  2. Open MetaTrader → click FileOpen Data Folder.
  3. Place the file in MQL4/Experts (MetaTrader 4) or MQL5/Experts (MetaTrader 5) — not the Indicators folder.
  4. Restart MetaTrader, then drag the EA from the Navigator window onto a chart.
  5. Enable AutoTrading (MT4) or Algo Trading (MT5) in the toolbar — the EA will not place any trades while this is off, even if it's attached to a chart.
  6. Review and adjust the input parameters in the EA's settings dialog before confirming.

A Word of Caution

Test on a demo account first, across different market conditions and multiple pairs, before risking real money — a backtest or a good run on a demo account never guarantees future results. This EA can and will lose on individual trades; the Standard Deviation confirmation filter reduces the number of false breakouts it acts on, but it cannot eliminate them, and it does nothing to protect against a breakout where volatility genuinely expanded right up until price reversed anyway — a volatility spike confirms that the market is moving more, not which direction it will keep moving, and a sharp move that immediately reverses will still pass this EA's filter and still get stopped out. Consider combining this EA with a trend filter or other read on market direction (a higher-timeframe check, price structure, or Trend vs Range) rather than trading a volatility-confirmed breakout in isolation, since expansion alone says nothing about which way a market ultimately continues. Both the MT4 and MT5 files below are source code — open and read them fully, and understand exactly what every parameter does, before attaching this EA to a live account. This content is general education, not personalized investment advice.

Download the EA

How to Install — MetaTrader 4

  1. Download the standard-deviation-breakout-ea.mq4 file below.
  2. Open MetaTrader 4 → click FileOpen Data Folder.
  3. Place the file in the MQL4/Experts folder.
  4. Restart MetaTrader 4, then drag the EA from the Navigator window onto the chart.

How to Install — MetaTrader 5

  1. Download the standard-deviation-breakout-ea.mq5 file below.
  2. Open MetaTrader 5 → click FileOpen Data Folder.
  3. Place the file in the MQL5/Experts folder.
  4. Restart MetaTrader 5, then drag the EA from the Navigator window onto the chart.

Both files are source code — open and review the full code before using it, for your own safety.

Download standard-deviation-breakout-ea.mq4

For MetaTrader 4 — this is source code (.mq4), open and review it fully before using it.

Download File

Download standard-deviation-breakout-ea.mq5

For MetaTrader 5 — this is source code (.mq5), open and review it fully before using it.

Download File