Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indexing Error

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

    Indexing Error

    My indicator is throwing an indexing error at the "Print - 1Time" line of code.
    Checking the prints...the system believes it is 6pm and not the current time which I'm guessing is causing the index error. How can I work around this?
    Code:
    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
    {
    Print("0Time"+Time[0]);
    for (int idx = ChartBars.FromIndex; idx <= ChartBars.ToIndex; idx++)
    {
    Print("1Time"+Time[0]);
    if(mid.IsValidDataPointAt(idx))
    {
    Print("2Time"+Time[0]);
    float x = chartControl.GetXByBarIndex(ChartBars, idx);
    float y = chartScale.GetYByValue(mid.GetValueAt(idx));
    mid is a t series calculated every bar

    Thanks
    Attached Files

    #2
    Hello mlprice12,

    Using Time[0] in a non-data-driven method like OnRender() requires synchronizing the bars with TriggerCustomEvent().


    You could instead use Bars.GetTime().


    Further, your for loop attempts to become one greater than the ChartBars ToIndex.
    idx <= ChartBars.ToIndex; idx++
    You would need to increment only if its less than, not less than or greater to.
    idx < ChartBars.ToIndex; idx++
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the feedback NinjaTrader_ChelseaB

      I changed the for loop and am still getting the same error at the same line of code.

      Comment


        #4
        I tried running it in a new workspace without other indicators and still the same issue.
        Weird part is it was working this morning until I changed a few different indictors' settings that are attached to the same chart.

        I attached the code if you want to take a look deeper.

        Thanks a lot
        Attached Files

        Comment


          #5
          Hello mlprice12,

          Which line of code is causing an error?

          Are you not seeing the print 0Time+Bars.GetTime(idx) appearing?
          Does the Log tab of the Control Center show the error is in OnRender()?

          I'm testing this on my end, and i'm seeing prints and no errors.
          Last edited by NinjaTrader_ChelseaB; 02-23-2022, 11:47 AM.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            NinjaTrader_ChelseaB
            Yes it is throwing OnRender and Time0 triggers but not Time1
            which means this is the line
            Code:
            if(mid.IsValidDataPointAt(idx))
            Last edited by mlprice12; 02-23-2022, 11:50 AM.

            Comment


              #7
              Hello mlprice12,

              Test that code by itself (comment out all of the other code in OnRender() other than the encapsulating loop).

              Still able to reproduce the error?

              What is the size of mid.Count?
              What is the value of idx?
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                With all other onrender commented out I still receive the same error.
                mid.Count = 74804
                idx = 80918

                error on bar 74803

                Comment


                  #9
                  Hello mlprice12,

                  The idx 80918 is larger than the mid series has bars. You are asking for an index that does not exist.

                  Note that the ChartBars are synchronized with the primary series, not the series added with AddDataSeries.

                  I think you might want to get the indexes of the added series with mid.GetBar() and use the time of BarsArray[0].GetTime(idx).
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    NinjaTrader_ChelseaB
                    You are right. I thought they would have the same number of bars because both series are based on 10 ticks.

                    I'm a little confused on the solution though. Do I need to have 2 different for loops?

                    Comment


                      #11
                      Hello mlprice12,

                      So your BarsArray 0, the primary, has a different number of bars than BarsArray 1.

                      And you want the information of a bar from an added series that is closing at approximately the same time as a bar on the chart?

                      If so, get the time of that bar on the chart with .GetTime(). Then use that time for BarsArray[1].GetBar() to get a bar index from the added series (that approximately has the time of the bar on the chart).

                      Below are links to the help guide on GetBar and GetTime.

                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        NinjaTrader_ChelseaB

                        Your assumptions are correct.

                        I believe this would work?
                        Code:
                        protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
                        {
                        base.OnRender(chartControl, chartScale);
                        // loop through only the rendered bars on the chart
                        for(int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++)
                        {
                        // get the time stamp at the selected bar index value
                        DateTime timeValue = Bars.GetTime(barIndex);
                        for (int idx = 0; idx < BarsArray[1].GetBar(timeValue); idx++)

                        Comment


                          #13
                          Or do I just replace idx with int barsAgo = CurrentBar - BarsArray[1].GetBar(timeValue);

                          Comment


                            #14
                            Hello mlprice12,

                            You have two loops which I am not understanding.

                            DateTime primaryBarTime = Bars.GetTime(barIndex);
                            int addedBarIndex = BarsArray[1].GetBar(primaryBarTime);
                            Print(BarsArray[1].GetTime(addedBarIndex));

                            Print these these values, is it getting the right bar?
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              @NinjaTrader_ChelseaB

                              Not sure if this changes anything but every bar is relevant. I'm rendering an ellipse on every bar.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              647 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              369 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              108 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              572 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              573 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X