#region Using declarations using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using System; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Xml.Serialization; using System.Windows.Media; #endregion namespace NinjaTrader.NinjaScript.Indicators { public class MACDHistogram : Indicator { private EMA fastEMA; private EMA slowEMA; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"MACD Histogram"; Name = "MACD Histogram"; Calculate = Calculate.OnPriceChange; IsOverlay = false; DisplayInDataBox = true; DrawOnPricePanel = false; PaintPriceMarkers = true; ScaleJustification = ScaleJustification.Right; IsSuspendedWhileInactive = true; FastEMAPeriod = 12; SlowEMAPeriod = 36; PositiveBrush = Brushes.LimeGreen; NegativeBrush = Brushes.Red; ArePlotsConfigurable = false; AddPlot(new Stroke(Brushes.Transparent, 0), PlotStyle.Bar, "Histogram"); } else if (State == State.Configure) { } else if (State == State.DataLoaded) { fastEMA = EMA(FastEMAPeriod); slowEMA = EMA(SlowEMAPeriod); } } protected override void OnBarUpdate() { if (CurrentBar < Math.Max(FastEMAPeriod, SlowEMAPeriod)) return; double macdValue = (fastEMA[0] - slowEMA[0]) / TickSize; double previousMacdValue = Histogram[1]; Histogram[0] = macdValue; // If no change in value, use previous bar color if (Histogram[0] == Histogram[1]) PlotBrushes[0][0] = PlotBrushes[0][1]; else { if (macdValue > previousMacdValue) PlotBrushes[0][0] = PositiveBrush; else PlotBrushes[0][0] = NegativeBrush; } } protected override void OnRender(ChartControl chartControl, ChartScale chartScale) { base.OnRender(chartControl, chartScale); foreach (Plot plot in Plots) plot.Width = chartControl.GetBarPaintWidth(ChartBars); } public override string DisplayName { get { return Name; } } [Browsable(false)] [XmlIgnore] public Series Histogram { get { return Values[0]; } } [NinjaScriptProperty] [Range(1, int.MaxValue)] [Display(Name = "Fast EMA Period", Order = 1, GroupName = "MACD Histogram")] public int FastEMAPeriod { get; set; } [NinjaScriptProperty] [Range(1, int.MaxValue)] [Display(Name = "Slow EMA Period", Order = 2, GroupName = "MACD Histogram")] public int SlowEMAPeriod { get; set; } [XmlIgnore] [Display(Name = "Positive Value", Order = 3, GroupName = "MACD Histogram")] public Brush PositiveBrush { get; set; } [Browsable(false)] public string PositiveBrushSerializable { get { return Serialize.BrushToString(PositiveBrush); } set { PositiveBrush = Serialize.StringToBrush(value); } } [XmlIgnore] [Display(Name = "Negative Value", Order = 4, GroupName = "MACD Histogram")] public Brush NegativeBrush { get; set; } [Browsable(false)] public string NegativeBrushSerializable { get { return Serialize.BrushToString(NegativeBrush); } set { NegativeBrush = Serialize.StringToBrush(value); } } } } #region NinjaScript generated code. Neither change nor remove. namespace NinjaTrader.NinjaScript.Indicators { public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase { private MACDHistogram[] cacheMACDHistogram; public MACDHistogram MACDHistogram(int fastEMAPeriod, int slowEMAPeriod) { return MACDHistogram(Input, fastEMAPeriod, slowEMAPeriod); } public MACDHistogram MACDHistogram(ISeries input, int fastEMAPeriod, int slowEMAPeriod) { if (cacheMACDHistogram != null) for (int idx = 0; idx < cacheMACDHistogram.Length; idx++) if (cacheMACDHistogram[idx] != null && cacheMACDHistogram[idx].FastEMAPeriod == fastEMAPeriod && cacheMACDHistogram[idx].SlowEMAPeriod == slowEMAPeriod && cacheMACDHistogram[idx].EqualsInput(input)) return cacheMACDHistogram[idx]; return CacheIndicator(new MACDHistogram(){ FastEMAPeriod = fastEMAPeriod, SlowEMAPeriod = slowEMAPeriod }, input, ref cacheMACDHistogram); } } } namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns { public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase { public Indicators.MACDHistogram MACDHistogram(int fastEMAPeriod, int slowEMAPeriod) { return indicator.MACDHistogram(Input, fastEMAPeriod, slowEMAPeriod); } public Indicators.MACDHistogram MACDHistogram(ISeries input , int fastEMAPeriod, int slowEMAPeriod) { return indicator.MACDHistogram(input, fastEMAPeriod, slowEMAPeriod); } } } namespace NinjaTrader.NinjaScript.Strategies { public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase { public Indicators.MACDHistogram MACDHistogram(int fastEMAPeriod, int slowEMAPeriod) { return indicator.MACDHistogram(Input, fastEMAPeriod, slowEMAPeriod); } public Indicators.MACDHistogram MACDHistogram(ISeries input , int fastEMAPeriod, int slowEMAPeriod) { return indicator.MACDHistogram(input, fastEMAPeriod, slowEMAPeriod); } } } #endregion