2024-07-01 17:09:12 +00:00
|
|
|
#region Using declarations
|
|
|
|
using NinjaTrader.Gui;
|
|
|
|
using NinjaTrader.Gui.Chart;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2024-07-17 15:19:26 +00:00
|
|
|
using System.ComponentModel;
|
2024-07-01 17:09:12 +00:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Windows.Media;
|
2024-07-17 15:18:58 +00:00
|
|
|
using System.Xml.Serialization;
|
2024-07-01 17:09:12 +00:00
|
|
|
#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
|
|
|
|
{
|
2024-07-17 15:18:58 +00:00
|
|
|
[XmlIgnore]
|
2024-07-01 17:09:12 +00:00
|
|
|
public Series<PivotPoint> Pivot;
|
|
|
|
|
|
|
|
private List<PivotPoint> pivots = new List<PivotPoint>();
|
|
|
|
|
|
|
|
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);
|
2024-07-23 16:01:50 +00:00
|
|
|
UseClosePrice = false;
|
2024-07-17 15:19:26 +00:00
|
|
|
|
|
|
|
// For use during backtesting in order to limit the number of pivots processed.
|
|
|
|
MaxPivots = 0;
|
2024-07-01 17:09:12 +00:00
|
|
|
}
|
|
|
|
if (State == State.DataLoaded)
|
|
|
|
{
|
|
|
|
Pivot = new Series<PivotPoint>(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.
|
|
|
|
|
2024-07-23 16:01:50 +00:00
|
|
|
if (GetHigh(centerBarIndex) < GetHigh(i)) isPivotHigh = false;
|
|
|
|
if (GetLow(centerBarIndex) > GetLow(i)) isPivotLow = false;
|
2024-07-01 17:09:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if there's any pivot high without an end bar index and at the same price level as the current potential pivot high.
|
2024-07-23 16:01:50 +00:00
|
|
|
bool existsConflictingPivotHigh = pivots.Any(p => p.IsPivotHigh && p.EndBarIndex == 0 && p.Level == GetHigh(centerBarIndex));
|
2024-07-01 17:09:12 +00:00
|
|
|
|
|
|
|
// Check if there's any pivot low without an end bar index and at the same price level as the current potential pivot low.
|
2024-07-23 16:01:50 +00:00
|
|
|
bool existsConflictingPivotLow = pivots.Any(p => p.IsPivotLow && p.EndBarIndex == 0 && p.Level == GetLow(centerBarIndex));
|
2024-07-01 17:09:12 +00:00
|
|
|
|
|
|
|
if (isPivotHigh && !existsConflictingPivotHigh)
|
|
|
|
{
|
2024-07-23 16:01:50 +00:00
|
|
|
PivotPoint pivot = new PivotPoint { IsPivotHigh = true, Level = GetHigh(centerBarIndex), StartBarIndex = CurrentBar - centerBarIndex };
|
2024-07-01 17:09:12 +00:00
|
|
|
pivots.Add(pivot);
|
|
|
|
Pivot[0] = pivot;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isPivotLow && !existsConflictingPivotLow)
|
|
|
|
{
|
2024-07-23 16:01:50 +00:00
|
|
|
PivotPoint pivot = new PivotPoint { IsPivotLow = true, Level = GetLow(centerBarIndex), StartBarIndex = CurrentBar - centerBarIndex };
|
2024-07-01 17:09:12 +00:00
|
|
|
pivots.Add(pivot);
|
|
|
|
Pivot[0] = pivot;
|
|
|
|
}
|
2024-07-17 15:19:26 +00:00
|
|
|
|
|
|
|
if (MaxPivots > 0 && pivots.Count > MaxPivots)
|
|
|
|
pivots.RemoveAt(0);
|
2024-07-01 17:09:12 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 16:01:50 +00:00
|
|
|
private double GetHigh(int barsAgo)
|
|
|
|
{
|
|
|
|
return UseClosePrice ? Close[barsAgo] : High[barsAgo];
|
|
|
|
}
|
|
|
|
|
|
|
|
private double GetLow(int barsAgo)
|
|
|
|
{
|
|
|
|
return UseClosePrice ? Close[barsAgo] : Low[barsAgo];
|
|
|
|
}
|
|
|
|
|
2024-07-01 17:09:12 +00:00
|
|
|
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; }
|
|
|
|
|
2024-07-23 16:01:50 +00:00
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Display(Name = "Use Close Price", Order = 2, GroupName = "Pivot")]
|
|
|
|
public bool UseClosePrice
|
|
|
|
{ get; set; }
|
|
|
|
|
|
|
|
[Display(Name = "Pivot High Stroke", Order = 3, GroupName = "Pivot")]
|
2024-07-01 17:09:12 +00:00
|
|
|
public Stroke PivotHighStroke { get; set; }
|
|
|
|
|
2024-07-23 16:01:50 +00:00
|
|
|
[Display(Name = "Pivot Low Stroke", Order = 4, GroupName = "Pivot")]
|
2024-07-01 17:09:12 +00:00
|
|
|
public Stroke PivotLowStroke { get; set; }
|
2024-07-17 15:19:26 +00:00
|
|
|
|
|
|
|
[Browsable(false)]
|
|
|
|
public int MaxPivots { get; set; }
|
2024-07-01 17:09:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#region NinjaScript generated code. Neither change nor remove.
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.Indicators
|
|
|
|
{
|
|
|
|
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
|
|
|
|
{
|
|
|
|
private PivotPoints[] cachePivotPoints;
|
2024-07-23 16:01:50 +00:00
|
|
|
public PivotPoints PivotPoints(int rollingWindow, bool useClosePrice)
|
2024-07-01 17:09:12 +00:00
|
|
|
{
|
2024-07-23 16:01:50 +00:00
|
|
|
return PivotPoints(Input, rollingWindow, useClosePrice);
|
2024-07-01 17:09:12 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 16:01:50 +00:00
|
|
|
public PivotPoints PivotPoints(ISeries<double> input, int rollingWindow, bool useClosePrice)
|
2024-07-01 17:09:12 +00:00
|
|
|
{
|
|
|
|
if (cachePivotPoints != null)
|
|
|
|
for (int idx = 0; idx < cachePivotPoints.Length; idx++)
|
2024-07-23 16:01:50 +00:00
|
|
|
if (cachePivotPoints[idx] != null && cachePivotPoints[idx].RollingWindow == rollingWindow && cachePivotPoints[idx].UseClosePrice == useClosePrice && cachePivotPoints[idx].EqualsInput(input))
|
2024-07-01 17:09:12 +00:00
|
|
|
return cachePivotPoints[idx];
|
2024-07-23 16:01:50 +00:00
|
|
|
return CacheIndicator<PivotPoints>(new PivotPoints(){ RollingWindow = rollingWindow, UseClosePrice = useClosePrice }, input, ref cachePivotPoints);
|
2024-07-01 17:09:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
|
|
|
|
{
|
|
|
|
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
|
|
|
|
{
|
2024-07-23 16:01:50 +00:00
|
|
|
public Indicators.PivotPoints PivotPoints(int rollingWindow, bool useClosePrice)
|
2024-07-01 17:09:12 +00:00
|
|
|
{
|
2024-07-23 16:01:50 +00:00
|
|
|
return indicator.PivotPoints(Input, rollingWindow, useClosePrice);
|
2024-07-01 17:09:12 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 16:01:50 +00:00
|
|
|
public Indicators.PivotPoints PivotPoints(ISeries<double> input , int rollingWindow, bool useClosePrice)
|
2024-07-01 17:09:12 +00:00
|
|
|
{
|
2024-07-23 16:01:50 +00:00
|
|
|
return indicator.PivotPoints(input, rollingWindow, useClosePrice);
|
2024-07-01 17:09:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.Strategies
|
|
|
|
{
|
|
|
|
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
|
|
|
|
{
|
2024-07-23 16:01:50 +00:00
|
|
|
public Indicators.PivotPoints PivotPoints(int rollingWindow, bool useClosePrice)
|
2024-07-01 17:09:12 +00:00
|
|
|
{
|
2024-07-23 16:01:50 +00:00
|
|
|
return indicator.PivotPoints(Input, rollingWindow, useClosePrice);
|
2024-07-01 17:09:12 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 16:01:50 +00:00
|
|
|
public Indicators.PivotPoints PivotPoints(ISeries<double> input , int rollingWindow, bool useClosePrice)
|
2024-07-01 17:09:12 +00:00
|
|
|
{
|
2024-07-23 16:01:50 +00:00
|
|
|
return indicator.PivotPoints(input, rollingWindow, useClosePrice);
|
2024-07-01 17:09:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|