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

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, 08: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 funk10101, Today, 12:02 AM
            1 response
            11 views
            0 likes
            Last Post NinjaTrader_LuisH  
            Started by GLFX005, Today, 03:23 AM
            1 response
            6 views
            0 likes
            Last Post NinjaTrader_Erick  
            Started by nandhumca, Yesterday, 03:41 PM
            1 response
            13 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by The_Sec, Yesterday, 03:37 PM
            1 response
            11 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by vecnopus, Today, 06:15 AM
            0 responses
            1 view
            0 likes
            Last Post vecnopus  
            Working...
            X