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

List object trouble

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

    List object trouble

    Hi, I've been having some trouble with using a 'List' Object in NinjaScript whilst building the framework for a new project. Can you help sort out what needs to be corrected?

    What I'm trying to do is this;

    1. Declare a new class 'clsLevel'.
    2. On each bar instantiate an instance of clsLevel and populate it with some data.
    3. Store each instance of 'clsLevel' into into a list object named 'Levels'.
    4. Later refer to the list and the objects stored in it.

    So I can get the necessary objects instantiated. However the 'Levels' list is instantiated when OnStartup() is called and then when I try to refer to the object in OnBarUpdate() using

    Levels.Add(candLevel)

    then the code fails with this message

    'Object reference not set to an instance of an object'

    and with that I'm stuck. Can you help me out here?

    Thanks

    Riskybiz

    Code:
    //Design the class
    
    namespace ClassDesign
    {
    	public class clsLevel
    	{
    		public bool isInitialised;
    		public DateTime startDateTime;
    		public DateTime endDateTime;
    		public int startBar;
    		public int endBar;
    		public double highVal;
    		public double lowVal;
    	
    		public clsLevel()
    		{
    			isInitialised = true;
    		}
    	}
    }
    
    
    //Instantiate the List object once at the initialization of the indicator
    
    protected override void OnStartUp()
    		{
        	// Set any variables or logic you wish to do only once at start of your indicator/strategy
    		List<clsLevel> Levels = new List<clsLevel>();
    		Levels.Capacity = 10;	
    		levelsInitiated = true;
    		Print("Levels Initiated: " + levelsInitiated.ToString());
    		Print("Capacity " + Levels.Capacity.ToString());
    		}
      		
    		/// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                		
    			// Use this method for calculating your indicator values. Assign a value to each
                // plot below by replacing 'Close[0]' with your own formula.
                			
    			candLevel = new clsLevel();
    			
    			Print("Candidate Initiated: " + candLevel.isInitialised.ToString());
    			
    			candLevel.startDateTime = Time[0];
    			candLevel.endDateTime = Time[0];
    			candLevel.startBar = CurrentBar;
    			candLevel.endBar = CurrentBar;
    			candLevel.highVal = High[0];
    			candLevel.lowVal = Low[0];
    			
    			Print("StartBar " + candLevel.startBar.ToString());//check to test accessing the object
    			
    			
    			Levels.Add(candLevel);
    			
    						
    			Plot0.Set(High[0]);//just plot something to see that it calculated
    			Plot1.Set(Low[0]);//just plot something to see that it calculated
    			
    				
    		}

    #2
    Originally posted by Riskybiz View Post
    Hi, I've been having some trouble with using a 'List' Object in NinjaScript whilst building the framework for a new project. Can you help sort out what needs to be corrected?

    What I'm trying to do is this;

    1. Declare a new class 'clsLevel'.
    2. On each bar instantiate an instance of clsLevel and populate it with some data.
    3. Store each instance of 'clsLevel' into into a list object named 'Levels'.
    4. Later refer to the list and the objects stored in it.

    So I can get the necessary objects instantiated. However the 'Levels' list is instantiated when OnStartup() is called and then when I try to refer to the object in OnBarUpdate() using

    Levels.Add(candLevel)

    then the code fails with this message

    'Object reference not set to an instance of an object'

    and with that I'm stuck. Can you help me out here?

    Thanks

    Riskybiz

    Code:
    //Design the class
     
    namespace ClassDesign
    {
        public class clsLevel
        {
            public bool isInitialised;
            public DateTime startDateTime;
            public DateTime endDateTime;
            public int startBar;
            public int endBar;
            public double highVal;
            public double lowVal;
     
            public clsLevel()
            {
                isInitialised = true;
            }
        }
    }
     
     
    //Instantiate the List object once at the initialization of the indicator
     
    protected override void OnStartUp()
            {
            // Set any variables or logic you wish to do only once at start of your indicator/strategy
            List<clsLevel> Levels = new List<clsLevel>();
            Levels.Capacity = 10;    
            levelsInitiated = true;
            Print("Levels Initiated: " + levelsInitiated.ToString());
            Print("Capacity " + Levels.Capacity.ToString());
            }
     
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
     
                // Use this method for calculating your indicator values. Assign a value to each
                // plot below by replacing 'Close[0]' with your own formula.
     
                candLevel = new clsLevel();
     
                Print("Candidate Initiated: " + candLevel.isInitialised.ToString());
     
                candLevel.startDateTime = Time[0];
                candLevel.endDateTime = Time[0];
                candLevel.startBar = CurrentBar;
                candLevel.endBar = CurrentBar;
                candLevel.highVal = High[0];
                candLevel.lowVal = Low[0];
     
                Print("StartBar " + candLevel.startBar.ToString());//check to test accessing the object
     
     
                Levels.Add(candLevel);
     
     
                Plot0.Set(High[0]);//just plot something to see that it calculated
                Plot1.Set(Low[0]);//just plot something to see that it calculated
     
     
            }
    You have a scope problem. Your Levels list should be declared as an indicator Class object, not in the OnStartUp() event.

    Comment


      #3
      Thanks, you got it in one. I moved the 'List' instantiation to where the variables are declared in

      'public class SupplyDemand : Indicator
      {'

      and bingo, success.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by zstheorist, Today, 07:52 PM
      0 responses
      3 views
      0 likes
      Last Post zstheorist  
      Started by pmachiraju, 11-01-2023, 04:46 AM
      8 responses
      149 views
      0 likes
      Last Post rehmans
      by rehmans
       
      Started by mattbsea, Today, 05:44 PM
      0 responses
      5 views
      0 likes
      Last Post mattbsea  
      Started by RideMe, 04-07-2024, 04:54 PM
      6 responses
      33 views
      0 likes
      Last Post RideMe
      by RideMe
       
      Started by tkaboris, Today, 05:13 PM
      0 responses
      5 views
      0 likes
      Last Post tkaboris  
      Working...
      X