Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Rectangle code causes error within strategy

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

    Rectangle code causes error within strategy

    I have the following code inside my indicator, inside OnBarUpdate

    Code:
    Rectangle myRect = Draw.Rectangle(this, "Rectangle" + CurrentBar, false, StartDateTime, High, EndDateTime, Low, Brushes.Silver, Brushes.Silver, 20);
    
    myRect.OutlineStroke = new Stroke(Brushes.Silver, 1);​
    When I embed this indicator inside my strategy I found I'm getting an error: "object reference not set to an instance of an object " and it ruins the strategy.

    It's this line that's causing the issue: myRect.OutlineStroke = new Stroke(Brushes.Silver, 1);​

    removing it solves the problem. What should I have done here?



    #2
    Hello several,

    The error is indicating a variable (object reference) has a null value (not set to instance of an object).
    https://ninjatrader.com/support/helpGuides/nt8/checking_for_null_references.htm?_gl=1*9tfmab*_gcl _au*MTEwNDE0ODg5Ni4xNzQxMTc5ODgz


    There are a few variables being supplied to the Draw.Rectangle() call which might be null.
    The myRect variable might be null if the Draw.Rectangle() call failed.

    For the start price (startY) and end price (endY) use High[0] and Low[0].
    I'm not certain how this was able to compile at all when using High and Low, as the full series would be the wrong object type (a double is required).
    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.


    Above the Draw.Rectangle() call try printing StartDateTime and EndDateTime to ensure these have values greater than the first bar (Time[CurrentBar]).
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi, I'm using doubles for the High Low vars passed to the rectangle.

      My Print:

      StartDateTime = 24/03/2025 09:30:00
      EndDateTime = 24/03/2025 09:50:00
      CurrentTime = 23/03/2025 18:01:00
      Indicator 'OpeningRange2': Error on calling 'OnBarUpdate' method on bar 949: Object reference not set to an instance of an object.​

      This error does not occur when the indicator is run on it's own. Only within a stratgegy inside strategy analyzer. But it seems to be the line of code with the stroke that causes the error. This line

      myRect.OutlineStroke = new Stroke(Brushes.Silver, 1);​

      I remove this line and no error. ​

      Comment


        #4
        Hello several,

        The High and Low are variables that hold series objects that are part of the base class of Strategy (or Indicator).

        You cannot add your own variables with the same name as the properties provided by the base class as this causes ambiguity.

        High is a Series<double> object. High[0] is a double object. By using an index this specifies a specific bar in the series. By not using an index this references the entire series not a specific bar value.
        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.

        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.


        If you have added your own High and Low variables, you need to rename them to have a new unique name that isn't already used.

        That said, is '24/03/2025 09:30:00' greater than the time of CurrentBar 0?
        Is '24/03/2025 09:50:00' greater than the time of CurrentBar 0?

        The line you are specifying is trying to use the myRect variable. If the Draw tool method call failed, it will return a null.
        If you try and use that variable when it is null, it will result in an 'object reference not set to an instance of an object' error.

        Something you are passing to the Draw.Rectangle() call is invalid which is resulting in the draw method call failing which causes the myRect variable to be assigned null which results in the error when trying to use that variable.

        Now that you have stated that 'I'm using doubles for the High Low vars passed to the rectangle.' this is now where my suspicion is.

        High and Low are series not doubles. High[0] and Low[0] are doubles.
        Did you define your own High and Low variables?

        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Sorry, I didn't mean to confuse but I have unique variable names for the High and Low. ORHigh and ORLow.

          At 9:50 I draw a rectangle around the bars from 9:30 to 9:50.

          The indicator works fine, on it's own. Just when I enter it in a strategy I get this error.
          Last edited by several; 03-25-2025, 01:58 PM.

          Comment


            #6
            Hello several,

            Are you providing different code than what is actually being run in the script?

            The specifics do make a difference.

            Is the indicator setting a plot value and the strategy using the plot value? (calling series values causes OnBarUpdate() to run in the indicator)
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              This is exactly the code:

              Code:
                              Rectangle myRect = Draw.Rectangle(this, "OR Rectangle" + CurrentBar, false, StartDateTime, ORHigh, EndDateTime, ORLow, Brushes.Silver, Brushes.Silver, 20);
                              myRect.OutlineStroke = new Stroke(Brushes.Silver, 1);    ​
              I'm not using any plot. I simply seek to use ORHigh / ORLow exposed to the strategy. It works if I remove the line relating to the stroke.

              Comment


                #8
                Hello several,

                Add a plot with AddPlot() even if you do not plan to use it.

                Set any value to the plot (like CurrentBar or 0) by assigning Value[0] to something.


                Call the plot value from the strategy. (For example assign the plot value to a variable and just don't use the variable)

                This should trigger OnBarUpdate() to run internally in the indicator to call the draw methods in the correct sequence.

                Again, you will want to ensure that any date used for the start and end dates has a date and time greater than bar 0.

                Let me know if this does not correct the error.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  I tried this but I must be doing something wrong. I get now 2 errors.

                  In the indicator:

                  Code:
                   // State.Configure
                   AddPlot(Brushes.Blue, "SignalPlot");
                  
                  //OnBarUpdate
                              if (
                                  (Times[1][0] == StartDateTime)
                                  && (BarsInProgress == 1)
                                  )
                              {
                                                  SignalPlot[0] = CurrentBar;
                  }
                  
                  // properties
                          [Browsable(false)]
                          [XmlIgnore]
                          public Series<double> SignalPlot
                          { get { return Values[0]; }}​​
                  
                  
                  ​
                  Now in the strategy:

                  Code:
                              if (
                                  ((Times[0][0].TimeOfDay == EndTime.TimeOfDay))
                                  )
                              {
                                  varplot = OR2.SignalPlot[0];
                  //Draw rectangle code here
                              }    ​
                  I got both the original error plus an index invalid error:

                  Code:
                  Indicator 'OpeningRange2': Error on calling 'OnBarUpdate' method on bar 949: Object reference not set to an instance of an object.
                  Strategy 'StratOpeningRange': Error on calling 'OnBarUpdate' method on bar 1378: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
                  ​
                  ​
                  And FYI this is how I'm getting the start/end times to draw the rectangle in the indicator:

                  Code:
                  //set defaults
                                  Time1                                        = DateTime.Parse("09:30", System.Globalization.CultureInfo.InvariantCulture);
                                  Time2                                        = DateTime.Parse("09:50", System.Globalization.CultureInfo.InvariantCulture);
                  ​
                  //onbarupdate
                  
                              StartDateTime = new DateTime(Times[1][0].Year, Times[1][0].Month, Times[1][0].Day, Time1.Hour, Time1.Minute, 0);
                              EndDateTime = new DateTime(Times[1][0].Year, Times[1][0].Month, Times[1][0].Day, Time2.Hour, Time2.Minute, 0);    ​
                  The data series 1 is simply a 1 minute series.
                  Last edited by several; 03-25-2025, 04:59 PM.

                  Comment


                    #10
                    Hello several,

                    Remove the conditions.

                    Assign the SignalPlot[0] on every bar update without a condition in the indicator.

                    Call the SignalPlot[0] on every bar update without a condition in the strategy.

                    Temporarily comment out the Draw tool method call.

                    Confirm the error no longer occurs.
                    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