Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Display a Value from an Indicator at the Top Left corner of the NQ Chart

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

    Display a Value from an Indicator at the Top Left corner of the NQ Chart

    Hello Forum,

    Hoping to get some help in Indicator Development. I'm still learning Ninjascript and couldn't quite figure how to do what I want - which is to display the ATR(8) value of NQ on the top left corner of my chart. Like this: ATR(8): ATR Value
    If someone here can point me to an indicator example which does this, it would be greatly appreciated or if you can show me how to do it even better.
    This will teach me two things: 1) How to display text and values on your chart; and 2) how to insert an existing NT8 indicator (in this case ATR) into a custom indicator and display it's value.
    Thank you so much!

    #2
    Hello,

    To create an indicator that displays the ATR(8) value of NQ (or any instrument) on the top left corner of your chart in NinjaTrader, follow these steps:
    1. Create a new custom indicator.
    2. Add the ATR indicator to your custom indicator.
    3. Draw the ATR value on the chart.

    Here’s a step-by-step example to achieve this: Step 1: Create a New Custom Indicator
    1. Open NinjaTrader.
    2. Go to New > NinjaScript Editor.
    3. In the NinjaScript Editor, right-click and select New Indicator.
    4. Name the indicator (e.g., ATRDisplay).
    Step 2: Add the ATR Indicator and Draw the Value


    Replace the generated code in the new indicator with the following code:

    Code:
    #region Using declarations
    using System;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript.StrategyAnalyzerColumns;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.NinjaScript.Strategies;
    using NinjaTrader.NinjaScript.Indicators;
    #endregion
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class ATRDisplay : Indicator
        {
            private ATR atrIndicator;
            private int atrPeriod = 8;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description = @"Displays the ATR(8) value on the top left corner of the chart.";
                    Name = "ATRDisplay";
                    Calculate = Calculate.OnEachTick;
                    IsOverlay = true; // Ensures the indicator is drawn on the price panel
                    AddPlot(Brushes.Transparent, "ATR"); // Hidden plot just to satisfy the indicator requirement
                }
                else if (State == State.DataLoaded)
                {
                    // Initialize the ATR indicator with the specified period
                    atrIndicator = ATR(atrPeriod);
                }
            }
    
            protected override void OnBarUpdate()
            {
                // Ensure there is enough data to calculate the ATR
                if (CurrentBar < atrPeriod)
                    return;
    
                // Get the current ATR value
                double currentATRValue = atrIndicator[0];
    
                // Draw the ATR value on the chart
                Draw.TextFixed(this, "ATRLabel", $"ATR({atrPeriod}): {currentATRValue:F2}", TextPosition.TopLeft, Brushes.White, new SimpleFont("Arial", 14), Brushes.Transparent, Brushes.Transparent, 0);
            }
    
            #region Properties
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="ATR Period", Description="Period for the ATR calculation", Order=1, GroupName="Parameters")]
            public int AtrPeriod
            {
                get { return atrPeriod; }
                set { atrPeriod = value; }
            }
            #endregion
        }
    }
    ​
    Explanation:
    1. Indicator Setup:
      • OnStateChange sets default properties and initializes the ATR indicator.
    2. Calculating ATR:
      • The ATR indicator is created with a period of 8.
      • atrIndicator[0] gets the current ATR value.
    3. Displaying the ATR Value:
      • Draw.TextFixed is used to display the ATR value on the chart at the top left corner.
      • TextPosition.TopLeft places the text at the top left.
      • The SimpleFont and color settings customize the appearance of the text.
    4. User-Defined Property:
      • The AtrPeriod property allows the ATR period to be set via the indicator’s properties panel.
    Step 3: Compile and Add to Chart
    1. Compile the script by clicking the Compile button in the NinjaScript Editor.
    2. Add the custom indicator to your chart:
      • Right-click on the chart.
      • Select Indicators.
      • Find and add ATRDisplay.
      • Configure the ATR Period if needed.

    This example will display the ATR(8) value on the top left corner of your chart, and you will learn how to insert and display values from an existing indicator in NinjaTrader 8.

    Comment


      #3
      Hi Ryan,
      Firstly thank you so much for the detailed steps outlined above.
      I did the "monkey test" to see if blindly following the instructions worked just by doing

      The first snag I found here:
      1. Open NinjaTrader.
      2. Go to New > NinjaScript Editor.
      3. In the NinjaScript Editor, right-click and select New Indicator. <- in the current NT8 release, you click the + sign on the Tab (not right-click) and that's when you are presented with option to Create a New Indicator.
      4. Name the indicator (e.g., ATRDisplay)
      You have the option of going through the Wizard or select Generate - I selected Generate.

      I then replaced all the code from Line 1 up to #region NinjaScript generated code. Neither change nor remove.

      Then tried to compile the code - it is complaining about:
      ATRDisplay.cs Error: Unexpected character '$" Code: CS1056 Line: 47 Column: 46

      It's this line of code: Draw.TextFixed(this, "ATRLabel", $"ATR({atrPeriod}): {currentATRValue:F2}", TextPosition.TopLeft, Brushes.White, new SimpleFont("Arial", 14), Brushes.Transparent, Brushes.Transparent, 0);

      Comment


        #4
        Hello,

        The error you're encountering is due to the use of string interpolation, which is available in C# 6.0 and later. If NinjaTrader is using an older version of C# that doesn't support string interpolation, you can switch to string concatenation or String.Format instead.

        Here is the corrected code using String.Format:

        Code:
        #region Using declarations
        using System;
        using NinjaTrader.Cbi;
        using NinjaTrader.Gui.Tools;
        using NinjaTrader.NinjaScript;
        using NinjaTrader.Data;
        using NinjaTrader.NinjaScript.StrategyAnalyzerColumns;
        using NinjaTrader.Gui.Chart;
        using NinjaTrader.NinjaScript.Strategies;
        using NinjaTrader.NinjaScript.Indicators;
        #endregion
        
        namespace NinjaTrader.NinjaScript.Indicators
        {
            public class ATRDisplay : Indicator
            {
                private ATR atrIndicator;
                private int atrPeriod = 8;
        
                protected override void OnStateChange()
                {
                    if (State == State.SetDefaults)
                    {
                        Description = @"Displays the ATR(8) value on the top left corner of the chart.";
                        Name = "ATRDisplay";
                        Calculate = Calculate.OnEachTick;
                        IsOverlay = true; // Ensures the indicator is drawn on the price panel
                        AddPlot(Brushes.Transparent, "ATR"); // Hidden plot just to satisfy the indicator requirement
                    }
                    else if (State == State.DataLoaded)
                    {
                        // Initialize the ATR indicator with the specified period
                        atrIndicator = ATR(atrPeriod);
                    }
                }
        
                protected override void OnBarUpdate()
                {
                    // Ensure there is enough data to calculate the ATR
                    if (CurrentBar < atrPeriod)
                        return;
        
                    // Get the current ATR value
                    double currentATRValue = atrIndicator[0];
        
                    // Draw the ATR value on the chart
                    Draw.TextFixed(this, "ATRLabel", String.Format("ATR({0}): {1:F2}", atrPeriod, currentATRValue), TextPosition.TopLeft, Brushes.White, new SimpleFont("Arial", 14), Brushes.Transparent, Brushes.Transparent, 0);
                }
        
                #region Properties
                [NinjaScriptProperty]
                [Range(1, int.MaxValue)]
                [Display(Name="ATR Period", Description="Period for the ATR calculation", Order=1, GroupName="Parameters")]
                public int AtrPeriod
                {
                    get { return atrPeriod; }
                    set { atrPeriod = value; }
                }
                #endregion
            }
        }
        ​
        1. String.Format:
          • The String.Format method is used to create the string with the ATR period and value.
          • String.Format("ATR({0}): {1:F2}", atrPeriod, currentATRValue) is equivalent to the interpolated string $"ATR({atrPeriod}): {currentATRValue:F2}".
        2. Draw.TextFixed:
          • This method draws the text on the chart in the specified position with the given formatting.

        This should resolve the compilation error and allow the code to work as expected in NinjaTrader.

        Comment


          #5
          Hey Ryan - sorry about this. We got past the first compilation error but after correcting that, 4 more errors popped up after an F5. I hope this is it. The error code indicates reference to a missing assembly. Thanks
          Attached Files
          Last edited by aguison; 06-06-2024, 08:10 AM.

          Comment


            #6
            Hi Ryan!
            Good news! I got past the compilation errors - see the screenshot of the ATRDisplay indicator working attached. Like I said, I'm just starting and got lucky - the compile errors was saying it couldn't find the libraries, so I looked up the libraries under my ATR indicator and used those instead of the ones in your code. And there was one declaration from your version which I added to my version. I commented the "using" code lines from yours and replaced them with mine.

            Thanks again for your help on this! Mission accomplished! Because of your help, you taught me how to display Text on my Chart and use an existing NT8 Indicator in a custom indicator.

            Here is the working version of the code:
            region Using declarations
            using System;
            //using NinjaTrader.Cbi;
            using NinjaTrader.Gui.Tools;
            //using NinjaTrader.NinjaScript;
            //using NinjaTrader.Data;
            //using NinjaTrader.NinjaScript.StrategyAnalyzerColumns;
            //using NinjaTrader.Gui.Chart;
            //using NinjaTrader.NinjaScript.Strategies;
            //using NinjaTrader.NinjaScript.Indicators;
            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.Data;
            using NinjaTrader.NinjaScript;
            using NinjaTrader.Core.FloatingPoint;
            using NinjaTrader.NinjaScript.DrawingTools;

            #endregion

            namespace NinjaTrader.NinjaScript.Indicators
            {
            public class ATRDisplay : Indicator
            {
            private ATR atrIndicator;
            private int atrPeriod = 8;

            protected override void OnStateChange()
            {
            if (State == State.SetDefaults)
            {
            Description = @"Displays the ATR(8) value on the top left corner of the chart.";
            Name = "ATRDisplay";
            Calculate = Calculate.OnEachTick;
            IsOverlay = true; // Ensures the indicator is drawn on the price panel
            AddPlot(Brushes.Transparent, "ATR"); // Hidden plot just to satisfy the indicator requirement
            }
            else if (State == State.DataLoaded)
            {
            // Initialize the ATR indicator with the specified period
            atrIndicator = ATR(atrPeriod);
            }
            }

            protected override void OnBarUpdate()
            {
            // Ensure there is enough data to calculate the ATR
            if (CurrentBar < atrPeriod)
            return;

            // Get the current ATR value
            double currentATRValue = atrIndicator[0];

            // Draw the ATR value on the chart
            Draw.TextFixed(this, "ATRLabel", String.Format("ATR({0}): {1:F2}", atrPeriod, currentATRValue), TextPosition.TopLeft, Brushes.Black, new SimpleFont("Arial", 14), Brushes.Transparent, Brushes.Transparent, 0);
            }

            region Properties
            [Range(1, int.MaxValue), NinjaScriptProperty]
            [Display(ResourceType = typeof(Custom.Resource), Name = "Period", GroupName = "NinjaScriptParameters", Order = 0)]
            public int Period
            { get; set; }
            //[NinjaScriptProperty]
            //[Range(1, int.MaxValue)]
            //[Display(Name="ATR Period", Description="Period for the ATR calculation", Order=1, GroupName="Parameters")]
            //public int AtrPeriod
            //{
            // get { return atrPeriod; }
            // set { atrPeriod = value; }
            //}
            #endregion
            }
            }

            Attached Files
            Last edited by aguison; 06-06-2024, 10:00 PM.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            580 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            335 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            102 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            554 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            552 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X