Loading…

Trailing Drawdown Aware Sizer

A fixed lot size, or a fixed percent of balance, ignores the one number that actually matters on a trailing-drawdown funded account: how much room is left before the trailing line. The same account with the same balance can be one good trade away from the limit or comfortably clear of it, depending on how high the peak balance has climbed since - a fixed sizing rule cannot tell the difference. This EA tracks the highest balance the account has ever reached and computes a trailing drawdown line at a configured percentage below that peak, the shape most funded-account trailing rules actually use. On every recalculation it takes the live buffer between current equity and that line, risks a configured percentage of that buffer against a configured stop distance, and turns the result into a lot size normalised to the traded symbol's minimum lot, lot step and maximum lot. Two ways to use the number, both running from the same formula so they never disagree: ADVISORY - the suggested lot is published as a GlobalVariable on every recalculation. A separate sizing EA on the same terminal reads it before its own OrderSend and caps its own size accordingly. This EA cannot intercept another program's trade request - MQL4 has no such hook - so this is a number offered for another program to respect, not a guarantee enforced on it. STANDALONE - two chart buttons, BUY (sized) and SELL (sized), let this EA size and send its own market order directly using the identical formula, with a stop-loss placed at the configured distance. This exists so the sizing logic can be exercised and observed directly, not only ever trusted blindly to another program's implementation. Notes and limitations - The suggested lot size is advisory when read by another program - this EA cannot force any other EA to respect it, and cannot intercept or resize an order that program sends on its own. - The trailing line here uses BALANCE as the peak reference, since that matches the most common published trailing-drawdown shape. If your programme's trailing line instead tracks equity (including floating P/L) at its peak, the two will disagree in a running trade - confirm which basis your agreement actually uses before relying on this figure. - When the buffer to the trailing line reaches zero, the suggested lot is 0 and the standalone buttons refuse to send an order at all, alerting instead. This is deliberately conservative, not a guarantee that 0 is the objectively correct answer for every rule variant. - Standalone orders use a fixed stop distance in points, not broker-validated against MODE_STOPLEVEL/MODE_FREEZELEVEL beforehand - a stop closer than the broker allows will be rejected by OrderSend and reported through the normal retry/alert path rather than silently adjusted. - Peak balance tracking starts from whatever the balance is the first time this EA attaches - it does not know about any higher balance the account reached before that, so attach it as early as possible in the account's life for an accurate trailing line.

Expert advisor · MT4 · v1.0 · Free

About Trailing Drawdown Aware Sizer

Trailing Drawdown Aware Sizer is an expert advisor (trading robot) for MetaTrader 4. It moves the stop loss behind price (trailing stop / break-even) and can size trades from your balance and risk percent. It has 8 settings, such as Trailing drawdown percent, Risk percent of buffer and Stop loss points. MQL source code included.

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

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

Settings

SettingDefaultWhat it does
Trailing drawdown percent10.0Trailing drawdown limit, percent of the highest balance ever reached
Risk percent of buffer20.0Percent of the remaining buffer to the trailing line to risk on one new position
Stop loss points300Stop distance in points used to convert risk money into a lot size
Magic number238100Magic number for orders this EA sends itself (standalone mode)
Show standalone buttonstrueShow the BUY/SELL chart buttons that let this EA size and send its own orders
Max retries5OrderSend retry attempts on a retryable error
Retry wait ms500Pause between retry attempts, in milliseconds
Recalc seconds15Minimum seconds between recalculations/publish

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: Trailing Stop & Break-even · Risk & Lot Size · MT4 Expert Advisor · MQL4 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria