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

Is it possible to limit drawing to a number of bars?

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

    Is it possible to limit drawing to a number of bars?

    Here's a basic indicator I wrote with help from the kind moderators here.

    It draws volume above and below bars on the chart. It was basically an experiment in learning Ninjascript.

    What I would like to know is it possible to limit how far back the indicator draws draws bars?

    I have another indicator which requires loading lots of days of data, but I'm working on a new indicator which I would only like to draw the last 10 bars.

    Is that possible?


    The code:
    Code:
            //create a class-level variable called myVolume
            private double myVolume;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Indicator here.";
                    Name                                        = "VolumeAboveAndBelow";
                    Calculate                                    = Calculate.OnEachTick;
                    IsOverlay                                    = true;
                    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;
                }
                else if (State == State.Configure)
                {
                }
            }
    
            protected override void OnBarUpdate()
            {
                //assign the VOL[0] indicator value to the double variable
                myVolume = VOL()[0];
    
                //check for up bar and use Draw.Text() to draw variable value on chart
                if (Close[0] > Open[0])
                    Draw.Text(this, "vol up bar" + CurrentBar, myVolume.ToString(), 0, High[0] + 5 * TickSize, Brushes.Red);
    
                //check for download bar and use Draw.Text() to draw variable value on chart
                if (Open[0] >= Close[0])
                    Draw.Text(this, "vol up bar" + CurrentBar, myVolume.ToString(), 0, Low[0] - 5 * TickSize, Brushes.ForestGreen);
    
    
    
            }
        }
    }​

    #2
    If you only wanted the last 50 bars to have text you could use CurrentBar since you have it in your text draw objects.
    if(CurrentBar > 50)
    RemoveDrawObject("vol up bar" + (CurrentBar-50));
    eDanny
    NinjaTrader Ecosystem Vendor - Integrity Traders

    Comment


      #3
      Hello Bob-Habanai,

      If you want to draw on the last 50 historical bars, check that the State is State.Historical as well.
      https://ninjatrader.com/support/foru...61#post1210861
      https://ninjatrader.com/support/helpGuides/nt8/currentbar.htm
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Thank you both for your replies!

        Comment


          #5
          A follow-up question.

          Is there a way to apply an if to all other sequential ifs?

          like
          Code:
          if ((State == State.Historical && CurrentBar == Count -50))
          then all other various if commands after with assuming the above if?

          e.g.
          if ((State == State.Historical && CurrentBar == Count -50))
          then if (Close[0] > Open[0])
          Draw.Text(this, "vol up bar" + CurrentBar, myVolume.ToString(), 0, High[0] + 5 * TickSize, Brushes.Red);
          then if (still assuming the first if, but not the second)(Open[0] >= Close[0])
          Draw.Text(this, "vol up bar" + CurrentBar, myVolume.ToString(), 0, Low[0] - 5 * TickSize, Brushes.ForestGreen);

          Thanks
          Last edited by Bob-Habanai; 12-29-2022, 04:04 AM.

          Comment


            #6
            Hello Bob-Habanai,

            This is done by nesting.

            if (/*outer if must be true*/)
            {
            if (/* outer if must be true and inner if must also be true*/)
            {
            }

            if (/* outer if must be true and second inner if must also be true, but not the first inner if*/)
            {
            }
            }
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Thanks again!

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Segwin, 05-07-2018, 02:15 PM
              14 responses
              1,789 views
              0 likes
              Last Post aligator  
              Started by Jimmyk, 01-26-2018, 05:19 AM
              6 responses
              837 views
              0 likes
              Last Post emuns
              by emuns
               
              Started by jxs_xrj, 01-12-2020, 09:49 AM
              6 responses
              3,294 views
              1 like
              Last Post jgualdronc  
              Started by Touch-Ups, Today, 10:36 AM
              0 responses
              13 views
              0 likes
              Last Post Touch-Ups  
              Started by geddyisodin, 04-25-2024, 05:20 AM
              11 responses
              63 views
              0 likes
              Last Post halgo_boulder  
              Working...
              X