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

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?



    Chris L.NinjaTrader Customer Service

    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.

        Chris L.NinjaTrader Customer Service

        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.

            Chris L.NinjaTrader Customer Service

            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.
                Chris L.NinjaTrader Customer Service

                Comment


                  #9
                  I use this sentence:

                  double avg = SMA(PeriodExitAvg)[0];

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by geddyisodin, Today, 05:20 AM
                  6 responses
                  34 views
                  0 likes
                  Last Post geddyisodin  
                  Started by trilliantrader, Today, 03:01 PM
                  0 responses
                  3 views
                  0 likes
                  Last Post trilliantrader  
                  Started by pechtri, 06-22-2023, 02:31 AM
                  9 responses
                  122 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by frankthearm, 04-18-2024, 09:08 AM
                  16 responses
                  67 views
                  0 likes
                  Last Post NinjaTrader_Clayton  
                  Started by habeebft, Today, 01:18 PM
                  1 response
                  8 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Working...
                  X