Add pivot level to 2B indicator

This commit is contained in:
moshferatu 2024-08-09 18:25:40 -07:00
parent 84e719c68b
commit 1401a580e8

View File

@ -17,6 +17,8 @@ namespace NinjaTrader.NinjaScript.Indicators
public bool Is2BDown { get; set; } public bool Is2BDown { get; set; }
public int StartBarIndex { get; set; } public int StartBarIndex { get; set; }
public int EndBarIndex { get; set; } public int EndBarIndex { get; set; }
public double PivotLevel { get; set; }
public int PivotBarIndex { get; set; }
} }
public class TwoB : Indicator public class TwoB : Indicator
@ -30,9 +32,13 @@ namespace NinjaTrader.NinjaScript.Indicators
private double pivotHigh; private double pivotHigh;
private double pivotLow; private double pivotLow;
private int pivotHighBarIndex;
private int pivotLowBarIndex;
private double mostRecentPivotHigh; private double mostRecentPivotHigh;
private double mostRecentPivotLow; private double mostRecentPivotLow;
private int mostRecentPivotHighBarIndex;
private int mostRecentPivotLowBarIndex;
private double twoBUpLevel; private double twoBUpLevel;
private int twoBUpStartBarIndex; private int twoBUpStartBarIndex;
@ -61,6 +67,7 @@ namespace NinjaTrader.NinjaScript.Indicators
PivotPeriod = 10; PivotPeriod = 10;
TwoBUpStroke = new Stroke(Brushes.LimeGreen, 3); TwoBUpStroke = new Stroke(Brushes.LimeGreen, 3);
TwoBDownStroke = new Stroke(Brushes.Red, 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. // For use during backtesting in order to limit the number of patterns processed.
MaxPatterns = 0; MaxPatterns = 0;
@ -88,14 +95,21 @@ namespace NinjaTrader.NinjaScript.Indicators
if (pivot != null) if (pivot != null)
{ {
if (pivot.IsPivotHigh) if (pivot.IsPivotHigh)
{
pivotHigh = pivot.Level; pivotHigh = pivot.Level;
pivotHighBarIndex = pivot.StartBarIndex;
}
else if (pivot.IsPivotLow) else if (pivot.IsPivotLow)
{
pivotLow = pivot.Level; pivotLow = pivot.Level;
pivotLowBarIndex = pivot.StartBarIndex;
}
} }
if (Open[0] < pivotHigh && High[0] > pivotHigh && mostRecentPivotHigh != pivotHigh) if (Open[0] < pivotHigh && High[0] > pivotHigh && mostRecentPivotHigh != pivotHigh)
{ {
mostRecentPivotHigh = pivotHigh; mostRecentPivotHigh = pivotHigh;
mostRecentPivotHighBarIndex = pivotHighBarIndex;
twoBDownLevel = Low[0]; twoBDownLevel = Low[0];
twoBDownStartBarIndex = CurrentBar; twoBDownStartBarIndex = CurrentBar;
@ -103,6 +117,7 @@ namespace NinjaTrader.NinjaScript.Indicators
else if (Open[0] > pivotLow && Low[0] < pivotLow && mostRecentPivotLow != pivotLow) else if (Open[0] > pivotLow && Low[0] < pivotLow && mostRecentPivotLow != pivotLow)
{ {
mostRecentPivotLow = pivotLow; mostRecentPivotLow = pivotLow;
mostRecentPivotLowBarIndex = pivotLowBarIndex;
twoBUpLevel = High[0]; twoBUpLevel = High[0];
twoBUpStartBarIndex = CurrentBar; twoBUpStartBarIndex = CurrentBar;
@ -116,7 +131,9 @@ namespace NinjaTrader.NinjaScript.Indicators
Level = twoBUpLevel, Level = twoBUpLevel,
Is2BUp = true, Is2BUp = true,
StartBarIndex = twoBUpStartBarIndex, StartBarIndex = twoBUpStartBarIndex,
EndBarIndex = twoBUpEndBarIndex EndBarIndex = twoBUpEndBarIndex,
PivotBarIndex = mostRecentPivotLowBarIndex,
PivotLevel = mostRecentPivotLow,
}; };
twoBs.Add(twoBUp); twoBs.Add(twoBUp);
CompletedPattern[0] = twoBUp; CompletedPattern[0] = twoBUp;
@ -132,7 +149,9 @@ namespace NinjaTrader.NinjaScript.Indicators
Level = twoBDownLevel, Level = twoBDownLevel,
Is2BDown = true, Is2BDown = true,
StartBarIndex = twoBDownStartBarIndex, StartBarIndex = twoBDownStartBarIndex,
EndBarIndex = twoBDownEndBarIndex EndBarIndex = twoBDownEndBarIndex,
PivotBarIndex = mostRecentPivotHighBarIndex,
PivotLevel = mostRecentPivotHigh,
}; };
twoBs.Add(twoBDown); twoBs.Add(twoBDown);
CompletedPattern[0] = twoBDown; CompletedPattern[0] = twoBDown;
@ -155,6 +174,9 @@ namespace NinjaTrader.NinjaScript.Indicators
var stroke = twoB.Is2BUp ? TwoBUpStroke : TwoBDownStroke; var stroke = twoB.Is2BUp ? TwoBUpStroke : TwoBDownStroke;
DrawHorizontalLine(startX, endX, chartScale.GetYByValue(twoB.Level), stroke); 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)] [Display(Name = "2B Down Stroke", GroupName = "2B", Order = 3)]
public Stroke TwoBDownStroke { get; set; } public Stroke TwoBDownStroke { get; set; }
[Display(Name = "Pivot Stroke", GroupName = "2B", Order = 4)]
public Stroke PivotStroke { get; set; }
[Browsable(false)] [Browsable(false)]
public int MaxPatterns { get; set; } public int MaxPatterns { get; set; }
} }