kvak wrote: Mon Jun 15, 2026 5:32 am
Hello my friend.
Here is updated version with new average pack and new prices pack.
Good evening again, Kvak; I’ve made a few more changes to the file for inclusion in eAverages, if possible.
However, I wasn't able to standardize everything for hardcoding within your kit, but here is an explanation of the functions of the new averages, by Claude:
Robust Averages Pack for the iCustomMa() averages kit
An add-on .mqh that plugs 17 robust estimators into any indicator built on the
standard iCustomMa(mode, price, length, r, bars, instanceNo) averages kit.
It does not replace anything. Existing averages keep working exactly as before,
and saved presets keep working too, because the new method codes start at 900
instead of continuing the existing numbering.
Tested target: STARC Bands mtf BT. It should drop into any other indicator
using the same kit, since it only touches the dispatcher's default: branch.
Why
The classic averages in the kit all break the same way: a single wick drags the
average with it. The mean has a breakdown point of 0% — one bad bar is enough.
The estimators here have breakdown points ranging from the trim fraction you set
up to 50%, so a spike moves the line very little or not at all.
That matters twice in a STARC-type indicator:
the midline — a robust centre stops chasing wicks, so the channel stays
where the body of the price action actually is
the band width — mean True Range is itself a mean, so one violent bar
inflates the channel for the whole averaging period afterwards
What is in the pack
Location estimators (codes 900–912). These work in both slots — the main
average and the ATR average.
CodeNameNotes900Median50% breakdown, the baseline robust average901Trimmed mean (symmetric)drops the top and bottom TrimFraction902Winsorized mean (symmetric)clamps instead of dropping903Trimmed mean (upper tail)for the ATR slot — TR spikes are one-sided904Winsorized mean (upper tail)same idea, clamped905Huber M-locationsoft downweighting, high efficiency906Tukey biweight locationfully redescending, ignores far outliers907Hampel 3-part locationthree-stage redescending psi908Adaptive trimmed meanpicks its own trim level per window909Tau locationone-step weighting off an S-scale910Hodges-Lehmannmedian of pairwise averages911Midhinge(Q1+Q3)/2912Trimean(Q1+2·Q2+Q3)/4
Residual scales (codes 920–923). These go in the ATR slot only and need
one extra hook (step 5 below). Instead of averaging True Range, they measure how
far price actually sits from the midline, then estimate the spread of that
distance robustly.
CodeNameNotes920MAD of residualsmedian absolute deviation, ×1.4826921IQR of residualsinterquartile range, ÷1.349922Qn of residualsRousseeuw–Croux, 50% breakdown, high efficiency923Sn of residualsRousseeuw–Croux, 50% breakdown, no location step
All four are normalized so they estimate sigma under normality.
Installation
1. Drop the file in place
Copy RobustAverages.mqh into MQL4/Include/.
2. Include it
Put this line just above the #define _maInstances block near the bottom of the
indicator, so the pack's own inputs land at the end of the properties window:
mql4#include <RobustAverages.mqh>
Including it at the very top also compiles — the pack's inputs will just appear
at the top of the list instead.
3. Add the methods to the dropdown
At the end of enMaTypes, add a comma after the last existing member and paste
the block. Do not renumber anything above it.
mql4enum enMaTypes
{
ma_sma, // Simple moving average
ma_ema, // Exponential moving average
// ... everything already there, unchanged ...
ma_dema, // Double exponential moving average - DEMA
ma_rmedian = 900, // Robust: Median
ma_rtrimmed = 901, // Robust: Trimmed mean (symmetric)
ma_rwinsor = 902, // Robust: Winsorized mean (symmetric)
ma_rtrimup = 903, // Robust: Trimmed mean (upper tail)
ma_rwinsorup = 904, // Robust: Winsorized mean (upper tail)
ma_rhuber = 905, // Robust: Huber M-location
ma_rtukey = 906, // Robust: Tukey biweight location
ma_rhampel = 907, // Robust: Hampel 3-part location
ma_radtrim = 908, // Robust: Adaptive trimmed mean
ma_rtau = 909, // Robust: Tau location
ma_rhodges = 910, // Robust: Hodges-Lehmann
ma_rmidhinge = 911, // Robust: Midhinge
ma_rtrimean = 912, // Robust: Trimean
ma_rmadres = 920, // Robust scale: MAD of residuals (atr slot only)
ma_riqrres = 921, // Robust scale: IQR of residuals (atr slot only)
ma_rqnres = 922, // Robust scale: Qn of residuals (atr slot only)
ma_rsnres = 923 // Robust scale: Sn of residuals (atr slot only)
};
4. Route the dispatcher
One line in iCustomMa(). Change:
mql4 default : return(price);
to:
mql4 default : return(iRobustMa(mode,price,length,r,bars,instanceNo));
iRobustMa returns the raw price for any unknown code, so the old fallback
behaviour is preserved exactly.
At this point the 13 location estimators already work in both slots. If you
do not want the residual scales, you can stop here.
5. Hook the centre line (residual scales only)
The ATR slot only ever receives the True Range value, so the residual scales need
to be told where the midline is. In the main loop, compute the middle average
first, feed it to the pack, then compute the ATR.
Find this:
mql4 double atr = iCustomMa(AtrMaType, tr, AtrPeriod, i,rates_total,0);
ma
= iCustomMa(StarcMaType,price,StarcPeriod,i,rates_total,1);
and make it:
mql4 ma = iCustomMa(StarcMaType,price,StarcPeriod,i,rates_total,1);
iRobustSetCenter(price,ma,i,rates_total);
double atr = iCustomMa(AtrMaType, tr, AtrPeriod, i,rates_total,0);
Swapping the two calls is safe — they use different instance slots and different
work buffers, so neither depends on the other.
If you select a residual scale without adding this hook, the bands collapse onto
the midline and a message explaining why is printed once to the Experts log. That
is deliberate — a visible failure beats a silently wrong channel.
Inputs the pack adds
InputDefaultWhat it doesRobustTrimFraction0.10fraction cut from each tail (symmetric trim/winsor)RobustUpperTail0.05fraction cut from the upper tail onlyRobustHuberK1.345Huber tuning constantRobustTukeyC4.685Tukey biweight tuning constantRobustMaxIter25iteration cap for the M-estimatorsRobustPairwiseCap200window cap for Qn / Sn / Hodges-Lehmann
The two tuning constants are the standard 95%-efficiency values. Lower them for
more robustness and less efficiency; raise them to move closer to a plain mean.
Things worth knowing before you judge the result
Your multipliers will need retuning if you use a residual scale. The four
residual scales estimate sigma. Mean True Range is a different quantity on a
different magnitude, so a multiplier of 2.0 will not produce a visually similar
channel. Put the old and new versions on the same chart and adjust until the
containment looks comparable, rather than assuming the old numbers carry over.
Qn, Sn and Hodges-Lehmann are O(n²) per bar. With an ATR period of 100 that
is roughly 5,000 pair operations on every bar, and Sn is heavier still.
RobustPairwiseCap limits how many of the most recent values enter the pairwise
step — lower it if the first load is slow. The other 14 methods are cheap.
Upper-tail variants belong in the ATR slot. True Range spikes are one-sided
by construction, so trimming symmetrically throws away the small values for no
reason. Use symmetric trimming on the midline and upper-tail trimming on the ATR.
Everything is computed in price points. No log or percent conversion happens
inside the pack, so whatever band construction the host indicator already does
keeps working untouched.
Recalculation. The pack uses the same forward-indexed work-buffer scheme as
the rest of the kit (r = bars-r-1, arrays sized to bars), so partial
recalculation behaves the same way the existing averages do.
Suggested starting points
Calmer midline, same feel — main average Trimmed mean (symmetric),
keep the ATR average as it is
Wick-proof channel — main average Tukey biweight location,
ATR average Winsorized mean (upper tail)
Mean-reversion / counter-trend — main average Tukey biweight location,
ATR average Qn of residuals, then retune the multipliers
Cheapest robust setup — main average Median, ATR average
Trimmed mean (upper tail)
Notes
The estimators follow the standard formulations: Huber and Tukey M-location by
iteratively reweighted least squares off a MAD scale, Hampel with the usual
1.7 / 3.4 / 8.5 breakpoints, Qn with the Rousseeuw–Croux finite-sample
correction, Sn as the asymptotically normalized median of row medians.
Note: I replied about the moving averages in your PM, but it's still in the outbox. I'll replicate the answer here as well to make it easier 
Thank you very much in advance.