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

Initialize() Clarification question...

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

    Initialize() Clarification question...

    Initialize() ....

    if I make a a change to an indicator property, and return to the chart, is Initialize() called again?

    Or is it called only the one time at start up, and that's it, never ever to be called again?


    I made some assumptions that Initialize() was being called after properties are changed in the Indicator Dialog box, but I'm finding that certain things are ignored in Initialize() after properties are modified....

    And somewhere in the forum, I thought I saw that initialize() could call into other indicator functions? Surely I was having a bad dream....


    Thanks

    Matt

    #2
    Originally posted by tazatek View Post
    Initialize() ....

    if I make a a change to an indicator property, and return to the chart, is Initialize() called again?

    Or is it called only the one time at start up, and that's it, never ever to be called again?
    Hi Matt,

    Initialize() is called exactely one time. You can prove it by applying the default constructor to your Indicator including a print statement. You will see, that Initialize() isn't called more often than the constructor.

    If you apply changes of the property grid window, then Ninja re-instantiates the concerning Indicator.

    Regards
    Ralph

    Comment


      #3
      Originally posted by Ralph View Post
      Hi Matt,

      Initialize() is called exactely one time. You can prove it by applying the default constructor to your Indicator including a print statement. You will see, that Initialize() isn't called more often than the constructor.

      If you apply changes of the property grid window, then Ninja re-instantiates the concerning Indicator.

      Regards
      Ralph
      hmmm... maybe I'm missing what you're saying here.

      When I put a print statement in my Initialize method and add it to a chart, I get one statement printed as expected.

      BUT - if I change a property and apply it to the chart - all hell breaks loose!

      1. Initialize is printed at least 4 times when I change a property.
      2. I had no internet connection when I did it, and I had a strange error from a DIFFERENT indicator about not being able to connect to cloud.forex.com (or some such site).

      So to me, this answers the 2 questions I had, and not in a very enlightening way.
      1. Initialize should only be called once (at least mentally, that's how it should work), but is in fact called multiple times.
      2. Indicators can access other indicators.

      I now understand why NinjaTrader is so adamant about not putting too much logic into the Initialize() method... because it can be called more than once, and at intervals that appear random.

      I would sure like some feedback from NinjaTrader as to why an indicator can access another indicators initialize() method, and explain any security implications that this might have.

      Also, to explain when Initialize() is/can be called.

      Matt

      Comment


        #4
        Matt, Initialize() is called many times in the course of normal operation of NinjaTrader. If you open up the indicators window from a chart, Initialize() is called. Then, if you add an indicator to the chart and hit apply Initialize() is called. Now, hit OK to close the indicator window and Initialize() is called again.

        If you hit F5 at any time when an indicator is applied to a chart, Initialize() is called.

        As you've correctly noticed, we do not recommend putting any more logic into Initialize() than necessary. Instead, for things you want to run just once, you can put it in OnBarUpdate() like this:
        Code:
        OnBarUpdate()
        {
            if (CurrentBar == 0)
            {
                // run one-time, when-the-indicator-starts calculations here
            }
        }
        As for why you may be seeing messages from other indicators, Initialize() is called for every indicator when you bring up the indicator window because NT needs to populate the indicator window with the indicators' names, properties, etc. So an indicator doesn't necessarily access another indicator's Initialize() method, they all just get executed at the same time.
        AustinNinjaTrader Customer Service

        Comment


          #5
          Matt, here is a better test-program:
          • Define a static integer counter variable for the concerning indicator.
          • Define a constructor, increment the static variable in the constructor and store this variable as an unique instantiation-ID.
          • Print this ID in Initialize() and Dispose().
          Here is what you will recognise:
          • Initialize() is called once per indicator's instantiation.
          • An indicator is instantiated quite often, but only one of these makes it to OnBarUpdate().
          Regards
          Ralph

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Max238, Today, 01:28 AM
          1 response
          22 views
          0 likes
          Last Post CactusMan  
          Started by giulyko00, Yesterday, 12:03 PM
          2 responses
          10 views
          0 likes
          Last Post giulyko00  
          Started by r68cervera, Today, 05:29 AM
          0 responses
          4 views
          0 likes
          Last Post r68cervera  
          Started by geddyisodin, Today, 05:20 AM
          0 responses
          6 views
          0 likes
          Last Post geddyisodin  
          Started by JonesJoker, 04-22-2024, 12:23 PM
          6 responses
          38 views
          0 likes
          Last Post JonesJoker  
          Working...
          X