Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnMarketData error

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

    OnMarketData error

    I have an indicator which used OnMarketData which works fine most of the time, but sometimes when using the Market Replay data feed, I get the following error;

    Error on calling 'OnMarketData' method for indicator 'MyMarketDepth'. You are accessing an index with a value that is invalid since its out of range. IE accessing a series [barsago] with a value of 5 when there are only 4 bars on the chart.

    Apparently there are some gaps in some of the data. Once the error occurs, the indicator stops working until I open and close the indicator window. Is there a snipet of code I can add to tell the indicator to ignore the nonexistent data? I should add that this usually happens long after there are many bars in the chart.

    Here is the code I'm using;

    protectedoverridevoid OnMarketDepth(MarketDepthEventArgs e)
    {

    if (e.MarketDataType == MarketDataType.Ask)
    {
    Ask1 = e.MarketDepth.Ask[
    0].Volume;
    Ask2 = e.MarketDepth.Ask[
    1].Volume;
    Ask3 = e.MarketDepth.Ask[
    2].Volume;
    Ask4 = e.MarketDepth.Ask[
    3].Volume;
    Ask5 = e.MarketDepth.Ask[
    4].Volume;
    Ask6 = e.MarketDepth.Ask[
    5].Volume;
    }


    if (e.MarketDataType == MarketDataType.Bid)
    {
    Bid1 = e.MarketDepth.Bid[
    0].Volume;
    Bid2 = e.MarketDepth.Bid[
    1].Volume;
    Bid3 = e.MarketDepth.Bid[
    2].Volume;
    Bid4 = e.MarketDepth.Bid[
    3].Volume;
    Bid5 = e.MarketDepth.Bid[
    4].Volume;
    Bid6 = e.MarketDepth.Bid[
    5].Volume;
    }


    //Print(Close[0] + " " + "Bid3 = " + Bid3+ " Bid2 = " + Bid2 + " Bid1 = " + Bid1 + " Ask1 = " + Ask1 + " Ask2 = " + Ask2 + " Ask3 = " + Ask3);
    }
    protectedoverridevoid OnBarUpdate()
    {
    if(CurrentBar < BarsRequired)return;
    if (Historical)
    return;

    MyAsk.Set((Ask1 + Ask2 + Ask3 + Ask4 + Ask5 ) /
    5);
    MyBid.Set((Bid1 + Bid2 + Bid3 + Bid4 + Bid5) /
    5);
    }

    Safetrading

    #2
    Hello safetrading,

    Thank you for your post.

    So I may investigate this matter further please answer the following questions.

    Does this occur on all instruments in Market Replay? If not, what instrument or instrument(s) does this occur on?

    Was the data recorded from your real-time data or downloaded from the NinjaTrader servers?

    At what point in the replay does this occur? Any specific time in the session or day of month?

    I look forward to your response.

    Comment


      #3
      I only trade CL, so that is the only instrument I have used it with. My Replay data is recorded automatically when I'm streaming data from Zenfire. As to when this occurs, it is very spuratic, no visible patterns that I have noticed.

      Safetrading

      Comment


        #4
        Hello safetrading,

        Thank you for your response.

        Please run your test again on replay and determine a date that this occurs. Then download this date from the NinjaTrader servers by going to File > Utilities > Download Replay Data..

        Run the test again after downloading the data and advise if the same item occurs again for your code in Market Replay.

        Comment


          #5
          OK, will do after my trading day is over today

          Comment


            #6
            Originally posted by safetrading View Post
            I have an indicator which used OnMarketData which works fine most of the time, but sometimes when using the Market Replay data feed, I get the following error;

            Error on calling 'OnMarketData' method for indicator 'MyMarketDepth'. You are accessing an index with a value that is invalid since its out of range. IE accessing a series [barsago] with a value of 5 when there are only 4 bars on the chart.

            Apparently there are some gaps in some of the data. Once the error occurs, the indicator stops working until I open and close the indicator window. Is there a snipet of code I can add to tell the indicator to ignore the nonexistent data? I should add that this usually happens long after there are many bars in the chart.

            Here is the code I'm using;

            protectedoverridevoid OnMarketDepth(MarketDepthEventArgs e)
            {

            if (e.MarketDataType == MarketDataType.Ask)
            {
            Ask1 = e.MarketDepth.Ask[
            0].Volume;
            Ask2 = e.MarketDepth.Ask[
            1].Volume;
            Ask3 = e.MarketDepth.Ask[
            2].Volume;
            Ask4 = e.MarketDepth.Ask[
            3].Volume;
            Ask5 = e.MarketDepth.Ask[
            4].Volume;
            Ask6 = e.MarketDepth.Ask[
            5].Volume;
            }


            if (e.MarketDataType == MarketDataType.Bid)
            {
            Bid1 = e.MarketDepth.Bid[
            0].Volume;
            Bid2 = e.MarketDepth.Bid[
            1].Volume;
            Bid3 = e.MarketDepth.Bid[
            2].Volume;
            Bid4 = e.MarketDepth.Bid[
            3].Volume;
            Bid5 = e.MarketDepth.Bid[
            4].Volume;
            Bid6 = e.MarketDepth.Bid[
            5].Volume;
            }


            //Print(Close[0] + " " + "Bid3 = " + Bid3+ " Bid2 = " + Bid2 + " Bid1 = " + Bid1 + " Ask1 = " + Ask1 + " Ask2 = " + Ask2 + " Ask3 = " + Ask3);
            }
            protectedoverridevoid OnBarUpdate()
            {
            if(CurrentBar < BarsRequired)return;
            if (Historical)
            return;

            MyAsk.Set((Ask1 + Ask2 + Ask3 + Ask4 + Ask5 ) /
            5);
            MyBid.Set((Bid1 + Bid2 + Bid3 + Bid4 + Bid5) /
            5);
            }

            Safetrading
            Use try-catch blocks and trap the exception.

            ref: http://msdn.microsoft.com/en-us/libr...=vs.90%29.aspx

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            580 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            336 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            103 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            554 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            552 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X