Monday, 20 August 2012

BUTTERWORTH NON REPAINTING


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

_SECTION_BEGIN("Butterworth Trend Trading System");
SetBarsRequired(100000,0);
GraphXSpace = 15;
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", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

//PositionSize=100000;
////////////////////////////////////////////////////////////////////////////

function Butterworth( mean, per )
{
CutoffPeriod=per;
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;

TBFilter[0]=C[0];
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];

}


return TBFilter1;
}

////////////////////////////////////////////////////////////////////////



PositionSize = 100000;

FAST =Optimize("FAST",29,2,50,1);
//ow =Optimize("SLOW",42,2,50,1);
RSIValue =17;

FBUTTER=Butterworth(C,FAST);
SBUTTER=Butterworth(FBUTTER,FAST);
RSV=RSI(17);

diff=FBUTTER-SBUTTER;

//Buy = diff>0 AND Ref(diff,-1)<0 AND RSI(17)>50 AND Ref(RSI(17),-1)<50;
//Sell =diff<0 AND Ref(diff,-1)>0 AND RSI(17)<50 AND Ref(RSI(17),-1)>50;

Cond=0;
ind =0;
for ( i=0; i<BarCount; i++)
{
if(diff[i]>0 AND RSV[i]>50)
{
Cond=1;
}

if(diff[i]<0 AND RSV[i]<50)
{
Cond=2;
}
Buy[i]=Cond==1 AND ind==2;
Sell[i]=Cond==2 AND ind==1;
ind = Cond;

}//end for

Short=Sell;
Cover=Buy;
Plot(FBUTTER,"FBUTTER",colorGreen);
Plot(SBUTTER,"SBUTTER",colorRed);


Title = EncodeColor(colorWhite)+ "5-13 EMA Channel Trading System" + " - " +  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(colorWhite)+
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(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);



_SECTION_END();

No comments:

Post a Comment