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

C# NinjaScript Indicator Best Practices

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

    C# NinjaScript Indicator Best Practices

    I'm trying to understand if there are any performance advantages in having an often-used calculation be calculated once via an indicator, and then re-used by other "hosting" indicators on the same chart/series/timeframe.

    Of course as a developer there are advantages, in that I only have to write the calculation code once, and it's easier to maintain. But I'm wondering what the performance advantages are, if any....

    I've asked a number of questions and NT support has been incredibly helpful in answering my emails. Thank you NT! I'll post here from now on as I haven't been able to find any threads on this topic.

    Currently I'm a bit stumped. I have two answers that seem to conflict. Perhaps my understanding of "hosting" is incorrect? Is there a difference between "hosting" and just accessing an indicator's exposed dataseries? Or perhaps the answer varies depending on whether COBC is true/false/left out?

    Below are the two questions/answers that seem to conflict:

    Question 1) I'm using the following code quite often:

    double changeCC = Close[0] - Close[1]

    ... I'm using this in many different indicators, all applied to the same dataseries (SPY 1min). I'm wondering if it is faster to:

    a) calculate double changeCC = Close[0] - Close[1] in just one indicator, store the result in a dataseries that is publicly visible, then reference that stored value from other hosting indicators. The assumed advantage is I'm not accessing the index values of Close[0] & Close[1] more than once in the original hosted indicator.

    b) just call double changeCC = Close[0] - Close[1] once inside each indicator that needs it. The thinking here is that it might be more taxing on system resources to access a dataseries value from another indicator rather than calculate it again. And here I'm guessing the answer depends on how complicated the calculation is... for something simple like Close[0] - Close[1] it might be faster to calculate again inside the indicator.

    Answer 1)
    1) As the indicator you are accessing has already been added to the script it exists in memory, it is less resource consuming to call the value from the indicator than to recalculate it.


    Question 2)
    2) Suppose I have an indicator called RangeRatio plotted on a chart. Then on that same chart I have a 2nd plot (hosting indicator) which uses RangeRatio in it's calculation. Is the hosting indicator using the same instance of RangeRatio that is already on the chart? Or are there now two instances in memory... one for the RangeRatio plot, and a second for the indicator accessing the RangeRatio calculation? And if two, then do I correctly understand RangeRatio is being calculated twice, once for each instance, upon every incoming tick?

    Answer 2)
    Yes, the call from the hosting indicator and the hosted indicator will occupy two instances in memory, calculate separately, and perform the calculations twice on each tick if CalculateOnBarClose is false.
    Last edited by ntfred; 02-28-2013, 07:43 PM.

    #2
    Hello ntfred,

    To answer a little more about resource usage with data series for answer 1, an indicator will only do calculations for new bars.

    If you put the calculations in an indicator, the calculations it does will be done once and the value from those calculations will be stored in the Values array for that indicator.

    This means when a new bar is made, only calculations will be done for that bar. All previous bars will already have their calculations done and the results of which stored in Values.

    That being said, the Close[0] - Close[1] calculation would need to be done on each bar as data comes in. Thus the amount of resources (memory and cpu usage) would be the same whether in an indicator or being calculated on the fly. This is because even if this was in an indicator the information you are requesting is coming from new data (Close[0]).

    But as an example, if you were however using Close[1] - Close[2] in the indicator, the calculation for this would already be stored and would not need new calculations and thus use less resources.

    For answer 2, each added instance of an indicator will take a separate location in memory. While answer 1 is still true, and indicators will only calculate for new data, they still take their own amounts of memory.

    Let me know if this does not resolve your inquiry.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you Chelsea, I understand everything you said. I think the way I worded my question was perhaps not clear.

      Consider the following example:

      - I create a new 1min chart of SPY.
      - I add to that chart a single indicator called "RangeRatio", which is plotted as a histogram in panel two.
      - at this point in time there are in existence exactly TWO dataseries... one for the SPY data and one for RangeRatio (which derives it's calcs from SPY 1min data and nothing else)
      - now I add a second indicator called "RangeRatioPlotLine", which looks like an OBV line and is plotted in panel three.

      RangeRatioPlotLine is my custom "hosting" indicator. By design it gets all of it's input data from RangeRatio, which is already on the chart. Data from RangeRatio comes in, RangeRatioPlotLine does some further calcs and plots a line.
      - at this point in time there "should" be exactly THREE dataseries... one for SPY, one for RangeRatio and one for RangeRatioPlotLine.

      My question: is RangeRatioPlotLine receiving it's input data from the EXACT SAME RangeRatio instance that is being calculated and plotted on the chart? Or is there now a new and separate secondary RangeRatio instance that was created when RangeRatioPlotLine was added to the chart?

      My assumption was that there would NOT be any new instances created since it is not necessary.

      I read the NinjaScript Launch Pad Book, and got the impression that there would NOT be any new/extra instances in memory. I've also read as many forums as I can find on the topic but now the answer seems unclear to me.

      Many thanks in advance for helping to clarify...

      Comment


        #4
        Hello ntfred,

        Thanks for your reply.

        at this point in time there "should" be exactly THREE dataseries... one for SPY, one for RangeRatio and one for RangeRatioPlotLine.
        This is incorrect. If each of these indicators uses only the primary data series there will only be one instance of the data series array. So there will only be one data series, the indicators will be referencing that same data series array.

        If, however, you use the Add() call to add a secondary time frame or to add a different instrument within these indicators, then there will be one new data series for each extra time frame or instrument per added indicator.

        is RangeRatioPlotLine receiving it's input data from the EXACT SAME RangeRatio instance that is being calculated and plotted on the chart?
        Each added instance of the indicator, on the other hand, will be supplying their values separately to their host (either chart or calling indicator). The calculations may be the same but they will all be using their own space in memory and coming to the value separately.


        Let me know if I can still be of assistance.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks Chelsea for the clarification. Very helpful. I think I get it now. Just to make sure I understand, would the following example be correct?

          Originally posted by ntfred View Post
          - I create a new 1min chart of SPY.
          - I add to that chart a single indicator called "RangeRatio", which is plotted as a histogram in panel two.
          - now I add a second indicator called "RangeRatioPlotLine", which looks like an OBV line and is plotted in panel three.

          RangeRatioPlotLine is my custom "hosting" indicator. By design it gets all of it's input data from RangeRatio, which is already on the chart. Data from RangeRatio comes in, RangeRatioPlotLine does some further calcs and plots a line.
          So now at this point in time the following exist in memory:
          - a BarsArray dataseries for SPY
          - one instance of the RangeRatioPlotLine indicator (hosting)
          - two instances of the RangeRatio indicator
          ----- the 1st one which is being plotted on the chart
          ----- the 2nd one which is being hosted by RangeRatioPlotLine

          Is this correct?

          Comment


            #6
            Hello ntfred,

            Yes this would be correct.

            Please let me know if I can still be of assistance.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Great, thanks for all the help!

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by CortexZenUSA, Today, 12:53 AM
              0 responses
              1 view
              0 likes
              Last Post CortexZenUSA  
              Started by CortexZenUSA, Today, 12:46 AM
              0 responses
              1 view
              0 likes
              Last Post CortexZenUSA  
              Started by usazencortex, Today, 12:43 AM
              0 responses
              5 views
              0 likes
              Last Post usazencortex  
              Started by sidlercom80, 10-28-2023, 08:49 AM
              168 responses
              2,266 views
              0 likes
              Last Post sidlercom80  
              Started by Barry Milan, Yesterday, 10:35 PM
              3 responses
              13 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Working...
              X