The Range Filter Amibroker AFL below is a trend following strategy that ignores minor price swings by only moving its filter line when price travels further than a smoothed average range. It is my Amibroker port of DonovanWall’s Range Filter Pine Script, backtested on Nifty Futures 15-minute data from 2011 to 2022, and the full AFL code with the equity curve and backtest report is included below.
Range Filter is a non-ATR-based trend following strategy inspired by Tradingview Pinescript coder – DonovanWall. According to him, Range Filter is an experimental study designed to filter out minor price action for a clearer view of trends. Here is the Amibroker Trend Following Strategy code based on the Range Filter.

- Range Filter uses an EMA of absolute bar-to-bar price changes, not ATR, to decide when a move is big enough to matter.
- Default settings: sampling period 15, range multiplier 2.5, source Close, traded on 15-minute Nifty Futures.
- Signals are taken on the next bar open, and the system is always in the market (long or short).
- Backtest 2011 to 2022 with 1 lot: 2,559 trades, 38.84% winners, profit factor 1.27, max system drawdown 15.03%.
- The AFL draws H-Band and L-Band target lines, a signal dashboard and percentage targets from the last signal.
How is the Range Filter calculated?
- A smooth average price range is calculated for the basis of the filter and multiplied by a specified amount.
- The filter is calculated by gating price movements that do not exceed the specified range.
- The target ranges are plotted to display the prices that will trigger filter movement.
In code terms, the smooth range is EMA(abs(src - Ref(src,-1)), per), smoothed again with an EMA of length 2 x per - 1, then multiplied by the range multiplier. The filter only moves up when price minus this range is above the previous filter value, and only moves down when price plus the range is below it. Everything in between is treated as noise, so the line stays flat.
| Parameter | Default | Effect of increasing it |
|---|---|---|
| Source | Close | Use Close for signals on completed bars |
| Sampling Period | 15 (range 5 to 200) | Smoother range, slower filter, fewer trades |
| Range Multiplier | 2.5 (range 1 to 5) | Wider bands, ignores larger swings, fewer whipsaws but later entries |
Range filter Amibroker AFL trading rules
- Buy: price is above the filter and the filter has been rising (upward count above zero).
- Short: price is below the filter and the filter has been falling (downward count above zero).
- Exit: a long is closed when a short signal comes and vice versa, so the strategy is always in a position.
- Execution: signals are delayed by one bar with
Ref(...,-1)and filled at the open, which avoids look-ahead bias.
Backtest settings and results
Strategy Timeframe : 15mins
Trading Symbol: Nifty Futures
Trading Commissions Used : 0.02% per leg
Historical Data Backtesting Period: 2011 – 2022
Position Sizing : 1 Lot (Fixed Lot)
Equity curve

Drawdown curve

Backtesting report

The key numbers from the report, on an initial capital of ₹2,00,000:
| Metric | All trades | Long | Short |
|---|---|---|---|
| Net profit | ₹10,33,560.78 (516.78%) | ₹7,76,879.10 | ₹2,56,681.68 |
| Annual return | 17.28% | 14.91% | 7.50% |
| Number of trades | 2,559 | 1,279 | 1,280 |
| Winners | 38.84% | ||
| Profit factor | 1.27 | 1.40 | 1.13 |
| Payoff ratio | 1.99 | 1.97 | 2.00 |
| Max system % drawdown | 15.03% | 17.01% | 30.18% |
| Transaction costs | ₹4,75,864.22 |
Two things stand out. The win rate is below 40%, which is normal for trend following: the system makes its money from winners that are about twice the size of losers. And the long side carries most of the profit, while shorts are weaker with a 30% drawdown, which fits a market that trended up for most of the test period. Transaction costs ate almost a third of gross profit, so check your own brokerage and slippage before trading it. For help reading these metrics, see understanding Amibroker backtesting metrics.
Absolute profit table (in thousands)

Range Filter – Amibroker AFL code
//Coded by Rajandran R - www.marketcalls.in / www.openalgo.in
//Code Inspired from Tradingview Pinescript
//Original Coded by : DonovanWall
//Tradingview Source Code : https://in.tradingview.com/script/lut7sBgG-Range-Filter-DW/
//Coded Date : 23rd - Aug 2022
_SECTION_BEGIN("Range Filter - Trading Strategy");
SetPositionSize(1*RoundLotSize,spsShares);
src = ParamField("Source",3);
// Sampling Period
per = param("Sampling Period",15,5,200,5);
// Range Multiplier
mult = param("Range Multiplier",2.5,1,5,0.5);
upward = 0;
dnward = 0;
// Smooth Average Range
function smoothrange(x, t, m)
{
wper = t * 2 - 1;
avrng = ema(abs(x - Ref(x,-1)), t);
smoothrng = ema(avrng, wper) * m;
return smoothrng;
}
smrng = smoothrange(src, per, mult);
// Range Filter
function rngfilter(x, r)
{
rngfilt = x;
//rngfilt := x > nz(rngfilt[1]) ? x - r < nz(rngfilt[1]) ? nz(rngfilt[1]) : x - r : x + r > nz(rngfilt[1]) ? nz(rngfilt[1]) : x + r
for(i=1;i<BarCount;i++)
{
if(x[i] > rngfilt[i-1])
{
if((x[i] - r[i]) < rngfilt[i-1])
{
rngfilt[i] = rngfilt[i-1];
}
else
{
rngfilt[i] = x[i]-r[i];
}
}
else
{
if((x[i] + r[i]) > rngfilt[i-1])
{
rngfilt[i] = rngfilt[i-1];
}
else
{
rngfilt[i] = x[i] + r[i];
}
}
}//end of for loop
return rngfilt;
}
filt = rngfilter(src, smrng);
upward = 0;
downward = 0;
// Filter Direction
for(i=1;i<BarCount;i++)
{
if(filt[i] > filt[i-1])
{
upward[i] = upward[i-1] + 1;
}
else{
if(filt[i] < filt[i-1])
{
upward[i] = 0;
}
else
{
upward[i] = upward[i-1];
}
}
}//end of for loop
for(i=1;i<BarCount;i++)
{
if(filt[i] < filt[i-1])
{
downward[i] = downward[i-1] + 1;
}
else{
if(filt[i] < filt[i-1])
{
dnward[i] = 0;
}
else
{
dnward[i] = dnward[i-1];
}
}//end of for loop
}//end of range filter function
// Target Bands
hband = filt + smrng;
lband = filt - smrng;
Plot(hband,"H-Band",colorBlue,styleLine);
Plot(lband,"L-Band",colorRed,styleLine);
longCond = (src > filt and src > Ref(src,-1) and upward > 0) or (src > filt and src < Ref(src,-1) and upward > 0);
shortCond = (src < filt and src < Ref(src,-1) and downward > 0) or (src < filt and src > Ref(src,-1) and downward > 0);
Buy = ExRem(longCond,shortCond);
Sell = ExRem(shortCond,longCond);
Short = Sell;
Cover = Buy;
Buy = Ref(Buy,-1);
Sell = Ref(Sell,-1);
Short = Ref(Short,-1);
Cover = Ref(Cover,-1);
BuyPrice = ValueWhen(Buy,Open);
SellPrice = ValueWhen(Sell,Open);
ShortPrice = ValueWhen(Short,Open);
CoverPrice = ValueWhen(Cover,Open);
buycontinue = Flip(Buy,Sell);
shortcontinue = Flip(Short,Cover);
barcolor = IIf(buycontinue,colorGreen,colorRed);
for( i = 0; i < BarCount; i++ )
{
if( Buy[i] ) PlotText("Buy",i,L[i],colorWhite,colorLime,-70);
if( Short[i] ) PlotText("Short",i,H[i],colorWhite,colorRed,70);
//if( Sell[i] ) PlotText( "Selln@" + C[ i ], i, H[ i ]+dist[i], colorRed, colorYellow );
}
/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
sig=0;
bars=0;
tar1=tar2=tar3=null;
for(i=BarCount-1;i>1;i--)
{
if(Buy[i] == 1)
{
entry = open[i];
sig = 1; //flag contains 1 when buy is valid
tar1 = entry + (entry * .0050);
tar2 = entry + (entry * .0092);
tar3 = entry + (entry * .0179);
bars = i;
i = 0;
}
if(Short[i] == 1)
{
sig = -1; //flag contains -1 wjhen short is valid
entry = open[i];
tar1 = entry - (entry * .0050);
tar2 = entry - (entry * .0112);
tar3 = entry - (entry * .0212);
bars = i; // i - holds the value of bar index, computing the barindex value when the signal happened
i = 0;
}
}//for loop
Offset = 20;
Clr = IIf(sig == 1, colorLime, colorRed);
//plot target lines
Plot(LineArray(bars-Offset, tar1, BarCount-1, tar1,1), "", Clr, styleLine|styleDots, Null, Null, Offset);
Plot(LineArray(bars-Offset, tar2, BarCount-1, tar2,1), "", Clr, styleLine|styleDots, Null, Null, Offset);
Plot(LineArray(bars-Offset, tar3, BarCount-1, tar3,1), "", Clr, styleLine|styleDots, Null, Null, Offset);
_SECTION_END();
_SECTION_BEGIN("Signal Dashboard");
messageboard = ParamToggle("Message Board","Show|Hide",1);
upcolor = ParamColor("Up Color",colorBlue);
dncolor = ParamColor("Down Color",colorRed);
if (messageboard == 1 )
{
GfxSelectFont( "Tahoma", 13, 100 );
GfxSetBkMode( 1 );
GfxSetTextColor( colorWhite );
//Dashboard color changes dynamically according to the signals continuation
color = IIf(buycontinue, upcolor, IIf(shortcontinue, dncolor, Null));
GfxSelectSolidBrush(SelectedValue(color));
pxHeight = Status( "pxchartheight" ) ;
xx = Status( "pxchartwidth");
Left = 1100;
width = 310;
x = 5;
x2 = 360;
y = pxHeight;
sigstatus = WriteIf(buycontinue,"Buy Signal @ ","Sell Signal @");
GfxSelectPen( colorGreen, 1); // broader color
GfxRoundRect( x, y - 98, x2, y , 7, 7 ) ;
GfxTextOut( ( "Marketcalls - Range Filter"),13,y-90);
GfxTextOut( (" "),27,y-100);
if(SelectedValue(Buycontinue))
{
GfxTextOut( ("Last " + sigstatus + " Signal came " + BarsSince(Buy) * Interval()/60 + " mins ago"), 13, y-70) ; // The text format location
GfxTextOut( ("" + sigstatus + " : " + ValueWhen(Buy,BuyPrice)), 13, y-50);
GfxTextOut( ("Profit/Loss : " + NumToStr(Close-ValueWhen(Buy,BuyPrice),1.2)), 13, y-30);
}
if(SelectedValue(Shortcontinue))
{
GfxTextOut( ("Last " + sigstatus + " Signal came " + BarsSince(short) * Interval()/60 + " mins ago"), 13, y-70) ; // The text format location
GfxTextOut( ("" + sigstatus + " : " + ValueWhen(Short,ShortPrice)), 13, y-50);
GfxTextOut( ("Current P/L : " + NumToStr(ValueWhen(Short,ShortPrice)-Close,1.2)), 13, y-30);
}
//GfxTextOut( ("Trailing SL : " + Ref(TrendSL,-1) + " (" + WriteVal(IIf(sig == -1,entry-sl,sl-entry), 2.2) + ")"), 13, y-40);
}
_SECTION_END();
_SECTION_BEGIN("Top Dashboard");
X0 = 10;
Y0 = 20;
procedure DrawData (Text, x1, y1, x2, y2, colorFrom, colorTo)
{
GfxSetOverlayMode(0);
GfxSelectFont("Verdana", 8.5, 700);
GfxSetBkMode(1);
GfxGradientRect(x1, y1, x2, y2, colorFrom, colorTo);
GfxDrawText(Text, x1, y1, x2, y2, 32|1|4|16);
}
DrawData (Name(), X0, Y0, X0+150, Y0+20, colorGrey40, colorblack);
DrawData (Date(), X0+155, Y0, X0+320, Y0+20, colorGrey40, colorblack);
DrawData ("Open : " + Open, X0+325, Y0, X0+450, Y0+20, colorGrey40, colorblack);
DrawData ("Close : " + Close, X0+455, Y0, X0+580, Y0+20, colorGrey40, colorblack);
DrawData ("High : " + High, X0+585, Y0, X0+710, Y0+20, colorGrey40, colorblack);
DrawData ("Low : " + Low, X0+715, Y0, X0+840, Y0+20, colorGrey40, colorblack);
_SECTION_END();
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", barcolor, styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();
Code notes and updated version (2026)
While reviewing the AFL I found that the second direction loop, which counts how many bars the filter has been falling, writes to a variable called dnward instead of downward in its reset branches. As a result downward is only above zero on bars where the filter actually fell, instead of staying positive until the filter turns up as in the original Pine Script. The backtest above was produced with the original code. If you want the short-side logic to match DonovanWall’s version exactly, replace the second loop with this:
// Filter Direction: downward count (corrected to match the Pine Script logic)
downward = 0;
for(i=1;i<BarCount;i++)
{
if(filt[i] < filt[i-1])
downward[i] = downward[i-1] + 1;
else if(filt[i] > filt[i-1])
downward[i] = 0;
else
downward[i] = downward[i-1];
}
Re-run the backtest after making the change, because the short entries can shift. Two smaller points: the sell arrow uses an offset of -45 where +45 would place it above the bar, and position size uses RoundLotSize from your symbol information. The Nifty lot size has changed several times since 2022, most recently from 75 to 65 for contracts expiring after 30 December 2025, so update the lot size in Symbol Information before backtesting recent data.
How to use the Range Filter Amibroker AFL
- Copy the code into a new formula in the Amibroker Formula Editor and save it, for example as RangeFilter.afl.
- Apply it to a 15-minute Nifty Futures chart to see the H-Band, L-Band, buy and short labels and the dashboard.
- Set the lot size in Symbol Information so
RoundLotSizepicks it up. - Open the Analysis window, select the formula, set periodicity to 15 minutes and commissions to match your broker, then run Backtest.
- Use Optimize on Sampling Period and Range Multiplier only after you have a baseline, and validate any change with walk forward testing.
Limitations of the Range Filter strategy
- Like every always-in-the-market trend follower, it gives back profits in sideways, choppy sessions.
- There is no stop loss beyond the reverse signal, so a single trade can draw down heavily on a gap.
- The percentage target lines are visual only; the backtest does not book profits at those levels.
- Results depend on the 15-minute timeframe and Nifty Futures; test separately before using it on stocks or options.
If you like converting TradingView indicators into AFL, see the Reversal Finder Pine Script to AFL conversion and the Pivot Reversal strategy conversion.
Frequently Asked Questions
What is the Range Filter indicator?
Range Filter is a trend following indicator by TradingView author DonovanWall. It moves only when price changes by more than a smoothed average range, so it filters out minor price action and shows the underlying trend more clearly.
What are the best settings for the Range Filter?
The defaults in this AFL are a sampling period of 15 and a range multiplier of 2.5 on 15-minute Nifty Futures. Higher values reduce whipsaws but delay entries; optimise only with out-of-sample testing.
Is the Range Filter better than Supertrend?
They solve the same problem differently. Supertrend uses ATR bands, while Range Filter uses an EMA of absolute price changes. Range Filter tends to stay flat during small pullbacks, but neither is better on every instrument, so backtest both on your market.
Learn by watching: free videos every week
Market Profile, order flow, Python, AI and OpenAlgo tutorials on YouTube.


thank you sir but shows some error .
if( Buy[i] ) PlotText(“Buy”,i,L[i],colorWhite,colorLime,-70);
error16 toomany arguements.
-S.Ghaneshakumar
Consider using Amibroker 6.0 or higher versions.
Sir, maybe using sell and cover signals as exit points for entries might work wonders. For ex if we are on SHORT side and it touches the Down color bar it would book profit in exit and wait. If a breakout happens to downside by breaking the down color bar area let it reenter with again the next downside color bar being the target. Same logc with upside as well
how to make it atr based seen as a trading view same in amibroker?
please guide