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

Ninjascript under the hood: what I've learned so far...

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

    Ninjascript under the hood: what I've learned so far...

    Through the Visual Studio debugger and Print() commands, I've learned the following:

    1. When an indicator method is encountered in a strategy (e.g. SMA(Period)), the program checks to see if that indicator already exists in the cache, and if it does, then the indicator's OnBarUpdate( ) is run. If that indicator is NOT found in the cache, then a new indicator object is initialized, and then the indicator's OnBarUpdate( ) is run for all bars up to the current bar.

    2. Code from an indicator's OnBarUpdate( ) does not run in the background at the close of each bar, but rather only runs when the indicator is referenced in the strategy's OnBarUpdate(). This distinction is obvious in situations when an indicator is only called sporadically from the strategy. For example, let's say you have anything like this in your strategy:

    Code:
    OnBarUpdate() 
    {
       //statements ...
    
       If SMA(Period)[0]>X
          If EMA(...)
          //Do something
    }
    On bars when the "if SMA.." evaluates to false, EMA's indicator method is not called, and therefore the OnBarUpdate() for EMA is not run. The EMA calculations are therefore behind. Later in the day, when "If SMA..." evaluates to true, EMA's OnBarUpdate is then run for all bars between the last update and the current bar.

    If anyone has other details they know of, please let me know.

    My particular questions at this point are:
    1) Is there a way to make the indicator run in the background so it does not need to "catch up" when it is later called?
    2) Does anyone have any info on how Ninjascript handles "catching up"? I can't imagine this being more efficient than simply calculating on each bar in real time in the first place. Even in strategies' that call an indicator every single bar, the check to see if the indicator is up to date will take a toll (although slight one).
    Last edited by YD777; 07-21-2016, 08:06 AM.

    #2
    Hello YD777,

    I have submitted a feature request to the product management team for this kind of documentation. I will follow up with more information as it is available. If the feature request already exists, a vote will be added to it. Please let us know if there are any other ways we can help.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      My particular questions at this point are:
      1) Is there a way to make the indicator run in the background so it does not need to "catch up" when it is later called?
      Force the call to the indicator by using a dummy variable that evaluates the indicator on every tick.
      2) Does anyone have any info on how Ninjascript handles "catching up"? I can't imagine this being more efficient than simply calculating on each bar in real time in the first place. Even in strategies' that call an indicator every single bar, the check to see if the indicator is up to date will take a toll (although slight one).
      I did not bother to determine how NT catches up, but I presume it would simply run the indicator, which will cause it to calculate all missing values.

      I did not bother because for me, it is simply more intuitive to use a named instance of the indicator, and make it update on every bar. I can then just reference the then, now already current, indicator object. I am not sure if it is necessarily more efficient computationally, but it almost certainly is faster in runtime.

      Comment


        #4
        Thanks for sharing your experiences, Koganam. Could you post a small example of what you would write in your code to accomplish this? Your solution definitely seems to keep your indicator up to date, but wouldn't it still be subjected to whatever hidden check that NT uses to make sure of this? Maybe I'm missing something.

        Comment


          #5
          Assign a double to your EMA that isn't being called every time in OnBarUpdate.

          Comment


            #6
            Originally posted by YD777 View Post
            Thanks for sharing your experiences, Koganam. Could you post a small example of what you would write in your code to accomplish this? Your solution definitely seems to keep your indicator up to date, but wouldn't it still be subjected to whatever hidden check that NT uses to make sure of this? Maybe I'm missing something.
            We shall use the ubiquitous SMA as an example.
            You have to declare the SMA variable as a class variable.
            Then you can assign/instantiate it in OnStartUp().
            Then you use it in OnBarUpdate().

            Because it was defined as a object type (a named instance of the class), you assign it without the "new" keyword.

            Code:
            private SMA yada_sma; //this is a named instance of the SMA.
            double dummy = 0.0; //this dummy variable will hold the value of our named instance.
            Code:
            protected override void OnStartUp()
            {
            yada_SMA = SMA(Close, 5);
            //etc;
            }
            Code:
            protected override void OnBarUpdate()
            {
            //here is where you use your named instance;
            //To force it to update on evey tick, just assign it to a dummy variable,
            //with local slope or class slope. I use class scope here, as it is, 
            //arguably, more efficient.
            
            dummy = yada_SMA[0];
            
            //et.c.
            }

            Comment


              #7
              Originally posted by YD777 View Post
              Thanks for sharing your experiences, Koganam. Could you post a small example of what you would write in your code to accomplish this? Your solution definitely seems to keep your indicator up to date, but wouldn't it still be subjected to whatever hidden check that NT uses to make sure of this? Maybe I'm missing something.
              If you add this indicator to a chart of the same time frame - it sounds like that would work too (keeping it up to date)?

              Comment


                #8
                Is there a way in NT8 to bypass this a #FORCE ALL UPDATES?

                Should there be?

                Just curious....

                Comment


                  #9
                  Originally posted by koganam;

                  [CODE
                  private SMA yada_sma; //this is a named instance of the SMA.
                  double dummy = 0.0; //this dummy variable will hold the value of our named instance.[/CODE]
                  Code:
                  protected override void OnStartUp()
                  {
                  yada_SMA = SMA(Close, 5);
                  //etc;
                  }
                  Code:
                  protected override void OnBarUpdate()
                  {
                  
                  dummy = yada_SMA[0];
                  
                  //et.c.
                  }
                  Thanks, this is similar to what I always do, except I had to add the dummy variable you suggested. Nice trick. The code does run faster, but I still believe NT's hidden check of completeness would still run each time "dummy = yada_SMA[0[" is encountered in the code (which would now be every bar). I'm sure it's not heavy on resources, but nonetheless seems inefficient and unavoidable since it's occurring in NT code we can't see/change.

                  Comment


                    #10
                    Originally posted by YD777 View Post
                    Thanks, this is similar to what I always do, except I had to add the dummy variable you suggested. Nice trick. The code does run faster, but I still believe NT's hidden check of completeness would still run each time "dummy = yada_SMA[0[" is encountered in the code (which would now be every bar). I'm sure it's not heavy on resources, but nonetheless seems inefficient and unavoidable since it's occurring in NT code we can't see/change.
                    No. NT is no longer trolling the cache, because it does not need to. You are referencing a specific named object.

                    That is the point of OOP. If you specify an object, its location is always known. And you are keeping the object up to date.

                    Then again, it is really your choice. Stick on a profiler and check it for yourself. In a simple case, it might not make much difference. In most use cases where the indicator has to be referenced multiple times, and COBC is false, you will see what a difference it makes. So much so, that in NT8, NT Development has finally itself adopted the model in its Strategy Builder.

                    You might want to read this post: it explains how we came to be here now.

                    ref: http://ninjatrader.com/support/forum...87&postcount=3
                    Last edited by koganam; 07-22-2016, 12:54 PM.

                    Comment


                      #11
                      Originally posted by koganam View Post
                      No. NT is no longer trolling the cache, because it does not need to. You are referencing a specific named object.

                      That is the point of OOP. If you specify an object, its location is always known. And you are keeping the object up to date.

                      Then again, it is really your choice. Stick on a profiler and check it for yourself. In a simple case, it might not make much difference. In most use cases where the indicator has to be referenced multiple times, and COBC is false, you will see what a difference it makes. So much so, that in NT8, NT Development has finally itself adopted the model in its Strategy Builder.

                      You might want to read this post: it explains how we came to be here now.

                      ref: http://ninjatrader.com/support/forum...87&postcount=3
                      I understand how referencing a specific object will let us avoid searching the cache to find the object. But that specific object contains a dataseries holding a value for each bar, right? How does NT know when the object's dataseries is complete or not? Your code is forcing the object to be updated each bar, but I assume NT has no way to know that, and therefore still has to check the dataseries for completeness each time? And if that's the case, avoiding that check would be an additional way to speed up the code, in addition to the avoiding the cache search you've already described.

                      I'm brand new to NT, so please forgive me if I'm way off. I appreciate your expertise on the subject.

                      Comment


                        #12
                        Doesn't calling Update() on the indicator do what you want?

                        http://ninjatrader.com/support/helpG...t7/?update.htm

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by jjanguda, Today, 10:15 PM
                        0 responses
                        7 views
                        0 likes
                        Last Post jjanguda  
                        Started by ScorpioTravis, Today, 09:41 PM
                        0 responses
                        6 views
                        0 likes
                        Last Post ScorpioTravis  
                        Started by DTSSTS, Today, 09:41 PM
                        0 responses
                        8 views
                        0 likes
                        Last Post DTSSTS
                        by DTSSTS
                         
                        Started by Segwin, Today, 06:33 PM
                        1 response
                        26 views
                        0 likes
                        Last Post NinjaTrader_ShawnB  
                        Started by bmaltz, Today, 08:14 PM
                        1 response
                        20 views
                        0 likes
                        Last Post NinjaTrader_ShawnB  
                        Working...
                        X