• 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

How to access Closes[0][0] from a nested class?

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

    How to access Closes[0][0] from a nested class?

    The only way I get it to work is by passing in "this" as NinjaScriptBase and assigning it to a static NinjaScriptBase member of the nested class. Like so:

    Code:
    public class MyStrategy : Strategy
    {
        NestedClass nestedClass;
    
        protected override void OnStateChange()
        {
            if (State == State.DataLoaded)
                NestedClass nestedClass = new NestedClass(this);
        }
    
        protected override void OnBarUpdate()
        {
            double something = nestedClass.GetSomething();
        }
    
        public class NestedClass
        {
            static NinjaScriptBase nsb;
    
            public NestedClass(NinjaScriptBase ninjaScriptBase)
            {
                nsb = ninjaScriptBase;
            }
    
            public double GetSomething()
            {
                return nsb.Closes[0][0] + 10 * nsb.TickSize;
            }
        }
    }
    The question is, if running several instances of the same strategy (for different instruments), would all be using the same static nsb field of the nested class? If yes, what would happen? An instrument might read the Close of another instrument?

    In general what should be the right approach to accessing Closes[][], CurrentBars[], Times[][] and so on from a nested class?

    Thank you.

    #2
    Hello digibob, thanks for your post.

    If the static class is within the Strategy class then a different static object would be made for each instance of the strategy at runtime.

    I found this post on StackOverflow that provides some additional solutions:

    https://stackoverflow.com/questions/...ia-nested-type

    The third solution on that page would not work since you can not directly instantiate strategy or indicator classes. Either making it static or inheriting the class instead of nesting it would be good solutions.

    Kind regards.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi Chris. Thank you for your reply. The two (relevant) solutions on StackOverflow define static in the outer class, as opposed to my code above where static is defined in the nested class. Does it make a difference where the static is defined (between outer and nested)?

      The inheritance solution uses a static too, thus I don't see the gain here. I would have liked to get rid of the static if possible. Though it seems there is no way around it.

      If the static class is within the Strategy class then a different static object would be made for each instance of the strategy at runtime.
      Just to make sure I understand, since you say static class whereas i'm using a static variable, so is my code safe for running several strategy instances?

      Thanks. Kind regards.

      Comment


        #4
        The answer to my question in the previous post is no, my code was not safe. I ran into inconsistent errors during optimization that showed the static is shared across the strategy instances (which is what you'd expect from a static, after all). So instead of passing in NSB to the nested class' constructor, it is now passed in directly to the nested class' method on each call, and there is no static involved. I'm not sure how come it works now (in optimization) as in all the examples I see, including those you referenced on StackOverflow, there is always a static involved, and MSDN and people on forums stress that a static is needed in order to access parent class' members.

        Could you please confirm that the below code (modification of the code in my first post) will properly run in realtime and each strategy instance will be reading only its own data and nothing would break? Thanks.

        Code:
        public class MyStrategy : Strategy
        {
            NestedClass nestedClass;
        
            protected override void OnStateChange()
            {
                if (State == State.DataLoaded)
                    NestedClass nestedClass = new NestedClass();
            }
        
            protected override void OnBarUpdate()
            {
                double something = nestedClass.GetSomething(this);
            }
        
            public class NestedClass
            {
                public NestedClass()
                {
                    // constructor
                }
        
                public double GetSomething(NinjaScriptBase nsb)
                {
                    return nsb.Closes[0][0] + 10 * nsb.TickSize;
                }
            }
        }

        Comment


          #5
          Hidigibob, Thanks for your reply.

          After testing this a bit more I found that inheritance or static classes would not be of much use here since your need the NinjaScriptBase object anyway. You might have noticed that we never directly instantiate indicator objects in our code. The NinjaTrader core has a factory that makes the object for us. Consequently, we would need to pass the NinjaScript object context if you want your sub-class to have access to the same data arrays (Close, Open, etc.). The implementation in your last post is a good solution, you can also make the nested class a partial class on Indicator and pass in the NinjaScriptBase object when you create the object e.g.

          Code:
          public class test : Indicator
          {
          NestedClass nestedClass;
          ...
          else if (State == State.DataLoaded)
          {
          nestedClass = new NestedClass(this);
          }
          
          protected override void OnBarUpdate()
          {
          //NestedClass nc = new NestedClass();
          Print(nestedClass.GetSomething());
          }
          
          public partial class NestedClass : Indicator
          {
          private NinjaScriptBase nsb;
          public NestedClass(NinjaScriptBase _nsb)
          {
          nsb = _nsb;
          }
          
          public double GetSomething()
          {
          return nsb.Closes[0][0] + 10 * nsb.TickSize;
          }
          }
          }
          Last edited by NinjaTrader_ChrisL; 06-25-2019, 07:53 AM.
          Chris L.NinjaTrader Customer Service

          Comment


            #6
            Hi Chris. Thanks for the confirmation + the example code. Appreciated.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by PeterGolz, 01-17-2020, 10:42 AM
            11 responses
            72 views
            0 likes
            Last Post reubenlasky  
            Started by utilizator, Today, 11:36 AM
            0 responses
            4 views
            0 likes
            Last Post utilizator  
            Started by EnveousColnago, Today, 07:24 AM
            1 response
            11 views
            0 likes
            Last Post NinjaTrader_ShawnB  
            Started by tornadoatc, Today, 10:31 AM
            0 responses
            4 views
            0 likes
            Last Post tornadoatc  
            Started by EnveousColnago, Yesterday, 05:43 PM
            6 responses
            34 views
            0 likes
            Last Post marcus2300  
            Working...
            X