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

Accessing additional tick data series during first bar

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

    Accessing additional tick data series during first bar

    I've added a Series<double> and initialized it to the main bars object like this:
    Code:
    private Series<double> test;
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Indicator here.";
                    Name                                        = "DateTest";
                    Calculate                                    = Calculate.OnEachTick;
                    IsOverlay                                    = false;
                    DisplayInDataBox                            = true;
                    DrawOnPricePanel                            = true;
                    DrawHorizontalGridLines                        = true;
                    DrawVerticalGridLines                        = true;
                    PaintPriceMarkers                            = true;
                    ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    IsSuspendedWhileInactive                    = true;
                }
                else if (State == State.Configure)
                {
                    AddDataSeries("ES 03-19", BarsPeriodType.Tick, 1);               
                }
                else if (State == State.DataLoaded)
                  {
                    test = new Series<double>(BarsArray[0]);
                  }        
            }
            protected override void OnBarUpdate()
            {            
                 //if the secondary series is not loaded yet, return
                 if(CurrentBars[1] == -1) {
                     return;
                 }else{
                     test[0] = Closes[1][0]
                 }            
            }
    My questions is, how do I assign a value to test[0] on the first bar? It is throwing this error:
    Code:
    Indicator 'DateTest': Error on calling 'OnBarUpdate' method on bar -1: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
    If I put an additional line at the top like this:
    Code:
    if(CurrentBars[0] == -1) return
    Then it skips all of the ticks that are a part of BarsArray[1] and does not process any of them. I was under the impression that if the second bars array's data is available to the OnBarUpdate() method, then the first bars data must be there too since it is always called first. I need to process the ticks that are available in BarsArray[1] and assign a value to the first bar of test[0] but test[0] is not able to be assigned to because BarsArray[0] is returning -1. If I wait until BarsArray[0] is not returning -1, then I miss all the ticks. What am I doing wrong here?

    #2
    Hello swcooke,

    You will need to check that each series has at least one bar.
    Code:
    if (CurrentBars[0] < 1 || CurrentBars[1] < 1)
    return;
    https://ninjatrader.com/support/help...urrentbars.htm

    If there were two added series you would also check that CurrentBars[2] is greater than 0.


    If you would want to ensure your code is only run during BarsInProgress 1.
    Code:
    if (BarsInProgress != 1 || CurrentBars[1] < 1)
    return;
    https://ninjatrader.com/support/help...inprogress.htm
    Last edited by NinjaTrader_ChelseaB; 02-17-2019, 09:12 PM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      What do I do with all of the ticks that occur on BarsArray[1] while BarsArray[0] is returning -1? Ignore them?

      Comment


        #4
        Hello swcooke,

        That depends on which bars in progress this code is running on and what data you are trying to access.

        If the code is running on BarsInProgress 1 and doesn't reference the primary bar in any way, then you can start processing on the first bar.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I don't think so but I am not the expert here. BarsArray[1] is being processed and I try to assign to my custom series, my custom series is not yet available until BarsArray[0] is not -1. However, if I wait until BarsArray[0] is not -1, I miss all the ticks that BarsArray[1] does have for processing. Clearly I am missing something. Can you share this with someone else there to see if they have any ideas? Is there an example that demonstrates how to process Bid/Ask changes like this? I have reviewed the Developing for Tick Replay section of the KB and I am not seeing this clearly.

          Comment


            #6
            Hello swcooke,

            That is correct, if there isn't a bar on the chart during those ticks for the other series, then no plot value for that bar can be set for that bar and those ticks will need to be ignored.

            Or you store them and then plot them with a later bar combined with all of the information for that later bar.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I would like to do as you suggest and store them in the CustomSeries I created. However that is not available until after BarsArray[0] has a CurrentBar that is not > -1. This API sucks. Been trying to 6 months to learn this damn thing with no luck. Does not seem very intuitive at all. Why give me all of my secondary bar data and not make the primary bar available to sync a custom series to?

              Comment


                #8
                Hello swcooke,

                I'm not understanding.

                A custom Series<T> by default is synchronized to the primary series.

                Below is a public link to the help guide.


                When you mention:
                "Why give me all of my secondary bar data and not make the primary bar available to sync a custom series to?"

                This is incorrect. A custom series is synced to the primary series.


                If you are adding a secondary series of a smaller granularity than the primary, the primary won't have a bar for the first few updates of the secondary. You can wait until the primary updates and then loop through all of those secondary bars if you want and then plot them or do whatever you want with them.

                If something doesn't exist, it doesn't exist. If you want to plot something on the screen, then the plot needs a bar to plot on. You can plot whatever values you want, once that slot is available.


                I'm not sure how the API (Application Programming Interface) is involved. NinjaScript is not using the NinjaTrader API. NinjaScripts are compiled and run internally in NinjaTrader.



                As far as storing the value, you can store this in a double value if you just need a single updated value, or in an array, or list, or whatever structure fits the data. If its double values you could use a List<double> object.

                But those secondary bar values are already in a series. You can probably just wait until the primary closes and then go through those bars do whatever calculations you want and then plot whatever values from it you want.
                Chelsea B.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by bortz, 11-06-2023, 08:04 AM
                47 responses
                1,610 views
                0 likes
                Last Post aligator  
                Started by jaybedreamin, Today, 05:56 PM
                0 responses
                9 views
                0 likes
                Last Post jaybedreamin  
                Started by DJ888, 04-16-2024, 06:09 PM
                6 responses
                19 views
                0 likes
                Last Post DJ888
                by DJ888
                 
                Started by Jon17, Today, 04:33 PM
                0 responses
                6 views
                0 likes
                Last Post Jon17
                by Jon17
                 
                Started by Javierw.ok, Today, 04:12 PM
                0 responses
                16 views
                0 likes
                Last Post Javierw.ok  
                Working...
                X