From 1401a580e8b7d5602eac692d676025e4c07f4e11 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Fri, 9 Aug 2024 18:25:40 -0700 Subject: [PATCH] Add pivot level to 2B indicator --- indicators/TwoB.cs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/indicators/TwoB.cs b/indicators/TwoB.cs index 5fe7cbc..99429fe 100644 --- a/indicators/TwoB.cs +++ b/indicators/TwoB.cs @@ -17,6 +17,8 @@ namespace NinjaTrader.NinjaScript.Indicators public bool Is2BDown { get; set; } public int StartBarIndex { get; set; } public int EndBarIndex { get; set; } + public double PivotLevel { get; set; } + public int PivotBarIndex { get; set; } } public class TwoB : Indicator @@ -30,9 +32,13 @@ namespace NinjaTrader.NinjaScript.Indicators private double pivotHigh; private double pivotLow; + private int pivotHighBarIndex; + private int pivotLowBarIndex; private double mostRecentPivotHigh; private double mostRecentPivotLow; + private int mostRecentPivotHighBarIndex; + private int mostRecentPivotLowBarIndex; private double twoBUpLevel; private int twoBUpStartBarIndex; @@ -61,6 +67,7 @@ namespace NinjaTrader.NinjaScript.Indicators PivotPeriod = 10; TwoBUpStroke = new Stroke(Brushes.LimeGreen, 3); TwoBDownStroke = new Stroke(Brushes.Red, 3); + PivotStroke = new Stroke(Brushes.Yellow, 3); // For use during backtesting in order to limit the number of patterns processed. MaxPatterns = 0; @@ -88,14 +95,21 @@ namespace NinjaTrader.NinjaScript.Indicators if (pivot != null) { if (pivot.IsPivotHigh) + { pivotHigh = pivot.Level; + pivotHighBarIndex = pivot.StartBarIndex; + } else if (pivot.IsPivotLow) + { pivotLow = pivot.Level; + pivotLowBarIndex = pivot.StartBarIndex; + } } if (Open[0] < pivotHigh && High[0] > pivotHigh && mostRecentPivotHigh != pivotHigh) { mostRecentPivotHigh = pivotHigh; + mostRecentPivotHighBarIndex = pivotHighBarIndex; twoBDownLevel = Low[0]; twoBDownStartBarIndex = CurrentBar; @@ -103,6 +117,7 @@ namespace NinjaTrader.NinjaScript.Indicators else if (Open[0] > pivotLow && Low[0] < pivotLow && mostRecentPivotLow != pivotLow) { mostRecentPivotLow = pivotLow; + mostRecentPivotLowBarIndex = pivotLowBarIndex; twoBUpLevel = High[0]; twoBUpStartBarIndex = CurrentBar; @@ -116,7 +131,9 @@ namespace NinjaTrader.NinjaScript.Indicators Level = twoBUpLevel, Is2BUp = true, StartBarIndex = twoBUpStartBarIndex, - EndBarIndex = twoBUpEndBarIndex + EndBarIndex = twoBUpEndBarIndex, + PivotBarIndex = mostRecentPivotLowBarIndex, + PivotLevel = mostRecentPivotLow, }; twoBs.Add(twoBUp); CompletedPattern[0] = twoBUp; @@ -132,7 +149,9 @@ namespace NinjaTrader.NinjaScript.Indicators Level = twoBDownLevel, Is2BDown = true, StartBarIndex = twoBDownStartBarIndex, - EndBarIndex = twoBDownEndBarIndex + EndBarIndex = twoBDownEndBarIndex, + PivotBarIndex = mostRecentPivotHighBarIndex, + PivotLevel = mostRecentPivotHigh, }; twoBs.Add(twoBDown); CompletedPattern[0] = twoBDown; @@ -155,6 +174,9 @@ namespace NinjaTrader.NinjaScript.Indicators var stroke = twoB.Is2BUp ? TwoBUpStroke : TwoBDownStroke; DrawHorizontalLine(startX, endX, chartScale.GetYByValue(twoB.Level), stroke); + + int pivotX = chartControl.GetXByBarIndex(ChartBars, twoB.PivotBarIndex); + DrawHorizontalLine(pivotX, startX, chartScale.GetYByValue(twoB.PivotLevel), PivotStroke); } } @@ -186,6 +208,9 @@ namespace NinjaTrader.NinjaScript.Indicators [Display(Name = "2B Down Stroke", GroupName = "2B", Order = 3)] public Stroke TwoBDownStroke { get; set; } + [Display(Name = "Pivot Stroke", GroupName = "2B", Order = 4)] + public Stroke PivotStroke { get; set; } + [Browsable(false)] public int MaxPatterns { get; set; } }