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

How does NT know which plot goes with which values?

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

    How does NT know which plot goes with which values?

    I'm trying to figure out how NinjaTrader knows to associate each plot with each value that is defined in OnBarUpdate().

    Initially I thought that maybe each plot added in State.SetDefaults would be assigned a value starting with 0 and incrementing with each new "AddPlot()", then this index would be used to determine which value in OnBarUpdate() was associated with each plot. To test this I changed the order around in both locations, however the plotted indicator remained unchanged even after removing and reapplying to the chart.


    #2
    The "Values" Array.

    Value[0] ( note the absence of the 's' ) seen in most indicators is just a reference to Values[0][0].

    Current value (E.g. [0] ) of the second plot can be found in Values[1][0].

    https://ninjatrader.com/support/help...t8/?values.htm

    HedgePlay

    Comment


      #3
      Originally posted by butt_toast View Post
      To test this I changed the order around in both locations, however the plotted indicator remained unchanged even after removing and reapplying to the chart.
      Both locations?

      Your testing sounds inconclusive to me.

      Without seeing the code, I would chalk that up as: you made no changes at all.

      Comment


        #4
        Originally posted by hedgeplay View Post
        The "Values" Array.

        Value[0] ( note the absence of the 's' ) seen in most indicators is just a reference to Values[0][0].

        Current value (E.g. [0] ) of the second plot can be found in Values[1][0].

        https://ninjatrader.com/support/help...t8/?values.htm

        HedgePlay
        Thank you so much for replying.

        Am I even in the ballpark here?

        That is a head scratcher.

        Once I get this to plot anything I feel like I can mess with it and get the ball rolling but I'm just spinning my wheels here.

        I bought the NinjaScript Programmer's Launch Pad ebook and the first tutorial skips right over this topic and I feel like just getting something to plot would be the natural first step.

        Code:
         public class anIndi : Indicator
        {
             private Series<double> aPleasePlotThis;
             protected override void OnStateChange()
        {
             if (State == State.SetDefaults)
        {
             Description = @"";
             Name = "anIndi";
             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;
             AddLine(Brushes.Black, 1, "please just plot something, anything");
        }
             else if (State == State.Configure)
        {
             aPleasePlotThis = new Series<double>(this);
        }
        }
        
        protected override void OnBarUpdate()
        {
             double aPleasePlotThis = 1.0;
             //please plot anything
             Value[0] = aPleasePlotThis;
        }
        
        
        }//end class
        Last edited by butt_toast; 07-25-2021, 10:47 AM.

        Comment


          #5
          Originally posted by bltdavid View Post

          Both locations?

          Your testing sounds inconclusive to me.

          Without seeing the code, I would chalk that up as: you made no changes at all.
          Sorry I had initially included code from a tutorial with a question more specific to that tutorial.

          I removed the code from the tutorial because I didn't want to share the author's IP and edited my question to be more generic.

          I have since posted some code that I created instead of the tutorial code.

          Comment


            #6
            Easiest question I will address all day.

            Change this line: Value[0] = aPleasePlotThis;

            To this: Value[0] = aPleasePlotThis[0]; /// Add [0] at the end

            HedgePlay

            Comment


              #7
              Originally posted by hedgeplay View Post
              Easiest question I will address all day.

              Change this line: Value[0] = aPleasePlotThis;

              To this: Value[0] = aPleasePlotThis[0]; /// Add [0] at the end

              HedgePlay
              I appreciate your help so much.

              It's now telling me: Cannot apply indexing with [] to an expression of type 'double'

              Comment


                #8
                Swap your OBU() for this one..

                Code:
                protected override void OnBarUpdate()
                {
                  double aPleasePlotThisLocal = 1.0; /// changed the name of this double .. should not match/override the data series name. 
                
                  aPleasePlotThis[0] = aPleasePlotThisLocal;
                
                  //please plot anything
                  Value[0] = aPleasePlotThis[0];
                }

                If that does not do it attach a *.cs file that compiles with but still does not plot.

                If it does not compile then as David said we will need to see the full real code you are trying to get to compile ( maybe a stripped down version of the real one will all basic/solvable compile errors resolved.) and the first few compiles errors left.

                HedgePlay
                Last edited by hedgeplay; 07-25-2021, 11:02 AM.

                Comment


                  #9
                  Originally posted by butt_toast View Post
                  Sorry I had initially included code from a tutorial with a question more specific to that tutorial.

                  I removed the code from the tutorial because I didn't want to share the author's IP and edited my question to be more generic.

                  I have since posted some code that I created instead of the tutorial code.
                  No apologies necessary.

                  In fact, the issue is with my explanation.

                  I should have elaborated more.

                  I presume, you did these two edits:
                  1. Swap the AddPlot lines.
                  2. Swap the indexes for the plot assignments.

                  If you make the changes at both locations, you effectively cancelled out
                  the changes and I'd expect the indicator to show the same visual plots.

                  When you made changes at both locations, the edit at the 2nd location will
                  re-align the plots back to the existing assignments -- that is, the edit at the
                  2nd location effectively reverts the intended effect of the 1st edit.

                  Comment


                    #10
                    Originally posted by hedgeplay View Post
                    Swap your OBU() for this one..

                    Code:
                    protected override void OnBarUpdate()
                    {
                    double aPleasePlotThisLocal = 1.0; /// changed the name of this double .. should not match/override the data series name. 
                    
                    aPleasePlotThis[0] = aPleasePlotThisLocal;
                    
                    //please plot anything
                    Value[0] = aPleasePlotThis[0];
                    }

                    If that does not do it attach a *.cs file that compiles with but still does not plot.

                    If it does not compile then as David said we will need to see the full real code you are trying to get to compile ( maybe a stripped down version of the real one will all basic/solvable compile errors resolved.) and the first few compiles errors left.

                    HedgePlay
                    That got it to compile, now the output window prints: "Indicator 'anIndi': Error on calling 'OnBarUpdate' method on bar 0: Index was outside the bounds of the array."

                    Googled that, found a potential solution on this forum:
                    Code:
                    if (CurrentBars[0] < 5 || CurrentBars[1] < 5 || CurrentBars[2] < 5)    
                         return;
                    And now the output window prints: "Indicator 'anIndi': Error on calling 'OnBarUpdate' method on bar 5: Index was outside the bounds of the array."

                    I've attached the .cs file.
                    Attached Files

                    Comment


                      #11
                      Originally posted by butt_toast View Post

                      That got it to compile, now the output window prints: "Indicator 'anIndi': Error on calling 'OnBarUpdate' method on bar 0: Index was outside the bounds of the array."

                      And now the output window prints: "Indicator 'anIndi': Error on calling 'OnBarUpdate' method on bar 5: Index was outside the bounds of the array."

                      I've attached the .cs file.
                      What Instrument, Bar Type and Bar Period is on your current test chart? something like " ES 1 Minute Bars, for 5 Days



                      Oh, by the way.. Hi David. Happy Sunday to you!



                      HedgePlay
                      Last edited by hedgeplay; 07-25-2021, 11:24 AM.

                      Comment


                        #12
                        Originally posted by hedgeplay View Post

                        What Instrument, Bar Type and Bar Period is on your current test chart? something like " ES 1 Minute Bars, for 5 Days



                        Oh, by the way.. Hi David. Happy Sunday to you!



                        HedgePlay
                        ES 5000 volume bars, for 3 Days
                        Attached Files

                        Comment


                          #13
                          Originally posted by butt_toast View Post

                          That got it to compile, now the output window prints: "Indicator 'anIndi': Error on calling 'OnBarUpdate' method on bar 0: Index was outside the bounds of the array."

                          Googled that, found a potential solution on this forum:
                          Code:
                          if (CurrentBars[0] < 5 || CurrentBars[1] < 5 || CurrentBars[2] < 5)
                          return;
                          And now the output window prints: "Indicator 'anIndi': Error on calling 'OnBarUpdate' method on bar 5: Index was outside the bounds of the array."
                          Ok. First, "good on ya"

                          Good homework and very important code to know use >>> "if (CurrentBars[0] < 5 || CurrentBars[1] < 5 || CurrentBars[2] < 5)"

                          In this case you only had one data series loaded ( CurrentBars[0] ) so the conditional tests to both CurrentBars[1] and CurrentBars[2] will drive that OBU() indexing error because those bar arrays do not exist until you load more data series in OSC().Configure above.

                          So in this case "if( CurrentBar < 0 )" or "if( CurrentBar < 30 )" might be all you need.

                          But as soon as you add more data series to an indi you will want a version of the example you found.

                          HedgePlay

                          Comment


                            #14
                            Originally posted by hedgeplay View Post

                            Ok. First, "good on ya"

                            Good homework and very important code to know use >>> "if (CurrentBars[0] < 5 || CurrentBars[1] < 5 || CurrentBars[2] < 5)"

                            In this case you only had one data series loaded ( CurrentBars[0] ) so the conditional tests to both CurrentBars[1] and CurrentBars[2] will drive that OBU() indexing error because those bar arrays do not exist until you load more data series in OSC().Configure above.

                            So in this case "if( CurrentBar < 0 )" or "if( CurrentBar < 30 )" might be all you need.

                            But as soon as you add more data series to an indi you will want a version of the example you found.

                            HedgePlay
                            Thanks!

                            Still no dice.

                            I swapped out the line you mentioned with the code you mentioned, and the error in the output window has changed to: "Indicator 'anIndi': Error on calling 'OnBarUpdate' method on bar 30: Index was outside the bounds of the array."

                            The number of hours I've sunk into getting a simple plot to plot has taken half of my Sunday so before I throw my computer out of the window I am going to have to step away.

                            I was able to make a strategy with WPF buttons and multiple data series but I can't even get a line to plot in an indicator after several hours, there has to be something I'm missing here. Maybe my frustration levels are too high at the "wasted" hours.

                            Time to play with the dog and forget about my inability to get one line to plot in an indicator.

                            Comment


                              #15
                              Second, you might have confused AddLine() which is great as the center line but often no need to reference in OUB() ..

                              Will AddPlot() which you really do want as the first Addxx() object in OSC().SetDefaults so that Value[0] (Values[0][0] ) will Plot the value you are assigning it in OBU().


                              So OSC() SetDefaults.

                              Code:
                              .
                              
                              {
                              if (State == State.SetDefaults)
                              {
                              Description = @"";
                              Name = "anIndi";
                              Calculate = Calculate.OnBarClose;
                              IsOverlay = false;
                              DisplayInDataBox = true;
                              PaintPriceMarkers = true;
                              
                              /// Many parms deleted..  Do not add this complexity unless you really need it ..
                                                       /// will often trip you up when you overlook a needed change in parms. 
                              
                              IsSuspendedWhileInactive = true;
                              
                              
                              AddPlot(Brushes.Blue, "MyPlot");  /// I added the plot line you really wanted..  
                              AddLine(Brushes.DarkGray, 0, NinjaTrader.Custom.Resource.NinjaScriptIndicatorZe roLine);
                                                       /// was "please just plot .." ,  use the NT default example. Also, as the LAST in the list
                              
                              }
                              
                              .

                              In OBU()

                              Code:
                              .
                              
                              protected override void OnBarUpdate()
                              {
                              //  /if (CurrentBars[0] < 5 || CurrentBars[1] < 5 || CurrentBars[2] < 5) return;  
                                                          //  Delete, too many arrays referenced when only one dataSeries was added 
                              
                              if(CurrentBar < 0) return;
                              
                              
                              if(CurrentBar > Count-50) // Can remove this conditional .. limits output to only last 50 bars
                              {
                              double aPleasePlotThisLocal = -0.2;
                              //please plot anything
                              aPleasePlotThis[0] = aPleasePlotThisLocal;
                              Value[0] = aPleasePlotThis[0];
                              }
                              }
                              .


                              Updated example indi attached

                              HedgePlay
                              Attached Files
                              Last edited by hedgeplay; 07-25-2021, 12:20 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by chumpsky33, Today, 03:21 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post chumpsky33  
                              Started by klaus w, Today, 02:09 AM
                              0 responses
                              8 views
                              0 likes
                              Last Post klaus w
                              by klaus w
                               
                              Started by RaddiFX, Yesterday, 11:57 PM
                              1 response
                              17 views
                              0 likes
                              Last Post NinjaTrader_Marco  
                              Started by Tonofit, Today, 12:00 AM
                              0 responses
                              12 views
                              0 likes
                              Last Post Tonofit
                              by Tonofit
                               
                              Started by ck6libra, Yesterday, 11:20 PM
                              1 response
                              17 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Working...
                              X