• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Building a Class within...but getting the typical error: "Object reference..."

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

    Building a Class within...but getting the typical error: "Object reference..."

    Hi,
    When I try to run the Algo, the typical error pops up: "Object reference not set to an instance of an object"
    Posting here, just in case anyone in the forum might indicate what I'm doing wrong initializing the Class Trendline.
    Thanks a lot!

    Code:
    namespace NinjaTrader.Strategy
    {
        [Description("vvv")]
    
        public class Trendline
        {
            public int position;
            public double rate;
    
            public Trendline()  
            {
                position = 0;
                rate = 0.00;
            }
        }
    
        public class VVV : Strategy
        {
            #region Variables    
            private Trendline[]             uptrend = new Trendline[1];
            private Trendline[]           downtrend = new Trendline[1];      
            #endregion
    
            protected override void Initialize()
            {
                Do stuff
            }
            protected override void OnStartUp()
            {
                uptrend = new Trendline[activedays + 1];
                downtrend = new Trendline[activedays + 1];
            }
    
            protected override void OnBarUpdate()                                        
            {    
                if ( CurrentBar < 1 )
                {
                    for (int i = 0; i < activedays; i++)
                    {            
                        uptrend[i] = new Trendline();
                        downtrend[i] = new Trendline();
                    }
                    for (int i = 0; i < activedays; i++)
                    {            
                        uptrend[i].position = 0;
                        uptrend[i].rate = 0.00;
                        downtrend[i].position = 0;
                        downtrend[i].rate = 0.00;
                    }
                }
    Last edited by pstrusi; 06-18-2019, 05:08 PM.

    #2
    I've been able to solve the error by doing this modification:

    Code:
    namespace NinjaTrader.Strategy
    {
        [Description("vvv")]
    
        public class Trendline
        {
            public int position;
            public double rate;
    
            public Trendline()  
            {
                position = 0;
                rate = 0.00;
            }
        }
    
        public class VVV : Strategy
        {
            #region Variables    
            private Trendline[]             uptrend = new Trendline[1];
            private Trendline[]           downtrend = new Trendline[1];      
            #endregion
    
            protected override void Initialize()
            {
                Do stuff
            }
            protected override void OnStartUp()
            {
                uptrend = new Trendline[activedays + 1];
                downtrend = new Trendline[activedays + 1];
                for (int i = 0; i < activedays; i++)
                    {            
                        uptrend[i] = new Trendline();
                        downtrend[i] = new Trendline();
                    }
            }
    
            protected override void OnBarUpdate()                                        
            {    
                if ( CurrentBar < 1 )
                {
    
                    for (int i = 0; i < activedays; i++)
                    {            
                        uptrend[i].position = 0;
                        uptrend[i].rate = 0.00;
                        downtrend[i].position = 0;
                        downtrend[i].rate = 0.00;
                    }
                }

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by yeshujbp, Today, 02:40 PM
    0 responses
    5 views
    0 likes
    Last Post yeshujbp  
    Started by Venom, Today, 01:05 PM
    0 responses
    9 views
    0 likes
    Last Post Venom
    by Venom
     
    Started by Dert66, Today, 12:11 PM
    0 responses
    7 views
    0 likes
    Last Post Dert66
    by Dert66
     
    Started by jjanguda, Today, 10:04 AM
    0 responses
    225 views
    0 likes
    Last Post jjanguda  
    Started by sambo81, Today, 09:20 AM
    1 response
    13 views
    0 likes
    Last Post sambo81
    by sambo81
     
    Working...
    X