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

Calling Update() not from the property

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

    Calling Update() not from the property

    If indicator I1 is accessing two vars of indicator I2, is it OK to avoid a redundant call to I2 Update() from the vars get properties and instead just call I2 Update() directly?

    From this (I1 code):

    // I2 vars properties contain Update()
    Print(I1().var1);
    Print(I2().var2);

    To this:

    // I2 vars properties don't contain a call to Update()
    I2().Update();
    Print(I1().var1);
    Print(I2().var2);

    #2
    savekad,

    I can't think of any reason that would cause any issues. Let us know if you run into any.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by savekad View Post
      If indicator I1 is accessing two vars of indicator I2, is it OK to avoid a redundant call to I2 Update() from the vars get properties and instead just call I2 Update() directly?

      From this (I1 code):

      // I2 vars properties contain Update()
      Print(I1().var1);
      Print(I2().var2);

      To this:

      // I2 vars properties don't contain a call to Update()
      I2().Update();
      Print(I1().var1);
      Print(I2().var2);
      I would like to be corrected but I doubt that that would work correctly. For one thing I2().Update(); seems to be what happens with OnBarUpdate(), so looks redundant.

      The way I understood it, we need to Update() in the Property getter because the asynchronous, event-driven nature of NT means that the value may not yet have been updated when we try to access it, so we need to force an update. Somehow, I think this will probably be more of an issue if COBC is false, but I can still envisage a scenario where it could still be an issue with COBC as true.

      Comment


        #4
        koganam, can you elaborate?

        I think that even if COBC is false, it would still be faster to call OBU just once for any further var accesses than to fire it every time you access a var from the same OBU call of the original indicator (I1 in our case), wouldn't it?

        Comment


          #5
          Originally posted by savekad View Post
          koganam, can you elaborate?

          I think that even if COBC is false, it would still be faster to call OBU just once for any further var accesses than to fire it every time you access a var from the same OBU call of the original indicator (I1 in our case), wouldn't it?
          It will be faster, as you are evidently calling updates less often. I doubt if it will be accurate, again because one may be accessing a value that has not yet been updated, because the system was off doing something else. Such is the nature of asynchronous events.

          Comment


            #6
            So you say that by removing Update() from a var getter, it might be inaccurate to access it even if the access is made right after an explicit call to Update() ?

            Comment


              #7
              Hello,

              Update() will only trigger if it needs an Update to get "up to speed". So you wont waste unneeded CPU cycles if the value is already up to date. Its always a good idea to have Update there as an insurance policy to make sure you always have up to date and correct values.

              -Brett

              Comment


                #8
                Clarification on Update() please

                Originally posted by NinjaTrader_Brett View Post
                Hello,

                Update() will only trigger if it needs an Update to get "up to speed". So you wont waste unneeded CPU cycles if the value is already up to date. Its always a good idea to have Update there as an insurance policy to make sure you always have up to date and correct values.

                -Brett
                Hello Brett,

                I have been wondering about this because I am refactoring some code into multiple indicators to be used by other indicators and strategies. Can you elaborate on your statement, "Update() will only trigger if it needs an Update to get "up to speed"."?

                I take this to mean that if I call Update() from within a property or from outside the indicator, the OnBarUpdate() method will only be called if it needs to "get up to speed". How does it know whether or not it needs to get up to speed?

                I was thinking that, for when COBC is true, I could put something like the following in the beginning of the OnBarUpdate() method, but it sounds from your comment that this would be redundant; that NT already manages that.

                Code:
                private int lastBarCalced = -1;
                
                protected override void OnBarUpdate()
                {
                     if (CurrentBar = lastBarCalced) return;
                     lastBarCalced = CurrentBar;
                     // continue with the method...
                }
                If my understanding is correct, does NT also "know" when an update is not needed for when COBC is false?

                Many Thanks.

                Scott
                sh_daggett
                NinjaTrader Ecosystem Vendor - NinjaLaunchpad

                Comment


                  #9
                  Hello,

                  Sure basically its a good idea to add Update() in the property so that when access a value it makes sure that you have the most update to date variables as possible.

                  How it works is it will update to the current CurrentBar index if it is not already there.

                  If COBC=False however this would always force an update since we would have to assume we are not up to date since we have no index to check against as your question lead me believe that is what you are asking on.

                  There is no cost saving measures we could do on COBC = false like what we can on COBC = true.

                  -Brett

                  Comment


                    #10
                    Originally posted by sh_daggett View Post
                    ...
                    Code:
                    private int lastBarCalced = -1;
                     
                    protected override void OnBarUpdate()
                    {
                         if (CurrentBar = lastBarCalced) return;
                         lastBarCalced = CurrentBar;
                         // continue with the method...
                    }
                    Scott
                    That sequence seems to be identical to just using "if (FirstTickOfBar)...". No?

                    Comment


                      #11
                      Originally posted by koganam View Post
                      That sequence seems to be identical to just using "if (FirstTickOfBar)...". No?
                      Hmmmm....well, I guess I was thinking that Update() might be called as a result of OnMarketData() accessing a property of a hosted indicator, in which case OnBarUpdate() of the hosted indicator might be called intrabar, even if COBC is true. I think Brett is saying that I don't need to worry about it because NT handles it in such a way that the extra calls to OnBarUpdate() in the hosted indicator don't happen. I'm probably just confused. What am I missing? (And thanks for the reply and your interest. I appreciate it!)

                      Scott
                      sh_daggett
                      NinjaTrader Ecosystem Vendor - NinjaLaunchpad

                      Comment


                        #12
                        Hello,

                        Right if COBC = true you should not have to worry about the extra calls as NT wont force OnBarUpdate to run if it already has processed that bar. If COBC = false however then it would recalculate each time.

                        -Brett

                        Comment


                          #13
                          Originally posted by NinjaTrader_Brett View Post
                          Hello,

                          Update() will only trigger if it needs an Update to get "up to speed". So you wont waste unneeded CPU cycles if the value is already up to date. Its always a good idea to have Update there as an insurance policy to make sure you always have up to date and correct values.

                          -Brett
                          This should go into the help/documentation. It is really important piece of puzzle and there is nothing like this in the docs. It would be really helpful for all developers to know this.

                          Comment


                            #14
                            misova, thanks for the pointer, we're always looking to enhance our documentation - I will make sure this part gets reviewed for our next major platform update implementation of Update().

                            Currently we discuss those topics here -



                            BertrandNinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by helpwanted, Today, 03:06 AM
                            1 response
                            12 views
                            0 likes
                            Last Post sarafuenonly123  
                            Started by Brevo, Today, 01:45 AM
                            0 responses
                            9 views
                            0 likes
                            Last Post Brevo
                            by Brevo
                             
                            Started by aussugardefender, Today, 01:07 AM
                            0 responses
                            5 views
                            0 likes
                            Last Post aussugardefender  
                            Started by pvincent, 06-23-2022, 12:53 PM
                            14 responses
                            242 views
                            0 likes
                            Last Post Nyman
                            by Nyman
                             
                            Started by TraderG23, 12-08-2023, 07:56 AM
                            9 responses
                            387 views
                            1 like
                            Last Post Gavini
                            by Gavini
                             
                            Working...
                            X