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

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.
    Emily C.NinjaTrader Customer Service

    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.​
        Emily C.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by bortz, 11-06-2023, 08:04 AM
        47 responses
        1,607 views
        0 likes
        Last Post aligator  
        Started by jaybedreamin, Today, 05:56 PM
        0 responses
        9 views
        0 likes
        Last Post jaybedreamin  
        Started by DJ888, 04-16-2024, 06:09 PM
        6 responses
        19 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by Jon17, Today, 04:33 PM
        0 responses
        6 views
        0 likes
        Last Post Jon17
        by Jon17
         
        Started by Javierw.ok, Today, 04:12 PM
        0 responses
        15 views
        0 likes
        Last Post Javierw.ok  
        Working...
        X