Code presented in the article “Oscillators Rock!” by Sylvain Vervoort, in the September 2013 issue of Technical Analysis of Stocks & Commodities magazine.
SIDEBAR: Svezlrb PercB oscillator
// SVEZLRBPercB is Copyright (C) 2012, Sylvain Vervoort < stocata.org> . // stocata.org reserves the right to modify this NinjaScript with each release. // Release V1.0 December, 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> /// smoothed zero-lagging percent B indicator on a rainbow based price series. /// Includes a slow modified stochastic following medium term moves. /// < /summary> [Description("Smoothed zero-lagging percent B indicator on rainbow price series.")] public class SVEZLRBPercB : Indicator { #region Variables private int stdevperiod = 18; // Default standard deviation look back period private int smooth = 3; // Default TEMA/EMA smoothing average private int periodK = 30; // Kperiod for Stochastics line private int smoothK = 3; // Slowing K period private DataSeries rainbow; private DataSeries EMA1; private DataSeries EMA2; private DataSeries ZLRB; private DataSeries den; private DataSeries nom; private DataSeries fastK; private DataSeries RBC; private double diff, rainbow_value; #endregion /// < summary> /// Configuring the indicator called once before any bar data. /// < /summary> protected override void Initialize() { Add(new Plot(Color.FromKnownColor(KnownColor.DodgerBlue),PlotStyle.Line, "PB_Plot")); Add(new Plot(Color.Red, "K")); Add(new Line(Color.Gray, 100, "Overbought")); Add(new Line(Color.Gray, 50, "Neutral")); Add(new Line(Color.Gray, 0, "Oversold")); Lines[0].Pen.DashStyle=DashStyle.Dot; Lines[1].Pen.DashStyle=DashStyle.Dot; Lines[1].Pen.Width=2; Lines[2].Pen.DashStyle=DashStyle.Dot; PaintPriceMarkers = false; Overlay = false; rainbow = new DataSeries(this); EMA1 = new DataSeries(this); EMA2 = new DataSeries(this); ZLRB = new DataSeries(this); den = new DataSeries(this); nom = new DataSeries(this); fastK = new DataSeries(this); RBC = new DataSeries(this); } /// < summary> /// Called on each bar update event (incoming tick) /// < /summary> protected override void OnBarUpdate() { if (CurrentBar < 1) // minimum 2 bars required return; // Create rainbow based data set rainbow_value = (5 * SMA(2)[0] + 4 * SMA(SMA(2), 2)[0] + 3 * SMA(SMA(SMA(2), 2), 2)[0] + 2 * SMA(SMA(SMA(SMA(2), 2), 2), 2)[0] + SMA(SMA(SMA(SMA(SMA(2), 2), 2), 2), 2)[0] + SMA(SMA(SMA(SMA(SMA(SMA(2), 2), 2), 2), 2), 2)[0] + SMA(SMA(SMA(SMA(SMA(SMA(SMA(2), 2), 2), 2), 2), 2), 2)[0] + SMA(SMA(SMA(SMA(SMA(SMA(SMA(SMA(2), 2), 2), 2), 2), 2), 2), 2)[0] + SMA(SMA(SMA(SMA(SMA(SMA(SMA(SMA(SMA(2), 2), 2), 2), 2), 2), 2), 2), 2)[0] + SMA(SMA(SMA(SMA(SMA(SMA(SMA(SMA(SMA(SMA(2), 2), 2), 2), 2), 2), 2), 2), 2), 2)[0]) / 20; rainbow.Set(rainbow_value); // Smoothing rainbow data in new data set ZLRB applying zero-lagging EMA1.Set(EMA(rainbow, smooth)[0]); EMA2.Set(EMA(EMA1, smooth)[0]); diff = EMA1[0] - EMA2[0]; ZLRB.Set(EMA1[0] + diff); // Plot percent B indicator from smoothed band at 2 standard deviations PB_Plot.Set((TEMA(ZLRB, smooth)[0] + 2 * StdDev(TEMA(ZLRB, smooth), stdevperiod)[0] - WMA(TEMA(ZLRB, smooth),stdevperiod)[0]) / (4 * StdDev(TEMA(ZLRB, smooth), stdevperiod)[0]) * 100); // Add a second plot for the slow stochastic // Use a numerator/denominator based on an averaged new data series RBC RBC.Set((rainbow[0] + Typical[0])/2); nom.Set(RBC[0] - MIN(Low, periodK)[0]); den.Set(MAX(High, periodK)[0] - MIN(RBC, periodK)[0]); // Calculate and smooth the Stochastic indicator if (den[0].Compare(0, 0.000000000001) == 0) fastK.Set(CurrentBar == 0 ? 50 : fastK[1]); else fastK.Set(Math.Min(100, Math.Max(0, 100 * nom[0] / den[0]))); K.Set(SMA(fastK, smoothK)[0]); } #region Properties [Browsable(false)] // prevents data series being displayed in properties [XmlIgnore()] // ensures indicator can be saved as part of a template public DataSeries PB_Plot { get { return Values[0]; } } /// < summary> /// Gets the slow K value. /// < /summary> [Browsable(false)] [XmlIgnore()] public DataSeries K { get { return Values[1]; } } [Description("PercentB Standard Deviation Period DEF = 18")] [Category("Parameters")] [Gui.Design.DisplayName("1. PercentB Deviation Period")] public int PercB_Deviation_Period { get { return stdevperiod; } set { stdevperiod = Math.Max(1, value); } } [Description("PercentB smoothing average DEF = 3")] [Category("Parameters")] [Gui.Design.DisplayName("2. PercentB Average")] public int PercB_Average { get { return smooth; } set { smooth = Math.Max(1, value); } } /// < summary> /// < /summary> [Description("Bars used for calculating K value DEF = 30")] [GridCategory("Parameters")] [Gui.Design.DisplayName("3. Stochastic Period")] public int Stochastic_Period { get { return periodK; } set { periodK = Math.Max(1, value); } } /// < summary> /// < /summary> [Description("Bars for slowing basic K values DEF = 3")] [GridCategory("Parameters")] [Gui.Design.DisplayName("4. Stochastic Slowing")] public int Stochastic_Slowing { get { return smoothK; } set { smoothK = Math.Max(1, value); } } #endregion } }