Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Drawing rectangle at the bottom of the chart

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

    Drawing rectangle at the bottom of the chart

    Hi,

    I am trying to create a rectangle at the bottom of the chart (it's part of a an indicator) but I don't see the rectangle on the chart although the script is compiled without errors:

    protected override void OnBarUpdate()
    {

    Draw.Rectangle(this, "Rectangle1", 10, Low[10] - TickSize, 5, High[5] + TickSize, Brushes.Blue);

    }​

    What is wrong with this script?
    Thanks in advance!
    jpapa
    NinjaTrader Ecosystem Vendor - jpapa

    #2
    Hello jpapa,

    When you say the bottom of the chart what do you mean?

    The drawing objects are locked to bars so they should be shown at specific prices and related to the bars ago used.

    Comment


      #3
      Hello Jesse,

      basically I want it below the low of the lowest low of the visible area of the chart. However my issue at this point isn't the position of the rectange but the fact that the the code above doesn't show any rectange. This is what I am trying to figure out at this point. Any suggestions?
      jpapa
      NinjaTrader Ecosystem Vendor - jpapa

      Comment


        #4
        Hello jpapa,

        I would recommend looking in the drawing tools properties menu to see what objects were drawn. You are drawing the rectangle from 10 bars ago to 5 bars ago so it should be somewhere on the historical portion of the chart.

        Comment


          #5
          Hello Jesse,

          I checked the drawing tools properties menu but there is nothing there. Here's the full code:

          namespace NinjaTrader.NinjaScript.Indicators
          {
          public class EventIndicator : Indicator
          {

          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Name = "Event Indicator";
          IsOverlay = true;
          Calculate = Calculate.OnEachTick;
          }
          else if (State == State.DataLoaded)
          {

          }
          }

          protected override void OnBarUpdate()
          {

          Draw.Rectangle(this, "Rectangle1", 10, Low[10] - TickSize, 5, High[5] + TickSize, Brushes.Blue);

          }
          }



          region Properties

          #endregion

          }​
          jpapa
          NinjaTrader Ecosystem Vendor - jpapa

          Comment


            #6
            Hello jpapa,

            With the code you have you should have a total of 1 object assuming OnBarUpdate was called and the indicator was applied. If you see no objects you likely did not apply the indicator or hit an error.

            To use 10 bars ago you need a CurrentBar check:

            if(CurrentBar < 10) return;

            Comment


              #7
              Thank you Jesse,

              another question. Is a way to have the rectangle printed in the middle at the bottom of the chart like for example the Tick Counter is always printed on the bottom right of the chart?
              jpapa
              NinjaTrader Ecosystem Vendor - jpapa

              Comment


                #8
                Hello jpapa,

                The text can be placed in corners of the chart using Draw.TextFixed, there is not an equivalent drawing object that would let you place an object in a specific part of the chart, they are linked to bars and prices. To place a rendering in the middle bottom of the chart would require using OnRender instead of using drawing tools.

                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.

                Comment


                  #9
                  Thank you Jesse!
                  jpapa
                  NinjaTrader Ecosystem Vendor - jpapa

                  Comment


                    #10
                    My goal is to create a script that will be dispaying a text message when an event is approaching. Initially I wanted to have orange and red circles/rectangles displayed but since you said that this isn't possible to be done at a a fixed position on the chart I replaced it with a text.

                    I created the code below but I can't see the text messages. Any suggestion of what might be wrong with the script?

                    region Using declarations
                    using System;
                    using System.Collections.Generic;
                    using System.ComponentModel.DataAnnotations;
                    using NinjaTrader.Gui.Tools;
                    using NinjaTrader.NinjaScript;
                    using NinjaTrader.NinjaScript.DrawingTools;
                    using System.Windows.Media;
                    #endregion

                    namespace NinjaTrader.NinjaScript.Indicators
                    {
                    public class EventIndicator : Indicator
                    {
                    private List<EventData> eventList = new List<EventData>();

                    protected override void OnStateChange()
                    {
                    if (State == State.SetDefaults)
                    {
                    Name = "Event Indicator";
                    IsOverlay = true;
                    Calculate = Calculate.OnEachTick;

                    // Default settings for 8 events
                    Event1Time = new TimeSpan(10, 0, 0);
                    Event1Importance = "High";

                    Event2Time = new TimeSpan(11, 30, 0);
                    Event2Importance = "Medium";

                    Event3Time = new TimeSpan(14, 0, 0);
                    Event3Importance = "High";

                    Event4Time = new TimeSpan(15, 15, 0);
                    Event4Importance = "Medium";

                    Event5Time = new TimeSpan(16, 45, 0);
                    Event5Importance = "High";

                    Event6Time = new TimeSpan(18, 0, 0);
                    Event6Importance = "Medium";

                    Event7Time = new TimeSpan(19, 30, 0);
                    Event7Importance = "High";

                    Event8Time = new TimeSpan(21, 0, 0);
                    Event8Importance = "Medium";
                    }
                    else if (State == State.DataLoaded)
                    {
                    ParseEvents();
                    }
                    }

                    protected override void OnBarUpdate()
                    {
                    DateTime currentTime = Time[0];
                    string displayText = string.Empty;
                    Brush textColor = Brushes.Transparent;

                    foreach (var evt in eventList)
                    {
                    if (currentTime >= evt.StartTime && currentTime <= evt.EndTime)
                    {
                    // Determine the message and color based on importance
                    displayText = evt.Importance == "High" ? "High Importance" : "Medium Importance";
                    textColor = evt.Importance == "High" ? Brushes.Red : Brushes.Orange;
                    break; // Only display one active event at a time
                    }
                    }

                    // Update the text display on the chart
                    if (!string.IsNullOrEmpty(displayText))
                    {
                    Draw.TextFixed(this, "EventMessage", displayText, TextPosition.BottomRight, textColor,
                    new SimpleFont("Arial", 16), Brushes.Transparent, Brushes.Transparent, 0);
                    }
                    else
                    {
                    RemoveDrawObject("EventMessage"); // Clear the message if no events are active
                    }
                    }

                    private void ParseEvents()
                    {
                    // Clear previous events
                    eventList.Clear();

                    // Add events to the list
                    AddEvent(Event1Time, Event1Importance);
                    AddEvent(Event2Time, Event2Importance);
                    AddEvent(Event3Time, Event3Importance);
                    AddEvent(Event4Time, Event4Importance);
                    AddEvent(Event5Time, Event5Importance);
                    AddEvent(Event6Time, Event6Importance);
                    AddEvent(Event7Time, Event7Importance);
                    AddEvent(Event8Time, Event8Importance);
                    }

                    private void AddEvent(TimeSpan eventTime, string importance)
                    {
                    // Use the date from the current bar for the trading session
                    DateTime sessionDate = Time[0].Date;
                    DateTime eventDateTime = sessionDate + eventTime;

                    eventList.Add(new EventData
                    {
                    EventTime = eventDateTime,
                    StartTime = eventDateTime.AddHours(-1), // Show message 1 hour before
                    EndTime = eventDateTime.AddMinutes(1), // Remove message 1 min after
                    Importance = importance
                    });
                    }


                    region Properties
                    [NinjaScriptProperty]
                    [Display(Name = "Event 1 Time", Description = "Time for Event 1 (HH:MM)", Order = 1, GroupName = "Events")]
                    public TimeSpan Event1Time { get; set; }

                    [NinjaScriptProperty]
                    [Display(Name = "Event 1 Importance", Description = "Importance of Event 1", Order = 2, GroupName = "Events")]
                    public string Event1Importance { get; set; }

                    [NinjaScriptProperty]
                    [Display(Name = "Event 2 Time", Description = "Time for Event 2 (HH:MM)", Order = 3, GroupName = "Events")]
                    public TimeSpan Event2Time { get; set; }

                    [NinjaScriptProperty]
                    [Display(Name = "Event 2 Importance", Description = "Importance of Event 2", Order = 4, GroupName = "Events")]
                    public string Event2Importance { get; set; }

                    [NinjaScriptProperty]
                    [Display(Name = "Event 3 Time", Description = "Time for Event 3 (HH:MM)", Order = 5, GroupName = "Events")]
                    public TimeSpan Event3Time { get; set; }

                    [NinjaScriptProperty]
                    [Display(Name = "Event 3 Importance", Description = "Importance of Event 3", Order = 6, GroupName = "Events")]
                    public string Event3Importance { get; set; }

                    [NinjaScriptProperty]
                    [Display(Name = "Event 4 Time", Description = "Time for Event 4 (HH:MM)", Order = 7, GroupName = "Events")]
                    public TimeSpan Event4Time { get; set; }

                    [NinjaScriptProperty]
                    [Display(Name = "Event 4 Importance", Description = "Importance of Event 4", Order = 8, GroupName = "Events")]
                    public string Event4Importance { get; set; }

                    [NinjaScriptProperty]
                    [Display(Name = "Event 5 Time", Description = "Time for Event 5 (HH:MM)", Order = 9, GroupName = "Events")]
                    public TimeSpan Event5Time { get; set; }

                    [NinjaScriptProperty]
                    [Display(Name = "Event 5 Importance", Description = "Importance of Event 5", Order = 10, GroupName = "Events")]
                    public string Event5Importance { get; set; }

                    [NinjaScriptProperty]
                    [Display(Name = "Event 6 Time", Description = "Time for Event 6 (HH:MM)", Order = 11, GroupName = "Events")]
                    public TimeSpan Event6Time { get; set; }

                    [NinjaScriptProperty]
                    [Display(Name = "Event 6 Importance", Description = "Importance of Event 6", Order = 12, GroupName = "Events")]
                    public string Event6Importance { get; set; }

                    [NinjaScriptProperty]
                    [Display(Name = "Event 7 Time", Description = "Time for Event 7 (HH:MM)", Order = 13, GroupName = "Events")]
                    public TimeSpan Event7Time { get; set; }

                    [NinjaScriptProperty]
                    [Display(Name = "Event 7 Importance", Description = "Importance of Event 7", Order = 14, GroupName = "Events")]
                    public string Event7Importance { get; set; }

                    [NinjaScriptProperty]
                    [Display(Name = "Event 8 Time", Description = "Time for Event 8 (HH:MM)", Order = 15, GroupName = "Events")]
                    public TimeSpan Event8Time { get; set; }

                    [NinjaScriptProperty]
                    [Display(Name = "Event 8 Importance", Description = "Importance of Event 8", Order = 16, GroupName = "Events")]
                    public string Event8Importance { get; set; }
                    #endregion

                    region Helper Classes
                    private class EventData
                    {
                    public DateTime EventTime { get; set; }
                    public DateTime StartTime { get; set; }
                    public DateTime EndTime { get; set; }
                    public string Importance { get; set; }
                    }
                    #endregion
                    }
                    }​
                    jpapa
                    NinjaTrader Ecosystem Vendor - jpapa

                    Comment


                      #11
                      Hello jpapa,

                      You will need to use Print statements to trace how that logic is working to get a better idea of what's happening. Just looking at the code you won't be able to tell what it actually does when running, the Print statements will allow you to trace how it logically works so you know where the problem is.

                      Comment


                        #12
                        Thank you, I will do that.
                        jpapa
                        NinjaTrader Ecosystem Vendor - jpapa

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        556 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