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

Most effective way to call indicator?

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

    Most effective way to call indicator?

    I need to read the value of an indicator on every single bar update. What is the most effective way to do this? Right now, I have a line such as: "double dVal = IndicatorName(x,y,z).Series[0]" in OnBarUpdate(). Is there a way to initialize the indicator once, and then use its value? If so, how?

    #2
    Hello trader2be,

    This approach should be fine. The biggest impact on resources could be seen if you were running the strategy with CalculateOnBarClose = false and only needed the indicator calculation once per bar. This sample can help combine both logic so you're not unnecessarily calculating the indicator every tick.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the quick response. I was under the impression that there was a way to declare/instantiate the indicator only once in Initialize() and then simply call its current value in OnBarUpdate(). Is that not the case?

      Comment


        #4
        It's possible to add the indicator to the strategies Initialize() method. Please see here for help with this. The main reason to do this is to do this is to see the indicators on the chart.

        You can also declare the indicator as a variable and there is a potential for small performance increase with this practice. Most users wouldn't see a difference here, but it relates to how many total instances of indicators you're running. If you use a named variable for your indicator, it does not have to iterate through all the cached indicators and this can lead to improved efficiency.

        You can also conditionally call your indicator, but if you need a value once per bar this doesn't offer any advantages.
        Last edited by NinjaTrader_RyanM1; 01-04-2011, 05:39 PM.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Ok, thanks again. My goal is really performance improvement in calling indicator values, so if you tell me that my original implementation in OnBarUpdate() is optimal, I will just stick with that.

          I don't know if it's available but I would be very interested in any tips/tricks to improve NinjaScript performance. I am not a seasoned C# programmer and therefore I would appreciate guidance. Has anyone written anything on this for NT?

          Comment


            #6
            I'm not aware of an article specific to coding. You can see general performance tips here:
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              An update to this thread - My previous response was incorrect and is corrected above.
              Ryan M.NinjaTrader Customer Service

              Comment


                #8
                Here is a good thread on optimizing NinjaScript.

                If you want a dataseries to capture the SMA of the MACD: (Note... MACD12 should have been MyNewMACD) in Variables: private DataSeries myMacdAvg; private bool init = false; private MACD MyNewMACD private SMA SMAofMACD in Initialize(): myMacdAvg = new DataSeries(this); in OnBarUpdate(); if(!init) { MyNewMACD= MACD(12,26,9); SMAofMACD=SMA(MyNewMACD, 7); // 7 period SMA of MACD(12,26,9) init = true; } MyMacdAvg.Set(SMAofMACD[0]); if(MyMacdAvg[0] > 0) // if current value of 7 period SMA of MACD(12,26,9)>0 ( Print("Hello …


                Dan
                eDanny
                NinjaTrader Ecosystem Vendor - Integrity Traders

                Comment


                  #9
                  Originally posted by NinjaTrader_RyanM View Post
                  It's possible to add the indicator to the strategies Initialize() method. Please see here for help with this. The main reason to do this is to do this is to see the indicators on the chart.

                  You can also declare the indicator as a variable and there is a potential for small performance increase with this practice. Most users wouldn't see a difference here, but it relates to how many total instances of indicators you're running. If you use a named variable for your indicator, it does not have to iterate through all the cached indicators and this can lead to improved efficiency.

                  You can also conditionally call your indicator, but if you need a value once per bar this doesn't offer any advantages.
                  Could you show me an example of how to declare the indicator as a variable?

                  Comment


                    #10
                    You would do something like this in your OnStartUp() in NT7 -

                    myIndicator = KAMA(Close, 2, 10, 30);
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      Thanks - I have implemented this change across the board and it seems to be working fine. Hard to quantify the efficiency gains but it does seem to be a cleaner implementation.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by GussJ, 03-04-2020, 03:11 PM
                      16 responses
                      3,281 views
                      0 likes
                      Last Post Leafcutter  
                      Started by WHICKED, Today, 12:45 PM
                      2 responses
                      19 views
                      0 likes
                      Last Post WHICKED
                      by WHICKED
                       
                      Started by Tim-c, Today, 02:10 PM
                      1 response
                      9 views
                      0 likes
                      Last Post NinjaTrader_ChelseaB  
                      Started by Taddypole, Today, 02:47 PM
                      0 responses
                      5 views
                      0 likes
                      Last Post Taddypole  
                      Started by chbruno, 04-24-2024, 04:10 PM
                      4 responses
                      53 views
                      0 likes
                      Last Post chbruno
                      by chbruno
                       
                      Working...
                      X