Announcement

Collapse
No announcement yet.

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, 01: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.
          https://msdn.microsoft.com/en-us/library/ms973875.aspx
          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, 05:00 AM.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by daven, Today, 09:49 AM
            0 responses
            1 view
            0 likes
            Last Post daven
            by daven
             
            Started by omermirza, 01-15-2021, 01:15 PM
            13 responses
            115 views
            0 likes
            Last Post NinjaTrader_Jim  
            Started by Island_Boy_Trading, Today, 07:39 AM
            2 responses
            14 views
            0 likes
            Last Post Island_Boy_Trading  
            Started by cmarkb, Today, 07:19 AM
            1 response
            11 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by wjadevries, Today, 03:29 AM
            1 response
            16 views
            0 likes
            Last Post NinjaTrader_Jim  
            Working...
            X