VALYNOXSTRATEGY RESEARCH TECHNOLOGY
AlphaPrimeAlphaPrime
ACADEMY / Indicators and signals

KAMA range filter for intrabar scalping: less lag at breakouts

Measure the range regime on raw bars with a short efficiency ratio, add a breakout override that switches the filter off at once, and decide the filter at each bar's close. Then judge it with on/off tests across several periods.

By · Published · updated · Editorial policy

Contents
  1. How do you detect a range regime without the filter lagging?
  2. What is a KAMA-style adaptive candle?
  3. Which range measures react fastest when price breaks out?
  4. How do you stop the filter from lagging at a breakout?
  5. How should a backtest fill intrabar scalping trades?
  6. How do you test a regime filter as an on/off switch?
  7. Will a range filter that works in one sample hold up out-of-sample?
  8. Frequently asked questions
  9. References

How do you detect a range regime without the filter lagging?

Measure the range regime on raw bars, not on the adaptive candles, with a short efficiency ratio. Add a breakout override that switches the filter off as soon as a completed bar closes outside the range. Decide the filter at each bar's close and apply it to the next bar. Then test it on and off across several periods.

A regime filter is a rule that decides, bar by bar, whether market conditions suit a strategy and switches its entries on or off. Intrabar scalping is a trading style that enters and exits within one or a few bars, often with orders that can fill while a bar is still forming. Lag is the number of bars between the moment the market changes and the moment the filter notices. For a range scalper, lag at a breakout is the costly part. Every bar that the filter still says "range", the strategy keeps fading a move that is no longer reversing. KAMA is one of the adaptive technical indicators, built to speed up in trends and slow down in noise.

What is a KAMA-style adaptive candle?

A KAMA-style adaptive candle is a candle whose open, high, low and close are each smoothed with Kaufman's Adaptive Moving Average. As a result, the candle follows price closely in trends and almost stops moving in ranges. The KAMA indicator page explains how the average is built. The short version: KAMA sets its own speed from the efficiency ratio. It behaves like a shadow that stays close when you walk quickly in one direction and stands still when you pace back and forth.

The standard settings use a 10-bar efficiency ratio, a fastest speed equal to a 2-bar average and a slowest speed equal to a 30-bar average. The smoothing weight between those two speeds is squared. At an efficiency ratio of 1, each new bar moves the line almost half of the distance to price. At an efficiency ratio of 0, each new bar moves it by less than one percent of that distance. That is slower than even a plain 30-bar average. Rather than fixing the 10-bar ratio and the 2-bar and 30-bar limits, the strategy generation in AlphaPrime (Valynox) can take them as parameter ranges to test.

This is why the candles look clean in a range: they barely move. It is also why they are slow at a breakout. The first breakout bars arrive while the efficiency ratio is still low, so the candles crawl until the ratio rises.

Adaptive candles fall behind at a breakout: hypothetical raw prices move sideways between 100 and 102 for ten bars, then break up to 106, 112 and 118. The adaptive candle line stays flat near 101 and is still far below price on the second and third breakout bars. Adaptive candles fall behind Hypothetical prices, raw vs adaptive Range Price Lag Adaptive Adaptive candles crawl at breakouts. So: measure the regime on raw bars.
Adaptive candles flatten in a range and fall behind price during the first breakout bars, because their speed depends on an efficiency ratio that is still low.

Which range measures react fastest when price breaks out?

The efficiency ratio on raw closes usually reacts fastest, because each breakout bar adds directly to the net move it measures. ATR ratios and bar overlap react more slowly, because they average bar sizes or overlaps over many bars. The efficiency ratio is the net price movement over a lookback divided by the total distance price travelled over the same bars. Think of a walk. Pacing up and down a hallway takes many steps but ends near the start, so the ratio is near 0. Walking down a straight street ends far away, so the ratio is near 1.

ER = |close now − close n bars ago| ÷ sum of the n absolute bar-to-bar close changes

Here n is the lookback in bars, and the vertical bars mean "ignore the sign", so a fall counts as distance just like a rise. The table compares the three common range measures.

MeasureWhat it readsRange readingWhere its lag comes from
Efficiency ratio on raw closes, short lookbackDirection compared with noiseLow valueOld choppy bars stay in the denominator until they leave the lookback
ATR ratio (short ATR ÷ long ATR)Current bar size compared with normal bar sizeLow value (quiet bars)Both averages smooth bar size. It also reads size, not direction, so wide two-way bars look like a breakout too
Bar overlapShare of each bar's high-low range that sits inside the previous bar's range, averaged over k barsHigh overlapOne breakout bar changes a k-bar average by only one k-th of its own value

Hypothetical example

Closes alternate between 100 and 102 for ten bars, so the net move is 0 and the 10-bar efficiency ratio is 0. The first breakout bar closes at 106: the net move is 4, the total distance is 24, and the ratio rises to 0.17. The second closes at 112: net 12, distance 28, ratio 0.43. The third closes at 118: net 16, distance 32, ratio 0.50. With a switch-off level of 0.3, the filter notices on the close of the second breakout bar. It is off for the third bar, which means two breakout bars were exposed.

How do you stop the filter from lagging at a breakout?

Add a hard breakout override on top of the slower measure, so that one completed bar outside the range is enough to switch entries off. The efficiency ratio decides when a range has formed. The override decides when the range has ended. Build it in this order:

  1. Decide at the close. Compute the filter at the close of each completed bar and apply it only to the next bar.
  2. Define the range box. The range box is the highest high and the lowest low of the last m completed bars, not counting the current bar.
  3. Override on a close outside the box. A completed bar that closes above the box high or below the box low switches the filter off at once.
  4. Use two thresholds. Switch on below one efficiency ratio level and off above a higher one, for example on below 0.2 and off above 0.3. The gap stops the filter from flickering on and off around a single line.
  5. Re-enter slowly. Stay off until the ratio has been back under the lower threshold for a few completed bars.

In the example above, the first breakout bar closed at 106, above a box high of 102. The override therefore switches the filter off one bar earlier than the efficiency ratio alone. The trade-off is false breakouts. A bar that closes outside the box and then returns into the range also switches the filter off, so count how many good range trades the override skips.

The breakout override is one bar faster: in the hypothetical example the efficiency ratio is 0, 0.17, 0.43 and 0.50 on bars 10 to 13 against an off level of 0.3. The override switches entries off at bar 11, while the efficiency ratio switches them off only at bar 12. The override is one bar faster Efficiency ratio per bar, hypothetical ER Off at 0.3 Bar 10 0 Bar 11 0.17 Override off Bar 12 0.43 ER off Bar 13 0.50 A close outside the box acts first. So: pair the ER with an override.
In the hypothetical example, the breakout override switches entries off one bar before the efficiency ratio crosses its threshold.

How should a backtest fill intrabar scalping trades?

Use only values that existed when the order was placed. When a stop and a target both fall inside one bar, do not assume that the favourable one came first. Scalps have tight stops and targets, so both mistakes inflate results quickly. Check each of these points:

  1. Take levels from completed bars. The current adaptive candle's close is a smoothed version of a close that does not exist yet. A level drawn from it moves during the bar and makes fills look perfect. Use the previous completed candle. The guide to look-ahead bias in backtests shows a one-bar check for this leak.
  2. Take the filter state from the previous close. The current bar's high and low are not final until it closes, so a range measure that uses them also leaks.
  3. Resolve stop versus target. A bar's open, high, low and close show how far price went, not in which order. Where you have lower-timeframe data inside the bar, use it. Where you do not, assume the stop was hit first.
  4. Treat the entry bar the same way. If a limit entry fills during a bar, you also do not know whether that bar's high came before or after the fill.
  5. Make limit fills strict. Require price to trade through the limit by at least one tick, not just touch it.
  6. Include costs in every run. A scalp's average trade is small, so check it against the round-trip cost, as explained in trading costs in backtests.

Report the share of trades where stop and target sat inside the same bar. Run the test once assuming stop-first and once assuming target-first. A wide gap between the two results means the result depends on an order you cannot see. AlphaPrime includes next-bar execution, fees and slippage and trading sessions in its backtesting, so every evaluation uses the same execution assumptions.

Stop or target first: one bar with entry at 100, stop at 99 and target at 101.5, all inside the bar's range from 98.5 to 102. If the high came first the target was hit; if the low came first the stop was hit. The bar's prices cannot tell which. Stop or target: which first? Both inside one bar Target Entry Stop High first Target hit Low first Stop hit Bar prices hide the order. So: assume the stop hit first.
When both the stop and the target lie inside one bar, the bar's prices cannot tell which was hit first.

How do you test a regime filter as an on/off switch?

Run the same strategy twice, once with the filter on and once with it off, change nothing else, and compare the results period by period. Follow these steps:

  1. Fix the base strategy, its parameters, costs and fill rules.
  2. Run it with the filter off.
  3. Run it with the filter on.
  4. Split the history into several periods, for example by year, so that each period contains both ranges and breakouts.
  5. Per period, compare trade count, net result after costs, average trade and maximum drawdown.
  6. List the trades the filter removed and check whether they are mostly losing trades around breakouts.
  7. Run a random switch that turns entries off for the same share of bars. If it improves results about as much as your filter does, your filter carries no information. It only trades less.

Hypothetical example

Net results in points after costs, filter off versus filter on:

PeriodTrades offNet offTrades onNet on
1400−60260+40
2380+30250+55
3420−120270−20
4390+10240−15

The filter helps in three periods and hurts in one. It removes about a third of the trades. The next question is whether the removed trades cluster around breakouts and whether a random switch would do the same.

Will a range filter that works in one sample hold up out-of-sample?

Often it will not, because a filter adds parameters that can fit the particular breakouts in your sample, so check robustness before you trust it. The lookback, the two thresholds and the box length are four new choices. Each extra choice is another chance to fit noise. Trying many filter variants and keeping the best one is what researchers call data snooping (White, 2000). Bailey and co-authors (2014) showed that, after enough trials, a strong in-sample result is easy to find even when no variant has an edge. Run these checks:

  • Neighbourhood. Nearby thresholds, for example 0.25 and 0.35 instead of 0.3, should give similar results. A single sharp peak points to a fit.
  • Walk-forward. Choose the filter settings on in-sample data only and judge them on the following period, as described in walk-forward analysis.
  • Other data. Run the same filter on another market or bar length. Range behaviour that is real tends to appear in more than one place.
  • Variant count. Write down how many filter versions you tried. A result chosen from fifty versions needs a higher bar than a result from three.

AlphaPrime's strategy validation includes walk-forward analysis, so a filter can be judged on periods its settings did not see.

What this means if you use AlphaPrime: treat a regime filter like any other entry condition. Give it a small parameter range, keep fees, slippage and next-bar execution in every evaluation, and judge it by walk-forward results rather than by its best backtest.

Frequently asked questions

What efficiency ratio value means the market is in a range?

No single value fits every market and bar length. Plot the efficiency ratio over your own history and look at where it sits during the ranges you can see on the chart. Then pick a threshold where neighbouring values give similar test results, not the one value with the best backtest.

Should a range filter use KAMA candles or raw bars?

Use raw bars for the filter. KAMA-style candles are slowest exactly when a range ends, so a filter built on them adds the candles' lag to its own. The adaptive candles remain useful for the trading signal itself.

Can a KAMA range filter detect a breakout early?

KAMA reacts after the efficiency ratio rises, which takes a bar or more after a breakout starts. A hard override, such as switching off when a completed bar closes outside the recent high-low box, reacts on the first breakout bar. Its cost is that false breakouts switch it off too.

How do I backtest a scalp when stop and target are inside the same bar?

Bar prices do not show whether the high or the low came first. Use lower-timeframe data inside the bar where you have it, and otherwise assume the stop was hit first. Run the test both ways: a large gap between the stop-first and target-first results shows how much the result depends on the unknown order.

Does a regime filter reduce overfitting?

Not by itself. A filter adds parameters, which gives a search more ways to fit the past. It helps only if it improves results in most periods, beats a random on/off switch and survives walk-forward testing.

References

  • Bailey, D. H., Borwein, J., López de Prado, M., & Zhu, Q. J. (2014). Pseudo-Mathematics and Financial Charlatanism: The Effects of Backtest Overfitting on Out-of-Sample Performance. Notices of the American Mathematical Society, 61(5), 458–471.
  • White, H. (2000). A Reality Check for Data Snooping. Econometrica, 68(5), 1097–1126.