Loading…

Darvas Box System

Many published "Darvas box" EAs actually implement a Donchian-style fixed lookback channel and call it a Darvas box, which loses the part of the original method that made it work: the box is not a fixed window of N bars, it is defined by the market's own behaviour after a new high. Darvas watched for a stock to make a new high, then waited to see whether the price would fail to exceed that high for several days running - if it did, the high became the box top, and the lowest point reached during that stall became the box bottom. The box's size and duration are entirely determined by how long the market takes to stall out after a new high, not by a number chosen in advance. This EA implements that state machine directly. Each closed bar either makes a new high (which resets the confirmation counter - Darvas required the stall to happen without being interrupted by a still-higher high) or fails to exceed the current reference high (which counts toward InpConfirmBars and updates the running box-bottom candidate to the lowest low seen so far in the stall). Once InpConfirmBars consecutive bars have failed to exceed the reference high, the box is confirmed - and the state machine immediately starts watching for the NEXT box above it, so the trailing-stop chain is always ready. Entry is a breakout of the confirmed box's top on a CLOSED bar's close, not an intrabar touch, so a stop-hunting wick through the box top does not trigger an entry the market did not actually confirm. The initial stop is the box's own bottom. As price continues higher and a new box confirms above the entry, the stop trails up to that new box's bottom - and only up, never down - which is the mechanism that let Darvas ride a strong trend while giving each fresh consolidation room to develop rather than trailing a fixed distance that would stop the trade out on ordinary pullbacks. An optional mirrored short-side box (a low the market fails to undercut for InpConfirmBars bars) is included for symmetry, since the classic method is directional by nature and a trader may want either side. Notes and limitations - The box state machine starts fresh on attach - it does not replay chart history to find an already-in-progress box, so the first box after attaching may take a while to confirm even on a symbol that was mid-trend. - Entry requires the breakout bar's CLOSE to clear the box boundary, not merely an intrabar touch - this avoids stop-hunt wicks but also means a breakout that reverses intrabar without closing beyond the box is correctly not traded, which can occasionally miss a fast move that closes back inside the box. - The trailing stop only ever tightens (up for longs, down for shorts) and is skipped for one bar if the new level falls inside the broker's minimum stop/freeze distance - it will apply on a later bar once price has moved enough to clear that distance. - Only one position at a time; a new confirmed box while already positioned only trails the stop, it is never treated as a fresh entry signal. - Lot sizing uses MODE_TICKVALUE/MODE_TICKSIZE, not a hardcoded contract size, so it is correct on gold, indices and JPY crosses as well as plain FX pairs. - Like any trend-following breakout system, this can produce a string of small losses in a choppy, range-bound market before a genuine trend develops - this is inherent to the method, not a configuration mistake.

Expert advisor · MT4 · v1.0 · Free

About Darvas Box System

Darvas Box System is an expert advisor (trading robot) for MetaTrader 4. It moves the stop loss behind price (trailing stop / break-even), can size trades from your balance and risk percent and has a news filter. It has 9 settings, such as Confirm bars, Allow shorts and Use risk sizing. MQL source code included.

It also trades breakouts.

Files in the download: DarvasBoxSystem.ex4, DarvasBoxSystem.mq4, DarvasBoxSystem.txt.

Trading robots can lose money. Check every setting and test on a demo account before using real funds.

Settings

SettingDefaultWhat it does
Confirm bars3Consecutive closed bars that must fail to exceed the high to confirm a box (the real Darvas rule)
Allow shortstrueAlso trade the mirrored short-side box (new low held for InpConfirmBars bars)
Use risk sizingtrueSize by percent risk to the box bottom/top instead of a fixed lot
Risk percent1.0Percent of equity risked per trade, when risk sizing is on
Fixed lots0.10Lot size when risk sizing is off, or as a sizing fallback
Magic number42201Magic number for orders opened by this EA
Slippage pips3Slippage tolerance, in pips
Max retries5Attempts per trade action before giving up
Retry wait ms500Pause between attempts, milliseconds

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Experts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
  6. Test it first on a demo account or in the Strategy Tester.

Download the PDF guide

Tags: Breakout · Channels & Envelopes · Trailing Stop & Break-even · Risk & Lot Size · News Trading · MT4 Expert Advisor · MQL4 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria