#region Using declarations using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Windows.Media; using System.Xml.Serialization; #endregion namespace NinjaTrader.NinjaScript.Indicators { public class PivotPoint { public bool IsPivotHigh { get; set; } public bool IsPivotLow { get; set; } public double Level { get; set; } public int StartBarIndex { get; set; } public int EndBarIndex { get; set; } } public class PivotPoints : Indicator { [XmlIgnore] public Series Pivot; private List pivots = new List(); protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Pivot Point (a.k.a. Swing High / Low) Indicator"; Name = "Pivot Points"; Calculate = Calculate.OnPriceChange; IsOverlay = true; DisplayInDataBox = true; DrawOnPricePanel = true; DrawHorizontalGridLines = true; DrawVerticalGridLines = true; PaintPriceMarkers = true; ScaleJustification = ScaleJustification.Right; IsSuspendedWhileInactive = true; RollingWindow = 10; PivotHighStroke = new Stroke(Brushes.LimeGreen, 3); PivotLowStroke = new Stroke(Brushes.Red, 3); } if (State == State.DataLoaded) { Pivot = new Series(this, MaximumBarsLookBack.Infinite); } else if (State == State.Historical) { SetZOrder(-1); // Display behind bars on chart. } } protected override void OnBarUpdate() { if (CurrentBar < RollingWindow) return; if (!IsFirstTickOfBar) return; foreach (var pivot in pivots) { if (pivot.EndBarIndex == 0) { if (pivot.IsPivotHigh && Close[1] > pivot.Level) pivot.EndBarIndex = CurrentBar - 1; else if (pivot.IsPivotLow && Close[1] < pivot.Level) pivot.EndBarIndex = CurrentBar - 1; } } bool isPivotHigh = true; bool isPivotLow = true; int centerBarIndex = (int)Math.Ceiling(RollingWindow / 2.0); for (int i = 1; i <= RollingWindow; i++) { if (i == centerBarIndex) continue; // The center bar is the potential pivot, skip it. if (High[centerBarIndex] < High[i]) isPivotHigh = false; if (Low[centerBarIndex] > Low[i]) isPivotLow = false; } // Check if there's any pivot high without an end bar index and at the same price level as the current potential pivot high. bool existsConflictingPivotHigh = pivots.Any(p => p.IsPivotHigh && p.EndBarIndex == 0 && p.Level == High[centerBarIndex]); // Check if there's any pivot low without an end bar index and at the same price level as the current potential pivot low. bool existsConflictingPivotLow = pivots.Any(p => p.IsPivotLow && p.EndBarIndex == 0 && p.Level == Low[centerBarIndex]); if (isPivotHigh && !existsConflictingPivotHigh) { PivotPoint pivot = new PivotPoint { IsPivotHigh = true, Level = High[centerBarIndex], StartBarIndex = CurrentBar - centerBarIndex }; pivots.Add(pivot); Pivot[0] = pivot; } if (isPivotLow && !existsConflictingPivotLow) { PivotPoint pivot = new PivotPoint { IsPivotLow = true, Level = Low[centerBarIndex], StartBarIndex = CurrentBar - centerBarIndex }; pivots.Add(pivot); Pivot[0] = pivot; } } protected override void OnRender(ChartControl chartControl, ChartScale chartScale) { base.OnRender(chartControl, chartScale); foreach (var pivot in pivots.ToArray()) { int startX = chartControl.GetXByBarIndex(ChartBars, pivot.StartBarIndex); int endX = pivot.EndBarIndex == 0 ? ChartPanel.X + ChartPanel.W : chartControl.GetXByBarIndex(ChartBars, pivot.EndBarIndex); var stroke = pivot.IsPivotHigh ? PivotHighStroke : pivot.IsPivotLow ? PivotLowStroke : new Stroke(Brushes.Gray); DrawHorizontalLine(startX, endX, chartScale.GetYByValue(pivot.Level), stroke); } } private void DrawHorizontalLine(int startX, int endX, int y, Stroke stroke) { SharpDX.Direct2D1.Brush dxBrush = stroke.Brush.ToDxBrush(RenderTarget); RenderTarget.DrawLine( new SharpDX.Vector2(startX, y), new SharpDX.Vector2(endX, y), dxBrush, stroke.Width, stroke.StrokeStyle ); dxBrush.Dispose(); } public override string DisplayName { get { return Name; } } [NinjaScriptProperty] [Range(1, int.MaxValue)] [Display(Name = "Rolling Window", Description = "Number of bars to use when checking for pivots", Order = 1, GroupName = "Pivot")] public int RollingWindow { get; set; } [Display(Name = "Pivot High Stroke", Order = 2, GroupName = "Pivot")] public Stroke PivotHighStroke { get; set; } [Display(Name = "Pivot Low Stroke", Order = 3, GroupName = "Pivot")] public Stroke PivotLowStroke { get; set; } } } #region NinjaScript generated code. Neither change nor remove. namespace NinjaTrader.NinjaScript.Indicators { public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase { private PivotPoints[] cachePivotPoints; public PivotPoints PivotPoints(int rollingWindow) { return PivotPoints(Input, rollingWindow); } public PivotPoints PivotPoints(ISeries input, int rollingWindow) { if (cachePivotPoints != null) for (int idx = 0; idx < cachePivotPoints.Length; idx++) if (cachePivotPoints[idx] != null && cachePivotPoints[idx].RollingWindow == rollingWindow && cachePivotPoints[idx].EqualsInput(input)) return cachePivotPoints[idx]; return CacheIndicator(new PivotPoints(){ RollingWindow = rollingWindow }, input, ref cachePivotPoints); } } } namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns { public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase { public Indicators.PivotPoints PivotPoints(int rollingWindow) { return indicator.PivotPoints(Input, rollingWindow); } public Indicators.PivotPoints PivotPoints(ISeries input , int rollingWindow) { return indicator.PivotPoints(input, rollingWindow); } } } namespace NinjaTrader.NinjaScript.Strategies { public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase { public Indicators.PivotPoints PivotPoints(int rollingWindow) { return indicator.PivotPoints(Input, rollingWindow); } public Indicators.PivotPoints PivotPoints(ISeries input , int rollingWindow) { return indicator.PivotPoints(input, rollingWindow); } } } #endregion