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

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.



    JesseNinjaTrader Customer Service

    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 doihaveto13, Today, 12:46 PM
      2 responses
      4 views
      0 likes
      Last Post doihaveto13  
      Started by Vitamite, Today, 12:48 PM
      1 response
      7 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by lorem, 04-25-2024, 09:18 AM
      15 responses
      64 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by owen5819, 05-11-2024, 02:24 PM
      3 responses
      21 views
      0 likes
      Last Post owen5819  
      Started by MrAdamSK, 04-02-2024, 11:25 PM
      3 responses
      41 views
      0 likes
      Last Post NinjaTrader_Zachary  
      Working...
      X