2023-10-18 17:27:58 +00:00
|
|
|
#region Using declarations
|
2024-07-13 12:29:13 +00:00
|
|
|
using NinjaTrader.Gui;
|
|
|
|
using NinjaTrader.Gui.Chart;
|
2023-10-18 17:27:58 +00:00
|
|
|
using System.Collections.Generic;
|
2024-07-17 18:10:06 +00:00
|
|
|
using System.ComponentModel;
|
2023-10-18 17:27:58 +00:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
using System.Windows.Media;
|
|
|
|
using System.Xml.Serialization;
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
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; }
|
|
|
|
}
|
|
|
|
|
2024-07-01 23:13:26 +00:00
|
|
|
[XmlIgnore]
|
2023-10-18 17:27:58 +00:00
|
|
|
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);
|
2024-07-13 12:39:45 +00:00
|
|
|
|
|
|
|
// For use when backtesting in order to limit the number of patterns processed.
|
2024-07-13 12:31:45 +00:00
|
|
|
MaxPatterns = 0;
|
2023-10-18 17:27:58 +00:00
|
|
|
}
|
|
|
|
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()
|
2024-06-14 13:04:26 +00:00
|
|
|
{
|
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;
|
|
|
|
|
2024-07-13 12:30:55 +00:00
|
|
|
int candleIndex = (State == State.Realtime) ? 1 : 0;
|
|
|
|
|
2023-10-18 17:27:58 +00:00
|
|
|
bool isSwingHigh = true, isSwingLow = true;
|
2024-07-13 12:30:55 +00:00
|
|
|
for (int i = (candleIndex + 1); i <= (BarLookback + 1); i++)
|
2023-10-18 17:27:58 +00:00
|
|
|
{
|
2024-07-13 12:30:55 +00:00
|
|
|
if (Highs[0][candleIndex] <= Highs[0][i]) isSwingHigh = false;
|
|
|
|
if (Lows[0][candleIndex] >= Lows[0][i]) isSwingLow = false;
|
2023-10-18 17:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Skip inside bars and get the proper penultimate candle.
|
2024-07-13 12:30:55 +00:00
|
|
|
int penultimateIndex = candleIndex + 1;
|
2023-10-18 17:27:58 +00:00
|
|
|
while (Highs[0][penultimateIndex] <= Highs[0][penultimateIndex + 1] && Lows[0][penultimateIndex] >= Lows[0][penultimateIndex + 1])
|
|
|
|
{
|
|
|
|
penultimateIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isSwingHigh)
|
|
|
|
{
|
|
|
|
detectedPatterns.Add(new Pattern
|
|
|
|
{
|
2024-07-13 12:30:55 +00:00
|
|
|
UltimateLevel = Highs[0][candleIndex],
|
2023-10-18 17:27:58 +00:00
|
|
|
PenultimateLevel = Lows[0][penultimateIndex],
|
|
|
|
StartBarIndex = CurrentBar - penultimateIndex,
|
|
|
|
IsSwingHigh = true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (isSwingLow)
|
|
|
|
{
|
|
|
|
detectedPatterns.Add(new Pattern
|
|
|
|
{
|
2024-07-13 12:30:55 +00:00
|
|
|
UltimateLevel = Lows[0][candleIndex],
|
2023-10-18 17:27:58 +00:00
|
|
|
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.
|
2024-07-13 12:30:55 +00:00
|
|
|
if ((pattern.IsSwingHigh && Closes[0][candleIndex] < pattern.PenultimateLevel && pattern.EndBarIndex == 0) ||
|
|
|
|
(pattern.IsSwingLow && Closes[0][candleIndex] > pattern.PenultimateLevel && pattern.EndBarIndex == 0))
|
2023-10-18 17:27:58 +00:00
|
|
|
{
|
2024-07-13 12:30:55 +00:00
|
|
|
pattern.EndBarIndex = CurrentBar - candleIndex;
|
2023-10-18 17:27:58 +00:00
|
|
|
DetectedPatterns[0] = pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for invalidated 3CRs.
|
2024-07-13 12:30:55 +00:00
|
|
|
if ((pattern.IsSwingHigh && Highs[0][candleIndex] > pattern.UltimateLevel && pattern.EndBarIndex == 0) ||
|
|
|
|
(pattern.IsSwingLow && Lows[0][candleIndex] < pattern.UltimateLevel && pattern.EndBarIndex == 0))
|
2023-10-18 17:27:58 +00:00
|
|
|
{
|
|
|
|
detectedPatterns.RemoveAt(i);
|
|
|
|
}
|
|
|
|
}
|
2024-07-13 12:31:45 +00:00
|
|
|
|
|
|
|
if (MaxPatterns > 0 && detectedPatterns.Count > MaxPatterns)
|
|
|
|
detectedPatterns.RemoveAt(0);
|
2023-10-18 17:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
|
|
|
|
{
|
|
|
|
base.OnRender(chartControl, chartScale);
|
|
|
|
|
2024-06-14 13:04:26 +00:00
|
|
|
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")]
|
2024-06-14 13:04:26 +00:00
|
|
|
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; }
|
2024-07-13 12:31:45 +00:00
|
|
|
|
2024-07-17 18:10:06 +00:00
|
|
|
[Browsable(false)]
|
2024-07-13 12:31:45 +00:00
|
|
|
[Range(0, int.MaxValue)]
|
|
|
|
public int MaxPatterns { get; set; }
|
2023-10-18 17:27:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#region NinjaScript generated code. Neither change nor remove.
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.Indicators
|
|
|
|
{
|
|
|
|
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
|
|
|
|
{
|
|
|
|
private ThreeCandleReversal[] cacheThreeCandleReversal;
|
2024-07-01 23:11:37 +00:00
|
|
|
public ThreeCandleReversal ThreeCandleReversal(int barLookback)
|
2023-10-18 17:27:58 +00:00
|
|
|
{
|
2024-07-01 23:11:37 +00:00
|
|
|
return ThreeCandleReversal(Input, barLookback);
|
2023-10-18 17:27:58 +00:00
|
|
|
}
|
|
|
|
|
2024-07-01 23:11:37 +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++)
|
2024-07-01 23:11:37 +00:00
|
|
|
if (cacheThreeCandleReversal[idx] != null && cacheThreeCandleReversal[idx].BarLookback == barLookback && cacheThreeCandleReversal[idx].EqualsInput(input))
|
2023-10-18 17:27:58 +00:00
|
|
|
return cacheThreeCandleReversal[idx];
|
2024-07-01 23:11:37 +00:00
|
|
|
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
|
|
|
|
{
|
2024-07-01 23:11:37 +00:00
|
|
|
public Indicators.ThreeCandleReversal ThreeCandleReversal(int barLookback)
|
2023-10-18 17:27:58 +00:00
|
|
|
{
|
2024-07-01 23:11:37 +00:00
|
|
|
return indicator.ThreeCandleReversal(Input, barLookback);
|
2023-10-18 17:27:58 +00:00
|
|
|
}
|
|
|
|
|
2024-07-01 23:11:37 +00:00
|
|
|
public Indicators.ThreeCandleReversal ThreeCandleReversal(ISeries<double> input , int barLookback)
|
2023-10-18 17:27:58 +00:00
|
|
|
{
|
2024-07-01 23:11:37 +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
|
|
|
|
{
|
2024-07-01 23:11:37 +00:00
|
|
|
public Indicators.ThreeCandleReversal ThreeCandleReversal(int barLookback)
|
2023-10-18 17:27:58 +00:00
|
|
|
{
|
2024-07-01 23:11:37 +00:00
|
|
|
return indicator.ThreeCandleReversal(Input, barLookback);
|
2023-10-18 17:27:58 +00:00
|
|
|
}
|
|
|
|
|
2024-07-01 23:11:37 +00:00
|
|
|
public Indicators.ThreeCandleReversal ThreeCandleReversal(ISeries<double> input , int barLookback)
|
2023-10-18 17:27:58 +00:00
|
|
|
{
|
2024-07-01 23:11:37 +00:00
|
|
|
return indicator.ThreeCandleReversal(input, barLookback);
|
2023-10-18 17:27:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|