Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Where to instantiate a class?

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

    Where to instantiate a class?

    Hey Everyone,

    I am trying to do some date calculations on bars to track time periods for an indicator.

    I created a class to do the work, and as a part of the class I need to hold some state for prior bar information.

    My question is, where do I instantiate this class? My thought is State.Configure or State.SetDefaults, but Im not sure. I know I cant do it on bar update because it will be constantly instantiating a new class and thus I can't hold state.

    thanks.

    #2
    Hello ErikY,

    You could likely use State.Configure/DataLoaded/Historical for that purpose. It just depends on what your class needs and when you need to access methods or properties in the class.

    If by holding state you mean you need a class instance that can persist a value across multiple bars, you would need a class level variable for that. Holding a class level variable and then instantiating it after State.Defaults but before OnBarUpdate would work.

    From your description you could also mean a Series<T> here. For example you need to do something with a single bars data and the class calculates those data points and returns some value or has methods associated to those values. You could then have a list for all bar data with each object being an instance of that class so you can have historical bars with those calculations as well. A Series<T> can be used for that situation, Series<MyClassType>. https://ninjatrader.com/support/help...ightsub=series
    In that use case you do create a new class for each OnBarUpdate call and store the class instance to the series similar to plotting.



    I look forward to being of further assistance.



    Comment


      #3
      Thanks Jessie. I ended up instantiating it as a local variable in the indicator and then I populate it onBarUpdate and it seems to be working. Thanks for your help!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      670 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      379 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      111 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      575 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      582 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X