I'm facing performance issues with my `OnRender` implementation in NinjaTrader, specifically when scrolling/zooming through the chart. The performance noticeably degrades, and I'm trying to understand the root cause.
I've implemented a dictionary to map bar indexes to pattern IDs, which I then iterate through in `OnRender` to render pattern IDs relative to their candles. Here's a simplified version of my approach:
// Dictionary holding bar index and Pattern IDs
private Dictionary<int, List<int>> erkannteMusterProIndex_Long = new Dictionary<int, List<int>>();
// OnRender implementation
protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
{
base.OnRender(chartControl, chartScale);
int fromIndex = ChartBars.FromIndex;
int toIndex = ChartBars.ToIndex;
// Durchlaufe alle Einträge im Dictionary
foreach (var entry in erkannteMusterProIndex_Long.Where(e => e.Key >= toIndex -25 && e.Key <= toIndex))
{
int barIndex = entry.Key; // Der Index der Kerze
List<int> musterListe = entry.Value; // Die Liste der Muster für diese Kerze
// Schriftart und Stil
SharpDX.DirectWrite.TextFormat textFormat = new SharpDX.DirectWrite.TextFormat(
NinjaTrader.Core.Globals.DirectWriteFactory,
"Arial",
SharpDX.DirectWrite.FontWeight.Bold, // Hier setzen wir den Text auf fett
SharpDX.DirectWrite.FontStyle.Normal,
14
);
SharpDX.Direct2D1.SolidColorBrush brush = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, SharpDX.Color.Green);
float baseYPosition = 0; // Initialisieren der Basis-Y-Position; // Initialisieren der Basis-Y-Position
for (int i = 0; i < musterListe.Count; i++)
{
int musterNummer = musterListe[i];
string text = musterNummer.ToString();
SharpDX.DirectWrite.TextLayout textLayout = new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Globals.DirectWriteFactory, text, textFormat, 100, 100 );
// Berechne die X-Position der aktuellen Bar im Chart
double barWidth = chartControl.BarWidth;
int xPosition = chartControl.GetXByBarIndex(ChartBars, barIndex) ;
// Berechne die Y-Position für das Zeichnen des Musters, basierend auf dem Low der Kerze plus einem Offset
// Die Verwendung von Low[barIndex] funktioniert in OnRender nicht direkt. Du musst den Wert umrechnen:
if ( i == 0 )
{
baseYPosition = chartScale.GetYByValue(Low.GetValueAt(barIndex)) + 5; // Offset nach unten für jedes Muster
}
float yPosition = baseYPosition + (textLayout.Metrics.Height * i);
// create a rectangle which will automatically resize to the width/height of the textLayout
textLayout.Metrics.Height);
// execute the render target draw rectangle with desired values
SharpDX.Vector2 startPoint = new SharpDX.Vector2(xPosition - 4, yPosition);
// Zeichne den Text bei den ersten sichtbaren Bar-Koordinaten als Beispiel
RenderTarget.DrawTextLayout(startPoint, textLayout, brush);
textLayout.Dispose();
}
// Aufräumen
textFormat.Dispose();
brush.Dispose();
}
}
Is there a more performance-optimized way to manage and render these pattern IDs for visible bars only? How can I improve the efficiency of this rendering logic to mitigate the scrolling/zooming performance impact?
Any insights or suggestions would be greatly appreciated!

Comment