#region Using declarations using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using System; using System.Collections.Generic; using System.ComponentModel; 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); UseClosePrice = false; // For use during backtesting in order to limit the number of pivots processed. MaxPivots = 0; } 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 (GetHigh(centerBarIndex) < GetHigh(i)) isPivotHigh = false; if (GetLow(centerBarIndex) > GetLow(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 == GetHigh(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 == GetLow(centerBarIndex)); if (isPivotHigh && !existsConflictingPivotHigh) { PivotPoint pivot = new PivotPoint { IsPivotHigh = true, Level = GetHigh(centerBarIndex), StartBarIndex = CurrentBar - centerBarIndex }; pivots.Add(pivot); Pivot[0] = pivot; } if (isPivotLow && !existsConflictingPivotLow) { PivotPoint pivot = new PivotPoint { IsPivotLow = true, Level = GetLow(centerBarIndex), StartBarIndex = CurrentBar - centerBarIndex }; pivots.Add(pivot); Pivot[0] = pivot; } if (MaxPivots > 0 && pivots.Count > MaxPivots) pivots.RemoveAt(0); } private double GetHigh(int barsAgo) { return UseClosePrice ? Close[barsAgo] : High[barsAgo]; } private double GetLow(int barsAgo) { return UseClosePrice ? Close[barsAgo] : Low[barsAgo]; } 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; } [NinjaScriptProperty] [Display(Name = "Use Close Price", Order = 2, GroupName = "Pivot")] public bool UseClosePrice { get; set; } [Display(Name = "Pivot High Stroke", Order = 3, GroupName = "Pivot")] public Stroke PivotHighStroke { get; set; } [Display(Name = "Pivot Low Stroke", Order = 4, GroupName = "Pivot")] public Stroke PivotLowStroke { get; set; } [Browsable(false)] public int MaxPivots { 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, bool useClosePrice) { return PivotPoints(Input, rollingWindow, useClosePrice); } public PivotPoints PivotPoints(ISeries input, int rollingWindow, bool useClosePrice) { if (cachePivotPoints != null) for (int idx = 0; idx < cachePivotPoints.Length; idx++) if (cachePivotPoints[idx] != null && cachePivotPoints[idx].RollingWindow == rollingWindow && cachePivotPoints[idx].UseClosePrice == useClosePrice && cachePivotPoints[idx].EqualsInput(input)) return cachePivotPoints[idx]; return CacheIndicator(new PivotPoints(){ RollingWindow = rollingWindow, UseClosePrice = useClosePrice }, input, ref cachePivotPoints); } } } namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns { public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase { public Indicators.PivotPoints PivotPoints(int rollingWindow, bool useClosePrice) { return indicator.PivotPoints(Input, rollingWindow, useClosePrice); } public Indicators.PivotPoints PivotPoints(ISeries input , int rollingWindow, bool useClosePrice) { return indicator.PivotPoints(input, rollingWindow, useClosePrice); } } } namespace NinjaTrader.NinjaScript.Strategies { public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase { public Indicators.PivotPoints PivotPoints(int rollingWindow, bool useClosePrice) { return indicator.PivotPoints(Input, rollingWindow, useClosePrice); } public Indicators.PivotPoints PivotPoints(ISeries input , int rollingWindow, bool useClosePrice) { return indicator.PivotPoints(input, rollingWindow, useClosePrice); } } } #endregion