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

Creating an array of object

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

    Creating an array of object

    Since Ninjascript is slightly different of VB, I'd like to clear some doubts doing this.

    I need to create an array of object with two variables, an integer and a double, which will change dynamically. So, the logic steps should be:

    1. Set a class variable with the two variables, I've created one called Trendline
    2. Set a simple constructor where variables are assigned with default values.
    3. Create an array for that Class above, in the sample is called: Uptrend
    4. Declare the size of that array, using the value of the constant ARRAY_SIZE
    5. Initialize that array of object in the program

    Here a little code, that I'd like you to see and confirm that it'd work fine.

    Code:
    {
            // This is the class to use as object
    	public class Trendline
    	{
    		private double Rate;
    		private int Position;
    
                    public Trendline()
                    {
                        Position=0;
    		    Rate=0.00;
                    }
    	}	
    	
    	public class XXX : Strategy
    	{
    		private int ARRAY_SIZE;
                    //This is the array of the class
    		Trendline[] Uptrend;			  	 															// Array of uptrends
    
    		protected override void OnStateChange()
    		{
    			if (State == State.SetDefaults)
    			{
    				Description									= @"xxxxxxx";
    				Name										= "XXX";
    				Calculate									= Calculate.OnPriceChange;
    			}
    			else if (State == State.Configure)
    			{
    				ARRAY_SIZE									= 20000;
    				Trendline[] Uptrend							= new Trendline[ARRAY_SIZE];
    			}
    		}
    
    		protected override void OnBarUpdate()
    		{
    			if ( CurrentBar < 2 )
    			{
    				for (int i = 0; i <ARRAY_SIZE; i++)
    				{			
    					Uptrend[i]= new Trendline();
    				}
    				return;
    			}
    			// Rest of code		
    		}
    
    		#region Properties
    	}
    }
    If there's something wrong please let me know, thanks
    Last edited by pstrusi; 09-10-2017, 02:40 AM.

    #2
    Hello pstrusi,

    This would fall under general C# education and would not be specific to NinjaScript.

    That said, everything looks fine.

    What is the error message you are getting, or what is not working?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello pstrusi,

      This would fall under general C# education and would not be specific to NinjaScript.

      That said, everything looks fine.

      What is the error message you are getting, or what is not working?
      Hi Chelsea, thanks for your answer on Sunday. Well, it was a problem of default values for the class Trendline, cause I had configured constructor wrong. So far it seems to pass initial phase. If any issue related to NT in this regard, I'll let you know.

      Regards

      Comment


        #4
        Originally posted by NinjaTrader_ChelseaB View Post
        Hello pstrusi,

        This would fall under general C# education and would not be specific to NinjaScript.

        That said, everything looks fine.

        What is the error message you are getting, or what is not working?
        Chelsea, despite the fact that compiles fine, now in the run it brings this error:

        Strategy ' X ': Error on calling 'OnBarUpdate' method on bar 0: Object reference not set to an instance of an object.

        After discarding any typos or logic basic errors, I think this is about the Constructor of the Class "Trendline". I'm aware that this might out of NT support field, but I'd appreciate highly if it's something pretty obvious to see.

        Should it that constructor built in other way ?

        Thanks

        Comment


          #5
          Hello pstrusi,

          As I was adding your code to a test script so that I could test and export, I noticed that you are declaring Uptrend twice in your strategy.

          Its declared at the top in the variables
          private int ARRAY_SIZE;
          //This is the array of the class
          Trendline[] Uptrend;

          And then declared again in OnStateChange.
          else if (State == State.Configure)
          {
          ARRAY_SIZE = 20000;
          Trendline[] Uptrend = new Trendline[ARRAY_SIZE];
          }

          Are you are only wanting to use this in OnStateChange()?

          If not, if you are wanting to use this anywhere, don't declare it a second time in OnStateChange. (You are declaring this by using the type in front.)

          Just do the assignment without a declaration.

          Uptrend = new Trendline[ARRAY_SIZE];

          Below is a publicly available link to the microsoft website on scope.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_ChelseaB View Post
            Hello pstrusi,

            As I was adding your code to a test script so that I could test and export, I noticed that you are declaring Uptrend twice in your strategy.

            Its declared at the top in the variables
            private int ARRAY_SIZE;
            //This is the array of the class
            Trendline[] Uptrend;

            And then declared again in OnStateChange.
            else if (State == State.Configure)
            {
            ARRAY_SIZE = 20000;
            Trendline[] Uptrend = new Trendline[ARRAY_SIZE];
            }

            Are you are only wanting to use this in OnStateChange()?

            If not, if you are wanting to use this anywhere, don't declare it a second time in OnStateChange. (You are declaring this by using the type in front.)

            Just do the assignment without a declaration.

            Uptrend = new Trendline[ARRAY_SIZE];

            Below is a publicly available link to the microsoft website on scope.
            https://msdn.microsoft.com/en-us/library/ms973875.aspx
            Chelsea, obviously you nailed with doble declaration of the Array.

            Thank you so much for your good will help ! I'll see what you sent and take action. I'll let you know.
            Last edited by pstrusi; 09-11-2017, 06:00 AM.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by cls71, Today, 04:45 AM
            0 responses
            1 view
            0 likes
            Last Post cls71
            by cls71
             
            Started by mjairg, 07-20-2023, 11:57 PM
            3 responses
            213 views
            1 like
            Last Post PaulMohn  
            Started by TheWhiteDragon, 01-21-2019, 12:44 PM
            4 responses
            544 views
            0 likes
            Last Post PaulMohn  
            Started by GLFX005, Today, 03:23 AM
            0 responses
            3 views
            0 likes
            Last Post GLFX005
            by GLFX005
             
            Started by XXtrader, Yesterday, 11:30 PM
            2 responses
            12 views
            0 likes
            Last Post XXtrader  
            Working...
            X