Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error accessing the first bidRows[1].Volume in OnMarketData() method

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

    Error accessing the first bidRows[1].Volume in OnMarketData() method

    I have setup the level2 book in OnMarketDepth and working fine. In OnMarketData() when try to access of the bidRows[1].Volume, sometimes works fine, some times return an error:

    **NT** Error on calling 'OnMarketData' method for strategy 'TestStrategyOnMarketData/58f61913f4884c2ba124fabc8b57cd92': You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

    May be OnMarketData is accesing the value too soon when OnMarketDepth() has not produce that value yet. How to overcome that?

    Code:
    protected override void OnMarketData(MarketDataEventArgs e)
    		{
    			if (e.MarketDataType == MarketDataType.Last)
    				{
    					tradePrice = e.Price;
    					tradeQty = e.Volume;
       			    }
    			if (e.MarketDataType == MarketDataType.Ask) 
    				{
    					askPrice = e.Price;
    					askQty = e.Volume;
    				}		
    			if (e.MarketDataType == MarketDataType.Bid) 
    				{
    					bidPrice = e.Price;
    					bidQty = e.Volume;
    				}
    			
    			Print(bidRows[1].Volume);
    			}

    #2
    Try removing: Print(bidRows[1].Volume);

    Where is that assigned? What is it doing?

    If you have an ask/bid before that, it isn't assigned. right? (Assuming you've done everything else every where correctly, which I'm not feeling at this point).







    Originally posted by er111222 View Post
    I have setup the level2 book in OnMarketDepth and working fine. In OnMarketData() when try to access of the bidRows[1].Volume, sometimes works fine, some times return an error:

    **NT** Error on calling 'OnMarketData' method for strategy 'TestStrategyOnMarketData/58f61913f4884c2ba124fabc8b57cd92': You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

    May be OnMarketData is accesing the value too soon when OnMarketDepth() has not produce that value yet. How to overcome that?

    Code:
    protected override void OnMarketData(MarketDataEventArgs e)
    		{
    			if (e.MarketDataType == MarketDataType.Last)
    				{
    					tradePrice = e.Price;
    					tradeQty = e.Volume;
       			    }
    			if (e.MarketDataType == MarketDataType.Ask) 
    				{
    					askPrice = e.Price;
    					askQty = e.Volume;
    				}		
    			if (e.MarketDataType == MarketDataType.Bid) 
    				{
    					bidPrice = e.Price;
    					bidQty = e.Volume;
    				}
    			
    			Print(bidRows[1].Volume);
    			}

    Comment


      #3
      Once the first value print out in the output window, it is working fine, that make me think it is the race reason between OnMarketData and OnMarketDepth, so looking for a way to handle this. bidRows[1].Volume assigned in the NinjaTrader-support suggested level2 book code as follow:

      Code:
      protected override void OnMarketDepth(MarketDepthEventArgs e)
      		{
      			//Create bid ask order book
      			List<LadderRow> rows = null;
      			#region Handle race condition in firstAskEvent/firstBidEvent
      			if (e.MarketDataType == MarketDataType.Ask)  //Incoming data is an ask data
      				{
      					rows = askRows;
      					if (firstAskEvent)
      						{
      							if (e.Operation == Operation.Update)
      							{
      								lock(e.MarketDepth.Ask)
      								{
      									for (int idx = 0; idx < e.MarketDepth.Ask.Count; idx++)
      										rows.Add(new LadderRow(e.MarketDepth.Ask[idx].Time, e.MarketDepth.Ask[idx].Price, e.MarketDepth.Ask[idx].Volume));
      								}
      							}
      							firstAskEvent = false;
      						}	
      				}
      			else if (e.MarketDataType == MarketDataType.Bid)  //Incoming data is a bid data
      				{
      					rows = bidRows;
      					if (firstBidEvent)
      					{
      						if (e.Operation == Operation.Update)
      						{
      							lock (e.MarketDepth.Bid)
      							{
      								for (int idx = 0; idx < e.MarketDepth.Bid.Count; idx++)
      									rows.Add(new LadderRow(e.MarketDepth.Bid[idx].Time, e.MarketDepth.Bid[idx].Price, e.MarketDepth.Bid[idx].Volume));
      							}
      						}
      						firstBidEvent = false;
      					}
      				}
      			if (rows == null)  //No incoming data
      				{
      					return;
      				}
      			#endregion	
      			#region Works on events that are not firstAskEvent/firstBidEvent
      			//Operation.Insert	
      			if (e.Operation == Operation.Insert)  //either a bid insert or an ask insert
      				{
      					if (e.Position >= rows.Count)
      						{
      							rows.Add(new LadderRow(e.Time, e.Price, e.Volume));
      						}
      					else
      						{
      							rows.Insert(e.Position, new LadderRow(e.Time, e.Price, e.Volume));
      						}
      				}
      			//Operation.Update	
      			else if (e.Operation == Operation.Update)
      				{
      					rows[e.Position].Time   = e.Time;
      					rows[e.Position].Price	= e.Price;  
      					rows[e.Position].Volume	= e.Volume;
      				}
      			//Operation.Remove	
      			else if (e.Operation == Operation.Remove && e.Position < rows.Count)
      				{
      					rows.RemoveAt(e.Position);
      				}
      				#endregion
      		}

      Comment


        #4
        er111222, yes very likely a race conditon with accessing you run into - as there would be no call / event sequence guarantee for those methods. What condition are you trying to setup, wouldn't it be easier to monitor for you condition in OnMarketData() and then set a variable as needed to be able to check if off directly in OnMarketDepth then?

        Comment


          #5
          If this is necessary for you, you're going to need to guarantee that the one happened before the other.

          Use a flag/variable.... but then you'll have a lot of unnecessary checks...

          Or keep restarting until it works

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by sjsj2732, Yesterday, 04:31 AM
          0 responses
          33 views
          0 likes
          Last Post sjsj2732  
          Started by NullPointStrategies, 03-13-2026, 05:17 AM
          0 responses
          286 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          285 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          133 views
          1 like
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          91 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Working...
          X