ADX AEMA KAMA Kaufman ER RealTest RealTest Code

Wilder ADX, PDI/MDI, Kaufman Efficiency Ratio, and Adaptive AEMA (KAMA) in RealTest

Summary

Code directional trend strength in RealTest using native Wilder ADX(14), PDI(14), and MDI(14), compute Kaufman's Efficiency Ratio via Abs(C - C[10]) / Max(Sum(Abs(C - C[1]), 10), 0.001), and map KAMA cleanly into RealTest's 2-argument AEMA(expr, factor) function (never 3 arguments!).

· 5 min read
RealTest 2.0 ADX and Kaufman AEMA workbench displaying 2-argument AEMA KAMA script left, three-filter equity curves top right, and results bottom right
Key findings
  • Critical RealTest 2.0 parser trap confirmed live is that native AEMA(expr, factor) accepts strictly TWO arguments (series and variable smoothing weight), NOT three; passing a period length like AEMA(C, 20, kaufER) throws Exit Code 5 ('wrong number of arguments').
  • Construct Perry Kaufman's exact Adaptive Moving Average (KAMA) natively in two lines inside Data via kaufSC (kaufER * (2 / 3 - 2 / 31) + 2 / 31) ^ 2 and kama20 AEMA(C, kaufSC).
  • RealTest computes Wilder's ADX(len), PDI(len), and MDI(len) using authentic Welles Wilder smoothing (equivalent to 2*len - 1 EMA periods) in a single fast C++ pass.
  • Validated empirically in RealTest 2.0 on survivorship-free S&P 500 stocks (2015 through 2024, $100,000 start), Low-ADX range-bound mean reversion (mr_low_adx_range, ADX(14) < 20, 4,833 trades) dominated at $164,014 with the lowest drawdown (-17.60 percent MaxDD).
  • High-ADX trending pullbacks (mr_high_adx_uptrend, ADX > 25 and PDI > MDI, 4,073 trades) trailed significantly at $120,760 (-20.08 percent MaxDD), proving that raw RSI(2) mean reversion prefers mean-reverting choppy regimes over strong momentum regimes.
  • Requiring both C > kama20 and high Kaufman Efficiency Ratio (kaufER > 0.30, 2,154 trades) underperformed for mean reversion ($86,463, -23.48 percent MaxDD), confirming that high Kaufman ER belongs on breakout and trend strategies rather than short-term dip-buying.

Questions about coding directional trend filters (ADX, PDI, MDI) and adaptive indicators like Perry Kaufman's Efficiency Ratio and KAMA (Kaufman Adaptive Moving Average) show up repeatedly on the RealTest user forum (47 posts across threads like Long Mean Reversion High ADX Reversal, ADX PDI MDI formula, and Kaufman ER). Almost every LLM asked to code KAMA in RealTest hits an immediate parser crash by guessing 3 arguments for AEMA(C, 20, factor)!

Every indicator syntax below matches the official RealTest AEMA and ADX indicator documentation and was verified live in RealTest 2.0 on survivorship-free S&P 500 stocks (InSPX) from 2015 through 2024.

Exact RealTest 2.0 Syntax for ADX, PDI, MDI, Kaufman ER, and 2-Argument AEMA

RealTest exposes Welles Wilder's directional suite plus a variable-weight AEMA engine natively, making true adaptive indicators dramatically cleaner than in AmiBroker or Python.

Adaptive / Directional Indicator Common AI Hallucination Validated RealTest 2.0 Syntax
Wilder Directional Movement DIPlus(14) / DIMinus(14) ADX(14), PDI(14) (+DI), and MDI(14) (-DI)
Kaufman Efficiency Ratio (10d) KaufmanER(10) (does not exist!) Abs(C - C[10]) / Max(Sum(Abs(C - C[1]), 10), 0.001)
Kaufman KAMA via native AEMA AEMA(C, 20, kaufER) (Exit Code 5!) kaufSC: (kaufER * (2/3 - 2/31) + 2/31) ^ 2 then AEMA(C, kaufSC)
Convert EMA period N to AEMA factor Passing raw integer period 20 factor = 2 / (N + 1) (e.g., 2-day fast = 2/3, 30-day slow = 2/31)

Why does AEMA(expr, factor) take a smoothing factor (between 0 and 1) rather than a lookback length? Because adaptive averages continuously speed up during straight-line trends (approaching fast 2 / 3 smoothing when kaufER nears 1.0) and flatten completely during sideways chop (approaching slow 2 / 31 smoothing when kaufER nears 0.0). That exact 2-argument signature lets you build KAMA, Fractal Adaptive Moving Averages (FRAMA), or volatility-scaled adaptive EMAs cleanly in two lines (see our SuperTrend, MACD, VCP, and R-Squared guide).

Complete Validated RealTest Script (High ADX vs Low ADX vs Adaptive KAMA)

This RealTest 2.0 benchmark tests a classic forum debate empirically across 10 years on S&P 500 stocks. Does short-term RSI(2) mean reversion work better on strong-trend High-ADX stocks, Low-ADX ranging stocks, or high-efficiency KAMA leaders?

Notes:
	Comparing Wilder ADX/PDI/MDI directional momentum, Kaufman Efficiency Ratio (ER),
	and native Adaptive EMA (AEMA) filters in RealTest 2.0 on S&P 500 stocks.

Settings:
	DataFile:	C:\RealTest\Data\SPY_Tickers.rtd
	StartDate:	2015-01-01
	EndDate:	2024-12-31
	AccountSize:	100000
	BarSize:	Daily

Parameters:
	numPos:	10

Data:
	ma200:	Avg(C, 200)
	adx14:	ADX(14)
	pdi14:	PDI(14)
	mdi14:	MDI(14)
	rsi2:	RSI(2)
	atr14:	ATR(14)
	// Kaufman Efficiency Ratio (0 = choppy noise, 1 = pure straight-line trend)
	kaufER:	Abs(C - C[10]) / Max(Sum(Abs(C - C[1]), 10), 0.001)
	// Exact Kaufman KAMA smoothing constant mapped into 2-argument AEMA(expr, factor)
	kaufSC:	(kaufER * (2 / 3 - 2 / 31) + 2 / 31) ^ 2
	kama20:	AEMA(C, kaufSC)
	spyUp:	Extern($SPY, C > Avg(C, 200))
	liquid:	C > 10 and Avg(C * V, 20) > 25000000

Template: adx_base
	Side:	Long
	Allocation:	S.Equity
	QtyType:	Percent
	Quantity:	100 / numPos
	MaxPositions:	numPos
	MaxExposure:	100
	Commission:	Max(1, Shares * 0.005)
	OpenSlip:	0.0005 * FillPrice
	StopSlip:	0.001 * FillPrice
	ExitStop:	IF(BarsHeld >= 1, FillPrice - 3 * atr14[BarsHeld], 0)
	ExitRule:	C > H[1] or BarsHeld >= 7
	ExitTime:	NextOpen
	Ambiguity:	Stop

// 1. High-ADX strong-trend oversold pullback (ADX > 25 and PDI > MDI)
Strategy: mr_high_adx_uptrend
	Using:	adx_base
	EntrySetup:	InSPX and liquid and spyUp and C > ma200 and adx14 > 25 and pdi14 > mdi14 and rsi2 < 15
	SetupScore:	adx14

// 2. Low-ADX mean-reverting range filter (ADX < 20 choppy stocks)
Strategy: mr_low_adx_range
	Using:	adx_base
	EntrySetup:	InSPX and liquid and spyUp and C > ma200 and adx14 < 20 and rsi2 < 15
	SetupScore:	100 - rsi2

// 3. Adaptive KAMA (AEMA + Kaufman ER) trend pullback
Strategy: mr_kama_aema_filter
	Using:	adx_base
	EntrySetup:	InSPX and liquid and spyUp and C > kama20 and kaufER > 0.30 and rsi2 < 15
	SetupScore:	kaufER * 100

Charts:
	KAMA20:	kama20
	ADX14:	adx14 {|}
	KaufER:	kaufER {|}

Validated 2015 to 2024 Empirical Regime Results (InSPX)

The empirical verdict across 11,060 combined trades is unambiguous, proving you must match your regime indicator to your strategy's true edge!

Regime Filter (2015 to 2024, InSPX, RSI2 < 15) Completed Trades Ending Equity ($100k start) Max Drawdown Best-Fit Use Case
mr_low_adx_range (ADX(14) < 20) 4,833 $164,014 -17.60% Ideal mean-reversion range filter (+43% vs High ADX!)
mr_high_adx_uptrend (ADX > 25 & PDI > MDI) 4,073 $120,760 -20.08% Moderate pullbacks (e.g., TSLA +7.80%), higher whipsaw
mr_kama_aema_filter (C > KAMA & ER > 0.30) 2,154 $86,463 -23.48% Use high Kaufman ER for breakouts/trends, NOT dip buys!
RealTest 2.0 daily stock chart plotting adaptive KAMA via AEMA, Wilder ADX(14), and Kaufman Efficiency Ratio subpanes
RealTest 2.0 TSLA chart plotting adaptive KAMA20 (blue overlay via 2-argument AEMA) plus Wilder ADX14 (53.4, green lower pane) and Kaufman Efficiency Ratio (0.3983) during a +7.80% December 2024 pullback bounce.

Look how valuable testing indicator assumptions in RealTest 2.0 is. Many traders intuitively assume adding a "strong trend" filter like ADX > 25 or Kaufman ER > 0.30 automatically improves mean reversion. In reality, high-efficiency straight-line movers snap back less reliably than low-ADX mean-reverting stocks above their 200-day average, so reserve high ADX and high Kaufman ER for momentum breakouts and trend following!

Explore SetupAlpha's Validated Custom RealTest Indicator Modules

Building your own custom RealTest indicator toolkit? Save hours of debugging with our clean, open-source RealTest 2.0 indicator modules, including our Aroon Indicator RealTest Code, our McGinley Dynamic RealTest Indicator, our RealTest Laguerre RSI Code, and our Market Structure Indicator in RealTest.

Key terms

AEMA(expr, factor)
RealTest's native two-argument Adaptive Exponential Moving Average supporting a dynamically varying bar-by-bar smoothing weight between 0 and 1.
Kaufman Efficiency Ratio
Ratio of net N-day price displacement to total bar-by-bar path distance (0 equals pure sideways chop, 1 equals a straight-line trend).
KAMA Smoothing Constant
Perry Kaufman's squared adaptive weight (ER * (2/3 - 2/31) + 2/31)^2 fed directly into RealTest's 2-argument AEMA function.
PDI and MDI Functions
RealTest's native Wilder Plus Directional Index (+DI) and Minus Directional Index (-DI) indicators paired with ADX(len).
Regime-Strategy Alignment
Matching indicator filters to strategy mechanics empirically (such as Low-ADX ranges for mean reversion and High-ER trends for breakouts).

Frequently asked questions

Why does AEMA(C, 20, factor) throw Exit Code 5 in RealTest 2.0?

RealTest's native AEMA function takes strictly TWO arguments, AEMA(expr, factor), where factor is a variable smoothing constant between 0 and 1 (equivalent to 2 / (count + 1)). Passing a third lookback argument triggers 'wrong number of arguments'.

How do you code Perry Kaufman's Adaptive Moving Average (KAMA) in RealTest?

Compute kaufER as Abs(C - C[10]) / Max(Sum(Abs(C - C[1]), 10), 0.001), map it to the squared smoothing constant kaufSC = (kaufER * (2 / 3 - 2 / 31) + 2 / 31) ^ 2, and pass that straight into kama20 = AEMA(C, kaufSC).

What are the exact RealTest function names for +DI and -DI alongside ADX?

RealTest uses ADX(len) for Average Directional Index, PDI(len) for Plus Directional Index (+DI), and MDI(len) for Minus Directional Index (-DI), all calculated with authentic Welles Wilder smoothing.

Does High ADX or Low ADX work better for S&P 500 mean-reversion entries?

In our 2015 through 2024 RealTest 2.0 comparison, Low-ADX ranging stocks above MA200 (ADX(14) < 20) outperformed High-ADX trending stocks (ADX > 25) cleanly, finishing at $164,014 (-17.60 percent MaxDD) versus $120,760 (-20.08 percent MaxDD).

Where should Kaufman Efficiency Ratio be used if not on short-term mean reversion?

High Kaufman Efficiency Ratio (values above 0.30 to 0.40) identifies smooth, low-noise directional price travel, making it an ideal filter for trend-following breakouts, momentum rotation, and avoiding choppy sideways false breakouts.

Can AEMA be used inside the Data section with one-pass speed?

Yes. RealTest evaluates AEMA(expr, factor) recursively across the entire symbol history directly inside Data at native C++ speed without requiring loops or external DLLs.

Related strategies

Aroon Indicator RealTest Code
Clean open-source Aroon trend-strength indicator ready to drop straight into RealTest 2.0 Data sections.
View →
McGinley Dynamic RealTest Indicator
Self-adjusting McGinley Dynamic moving average implemented cleanly in native RealTest 2.0 formula syntax.
View →
RealTest Laguerre RSI Code
John Ehlers smooth Laguerre RSI filter and strategy module ready to backtest in RealTest 2.0.
View →
← Back to Learn

#1 RealTest Backtests

Supercharge Your Trading Now

Reduce drawdown, build diversification, or speed up your development time.

Browse RealTest Strategies