Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator error is not notified to the strategy

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

    Indicator error is not notified to the strategy

    Hello,

    My strategy has received the following error:

    -------------------------
    2021-04-21 04:01:12:554|3|4|Indicator 'SMA': Error on calling 'OnBarUpdate' method on bar 258: 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.
    2021-04-21 04:01:12:565|3|4|Strategy 'Nasdaq_2': Error on calling 'EventHandlerBarsUpdate' method: Object reference not set to an instance of an object.
    2021-04-21 04:01:12:566|1|4|Disabling NinjaScript strategy 'Nasdaq_2/195500170'
    --------------------------

    It seems that the error occurs within the code of the SMA indicator. After the error, NinjaTrader disables the strategy.
    The problem is that this error in the indicator is not transmitted to the strategy (for example as an Exception), and since the strategy has the RealtimeErrorHandling parameter set to RealtimeErrorHandling.IgnoreAllErrors, the disabling of the strategy by NinjaTrader supposes the cancellation of live orders, but does not close the open position because my custom code to close the open position in case of error (rejected, overfill, exception, ...) is not called.

    The strategy has been running for months in production with a real account. It works to the tick, and that call to the SMA is made with each tick, and it is the first time this error occurs.

    The MaximumBarsLookBack parameter is set to MaximumBarsLookBack.Infinite.

    One way to avoid this issue would be to receive an exception from the SMA indicator, in order for the strategy to close the open position.

    Thanks

    #2
    Hello, thanks for your post.

    Is the strategy calling AddChartIndicator and is Calculating OnEachTick or OnPriceChange? The SampleMACrossover uses SMA objects. Is your straetgy initializing these SMAs any differently?
    Could you also make a reduced example that replicates the error and send it to me?



    Comment


      #3
      Hello Chris,

      The strategy does not call AddChartIndicator.

      The strategy works with Calculate = Calculate.OnPriceChange;

      The call is like the one in the 'SampleMACrossOver', with a reference variable to SMA indicator, asking for index [0] (ie, CurrentBar). And within the OnBarUpdate of the strategy. I can't find any explanation for the error because there were enough bars (at least one).

      This simple code snippet causes the error if the strategy is loaded on a chart with less than 200 bars. The position remains open with 1 Long and the strategy disabled.

      Code:
      bool firstTime = true;
      bool secondTime = false;
      
      protected override void OnBarUpdate()
      {
        if (State != State.Realtime)
          return;
      
        if (firstTime)
        {
          EnterLong();
          firstTime = false;
          secondTime = true;
          return;
        }
      
        if (secondTime)
          Print(SMA(60)[200]);
      }

      If the error could be caught as an exception, and the 'RealtimeErrorHandling' parameter was set to 'IgnoreAllErrors' the strategy code itself could close the position.

      Thanks

      Comment


        #4
        Hello, thanks for your reply.

        Why would there not be a CurrentBar check at the top to prevent this? e.g.
        if(CurrentBar < 200) return; //200, or any maximum lookback period

        If it is known the chart would not have at least 200 bars, using a lookback of SMA(60)[200] would not work in any case, so add more days to load onto the chart.

        Comment


          #5
          Hello Chris,

          the problem happened because I was accessing the OnBarUpdate with SMA (n) [0] and this gave an error, but it is impossible because I am accessing the bar [0], the same one in which the OnBarUpdate is being executed since it necessarily has to exist at least one bar for the OBU to run, so SMA (n) [0] can't give me that error. And it doesn't matter what the period is worth. (CurrentBar could be -1 in multitimeframe contexts, but it is not the case).

          Something strange had to happen that has nothing to do with the code (maybe the datafeed, the internal bar cache, I don't know).

          Even if that inexplicable mistake happened again, I could face it if my strategy could capture it. But it is not captured, and I wrote you a simple code example as you asked for you to replicate the problem that is summarized in that after the error the strategy is disabled but the position remains open.

          Summary: an internal error occurs in an indicator that is called from a strategy, the strategy cancels the live orders and disables the strategy ... but does not close the open position. The problem is that it does not close the open position. One solution would be for the indicator to raise an exception that the strategy could catch in order to close the position itself (with RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors).

          Thanks
          Last edited by cls71; 04-23-2021, 12:10 AM.

          Comment


            #6
            Hello, thanks for your reply.

            Do you have a reduced example that I can run on my PC that will reproduce this EventHandlerBarsUpdate error?

            Best regards.

            Comment


              #7
              Hello Chris,

              I cannot because the error occurs inside the SMA indicator standard code :

              -------------------------
              2021-04-21 04:01:12:554|3|4|Indicator 'SMA': Error on calling 'OnBarUpdate' method on bar 258: 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.
              2021-04-21 04:01:12:565|3|4|Strategy 'Nasdaq_2': Error on calling 'EventHandlerBarsUpdate' method: Object reference not set to an instance of an object.
              2021-04-21 04:01:12:566|1|4|Disabling NinjaScript strategy 'Nasdaq_2/195500170'
              --------------------------


              This error is one thing. It would be necessary to find out why it happens and solve it.

              And another thing is how to deal with this error or a similar one, if it happened again. If the error were propagated as an exception, the calling code - the strategy in this case - could conveniently catch and process it.

              Thanks

              Comment


                #8
                Hi cls71,

                How are you using this in the Strategy? I do not see how you are initializing and using the SMA in the Nasdaq_2 strategy. The SampleMaCrossover strategy is using SMA objects without this object reference error.

                Comment


                  #9
                  I use this sentence:

                  double avg = SMA(PeriodExitAvg)[0];

                  Comment


                    #10
                    Is there any solution to this problem?

                    I am experiencing the same issue. The indicator generates an error, and this error causes the strategy to be disabled.

                    To isolate the issue further, I only added the indicator to be displayed on the screen without using it in any way (meaning I do not access any values through its index in the strategy).

                    The strategy is set to OnEachTick calculation mode, and I add the ADX indicator in the OnStateChange method under the else if(State == State.DataLoaded) section as follows:

                    filterADX_Ind = ADX(ADXPeriod);
                    filterADX_Ind.Plots[0].Brush = ADXColor;
                    filterADX_Ind.Plots[0].Width = ADXWidth;

                    AddChartIndicator(filterADX_Ind);


                    At some point, the error occurs.

                    The image shows the log where the error is generated by the indicator, and then the strategy produces another error and gets disabled.

                    But again, I emphasize that within the strategy code, I do not use the indicator to confirm that the issue comes directly from the indicator itself.

                    Has anyone found a solution?


                    Click image for larger version

Name:	imagen.png
Views:	100
Size:	11.7 KB
ID:	1333059

                    Comment


                      #11
                      Hello ervin,

                      May I confirm you are connected using the NinjaTrader connection type in the Connections menu > configure window?

                      Please try running the repair from the NinjaTrader 8.1.4.1 installer and then reconnect.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Yes, I am using version 8.1.4.1, and the error occurs both when I am connected to data and when I run the strategy in Replayer.

                        Comment


                          #13
                          Hello ervin,

                          I was not able to confirm, who are you connected to in the Connections menu?

                          Also, please run the repair from the NinjaTrader installer.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            I am connecting with Rithmic.
                            I already ran the repair, but it remains the same.​

                            Comment


                              #15
                              Hello ervin,

                              Please create a clean testing environment.

                              To create a clean temporary NinjaTrader folder:
                              1. Shutdown NinjaTrader
                              2. Rename (My) Documents\NinjaTrader 8\ to (My) Documents\NinjaTrader 8.original\
                              3. Download the NinjaTrader installation package from the link below
                                1. NinjaTrader - Download
                              4. Run the repair from this (if the repair is unsuccessful, uninstall and then re-install NinjaTrader)
                              This will recreate the NinjaTrader 8 folder in (My) Documents.

                              Please import this one test script only and test for the behavior.​
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Today, 05:17 AM
                              0 responses
                              38 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              124 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              64 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              41 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              46 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X