Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Issue in accessing cumulativeDelta indicator values

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

    Issue in accessing cumulativeDelta indicator values

    Hi

    I am writing a strategy and trying to access two values within the indicator.

    cumulativeDelta.DeltaClose[0]
    cumulativeDelta.DeltaClose[1]

    I can access index 0 but index 1 gives me an error:

    an error happened on OnBarUpdate: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
    The code I apply reading through the previous threads and documentation:
    Code:
    onstateChange()
    {
        if (State == State.Configure)
              {
                 AddDataSeries(Data.BarsPeriodType.Tick, 1);
                 AddDataSeries("ES 12-24", Data.BarsPeriodType.Second, 15, Data.MarketDataType.Last);
              }​
    }


    And in onBarUpdate
    Code:
    if (BarsInProgress == 0)
    {
        double currentDelta = cumulativeDelta.DeltaClose[0];
        double previousDelta = cumulativeDelta.DeltaClose[1]; // This one gives error​
    }


    Can someone kindly help please?


    Thanks
    Ben
    Last edited by benghabili; 10-17-2024, 01:50 PM.

    #2
    Hello benghabili,

    There must be at least two bars processed to call back 1 bar.

    if (BarsInProgress == 0)
    {
    double currentDelta = cumulativeDelta.DeltaClose[0];
    if (CurrentBar > 1)
    {

    double previousDelta = cumulativeDelta.DeltaClose[1]; // This one gives error​
    }
    }
    else if (BarsInProgress == 1)
    {
    // We have to update the secondary series of the hosted indicator to make sure the values we get in BarsInProgress == 0 are in sync
    cumulativeDelta.Update(cumulativeDelta.BarsArray[1].Count - 1, 1);
    }​
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the response. I didn't share the full code but I am checking this already.
      Code:
      if (CurrentBar < 1)
      {
         return;
      }
      I don't handle the (BarsInProgress == 1) though

      Comment


        #4
        NinjaTrader_ChelseaB as I mentioned, I applied the solution you suggested already but that doesn't make any changes. anything I may miss. Do you have any example code that you can share if possible please

        Comment


          #5
          Hello benghabili,

          I've tested the code suggested in post # 2 and I am not able to produce an error.

          Using the test script attached without making any modifications to it, are you seeing the test script produces errors?
          CumulativeDeltaTest_NT8.zip
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            NinjaTrader_ChelseaB I see. No I don't see error , however, I can see that you are adding cumulativeDelta programmatically. That is what I have done it before but for performance purposes, I am adding the indicator manually and try to use the data generated by the indicator.

            Apologies, I should have mentioned that I am trying to extract data from the manually added indicator to the system.

            Here is the class I created to fetch data from the indicator:

            Code:
            using System;
            using NinjaTrader.NinjaScript.Indicators;
            using NinjaTrader.NinjaScript;
            namespace NinjaTrader.NinjaScript.Strategies
            {
                public static class DeltaReversalBasicIndicators
                {
                    public static OrderFlowCumulativeDelta FindOrderFlowCumulativeDelta(DeltaReversalBasic strategy)
                    {
                        // Check if indicators have been manually added to the chart
                        if (strategy.ChartControl != null)
                        {
                            foreach (var indicator in strategy.ChartControl.Indicators)
                            {
                                // Check if the indicator is an instance of OrderFlowCumulativeDelta
                                if (indicator is OrderFlowCumulativeDelta)
                                {
                                    return (OrderFlowCumulativeDelta)indicator;
                                }
                            }
                        }
                        return null; // Indicator not found
                    }
                }
            }​
            and call it like :


            Code:
            else if (State == State.DataLoaded)
            {
                if (enableBacktestMode)
                {
                      // Instantiate the OrderFlowCumulativeDelta indicator properly
                      cumulativeDelta = OrderFlowCumulativeDelta(CumulativeDeltaType.BidAs k, CumulativeDeltaPeriod.Session, 0);
                      AddChartIndicator(cumulativeDelta);
                     SetCumulativeData(cumulativeDelta);
                }
               else
               {
                   cumulativeDelta = DeltaReversalBasicIndicators.FindOrderFlowCumulati veDelta(this);
                  SetCumulativeData(cumulativeDelta);
                         if (cumulativeDelta == null)
                         {
                              throw new Exception("Order Flow Cumulative Delta not found. Please add it manually.");
                         }
                }
            
            }​
            Since I had a performance issue, causing the lagging chart, I am trying to minimise the load to the application. Not sure if I get better performance using added indicator manually over programmatically ?!

            Comment


              #7
              Hello benghabili,

              Calling the indicator method, assigning this to a variable, and adding the indicator instance to a chart with AddChartIndicator() would not use any more resources than manually adding the indicator to the chart. The resource use would be the same.

              The issue is likely with attempting to use an indicator on a chart instead of calling the indicator as I have done in the example I have provided you.

              The approach in the example I provided you works without error, and would be the supported approach.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Hi,

                Thanks for the clarification. Does that mean for every single indicator that I want to use in any strategy, would need to be added in the code and the strategy can not consume the data generated by that?

                Or simple indicators are fine such as EMA, SMA? etc?

                Comment


                  #9
                  Hello benghabili,

                  Every indicator used by the strategy should be called as a method from that strategy.
                  Chelsea B.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by NullPointStrategies, Yesterday, 05:17 AM
                  0 responses
                  55 views
                  0 likes
                  Last Post NullPointStrategies  
                  Started by argusthome, 03-08-2026, 10:06 AM
                  0 responses
                  132 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  73 views
                  0 likes
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  45 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Started by TheRealMorford, 03-05-2026, 06:15 PM
                  0 responses
                  49 views
                  0 likes
                  Last Post TheRealMorford  
                  Working...
                  X