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

When to use double vs Series<double>

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

    When to use double vs Series<double>

    Been working on writing a strategy in NinjaTrader for the last 2 months and so far the hardest part for me has been understanding when to initialize a variable as a private double or a private Series<double>. Ironically it's been easier to understand when an indicator requires either; is your number based on a chart value? If yes then it's most likely a double. Is it a static value, or at least static when it's called to update? It's probably a double.

    I've found knowing when to implement either in a strategy much more difficult. Anyone have any good rules on when they like to use a double vs series double? Would you use a series double for an indicator output in the strategy?

    #2
    Think deeper.

    If you need to maintain a value on a per bar basis -- then you need a Series.

    Note I said 'maintain' -- this means more that just 'calculate'.
    For ex, if you need the past values from past calculations to calculate the newest value.

    See that part about "needing past values"? Well, that's how you know you need a Series.
    The results of past calculations, one result per bar, can be easily maintained in a Series.

    Once you see the elegance of it all, you can then realize that the whole point of the Series
    concept is to maintain past values on a per bar basis.

    On the other hand, some times all you care about is the current value of something,
    such as the current Unrealized Profit, or the current total of all your losses during
    the current session. You usually don't need these values saved on a per bar basis, the
    current value is independent of the bars where the trades occurred, and this current
    up to date value is always what you want to know. You don't care about the PnL 2 or
    3 bars ago, like you might when accessing Close[2] or EMA(34)[3].

    See the difference?
    It boils down to needing 'past' values vs needing just the 'current' value.

    Even if all you need is the 'current' value, but you need the 'past' values to get to
    that 'current' value, well, guess what, you'll still need a Series for those 'past' values,
    but only if those past values really mean: one past value per each bar.

    Burn this into your brain: a Series means saving values on a per bar basis.
    If you don't need to do that, you don't need a Series.
    Last edited by bltdavid; 09-12-2021, 10:56 PM.

    Comment


      #3
      Hello itsthefriz,

      bltdavid is correct. A double variable saves a single double value. A Series<double> is a collection that has a slot for every bar on the chart to save a double value.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        bltdavid Thanks for the reply. So if someone was to sum it up the answer would simply be "to have access to a historical value at a future date?" I would assume this is why most indicators that use a rolling window of values require a series - because they would need the values stored through that entire rolling window?

        On the opposite hand a double would simply be the value at that instant and nothing else?

        I'm working up a strategy and I'd like to monitor the change in a variable over time and the variable is made up of several sub variables. If I was to plot my variable would I then need to make it a series (because the plot should be holding historical data) in which case all the variables making up the plotted variables would also need to be a series?

        Comment


          #5
          Yes, "past values" is exactly equal to "historical values".

          The answer is "yes" on all counts.

          I think you have it.

          I might add this: when the word "plot" is used, then, by definition,
          this involves "historical values" (Why? Because OnRender occasionally
          must redraw the entire chart, and each individual indicator's OnRender
          must redraw their own plots, so all plot data is stored in a Series. Why?
          Because "past values on a per bar basis" is exactly what a Plot is.)

          In other words,
          "Every Plot, by definition, has a Series<double> associated with it".

          Last edited by bltdavid; 09-15-2021, 01:06 AM.

          Comment


            #6
            Thanks blt! That helps a lot to clarify things!

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Kaledus, Today, 01:29 PM
            5 responses
            12 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by Waxavi, Today, 02:00 AM
            1 response
            8 views
            0 likes
            Last Post NinjaTrader_LuisH  
            Started by alifarahani, Today, 09:40 AM
            5 responses
            23 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by gentlebenthebear, Today, 01:30 AM
            3 responses
            16 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by PhillT, Today, 02:16 PM
            2 responses
            7 views
            0 likes
            Last Post PhillT
            by PhillT
             
            Working...
            X