public class MACDUpDownPlus : Indicator { private Series<double> fastEma; private Series<double> slowEma; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Indicator here."; Name = "MACDUpDownPlus"; Calculate = Calculate.OnPriceChange; IsOverlay = true; DisplayInDataBox = true; DrawOnPricePanel = true; DrawHorizontalGridLines = true; DrawVerticalGridLines = true; PaintPriceMarkers = true; ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right; //Disable this property if your indicator requires custom values that cumulate with each new market data event. //See Help Guide for additional information. IsSuspendedWhileInactive = true; Fast = 12; Slow = 26; Smooth = 9; MACDUP = Brushes.Green; MACDDOWN = Brushes.Red; AVGUP = Brushes.Magenta; AVGDOWN = Brushes.Black; AddPlot(Brushes.Green, "Macd"); AddPlot(Brushes.DarkViolet, "Avg"); AddPlot(new Stroke(Brushes.Navy, 2), PlotStyle.Bar, "Diff"); AddLine(Brushes.DarkGray, 0, "Zero line"); } else if (State == State.Configure) { } else if (State == State.DataLoaded) { fastEma = new Series<double>(this); slowEma = new Series<double>(this); } } protected override void OnBarUpdate() { if (CurrentBar == 0) { fastEma[0] = Input[0]; slowEma[0] = Input[0]; Value[0] = 0; Avg[0] = 0; Diff[0] = 0; } else { fastEma[0] = (2.0 / (1 + Fast)) * Input[0] + (1 - (2.0 / (1 + Fast))) * fastEma[1]; slowEma[0] = (2.0 / (1 + Slow)) * Input[0] + (1 - (2.0 / (1 + Slow))) * slowEma[1]; double macd = fastEma[0] - slowEma[0]; double macdAvg = (2.0 / (1 + Smooth)) * macd + (1 - (2.0 / (1 + Smooth))) * Avg[1]; Value[0] = macd; Avg[0] = macdAvg; Diff[0] = macd - macdAvg; } // Plots MACD color when rising or falling if (IsRising(MACD(Fast, Slow, Smooth))) { PlotBrushes[0][0] = MACDUP; } else if (IsFalling(MACD(Fast, Slow, Smooth))) { PlotBrushes[0][0] = MACDDOWN; } // Plots AVG color when rising or falling if (IsRising(AVG(Fast, Slow, Smooth))) { PlotBrushes[0][0] = AVGUP; } else if (IsFalling(AVG(Fast, Slow, Smooth))) { PlotBrushes[0][0] = AVGDOWN;
Thank you.
Comment