Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in)

Two Pole Butterworth Filter – Amibroker AFL code

2 min read

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);

SetTradeDelays(1,1,1,1);

_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);

 rad2Deg = 45/tempReal;

 deg2Rad = 1.0/ rad2Deg;

 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);



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


Buy=ExRem(Buy,Sell);

Sell=ExRem(Sell,Buy);


Short=Sell;
Cover=Buy;


BuyPrice=ValueWhen(Buy,C);
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)+
WriteIf(Sell , "Total Profit/Loss for the Last Trade Rs."+(C-BuyPrice)+"","")+
WriteIf(Buy  , "Total Profit/Loss for the Last trade Rs."+(SellPrice-C)+"","");

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(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();

Download Two Pole Butterworth Filter AFL Code for Amibroker

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
httpv://www.youtube.com/watch?v=WGSBAsGNGnM

Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in)

[Live Coding Webinar] Build Your First Trading Bridge for…

In this course, you will be learning to build your own trading bridge using Python. This 60-minute session is perfect for traders, Python enthusiasts,...
Rajandran R
1 min read

[Webinar] Understanding and Mitigating Curve Fitting in System Trading

"Understanding and Mitigating Curve Fitting in System Trading"! This dynamic session aims to equip novice to intermediate traders, quantitative analysts, and financial engineers with...
Rajandran R
1 min read

P-Signal Strategy Long Only Strategy – Amibroker AFL Code

This tutorial provides an overview of the P-Signal reversal strategy, a quantitative trading strategy that utilizes statistical parameters and error functions to generate probabilistic...
Rajandran R
2 min read

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

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

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

  3. Hello sir … which is the best Intraday trading Formula use in intraday trader… i m new in charting…

  4. 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.

  5. 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.

  6. 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.

  7. 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!!

  8. Rajandran

    sir !

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

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

  10. 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

  11. 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

  12. 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.
    Please help/assist.

    Regards,
    Hardik

  13. Hello Rajandran Sir,

    The error is regarding:

    Variable ‘tbfilter’ used without having been initialized.

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

    Awaiting your response soon.

    Regards,
    Hardik

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

  15. 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.

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More