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 to initialize a Series array of int ?

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

    How to initialize a Series array of int ?

    "Error on calling 'OnBarUpdate' method on bar 11: Object reference not set to an instance of an object." Compilation is OK. Thanks for your help

    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class aaTest : Indicator
        {
            private Series<int>[] aaa;
            
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your nouveau custom Indicator here.";
                    Name                                        = "aaTest";
                    Calculate                                    = Calculate.OnBarClose;
                    IsOverlay                                    = true;
                    DisplayInDataBox                            = false;
                    DrawOnPricePanel                            = true;
                    DrawHorizontalGridLines                        = true;
                    DrawVerticalGridLines                        = true;
                    PaintPriceMarkers                            = true;
                    ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    IsSuspendedWhileInactive                    = true;
                }
                else if (State == State.Configure)
                {
                }
                else if (State == State.DataLoaded)
                {
                    aaa= new Series<int>[8];
                }      
            }
            
            protected override void OnBarUpdate()
            {
                if (BarsInProgress != 0 || CurrentBar <= 10 )
                   return;
                
                for(int i=0; i<=7; i++)
                  aaa[i][0]= 2;
            }
        }
    }

    #2
    Hello vindiou,

    Thank you for your note.

    To Declare,
    Code:
    private int[] aaa;
    To initialize,
    Code:
    aaa = new int[8];
    To Set,
    Code:
    for(int i=0; i<=7; i++)
    aaa[i]= 2;
    Please let us know if you need further assistance.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Sorry but you didn't read carefully my original question and my code example.

      I didn't ask for :

      An array of int (very simple) BUT
      for SERIES arrays of int.

      I need 8 series of int per bar = one series array (of dimension eight) int per bar.

      example :aaa[2][4] = the current prior/last 5th int element of the 3rd serie.

      Thanks

      Comment


        #4
        Hello vindiou,

        To declare a series of int arrays,
        Code:
        private Series<int[]>MySeries;
        To initialize,
        Code:
        MySeries = new Series<int[]>(this, MaximumBarsLookBack.Infinite);
        To set,
        Code:
        MySeries[0] = new []{0,1,2,3};
        To Reference,
        Code:
        Print(MySeries[0][0] + " " + MySeries[0][1]);
        Please let us know if you need further assistance.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          Thanks but that still isn't what I need.

          I don't need a Series int array
          BUT
          an array of Series int

          This is why I declared :
          private Series<int>[] aaa;

          Maybe I should initialize aaa to : ?
          for (int i=1; i<=8; i++)
          aaa[i] = new Series<int>(this,MaximumBarsLookBack.TwoHundredFif tySix);

          The compiler seems to accept this, but how do I set aaa?
          Moreover, how do I reference aaa if for example, I need to know what was the value of the 4th last/prior element of my 3rd Series for the current bar?
          aaa[3][4] doesn't seem to work...
          Last edited by vindiou; 08-28-2018, 12:33 AM.

          Comment


            #6
            Hello vindiou,

            In your code you could set aaa with,
            aaa[0][i]= 5;

            And you could reference the 4th element of the third series with,
            aaa[3][4]

            Assuming its been set and is not null.

            I would suggest you look into Lists in C# as they are easier to work with,
            Create a new List, add elements to it, and loop over its elements with for and foreach.


            Please let us know if you need further assistance.
            Alan P.NinjaTrader Customer Service

            Comment


              #7
              Sorry but your solution doesn't work, try it.

              Indicator 'aaTest': Error on calling 'OnBarUpdate' method on bar 11: Object reference not set to an instance of an object.

              aaa[i][0]= 5; OR aaa[0][i]= 5; gives the same error message

              BTW, my question is on arrays, not lists.

              Here's the code :

              Code:
              namespace NinjaTrader.NinjaScript.Indicators
              {
                  public class aaTest : Indicator
                  {
                      private Series<int>[] aaa;
                      
                      protected override void OnStateChange()
                      {
                          if (State == State.SetDefaults)
                          {
                              Description                                    = @"Enter the description for your nouveau custom Indicator here.";
                              Name                                        = "aaTest";
                              Calculate                                    = Calculate.OnBarClose;
                              IsOverlay                                    = true;
                              DisplayInDataBox                            = false;
                              DrawOnPricePanel                            = true;
                              DrawHorizontalGridLines                        = true;
                              DrawVerticalGridLines                        = true;
                              PaintPriceMarkers                            = true;
                              ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                              IsSuspendedWhileInactive                    = true;
                          }
                          else if (State == State.Configure)
                          {
                          }
                          else if (State == State.DataLoaded)
                          {
                              aaa= new Series<int>[8];
                          }      
                      }
                      
                      protected override void OnBarUpdate()
                      {
                          if (BarsInProgress != 0 || CurrentBar <= 10 )
                             return;
                          
                          for(int i=0; i<=7; i++)
                            aaa[i][0]= 5;
                      }
                  }
              }

              Comment


                #8
                Hello vindiou,

                If you would like a series where each slot in the series contains 8 values, you could use,

                Code:
                private Series<int[]>MySeries1;
                Code:
                else if (State == State.Configure)
                {
                MySeries1 = new Series<int[]>(this, MaximumBarsLookBack.Infinite);
                }
                Code:
                protected override void OnBarUpdate()
                {
                if(CurrentBar<100) return;
                		
                MySeries1[0]= new int[8];	
                			
                for(int i=0; i<=7; i++)
                {
                    MySeries1[0][i]= i;	
                }
                foreach(int i in  MySeries1[0])
                {
                Print(i.ToString());
                }
                }
                If this is not what you’re looking for, please provide more information on what you’re trying to do exactly, as the following is confusing.

                I don't need a Series int array
                BUT
                an array of Series int


                I didn't ask for :
                An array of int (very simple) BUT
                for SERIES arrays of int.


                Please let us know if you need further assistance.
                Alan P.NinjaTrader Customer Service

                Comment


                  #9
                  I don't understand why you have difficulties to understand my original request.
                  I feel like you are trying to avoid to answer to it and find another solution because of some reason, maybe NinjaTrader hasn't implemented this.

                  Once again, I don't need a single Series of an int Array
                  I need an Array of Series (of int type).

                  Do you understand the difference between ??

                  private Series<int[]>MySeries1;
                  and
                  private Series<int>[] MySeries1;

                  Why didn't you test my last code and tell me what's wrong with it?

                  Thanks
                  Last edited by vindiou; 08-29-2018, 01:31 PM.

                  Comment


                    #10
                    Hello vindiou,

                    Thank you for your response.

                    To create, initialize, and set the values of an Array of Series<int> please see the basic code below. I have also attached the indicator for your reference to this post.

                    Please keep in mind we do not provide programming education nor do we provide debugging assistance.
                    Code:
                    public class TestArrayOfSeriesType : Indicator
                    	{
                    		private Series<int>[] SeriesArray;
                    		
                    		protected override void OnStateChange()
                    		{
                    			if (State == State.SetDefaults)
                    			{
                    				Description									= @"";
                    				Name										= "TestArrayOfSeriesType";
                    			}
                    			else if (State == State.DataLoaded)
                    			{
                    				SeriesArray = new Series<int>[8];
                    				
                    				for (int i = 0; i < SeriesArray.Length; i++)
                    					SeriesArray[i] = new Series<int>(this, MaximumBarsLookBack.TwoHundredFiftySix);
                    			}
                    		}
                    
                    		protected override void OnBarUpdate()
                    		{
                    			SeriesArray[0][0] = CurrentBar;
                    			
                    			Print("");
                    			Print(Time[0]);
                    			Print(SeriesArray.Length);
                    			Print(SeriesArray[0][0]);
                    		}
                    	}
                    Please let me know if you have any questions.
                    Attached Files

                    Comment


                      #11
                      Excellent Patrick, have a good day.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by DJ888, Today, 10:57 PM
                      0 responses
                      1 view
                      0 likes
                      Last Post DJ888
                      by DJ888
                       
                      Started by MacDad, 02-25-2024, 11:48 PM
                      7 responses
                      158 views
                      0 likes
                      Last Post loganjarosz123  
                      Started by Belfortbucks, Today, 09:29 PM
                      0 responses
                      7 views
                      0 likes
                      Last Post Belfortbucks  
                      Started by zstheorist, Today, 07:52 PM
                      0 responses
                      7 views
                      0 likes
                      Last Post zstheorist  
                      Started by pmachiraju, 11-01-2023, 04:46 AM
                      8 responses
                      151 views
                      0 likes
                      Last Post rehmans
                      by rehmans
                       
                      Working...
                      X