Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Dynamically updating Plots[0].Width in OnRender

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

    Dynamically updating Plots[0].Width in OnRender

    I am trying to get the Width of one of my plots to match the width of another minus 1 pixel. I am trying to confirm that OnRender is the right place to do that and also inquire as to why this isn't working:

    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class BarWidthTest : Indicator
        {
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Displays the Average Volume for a particular time based on N previous days.";
                    Name                                        = "BarWidthTest";
                    Calculate                                    = Calculate.OnBarClose;
                    IsOverlay                                    = false;
                    DisplayInDataBox                            = true;
                    DrawOnPricePanel                            = true;
                    DrawHorizontalGridLines                        = true;
                    DrawVerticalGridLines                        = true;
                    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;
                    AddPlot(new Stroke(Brushes.Black), PlotStyle.Bar, "AverageVolume");
                    AddPlot(new Stroke(Brushes.Red,5), PlotStyle.Bar, "VolumeIndicator");
                    Plots[0].AutoWidth = true;
    
                }
                else if (State == State.DataLoaded)
                {
    
                }
                else if (State == State.Configure)
                {
                }
            }
    
            protected override void OnBarUpdate()
            {
                if(CurrentBar < 0) return;
    
                AverageVolume[0] = Volume[0]/2;
                VolumeIndicator[0] = Volume[0];
            }
            protected override void OnRender(ChartControl chartControl, ChartScale chartScale) {
                Plots[1].Width = Plots[0].Width - 1;
            }        
    
    
    
            #region Properties
            [Browsable(false)]
            [XmlIgnore]
            public Series<double> VolumeIndicator
            {
                get { return Values[1]; }
            }
            [Browsable(false)]
            [XmlIgnore]
            public Series<double> AverageVolume
            {
                get { return Values[0]; }
            }        
            #endregion
    
        }
    }

    #2
    Hello swcooke,

    Thanks for your post.

    You would not need Onrender() for this.

    You can move your plot width coding into State.DataLoaded so it is only executed once.

    I highly recommend you also add code to prevent setting a value smaller than 1.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Paul,

      Plot[0] is set by this line:

      Code:
      Plots[0].AutoWidth = true;
      Since that is a dynamic width, I don't think your suggestion would do the trick. That is why I was trying to use OnRender.

      Comment


        #4
        Hello swcooke,

        Thanks for your reply.

        Yes, I missed that, thanks.

        In OnRender() you would need to use base.OnRender(chartControl, chartScale); See the 2nd tip here: https://ninjatrader.com/support/help.../?onrender.htm

        In OnRender you can obtain the current bar width from BarWidth: https://ninjatrader.com/support/help...l_barwidth.htm which is a double so just convert to int type and subtract 1 and set Plots[1].width to that value.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Thank you Paul. Here is the code I went with in case it helps anyone else:

          Code:
                  protected override void OnRender(ChartControl chartControl, ChartScale chartScale) {
                      base.OnRender(chartControl, chartScale);
                      Plots[1].Width = Math.Max((float)chartControl.BarWidth - 3,2);
                  }

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by set2win, 08-04-2021, 09:23 AM
          39 responses
          1,000 views
          0 likes
          Last Post WaleeTheRobot  
          Started by md4866, Today, 08:15 PM
          0 responses
          6 views
          0 likes
          Last Post md4866
          by md4866
           
          Started by mjbatts91, Yesterday, 04:48 PM
          2 responses
          23 views
          0 likes
          Last Post mjbatts91  
          Started by pibrew, Today, 06:10 PM
          1 response
          19 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by actualfacts.2021, 07-25-2021, 03:11 PM
          8 responses
          1,187 views
          0 likes
          Last Post linkcou
          by linkcou
           
          Working...
          X