ninjatrader/indicators/ThreeCandleReversal.cs

251 lines
9.2 KiB
C#
Raw Normal View History

2023-10-18 17:27:58 +00:00
#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Gui.Tools;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
//This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
public class ThreeCandleReversal : Indicator
{
public class Pattern
{
public double UltimateLevel { get; set; }
public double PenultimateLevel { get; set; }
public int StartBarIndex { get; set; }
public int EndBarIndex { get; set; }
public bool IsSwingHigh { get; set; }
public bool IsSwingLow { get; set; }
}
public Series<Pattern> DetectedPatterns;
private List<Pattern> detectedPatterns = new List<Pattern>();
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Credit to @BestForexMethod.";
Name = "3CR";
Calculate = Calculate.OnPriceChange;
IsOverlay = true;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = ScaleJustification.Right;
IsSuspendedWhileInactive = true;
BarLookback = 5;
BullishLevelStroke = new Stroke(Brushes.LimeGreen, DashStyleHelper.Solid, 2);
BearishLevelStroke = new Stroke(Brushes.Red, DashStyleHelper.Solid, 2);
}
if (State == State.DataLoaded)
{
DetectedPatterns = new Series<Pattern>(this);
}
else if (State == State.Historical)
{
SetZOrder(-1); // Display behind bars on chart.
}
}
protected override void OnBarUpdate()
{
2023-10-18 17:27:58 +00:00
if (CurrentBar < BarLookback + 1)
return;
for (int i = detectedPatterns.Count - 1; i >= 0; i--)
{
var pattern = detectedPatterns[i];
if (pattern.IsSwingHigh && Closes[0][0] > pattern.UltimateLevel && pattern.EndBarIndex == 0)
{
detectedPatterns.RemoveAt(i);
}
else if (pattern.IsSwingLow && Closes[0][0] < pattern.UltimateLevel && pattern.EndBarIndex == 0)
{
detectedPatterns.RemoveAt(i);
}
}
DetectedPatterns[0] = null;
bool isSwingHigh = true, isSwingLow = true;
for (int i = 2; i <= (BarLookback + 1); i++)
{
if (Highs[0][1] <= Highs[0][i]) isSwingHigh = false;
if (Lows[0][1] >= Lows[0][i]) isSwingLow = false;
}
// Skip inside bars and get the proper penultimate candle.
int penultimateIndex = 2;
while (Highs[0][penultimateIndex] <= Highs[0][penultimateIndex + 1] && Lows[0][penultimateIndex] >= Lows[0][penultimateIndex + 1])
{
penultimateIndex++;
}
if (isSwingHigh)
{
detectedPatterns.Add(new Pattern
{
UltimateLevel = Highs[0][1],
PenultimateLevel = Lows[0][penultimateIndex],
StartBarIndex = CurrentBar - penultimateIndex,
IsSwingHigh = true
});
}
else if (isSwingLow)
{
detectedPatterns.Add(new Pattern
{
UltimateLevel = Lows[0][1],
PenultimateLevel = Highs[0][penultimateIndex],
StartBarIndex = CurrentBar - penultimateIndex,
IsSwingLow = true
});
}
for (int i = detectedPatterns.Count - 1; i >= 0; i--)
{
var pattern = detectedPatterns[i];
// Check for completed 3CRs.
if ((pattern.IsSwingHigh && Closes[0][1] < pattern.PenultimateLevel && pattern.EndBarIndex == 0) ||
(pattern.IsSwingLow && Closes[0][1] > pattern.PenultimateLevel && pattern.EndBarIndex == 0))
{
pattern.EndBarIndex = CurrentBar - 1;
DetectedPatterns[0] = pattern;
}
// Check for invalidated 3CRs.
if ((pattern.IsSwingHigh && Highs[0][1] > pattern.UltimateLevel && pattern.EndBarIndex == 0) ||
(pattern.IsSwingLow && Lows[0][1] < pattern.UltimateLevel && pattern.EndBarIndex == 0))
{
detectedPatterns.RemoveAt(i);
}
}
}
protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
{
base.OnRender(chartControl, chartScale);
foreach (var pattern in detectedPatterns.ToArray())
2023-10-18 17:27:58 +00:00
{
int startX = chartControl.GetXByBarIndex(ChartBars, pattern.StartBarIndex);
int endX = pattern.EndBarIndex == 0 ? ChartPanel.X + ChartPanel.W : chartControl.GetXByBarIndex(ChartBars, pattern.EndBarIndex);
var levelToDraw = pattern.PenultimateLevel;
Stroke currentStroke = pattern.IsSwingHigh ? BearishLevelStroke : BullishLevelStroke;
DrawHorizontalLine(startX, endX, chartScale.GetYByValue(levelToDraw), currentStroke);
}
}
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; }
}
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(Name = "Bar Lookback", Description = "Number of bars to compare for detecting swing highs / lows", Order = 1, GroupName = "3CR")]
public int BarLookback { get; set; }
2023-10-18 17:27:58 +00:00
[XmlIgnore]
[Display(Name = "Bullish Level", Description = "Stroke for bullish level drawn on chart", Order = 2, GroupName = "3CR")]
public Stroke BullishLevelStroke { get; set; }
[XmlIgnore]
[Display(Name = "Bearish Level", Description = "Stroke for bearish level drawn on chart", Order = 3, GroupName = "3CR")]
public Stroke BearishLevelStroke { get; set; }
}
}
#region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private ThreeCandleReversal[] cacheThreeCandleReversal;
public ThreeCandleReversal ThreeCandleReversal(int barLookback)
2023-10-18 17:27:58 +00:00
{
return ThreeCandleReversal(Input, barLookback);
2023-10-18 17:27:58 +00:00
}
public ThreeCandleReversal ThreeCandleReversal(ISeries<double> input, int barLookback)
2023-10-18 17:27:58 +00:00
{
if (cacheThreeCandleReversal != null)
for (int idx = 0; idx < cacheThreeCandleReversal.Length; idx++)
if (cacheThreeCandleReversal[idx] != null && cacheThreeCandleReversal[idx].BarLookback == barLookback && cacheThreeCandleReversal[idx].EqualsInput(input))
2023-10-18 17:27:58 +00:00
return cacheThreeCandleReversal[idx];
return CacheIndicator<ThreeCandleReversal>(new ThreeCandleReversal(){ BarLookback = barLookback }, input, ref cacheThreeCandleReversal);
2023-10-18 17:27:58 +00:00
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.ThreeCandleReversal ThreeCandleReversal(int barLookback)
2023-10-18 17:27:58 +00:00
{
return indicator.ThreeCandleReversal(Input, barLookback);
2023-10-18 17:27:58 +00:00
}
public Indicators.ThreeCandleReversal ThreeCandleReversal(ISeries<double> input , int barLookback)
2023-10-18 17:27:58 +00:00
{
return indicator.ThreeCandleReversal(input, barLookback);
2023-10-18 17:27:58 +00:00
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.ThreeCandleReversal ThreeCandleReversal(int barLookback)
2023-10-18 17:27:58 +00:00
{
return indicator.ThreeCandleReversal(Input, barLookback);
2023-10-18 17:27:58 +00:00
}
public Indicators.ThreeCandleReversal ThreeCandleReversal(ISeries<double> input , int barLookback)
2023-10-18 17:27:58 +00:00
{
return indicator.ThreeCandleReversal(input, barLookback);
2023-10-18 17:27:58 +00:00
}
}
}
#endregion