Kaufman Adaptive Moving Average System
RISK WARNING: this EA places real market orders with a protective stop on every trade. Confirm behaviour on a demo account before running it live, and size conservatively - InpRiskPercent controls how much of the account each trade risks. Perry Kaufman's Adaptive Moving Average (KAMA, also called AMA), from "Smarter Trading" (1995), adjusts its own smoothing speed to how efficiently price is trending: an efficiency ratio compares net directional movement to total movement over a lookback window, and that ratio is squeezed between a fast and a slow smoothing constant so the average tracks closely in a clean trend and nearly flattens in chop. MQL4 has no built-in AMA indicator function, so this EA computes the full series from Kaufman's published formula every new bar. That recomputation matters for a structural reason, not just as an implementation detail: KAMA has no closed-form update from one bar to the next, because each value depends recursively on the previous KAMA value and the previous one on the one before that, back to wherever the series was seeded. A custom indicator carries that running state in a persistent buffer across ticks. A plain EA has no equivalent buffer, so rather than invent an ad hoc persistence mechanism this EA recomputes a bounded lookback window of KAMA fresh on every new bar - more CPU work than an incremental update, but the honest way to get Kaufman's actual number. The trading rule is Kaufman's own original one: trade the direction the average is currently sloping, once that slope clears a noise filter sized from the recent average bar-to-bar KAMA movement - not a price/average crossover, which is a common but different rule attributed to the same indicator. A protective ATR-based stop is added on top of Kaufman's stop-and-reverse behaviour, because reversing purely on the next opposite signal with no stop at all leaves the risk on any single trade completely unbounded between signals. That addition is a deliberate departure from the historical source and is stated here plainly rather than left silent. Position size is computed from account risk percentage and the trade's actual ATR stop distance, converted to lots through tick value and tick size rather than a hardcoded contract size - the conversion this catalogue uses everywhere real money is at stake, because a fixed contract size silently mis-sizes metals, indices and JPY crosses. Notes and limitations Recomputing InpHistoryBars of KAMA on every new bar is deliberate but not free - very large values combined with a very short timeframe can add up in CPU time; the default of 200 bars is comfortable on any normal timeframe. Only one position is held at a time; an opposite signal closes the current position and reverses rather than adding to it. Like any trend-following average, this does poorly in a directionless, choppy market - the efficiency ratio it depends on is specifically what falls toward zero in exactly that condition, which is the intended behaviour, not a defect.
Expert advisor · MT4 · v1.0 · Free
About Kaufman Adaptive Moving Average System
Kaufman Adaptive Moving Average System is an expert advisor (trading robot) for MetaTrader 4 based on ATR. It can size trades from your balance and risk percent. It has 12 settings, such as Magic, Er period and Fast period. MQL source code included.
Files in the download: KaufmanAdaptiveMovingAverageSystem.ex4, KaufmanAdaptiveMovingAverageSystem.mq4, KaufmanAdaptiveMovingAverageSystem.txt.
Trading robots can lose money. Check every setting and test on a demo account before using real funds.
Settings
| Setting | Default | What it does |
|---|---|---|
| Magic | 20261208 | Magic number for this system's own orders |
| Er period | 10 | Efficiency ratio lookback (Kaufman's default is 10) |
| Fast period | 2 | Fastest EMA period folded into the smoothing constant |
| Slow period | 30 | Slowest EMA period folded into the smoothing constant |
| History bars | 200 | Bars of KAMA recomputed fresh each new bar |
| Filter factor | 0.1 | Slope must exceed this times the recent average |KAMA change| to signal |
| Filter lookback | 20 | Bars used to measure the recent average |KAMA change| for the filter |
| Atr stop multiple | 2.0 | Protective stop distance as a multiple of ATR(14) |
| Risk percent | 1.0 | Risk % of balance per trade |
| Max slippage points | 5 | Slippage tolerance passed to OrderSend |
| Max retries | 5 | Attempts per order action before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Experts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
- Test it first on a demo account or in the Strategy Tester.
Tags: Moving Average · Risk & Lot Size · ATR · MT4 Expert Advisor · MQL4 Source Code