# Two Pole Butterworth Filter – Amibroker AFL code

Two Pole Butterworth Filter is one of the common topics found in Digital Signal Processing(DSP). Two Pole Butterworth Filter is used to design a Low Pass Filter with bettter smoothing response. And it is also discussed in Cybernetic Analysis for Stocks and Futures in Chapter 13 – SuperSmoother P.No 191

Here is the Easy Language code from TradeStation. And I had attempted to code the same in Amibroker AFL code.

``````{2 Pole Butterworth Filter – //// From ‘Cybernetic Analysis for Stocks and Futures’ by John Ehlers //// code compiled by dn
} // plot on a subgraph separate from the price region.

Input: Price((H+L)/2), Period(15);
Var: a1(0), b1(0), coef1(0), coef2(0), coef3(0), Butter(0);

a1=expvalue(-1.414*3.14159/Period);
b1=2*a1*Cosine(1.414*180/Period);
coef2=b1;
coef3=-a1*a1;
coef1=(1-b1+a1*a1)/4;
Butter = coef1*(Price+2*Price[1]+Price[2])+ coef2*Butter[1] + coef3*Butter[2];
If CurrentBar<3 then Butter=Price; Plot1(Butter,”Butter”);``````

Nifty 15min charts – Two Pole Butterworth Filter

In the above charts Two Pole Butterworth Filter is applied and it shows how smooth the butterworth filter is. To check how smooth the filter is you can try and compare with other traditional moving averages like EMA, MA, WMA.

AFL Formula for Plotting Two Pole Butterworth Filter

``````
/* Done      by    Rajandran R */
/* Author of www.marketcalls.in  */

_SECTION_BEGIN("Two Pole Butterworth Filter");

SetBarsRequired(100000,0);
GraphXSpace = 15;
SetChartOptions(0,chartShowArrows|chartShowDates);

SetBarFillColor(IIf(C>O,ParamColor("Candle UP Color", colorGreen),IIf(C<=O,ParamColor("Candle Down Color", colorRed),colorLightGrey)));
Plot(C,"\nPrice",IIf(C>O,ParamColor("Wick UP Color", colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

CutoffPeriod=15;

Shift=0;

coef1=coef2=coef3=coef4=0;

tempReal= atan(1.0);

pi = atan(1.0)* 4;

a1 = exp(-sqrt(2) * pi / CutoffPeriod);

b1 = 2 * a1 * cos(deg2Rad * sqrt(2) * 180 / CutoffPeriod);

coef2 = b1;

coef3 = -a1 * a1;

coef1 = (1 - b1 + a1 * a1) / 4;

mean=C;

TPBFilter[0]=Null;

up[0]=Null;

down[0]=Null;

for(i = 1; i <BarCount; i++){

if(i>3){

TBFilter[i]=coef1 *(mean[i]+2.0*mean[i-1] +mean[i-2])+coef2*TBFilter[i-1]+coef3*TBFilter[i-2];

}else{

TBFilter[i]=mean[i];

}

if(TBFilter[i]>TBFilter[i-1]){

up[i]=TBFilter[i];

up[i-1]=TBFilter[i-1];

}else{

down[i]=TBFilter[i];

down[i-1]=TBFilter[i-1];

}

}

TBFilter1[BarCount-1]=Null;

for(i=0;i<BarCount-1;i++)

{

TBFilter1[i]=TBFilter[i];

}

Plot(TBFilter1,"TBFilter",IIf(down==0,colorGreen,colorRed),styleThick);

Sell= TBFilter1<Ref(TBFilteR1,-1);

Short=Sell;

SellPrice=ValueWhen(Sell,C);
ShortPrice=ValueWhen(Short,C);
CoverPrice=ValueWhen(Cover,C);

Title = EncodeColor(colorWhite)+ "Two Pole Butterworth Filter AFL code from www.marketcalls.in" + " - " +  Name() + " - " + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorWhite) +
"  - " + Date() +" - "+"\n" +EncodeColor(colorRed) +"Op-"+O+"  "+"Hi-"+H+"  "+"Lo-"+L+"  "+
"Cl-"+C+"  "+ "Vol= "+ WriteVal(V)+"\n"+
EncodeColor(colorLime)+
WriteIf (Buy , " GO LONG / Reverse Signal at "+C+"  ","")+
WriteIf (Sell , " EXIT LONG / Reverse Signal at "+C+"  ","")+"\n"+EncodeColor(colorYellow)+

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

_SECTION_END();``````

Disclaimer : It is just a indicator and not a trading system the Buy and Sell Signals are added for the purpose of identifying turn in the filter.

Here is the video preview of upcoming Super Trend AFL code in Marketcalls

## Mini Certification Course on Algorithmic Trading

Welcome to the Mini Certification on Algorithmic Trading using Amibroker and OpenAlgo! This comprehensive 4-part series is designed to equip you with the knowledge...

## Pivot Reversal Strategy – Tradingview Pinescript to Amibroker AFL…

Pivot Reversal Strategy is a popular in-built strategy in Tradingview Platform. Here I had attempted to convert the pinescript v5 to Amibroker AFL Code....

## 23 Replies to “Two Pole Butterworth Filter – Amibroker AFL code”

1. Udit says:

What is the procedure of adding AFL code in Amibroker ? Or Where can I found its procedure ?

2. sidharth says:

Hi
can u please share the corresponding mql4 for gcimt4 for the same indicator……………

3. chirag says:

Error Variable not initialization

1. @chirag : Which Amibroker version you are using?

4. chirag says:

5.20

5. chirag says:

6. chirag says:

Hello sir i m use 5.20 Amibrocker … error 29 variable

7. Ramakrishnan says:

Sir,
I am using Amibroker 5.1 Ver. I have Downloaded your Two Pole-Butterworth-Filter-Amibroker-Afl-Code, but it is showing error 29. Pls rectify and give it at your earliest will make many of them with a Great Joy. Also God Bless YOU for your help without expecting anything from the readers. Keep it up. I pray to God “May All Your Dreams Come True In Reality”. Thank You.

8. I like the 2 pole Butterworth filter. You combine it with price line is fast moving averages to generate good buy and sell signals. Or you can apply swing theory on the waves of the smoothed price.

9. I looked up the theory on the Butterworth filter and then compared this with MA/EMA etc. This can be effectively used by swing traders as the low and high swing noise is filtered out effectively. Traders, should develop and test their strategy.

10. joy singh says:

Rajandran
sir !

nice afl ,thanks lot for sharing! sir i have a one mql4 cod , how can change for AFL, could u tell me please!! thanks a lot!!

1. @joy singh

you need programming knowledge in both mql4 and AFL codes to do that.

11. joy singh says:

Rajandran

sir !

i don’t have this ,could u please convert me if u kind sir! thanks a lot!!!!!!!

12. joy singh says:

Rajandran
sir !
please if u kindness for , please sir! thanks a lot! here is file,

13. Hardik says:

Hello Sir,

I am using Amibroker version 5.20, and facing below mentioned error at the time of implementation of indicator.
========================================
Two Pole Butterworth Filter :
}
}

Plot (TBFilter,

———-^

Error 29.

Variable ‘tbfilter’ used without having been initialized.

Use ‘Edit Formula’ to correct error”
=====================================

It is my kind request to please assist for resolution.

Regards,
Hardik

14. Hardik says:

Hello Rajandran Sir,

Thank you for your quick response.

Could you please assist, with how to proceed for resolution?
Because I have tried one by one with each file code mentioned in (v3a.odt, v3b.odt) but neither of them works. If you will elaborate process step by step for resolution, That would be a great help.

Regards,
Hardik

15. Hardik says:

Hello Rajandran Sir,

It would be a great help, if you can assist on query.
Because I have tried one by one with each file code mentioned in (v3a.odt, v3b.odt) but neither of them works. The code is still showing error for both those files, which you have shared. I am using Amibroker version 5.20.

Regards,
Hardik

16. Hardik says:

Hello Rajandran Sir,

The error is regarding:

Variable ‘tbfilter’ used without having been initialized.

if you can assist, that would be a great help.

Regards,
Hardik

17. Nitin Agarwal says:

In what timeframe this can be used and which strategy is best suitable for daily timeframr

1. You can try with Daily and Weekly(Long and Exit)

18. Edward says:

Hi Rajandran,

Many thanks for sharing this indicator with the public. May I know why is the current candle not being painted yet? its like we have to wait until tomorrow for the indi to paint.