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

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 mgco4you, Today, 09:46 PM
            1 response
            3 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by wzgy0920, Today, 09:53 PM
            0 responses
            3 views
            0 likes
            Last Post wzgy0920  
            Started by Rapine Heihei, Today, 08:19 PM
            1 response
            8 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by Rapine Heihei, Today, 08:25 PM
            0 responses
            6 views
            0 likes
            Last Post Rapine Heihei  
            Started by f.saeidi, Today, 08:01 PM
            1 response
            9 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Working...
            X