Code presented in the article “Indicator Rules for Swing Trading Strategy, Part 2” by Sylvain Vervoort, in the June 2013 issue of Technical Analysis of Stocks & Commodities magazine.
the file “SVEHLZZperc.zip” can also be downloaded by right-clicking here and saving the file.
// NinjaTrader script // SVEHLZZPercent is Copyright (C) 2012, Sylvain Vervoort <stocata.org>. // stocata.org/S. Vervoort reserve the right to modify this indicator with each release. // Release V1.0 October 2012. #region Using declarations using System; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Data; using NinjaTrader.Gui.Chart; #endregion // This namespace holds all indicators and is required. Do not change it. namespace NinjaTrader.Indicator { /// <summary> /// High-Low ZigZag indicator. /// </summary> [Description("High-Low ZigZag indicator")] public class SVEHLZZPercent : Indicator { // --------------------------------------------------------------------------------- #region Variables private double ZZPercent = 5; private int ATRPeriod = 5; private double ATRFactor = 1.5; private Color zigZagColor = Color.DodgerBlue; private int linewidth = 1; private int trend; // confirmed trend of current zigzag, 1=up, -1=down private int lhb; // last high bar count of last swing high private int uplineid; // last used up line (name) id private int llb; // last low bar count of last swing low private int downlineid; // last used down line name private double hh; // New Higher high private double ll; // New lower low private double HLPivot; // The high-low pivot level switch #endregion // ---------------------------------------------------------------------------------------- /// <summary> /// Method used to configure the indicator and only called once before any data is loaded. /// </summary> protected override void Initialize() { CalculateOnBarClose = true; // Calculate on completed bar only AutoScale = false; // Auto scaling done by price data Overlay = true; // Overlay on price data } // -------------------------------------------------------------------------------- /// <summary> /// Method called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { if (CurrentBar < 1) // minimum 2 bars required { ll = Low[0]; hh = High[0]; llb = lhb = 0; uplineid = downlineid = CurrentBar; trend = 1; // just assume trend is up at start return; } // ATR factor = 0, only use percent setting if (ATRFactor == 0) HLPivot = (ZZPercent*0.01); // Zigzag percent = 0, only use ATR else if (ZZPercent == 0) HLPivot = ATR(ATRPeriod)[0]/Close[0]*ATRFactor; // Use both influences else HLPivot = ZZPercent*0.01 + ATR(ATRPeriod)[0]/Close[0]*ATRFactor; // -------------------------------------------------------------------------------- // look for swing points and draw lines // trend is up, look for new swing low if (trend > 0) { if (High[0] >= hh) { // new higher high detected hh = High[0]; lhb = CurrentBar; // RemoveDrawObject("uplineid.ToString"); // Not required, handled by drawing line with the same name id DrawLine(uplineid.ToString(), AutoScale, CurrentBar-llb, ll, CurrentBar-lhb, hh, zigZagColor, DashStyle.Solid, linewidth); } else if (Low[0] < hh - hh*HLPivot) { // found a swing low ll = Low[0]; llb = CurrentBar; downlineid = CurrentBar; trend = -1; DrawLine(downlineid.ToString(), AutoScale, CurrentBar-lhb, hh, CurrentBar-llb, ll, zigZagColor, DashStyle.Solid, linewidth); } } // else trend is down, look for new swing high else { if (Low[0] <= ll) { // new lower low detected ll = Low[0]; llb = CurrentBar; // RemoveDrawObject("downlineid.ToString"); // Not required, handled by drawing line with the same name id DrawLine(downlineid.ToString(), AutoScale, CurrentBar-lhb, hh, CurrentBar-llb, ll, zigZagColor, DashStyle.Solid, linewidth); } else if (High[0] > ll + ll*HLPivot) { // found a swing high hh = High[0]; lhb = CurrentBar; uplineid = CurrentBar; trend = 1; DrawLine(uplineid.ToString(), AutoScale, CurrentBar-llb, ll, CurrentBar-lhb, hh, zigZagColor, DashStyle.Solid, linewidth); } } } // ----------------------------------------------------------------------------- #region Properties [Description("ZigZag percent influence DEF = 5%, 0 = NO influence")] [Category("Parameters")] public double __ZigZag__Percentage { get { return ZZPercent; } set { ZZPercent = Math.Max(0, value); } } [Description("ATR Look back Period DEFAULT = 5")] [Category("Parameters")] public int _ATR__Period { get { return ATRPeriod; } set { ATRPeriod = Math.Max(1, value); } } [Description("ZigZag ATR factor DEF = 1.5, 0 means NO influence")] [Category("Parameters")] public double _ATR_Factor { get { return ATRFactor; } set { ATRFactor = Math.Max(0, value); } } [Description("Color of the individual zigzag lines DEFAULT = DodgerBlue")] [Category("Parameters")] public Color ZigZag_Color { get { return zigZagColor; } set { zigZagColor = value; } } [Browsable(false)] public string PainttrendcolorSerialize { get { return NinjaTrader.Gui.Design.SerializableColor.ToString(zigZagColor); } set { zigZagColor = NinjaTrader.Gui.Design.SerializableColor.FromString(value); } } #endregion } }