Silent bug in code

I have a method that works via a mouse click. However it ONLY produces text the second time I use it - the first time i get everything except the text. First time I run through it I get no text printed. I get the lines but no text. When I debug and print the text there are no errors and the text strings are valid and complete???

I am at a complete loss with this one. I have tried try/catch and i get no captures.

			#region DrawMaxExcursions
	void DrawMaxExcursions(ChartScale chartScale, float labelStartX)
	{
		try
		{
	    if (myEntry == 0 || TickSize <= 0 )
	        return;
	
	    void DrawSingleExcursion(double excursionTicks, double excursionPrice, SharpDX.Direct2D1.Brush brush)
	    {
	        if (excursionTicks == 0)
	            return;
	
	        float y = chartScale.GetYByValue(excursionPrice);
	        float offsetX = labelStartX - labelOffsetX;
	
	        double rValue = (excursionTicks * TickSize) / R;
	        string line1 = $"{rValue:0.0R}";         // R-multiple
	        string line2 = $"{excursionTicks:0}";   // Ticks
		
	        var fontCollection = Globals.DirectWriteFactory.GetSystemFontCollection(false);
	        using var textFormat = new SharpDX.DirectWrite.TextFormat(
	            Globals.DirectWriteFactory,
	            "Segoe UI",
	            fontCollection,
	            SharpDX.DirectWrite.FontWeight.Normal,
	            SharpDX.DirectWrite.FontStyle.Normal,
	            SharpDX.DirectWrite.FontStretch.Normal,
	            12f,
	            "en-us");
	
	        using var layout1 = new SharpDX.DirectWrite.TextLayout(Globals.DirectWriteFactory, line1, textFormat, 100f, 20f);
	        using var layout2 = new SharpDX.DirectWrite.TextLayout(Globals.DirectWriteFactory, line2, textFormat, 100f, 20f);
	
	// Y-positions
	float line1Y = y - layout1.Metrics.Height;
	float line2Y = y + 2f; // Slight padding between lines
	
	// X-centering
	float line1X = offsetX;
	float line2X = offsetX + (layout1.Metrics.Width - layout2.Metrics.Width) / 2f;
		Print("Line 1 = "+line1);
			Print("Line2 = "+line2);
	RenderTarget.DrawTextLayout(new SharpDX.Vector2(line1X, line1Y), layout1, brush);
	RenderTarget.DrawTextLayout(new SharpDX.Vector2(line2X, line2Y), layout2, brush);
	
	
	        RenderTarget.DrawLine(
	            new SharpDX.Vector2(singleTickStart - labelOffsetX, y),
	            new SharpDX.Vector2(singleTickEnd - labelOffsetX, y),
	            brush,
	          5f);
	    }
	using var brushGold  = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget,SharpDX.Color.Gold);
	using var brushPink  = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget,SharpDX.Color.HotPink);
	    // Draw MFE (Gold) and MAE (Fuchsia)
	    DrawSingleExcursion(myExcursion.MaxMFE, myExcursion.BestPriceReached, brushGold);
	    DrawSingleExcursion(myExcursion.MaxMAE, myExcursion.WorstPriceReached, brushPink);
		}
			    catch (Exception ex)
    {
        Print($"[Exception] {ex.Message}\n{ex.StackTrace}");
    }
	}
	
	
	#endregion

Just a guess: text coordinates are off screen.

1 Like

Maybe instead of just debugging with text… try attaching Ninja to Visual Studio and step through the code.

I would like to use Visual Studio but I have absolutely no idea how to use it - thinking of taking an on line lesson as it would appear the next stage in my development. By the way ChatGPT couldn’t solve this one either which is fairly unusual.

It’s just an IDE and a million times better than using the NinjaScript Editor. You just need to make sure the NinjaScript Editor is up still and it compiles when you save in Visual Studio. AI can’t solve every coding issue. It seems that whatever issue you have, you need to step through the code and see what’s happening.

https://ninjatrader.com/support/helpguides/nt8/NT%20HelpGuide%20English.html?visual_studio_debugging.htm

After you describe what the issue is (do not make assumptions or suggestions what the problem is if you are not sure) ask AI to add debug to console. Paste console output to AI.