Resolve issue where patterns were temporarily removed from the chart until the beginning of a new bar and another issue with respect to concurrent modification of the detected patterns

This commit is contained in:
moshferatu 2024-06-14 06:04:26 -07:00
parent 82fec0811b
commit c3b61fafb1

View File

@ -40,8 +40,6 @@ namespace NinjaTrader.NinjaScript.Indicators
private List<Pattern> detectedPatterns = new List<Pattern>();
private int barLookback;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
@ -62,9 +60,6 @@ namespace NinjaTrader.NinjaScript.Indicators
BullishLevelStroke = new Stroke(Brushes.LimeGreen, DashStyleHelper.Solid, 2);
BearishLevelStroke = new Stroke(Brushes.Red, DashStyleHelper.Solid, 2);
}
else if (State == State.Configure)
{
}
if (State == State.DataLoaded)
{
DetectedPatterns = new Series<Pattern>(this);
@ -76,11 +71,10 @@ namespace NinjaTrader.NinjaScript.Indicators
}
protected override void OnBarUpdate()
{
{
if (CurrentBar < BarLookback + 1)
return;
// Real-time processing.
for (int i = detectedPatterns.Count - 1; i >= 0; i--)
{
var pattern = detectedPatterns[i];
@ -95,10 +89,6 @@ namespace NinjaTrader.NinjaScript.Indicators
}
}
// Bar close processing.
if (!IsFirstTickOfBar)
return;
DetectedPatterns[0] = null;
bool isSwingHigh = true, isSwingLow = true;
@ -161,7 +151,7 @@ namespace NinjaTrader.NinjaScript.Indicators
{
base.OnRender(chartControl, chartScale);
foreach (var pattern in detectedPatterns)
foreach (var pattern in detectedPatterns.ToArray())
{
int startX = chartControl.GetXByBarIndex(ChartBars, pattern.StartBarIndex);
int endX = pattern.EndBarIndex == 0 ? ChartPanel.X + ChartPanel.W : chartControl.GetXByBarIndex(ChartBars, pattern.EndBarIndex);
@ -190,11 +180,7 @@ namespace NinjaTrader.NinjaScript.Indicators
[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 { return barLookback; }
set { barLookback = Math.Max(1, value); } // Ensure the value is at least 1
}
public int BarLookback { get; set; }
[NinjaScriptProperty]
[XmlIgnore]