Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Custom ISeries

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

    Custom ISeries

    Hello,

    i would like to know if it is possible to create a new Series<T> which holds a custom class or a custom object with a few member variables instead of just a single value?

    The definition says:

    Definition

    A Series<T> is a special generic type of data structure that can be constructed with any chosen data type and holds a series of values equal to the same number of elements as bars in a chart.
    https://ninjatrader.com/support/help...ml?seriest.htm

    Could you please tell me how this could be implemented in an indicator?

    Thank you,
    Mike

    #2
    Hello Mike,

    Yes, Series can hold a custom class.

    Code:
    // in the scope of the class
    public Class MyClass
    {
      public double MyProperty;
    }
    
    private Series<MyClass> mySeries;
    
    // in the scope of OnStateChange() when State is State.DataLoaded
    mySeries = new Series<MyClass>(this);
    
    // in the scope of OnBarUpdate()
    mySeries[0] = new MyClass();
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello NinjaTrader_ChelseaB,

      thank you for your reply.
      This works but now i ran into another problem.

      I try to create an indicator which has a second 1 Tick DataSeries and store the incoming tick-data into a new Series<T> object of the Main DataSeries. The indicator should work without TickReplay.

      The Problem is that a new Series<T> item is added on the transition from Historical to Realtime processing.
      Even IsFirstTickOfBar fires on this event when its not even close to a new bar forming on the chart.

      I've attached you my Test-Script.
      You can add it to a 5 Minute chart together with a VOL indicator to see the actual problem.
      Is there any way to fix this?

      I've tried to make this work with Calculate = OnBarClose but then the stored data from the 1-Tick Series are lagging 1 Tick behind the actual ticks.

      Thank you,
      Mike
      Attached Files
      Last edited by mk77ch; 08-22-2023, 01:14 AM.

      Comment


        #4
        Hello Mike,

        I am not seeing this is the case.

        I've opened a 60 minute ES 09-23 chart and added your indicator.

        This indicator is designed not to run the loop to print in historical, but we can see from the output that BarsInProgress 0 prints last time for a historical bar, and then in real-time there are prints for each tick, but there is no bar close in real-time and the new BarItem() class object is not created in real-time as no real-time bar has yet closed.

        In other words, there is no real-time bar close and no new class object when the script transitions to real-time.

        Attached is the output saved to a text file.
        Attached Files
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello NinjaTrader_ChelseaB,

          thank you for your answer.
          The problem is the last "new bar" entry in your attached text file.

          new barItem 38 39 39 Historical 0 True

          this entry happens on the transition and not when the latest bar on the chart opens. the 1 tick prints after this last "new bar" entry are just the cached ticks until the indicator is loaded.

          when you add a volume indicator to your chart, you can see that the values don't match for the last and the current bar with the output of the output log. if you let it run for a few bars, the value will match again.

          Thank you,
          Mike

          Comment


            #6
            Hello Mike,

            new barItem 38 39 39 Historical 0 True

            This would be the last fully closed historical bar.

            Print the time of the bar and you can match this to the second to last bar on the chart.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hey NinjaTrader_ChelseaB,

              if this is the case, then why does the volume which is printed out not matching with the volume of the last and current bar on the chart?

              Thank you,
              Mike

              Comment


                #8
                and why does BarItems[1].vol + BarItems[0].vol match the volume of the current, last bar on the chart?

                Comment


                  #9
                  Hello mk77ch,

                  I'm not seeing that "BarItems[1].vol + BarItems[0].vol" is being printed in this script, but that would be two bars volume.

                  Your print for each tick is only being printed in real-time.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Hey NinjaTrader_ChelseaB, thank you for sticking with me on this one, i am currently not at the pc but i will provide the exact details later on.

                    Thank you,
                    Mike

                    Comment


                      #11
                      Hello NinjaTrader_ChelseaB,

                      i would like to explain again what i try to do and where I currently struggle with NinjaScript.

                      I've created a FootPrint indicator for NinjaTrader (https://nexusfi.com/local_links.php?...27&linkid=2565).
                      This indicator is using Tick Replay.
                      To optimize performance, I would like to rewrite the indicator to use Calculate = OnEachTick without Tick-Replay and to add a 1 Tick DataSerie instead.

                      To store the ladder data for the FootPrint and a lot of other information, I store all this per-bar-data in a custom BarItem class and use a custom Series<T> to store these data for each bar on the chart. As the Series<T> is synchronized with the Chart Bars and comes with a few handy methods, this seems to be ideal for this use-case.

                      So instead of populating my BarItems with data from OnMarketData and TickReplay, I would like to populate the data using the added 1 Tick DataSerie without Tick-Replay.

                      Now the actual problems arise when I try to write data from the 1 Tick DataSerie into my Series<BarItem>, which is synchronized with the ChartBars.

                      There is an explanation here: https://ninjatrader.com/support/help...nstruments.htm on how bar data is referenced (How Bars Data is Referenced).

                      Another info I've got is this one:

                      "When using a secondary data series without tick replay, there is only one Bar Update event for each historical tick of the primary series, and that must be thought of as the LAST, not the first, tick of that bar. So remember that, because BarsInProgress[1] events occur AFTER BarsInProgress[0] events, the subsequent events of the secondary series BarsArray[1] apply to the latest PREVIOUS bar, not the current bar, of the primary series BarsArray[0].​"

                      So my current quest is to find out how I could correctly reference the BarItem I have to write into from my 1 Tick DataSerie.
                      The end goal would be to have a BarItem for each ChartBar with all the needed volume information for that bar (Volume, Volume at Ask, Volume at Bid, Delta, Ladder etc...).

                      I hope this makes things more clear in what I try to achive.
                      Thank you, Mike

                      Comment


                        #12
                        Hello Mike,

                        "So instead of populating my BarItems with data from OnMarketData and TickReplay, I would like to populate the data using the added 1 Tick DataSerie without Tick-Replay."

                        This appears to be what this script does.

                        Without TickReplay the script will be using Calculate.OnBarClose in historical data. This also means any indicator calls would be OnBarClose

                        While I was testing I did not have TickReplay enabled.

                        "how I could correctly reference the BarItem I have to write into from my 1 Tick DataSerie"

                        Any series would be referencing the most recently fully closed bar with barsAgo [0].

                        Below is a link to the help guide on how bar information is referenced.


                        Instead of saving information to the most recently closed bar, hold that information in a variable until the primary bar updates.
                        Code:
                        private BarItem thisBarItem;
                        
                        if(BarsInProgress == 0)
                        {
                            BarItems[0] = thisBarItem;
                            thisBarItem = null;
                        }
                        
                        if(BarsInProgress == 1)
                        {
                            if (thisBarItem == null)
                                thisBarItem = new BarItem();
                        
                            thisBarItem.vol += Volume[0];
                        }
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Hey NinjaTrader_ChelseaB,

                          thank you very much for your message.
                          I changed the code to:

                          HTML Code:
                          private Series<BarItem> BarItems;
                          private BarItem currBarItem;
                          ...
                          
                          Calculate = Calculate.OnEachTick;
                          ...
                          
                          else if(State == State.Configure)
                          {
                              AddDataSeries(Data.BarsPeriodType.Tick, 1);
                          }​
                          else if(State == State.DataLoaded)
                          {
                              BarItems = new Series<BarItem>(BarsArray[0], MaximumBarsLookBack.Infinite);
                          }
                          ...​
                          
                          if(State == State.Historical)
                          {
                             if(BarsInProgress == 0)
                             {
                                BarItems[0] = currBarItem;
                                currBarItem = null;
                             }
                             if(BarsInProgress == 1)
                             {
                                if(currBarItem == null)
                                {
                                   currBarItem = new BarItem();
                                }
                                currBarItem.vol += Volume[0];
                             }
                          }
                          if(State == State.Realtime)
                          {
                             if(BarItems[0] == null)
                             {
                                BarItems[0] = new BarItem();
                             }
                             if(BarsInProgress == 1)
                             {
                                BarItems[0].vol += Volume[0];
                             }
                          }​
                          and it worked!
                          Thank you very much for your time and effort.
                          This has bugged me now for quite some time.

                          All the best,
                          Mike

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          574 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          332 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          101 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                          0 responses
                          553 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          551 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X