Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error on calling 'EventHandlerMarketData' method:

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

    Error on calling 'EventHandlerMarketData' method:

    I get the below error message sometimes. What could be the reason why this is happening?

    "Indicator 'IndicatorName': Error on calling 'EventHandlerMarketData' method: Index was outside the bounds of the array."

    Code:
    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class IndicatorName : Indicator
        {
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"";
                    Name                                        = "IndicatorName";
                    Calculate                                    = Calculate.OnEachTick;
                    IsOverlay                                    = false;
                    DisplayInDataBox                            = true;
                    DrawOnPricePanel                            = true;
                    DrawHorizontalGridLines                        = true;
                    DrawVerticalGridLines                        = true;
                    PaintPriceMarkers                            = true;
                    ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive                    = true;
                    AddPlot(Brushes.DodgerBlue, "Difference");
                    AddLine(Brushes.DarkGray, 0, "ZeroLine");
                }
                else if (State == State.Configure)
                {
                    AddDataSeries(Data.BarsPeriodType.Tick, 1);
                }
                else if (State == State.DataLoaded)
                {                
                }
            }
    
            protected override void OnMarketData(MarketDataEventArgs e)
            {
                //logic
            }
    
            protected override void OnBarUpdate()
            {
                if (CurrentBars[0] < 12 || CurrentBars[1] < 12)
                    return;
    
                //logic
    
    
            }​
    [I][B][/B][/I]


    #2
    Hello AgriTrdr,

    Thank you for your post.

    You may need to add a null reference check somewhere within your logic. We have a help guide tip page about checking for null references here:


    For example, some properties are noted in the help guide with the following information:
    "Additional Access Information
    This property can be accessed without a null reference check in the OnBarUpdate() event handler. When the OnBarUpdate() event is triggered, there will always be an Instrument object. Should you wish to access this property elsewhere, check for null reference first. e.g. if (Instrument != null)"

    The Instrument.FullName property is one example where this note comes into play if you were using this inside of OnMarketData():


    If you are not sure where to start, adding prints to your script can help to debug and narrow down which line of code might be causing an error. For more information about using prints to debug your script:


    Please let us know if we may be of further assistance.

    Comment


      #3
      Thanks for providing the links. Would a null reference like the below work?

      Code:
      protected override void OnBarUpdate()
      {
      if (Bars == null)
      return;
      
      
      if (CurrentBars[0] < 24 || CurrentBars[1] < 24)
      return;
      ​
      
      //logic
      
      
      }​​

      Comment


        #4
        Hello AgriTrdr,

        Thank you for your reply.

        Per the note, many of the relevant properties can be accessed inside of OnBarUpdate() without a null reference check. This means the null reference check isn't necessarily needed inside of OnBarUpdate() where you have it in your snipped. The error message mentions Error on calling 'EventHandlerMarketData' method which would suggest that the null reference check is needed inside of OnMarketData(). You could add print statements inside of OnMarketData() and see which lines of code, if any, inside of the method are being reached before the error appears. This can help to narrow down where a null reference may be needed.

        Please let us know if we may be of further assistance.​

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        54 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        130 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        72 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        44 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