Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Learning how to plot rectangles

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

    Learning how to plot rectangles

    I wanted to learn how to plot rectangles so I went here: https://ninjatrader.com/support/help..._rectangle.htm

    Copied and pasted the code from that example into wizard generated code and no drawing object was visible, also no drawing object in list of drawing objects.

    Can someone please help me understand why nothing is plotting?

    Note that this is wizard-generated code with the example pasted in.

    Code:
    public class MAGZONESv1 : Indicator
    {
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "MAGZONESv1";
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = false;
    DrawVerticalGridLines = false;
    PaintPriceMarkers = true;
    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
    //See Help Guide for additional information.
    IsSuspendedWhileInactive = true;
    MinMagLevel = 5;
    }
    else if (State == State.Configure)
    {
    }
    }
    
    protected override void OnBarUpdate()
    {
    // Draws a blue rectangle from the low 10 bars back to the high of 5 bars back
    Draw.Rectangle(this, "tag1", 10, Low[10] - TickSize, 5, High[5] + TickSize, Brushes.Blue);
    
    // Draws a blue rectangle from the low 10 bars back to the high of 5 bars back with
    // a fill color or pale green with a transparency level of 2
    Draw.Rectangle(this, "tag1", false, 10, Low[10] - TickSize, 5, High[5] + TickSize, Brushes.PaleGreen, Brushes.PaleGreen, 2);
    }
    
    #region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="MinMagLevel", Order=1, GroupName="Parameters")]
    public int MinMagLevel
    { get; set; }
    #endregion
    
    }

    #2
    Hello WalterSkinner,

    Are you ensuring there are 10 bars before calling 10 bars back?

    I am finding this line of code works without issue on my end.

    Below is a link to a video of the test.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello WalterSkinner,

      Are you ensuring there are 10 bars before calling 10 bars back?

      I am finding this line of code works without issue on my end.

      Below is a link to a video of the test.
      https://drive.google.com/file/d/1TcS...w?usp=drivesdk
      No, the code did not check for bars first that did it.

      Thanks for the help.

      Comment


        #4
        Originally posted by NinjaTrader_ChelseaB View Post
        Hello WalterSkinner,

        Are you ensuring there are 10 bars before calling 10 bars back?

        I am finding this line of code works without issue on my end.

        Below is a link to a video of the test.
        https://drive.google.com/file/d/1TcS...w?usp=drivesdk
        Quick follow up question: how can I make these rectangles extend to the right until they are traded through?

        Attached Files

        Comment


          #5
          Hello WalterSkinner,

          On each new bar call the draw method again using the same tag name and use 0 as the end bars ago.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Got it pretty close. What I had to do was create a list for the tags and a list for the CurentBars[0] value (multiple data series). Added to the lists each time a rectangle was drawn, and then looped through each item in the list on each bar to redraw the rectangle using the tag list for the tag and CurrentBars[0]-indexList[i] for the bars back for the high and low.

            Not sure if this is what you meant but the boxes are now drawn very close to how I'd drawn them manually.
            Attached Files

            Comment


              #7
              Originally posted by NinjaTrader_ChelseaB View Post
              Hello WalterSkinner,

              On each new bar call the draw method again using the same tag name and use 0 as the end bars ago.
              This is what I'm using to draw the bars and it works great. When I try to limit the number of rectangles drawn on a given bar, I tried to use a variable in place of the bold code to cap the number of loops to hopefully cap the number of rectangles drawn on each bar, but that did not work.

              How would I limit the number of rectangles?

              Code:
              for(int i=0; i < [B]tags.Count[/B]; i++)
              {
                   barsBack = CurrentBar-indexList[i];
                   Draw.Rectangle(this, tags[i], 0, bottomBoxList[i], barsBack, topBoxList[i], true, "templateName");
              }
              Edit: there's an index out of range error in the log
              Last edited by WalterSkinner; 08-02-2021, 04:42 PM.

              Comment


                #8
                Hello WalterSkinner,

                You can choose to use arrays or lists with your own custom logic.

                Print the tag name tags[i] before calling the drawing object. Are you seeing objects that do not exist?

                You can also loop through the DrawObjects collection if you are moving multiple drawing objects and want to loop through them. The tagNames are available if you want to re-use these and move re-draw the object.
                https://ninjatrader.com/support/help...rawobjects.htm

                What is the invalid index?
                Use print to find the invalid index.

                Is barsBack less than CurrentBar?
                Is i less than indexList.Count()? Is i less than tags.Count() and bottomBoxList.Count() and topBoxList.Count()?

                Below is a link to a forum post that demonstrates how to use prints to understand behavior.
                Last edited by NinjaTrader_ChelseaB; 08-03-2021, 08:08 AM.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9

                  What I ended up doing was removing the 0 position item from every list I was using, just before creating a rectangle.

                  In case anyone comes across this thread with the same issue:

                  Code:
                  //pop out the oldest list item if it's old enough
                  if(tags.Count >= numTagsToStore)
                  {
                       tags.RemoveAt(0);
                       indexList.RemoveAt(0);
                       topBoxList.RemoveAt(0);
                       bottomBoxList.RemoveAt(0);
                  }

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  571 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  330 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
                  548 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  548 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X