Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Fixing issue with Brushes

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Fixing issue with Brushes

    Hello,
    I'm getting following error in lines 23 and 57 "The name 'Brushes' does not exist in the current context". Could you please point me in the direction what I'm doing wrong here?

    Code:
    #region Using declarations
    using System;
    using NinjaTrader.Gui.Chart;  
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Tools;
    using SharpDX;
    using SharpDX.DirectWrite;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class BarRangeBodyRatio : Indicator
        {
            private SimpleFont textFont;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description = "Displays the ratio of bar range to bar body size in the top right corner.";
                    AddPlot(Brushes.Transparent, "Ratio");
                }
                else if (State == State.Configure)
                {
                    textFont = new SimpleFont("Arial", 14) { Bold = true };  
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (CurrentBar < 1)
                    return;
    
                double barRange = High[0] - Low[0];
                double bodySize = Math.Abs(Close[0] - Open[0]);
                double ratio = bodySize > 0 ? barRange / bodySize : 0;
    
                Values[0][0] = ratio;
            }
    
            protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
            {
                base.OnRender(chartControl, chartScale);
    
                if (Bars == null || Bars.Count < 1 || chartControl == null)
                    return;
    
                string text = $"Range/Body: {Values[0][0]:0.00}";
    
                // Set the position for top-right corner display
                float xPos = chartControl.CanvasRight - 150;  // Position from right edge
                float yPos = 10;  // Position from top
    
                // Draw text on the chart
                RenderTarget.DrawText(text, textFont, new SharpDX.RectangleF(xPos, yPos, 200, 50), Brushes.Black.ToDxBrush(RenderTarget));
            }
        }
    }​

    #2
    Hello Ludwik,

    You've removed the necessary default using statements.

    Create a new Indicator using the NinjaScript Editor. Copy the default using statements from the top of the script to your original script. The delete the new script.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I did that but now I'm getting following error: Click image for larger version

Name:	Grid.png
Views:	494
Size:	9.8 KB
ID:	1331821Here is the code:

      Code:
      #region Using declarations
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.ComponentModel.DataAnnotations;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows;
      using System.Windows.Input;
      using System.Windows.Media;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Gui;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Gui.SuperDom;
      using NinjaTrader.Gui.Tools;
      using NinjaTrader.Data;
      using NinjaTrader.NinjaScript;
      using NinjaTrader.Core.FloatingPoint;
      using NinjaTrader.NinjaScript.DrawingTools;
      #endregion
      
      //This namespace holds Indicators in this folder and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.Indicators
      {
          public class BarRangeBodyRatio : Indicator
          {
              private SimpleFont textFont;
      
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description = "Displays the ratio of bar range to bar body size in the top right corner.";
                      AddPlot(Brushes.Transparent, "Ratio");
                  }
                  else if (State == State.Configure)
                  {
                      textFont = new SimpleFont("Arial", 14) { Bold = true };  
                  }
              }
      
              protected override void OnBarUpdate()
              {
                  if (CurrentBar < 1)
                      return;
      
                  double barRange = High[0] - Low[0];
                  double bodySize = Math.Abs(Close[0] - Open[0]);
                  double ratio = bodySize > 0 ? barRange / bodySize : 0;
      
                  Values[0][0] = ratio;
              }
      
              protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
              {
                  base.OnRender(chartControl, chartScale);
      
                  if (Bars == null || Bars.Count < 1 || chartControl == null)
                      return;
      
                  string text = $"Range/Body: {Values[0][0]:0.00}";
      
                  // Set the position for top-right corner display
                  float xPos = chartControl.CanvasRight - 150;  // Position from right edge
                  float yPos = 10;  // Position from top
      
                  // Draw text on the chart
                  RenderTarget.DrawText(text, textFont, new SharpDX.RectangleF(xPos, yPos, 200, 50), Brushes.Black.ToDxBrush(RenderTarget));
              }
          }
      }​
      Attached Files

      Comment


        #4
        Hello Ludwik,

        The textFont variable you have is a SimpleFont object type and not a TextFormat. SimpleFont is used for Drawing tool methods like Draw.Text() but is not used in SharpDX rendering.

        RenderTarget.DrawText(string text, TextFormat textFormat, RectangleF layoutRect, Brush defaultForegroundBrush)
        Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.


        You will need to supply a SharpDX.DirectWrite.TextFormat to the RenderTarget.DrawText() method.

        The SampleCustomRender indicator included with NinjaTrader demonstrates this on lines 250 - 256.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        558 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        324 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        545 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        547 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X