Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Ninjascript Array handling

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

    Ninjascript Array handling

    I have a dataseries that is "isolated" in a if statement. When the if criterias are not met, NT still gives the dataseries a value (0).

    Like this: 11,0,0,0,13,0,0,0,0...

    In C# I would expect this array to look like this: 11,13...

    Is an array in Ninjascript always a "bars array"?

    #2
    Correct, a dataSeries is tied to the bars it's linked to - you should always set a value and then filter in your conditions as needed as otherwise exceptions accessing the series could be the result.

    Comment


      #3
      Hi Freen,

      Yes, DataSeries are always synced to bars. There will be the same number of elements in the data series as bars.



      Calling the Reset() method is unique and can be very powerful for custom indicator development. DataSeries objects can hold null values which simply means that you do not want to store a value for the current bar. Mathematically, you can correctly assign a value of zero however if the DataSeries was the primary DataSeries of an indicator whose values would be used for plotting, you may NOT want a zero value plotted. Meaning, you want a zero value for proper calculations but not a zero value for chart visualization. The Reset() method allows you to reset the current bar's DataSeries value to a zero for calculation purposes but NinjaScript would ignore this value when it plotted it on a chart.
      Ryan M.NinjaTrader Customer Service

      Comment


        #4
        Thanks Ryan and Bertrand, I´m always impressed by your promtness. And thanks for the additonal info Ryan, you answered my sequel question.

        Comment


          #5
          Can´t find the Reset() method in the Alphabetic reference. Would you kindly point me to it´s syntax and usage?!

          Tried this but I still get the zeroes:

          if (highestbar<1) // ie: highestbar=0
          {
          HighestbarDS.Reset();
          }
          else
          {
          HighestbarDS.Set(highestbar);
          }


          ...and NT won´t allow a "Null" setting:


          if (highestbar<1)
          {
          HighestbarDS.Set(Null);
          }
          else
          {
          HighestbarDS.Set(highestbar);
          }


          Guess I´ll have to read the array in loop excluding the zeroes into a new array.
          Last edited by FREEN; 02-10-2011, 10:56 AM.

          Comment


            #6
            DataSeries reset is called with:

            myDataSeries.Reset();
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              DataSeries.Set(intbarsAgo, doublevalue)

              You can also set the value for historical bars by including a "barsAgo" value that represents the number of bars ago that you want the double value to be stored at.
              Q 1: Is the old value simply overwritten or does it have to be reset before set?

              Q 2: Is the rest of the array unaffected?


              //Fredrik

              Comment


                #8
                Originally posted by FREEN View Post
                Q 1: Is the old value simply overwritten or does it have to be reset before set?

                Q 2: Is the rest of the array unaffected?


                //Fredrik
                Found out testing it;

                A 1: Yes, it´s simply overwritten. No, it needn´t be reset before it´s overwritten!

                A 2: Yes!
                Last edited by FREEN; 02-13-2011, 05:13 PM.

                Comment


                  #9
                  As a conclusion and for anyone in need, here´s the snippet that filters all "barsAgo zeroes" added by NT to an array out. In my case I wanted an array of peaks instead of an "barsAgo array". Example;

                  input ds: 0,0,0,0,0,0,12,0,0,0,0,56,0,0,0,0,0,0,0,87,0,0,0 (HighestbarDS in the code)

                  output ds: 12, 56, 87 (PeaksDS in the code)

                  Code snippet:

                  int i = 0; // used in reading "HighestbarsDS" array index value
                  int j = 0; // used in setting "PeaksDS" custom array index value

                  while (i < 256) // maximum bars lookback if not set to infinite
                  {
                  if (HighestbarDS[i] != 0) // filter out all zeroes
                  {
                  PeaksDS.Set( j, (int)HighestbarDS[i]); // read from HighestbarDS[i]- set to PeaksDS[j]
                  j++; // incrementing only when HighestbarsDS != 0
                  }
                  i++; // incrementing for each barsAgo index value
                  }

                  Comment


                    #10
                    Frederic, your conclusions are correct - thanks for sharing the snippet with our members here.

                    Comment


                      #11
                      De nada Bertrand. You guys make an excellent job and I´m happy if I can contribute with more than just newbie questions.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      582 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      338 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      103 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      554 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      552 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X