Code presented in the article “Within The Volatility Band” by Sylvain Vervoort, in the August 2013 issue of Technical Analysis of Stocks & Commodities magazine.
SIDEBAR: SVEVolatilityBand Indicator
// SVEVolatilityBand is Copyright (C) 2012, Sylvain Vervoort <stocata.org>. // stocata.org reserves the right to modify this NinjaScript with each release. // Release V1.0 March, 2012. #region Using declarations using System; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.ComponentModel; using System.Xml.Serialization; using NinjaTrader.Data; using NinjaTrader.Gui.Chart; #endregion // This namespace holds all indicators and is required. Do not change it. namespace NinjaTrader.Indicator { /// <summary> /// The SVEVolatilityBand uses an upper and lower band over price bars. /// Price moves mostly withing this band. /// </summary> [Description("The SVEVolatilityBand uses an upper and lower volatility band over price bars")] public class SVEVolatilityBand : Indicator { #region Variables private int average = 8; // Band average private int volperiod = 13; // Volatility Summing period private double devfact = 3.55; // Deviation factor private double lowbandadjust = 0.9; // Low band adjustment factor private double devhigh, devlow; private DataSeries typical; private DataSeries deviation; private DataSeries medianaverage; #endregion /// <summary> /// This method is used to configure the indicator, called once before any bar is loaded. /// </summary> protected override void Initialize() { Add(new Plot(Color.Magenta, PlotStyle.Line, "Upper")); Add(new Plot(Color.Magenta, PlotStyle.Line, "Lower")); Add(new Plot(Color.Magenta, PlotStyle.Line, "Median")); Plots[0].Pen.Width = 2; Plots[0].Pen.DashStyle = DashStyle.Dot; Plots[1].Pen.Width = 2; Plots[1].Pen.DashStyle = DashStyle.Dot; Plots[2].Pen.Width = 1; Plots[2].Pen.DashStyle = DashStyle.Solid; typical = new DataSeries(this); deviation = new DataSeries(this); medianaverage = new DataSeries(this); Overlay = true; PaintPriceMarkers = false; } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { if (CurrentBar < 1) return; // need 2 bars to start // basic volatility in new "typical" data series if (Typical[0] >= Typical[1]) typical[0] = Typical[0] - Low[1]; else typical[0] = Typical[1] - Low[0]; // basic deviation based on "typical" over a period and a factor deviation[0] = SUM(typical,volperiod)[0] / volperiod * devfact; // the average deviation high and low band devhigh = EMA(deviation,average)[0]; devlow = EMA(deviation,average)[0] * lowbandadjust; // the middle average reference medianaverage[0] = EMA(Typical,average)[0]; // creation of the upper and lower band if (CurrentBar > average*3+2*volperiod) // show only after it is stable { Upper.Set(EMA(medianaverage,average)[0] + devhigh); Lower.Set(EMA(medianaverage,average)[0] - devlow); Median.Set(SMA(medianaverage,average)[0]); } } #region Properties /// <summary> /// </summary> [Browsable(false)] [XmlIgnore()] public DataSeries Upper { get { return Values[0]; } } /// <summary> /// </summary> [Browsable(false)] [XmlIgnore()] public DataSeries Lower { get { return Values[1]; } } /// <summary> /// </summary> [Browsable(false)] [XmlIgnore()] public DataSeries Median { get { return Values[2]; } } /// <summary> /// </summary> [Description("The Volatility Band average DEF=8")] [GridCategory("Parameters")] [Gui.Design.DisplayName("1. Band Average")] public int Vol_Band_Average { get { return average; } set { average = Math.Max(1, value); } } /// <summary> /// </summary> [Description("The Volatility Look Back Summing Period DEF=13")] [GridCategory("Parameters")] [Gui.Design.DisplayName("2. Summing Preiod")] public int Vol_Summing_Period { get { return volperiod; } set { volperiod = Math.Max(1, value); } } /// <summary> /// </summary> [Description("The Deviation Factor DEF=3.55")] [GridCategory("Parameters")] [Gui.Design.DisplayName("3. Deviation Factor")] public double Deviaton_Factor { get { return devfact; } set { devfact = Math.Max(0.1, value); } } /// <summary> /// </summary> [Description("Low band Adjustment DEF=0.9")] [GridCategory("Parameters")] [Gui.Design.DisplayName("4. Lower Band Adjust")] public double Low_Band_Adjust { get { return lowbandadjust; } set { lowbandadjust = Math.Max(0.01, value); } } #endregion } }