20 February 2011

Zig Zag Indicator with Valid Entry and Exit Points

The Zig Zag indicator identifies pivot points but looks into the future - (beyond the right edge of the chart) to do this. This indicator plots a dot at the pivot point and an arrow at the bar when the pivot becomes known. The price bars are colored red for a pivot downtrend, green for a pivot uptrend, and blue at the known pivot point. Three addional horizontal lines are plotted and tied to the selected bar - the current close and +/- x% from the current close. The x% is the same % used in the Zig Zag function.

Formula:
//z_ZigZagValid
// ******** CHARTING
PercentChange = 6;
mystartbar = SelectedValue(BarIndex()); // FOR GRAPHING

mystartbardate = LastValue(ValueWhen(mystartbar == BarIndex(), DateNum(),1));

InitialValue = LastValue(ValueWhen(mystartbardate == DateNum(), C , 1 ) ) ;
Temp1 = IIf(BarIndex() >= mystartbar, InitialValue, Null) ;
Plot(Temp1, " ", colorBlack,styleLine);
Plot((1+(LastValue(PercentChange)/100))*(Temp1), " ", colorGreen, styleLine) ;
Plot((1-(LastValue(PercentChange)/100))*(Temp1), " ", colorRed, styleLine) ;

ZZ = Zig(C,LastValue(PercentChange)) ;
PivotLow = Ref(IIf(Ref(ROC(ZZ,1),-1) < 0 AND ROC(ZZ,1) > 0, 1, Null),1);
PivotHigh = Ref(IIf(Ref(ROC(ZZ,1),-1) > 0 AND ROC(ZZ,1) < 0, 1, Null),1); PlotShapes( shapeCircle*PivotLow, colorGreen,0, L, -20) ; PlotShapes( shapeCircle*PivotHigh,colorRed,0,H, 20) ; Buy_Valid = IIf(C>(1+(LastValue(PercentChange)/100))*(ValueWhen(PivotLow, C,
1))
AND ROC(ZZ,1) > 0,1,0);
Sell_Valid = IIf(C<(1-(LastValue(PercentChange)/100))*(ValueWhen(PivotHigh, C, 1)) AND ROC(ZZ,1) < 0,1,0); Buy_Valid = ExRem(Buy_Valid,Sell_Valid); Sell_Valid = ExRem(Sell_Valid,Buy_Valid); PlotShapes( shapeUpArrow*Buy_Valid, colorGreen,0, L, -20); PlotShapes( shapeDownArrow*Sell_Valid, colorRed,0,H, -20) ; BarColors = IIf(BarsSince(Buy_Valid) < BarsSince(Sell_Valid) AND BarsSince(Buy_Valid)!=0, colorGreen, IIf(BarsSince(Sell_Valid) < BarsSince(Buy_Valid) AND BarsSince(Sell_Valid)!=0, colorRed, colorBlue)); Plot(C, " ", BarColors, styleBar ) ; Plot(ZZ," ", colorLightGrey,styleLine|styleThick); Plot(ZZ," ", BarColors,styleDots|styleNoLine); Title = Name() + " " + Date() + WriteIf(PivotLow, " Up Pivot ","")+WriteIf(PivotHigh," Down Pivot ","")+ WriteIf(Buy_Valid, " Buy Point ", "") + WriteIf(Sell_Valid, " Sell Point ", "") ; listing 2 _SECTION_BEGIN("Price"); SetChartOptions(0,chartShowArrows|chartShowDates); _N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) )); Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); if( ParamToggle("Tooltip shows", "All Values|Only Prices" ) ) { ToolTip=StrFormat("Open: %g\nHigh: %g\nLow: %g\nClose: %g (%.1f%%)\nVolume: "+NumToStr( V, 1 ), O, H, L, C, SelectedValue( ROC( C, 1 ))); } _SECTION_END(); _SECTION_BEGIN("ZIG"); P = ParamField( "Price field" ); change = Param("% change",5,0.1,25,0.1); z = Zig(P, change); Plot( z, _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") ); myBuy = IIf(z > Ref(z, -1), 1, 0);
mySell = IIf(z < Ref(z, -1), 1, 0);
Buy = Cover = ExRem(myBuy, mySell);
Sell = Short = ExRem(mySell, mybuy);
Filter = Buy OR Sell;
AddColumn(Buy, "Buy", 1.0);
AddColumn(Sell, "Sell", 1.0);
_SECTION_END();

No comments:

My Blog List

Total Pageviews

Search This Blog

Followers