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 CarlTrading, 03-31-2026, 09:41 PM
      1 response
      80 views
      1 like
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      40 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      63 views
      2 likes
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      63 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      54 views
      0 likes
      Last Post CarlTrading  
      Working...
      X