I'm receiving a random error:
Error on triggering custom event for NinjaScript 'Depth of Market Scalper v1.2' on bar 21289: Collection was modified; enumeration operation may not execute.
Disabling NinjaScript strategy 'Depth of Market Scalper v1.2/205452735'
I'm utilizing the provided project that grabs the elements of the DOM and build out the books. I haven't been able to figure out what's causing this and was hoping you might have some insight.
protected override void OnMarketDepth(MarketDepthEventArgs e)
{
List<LadderRow> rows = null;
// Checks to see if the Market Data is of the Ask type
if (e.MarketDataType == MarketDataType.Ask)
{
rows = askRows;
// Due to race conditions, it is possible the first event is an Update operation instead of an Insert. When this happens, populate your Lists via e.MarketDepth first.
if (firstAskEvent)
{
if (e.Operation == Operation.Update)
{
// Lock the MarketDepth collection to prevent modification to the collection while we are still processing it
lock (e.Instrument.MarketDepth.Asks)
{
for (int idx = 0; idx < e.Instrument.MarketDepth.Asks.Count; idx++)
rows.Add(new LadderRow(e.Instrument.MarketDepth.Asks[idx].Price, e.Instrument.MarketDepth.Asks[idx].Volume, e.Instrument.MarketDepth.Asks[idx].MarketMaker));
}
}
firstAskEvent = false;
}
}
// Checks to see if the Market Data is of the Bid type
else if (e.MarketDataType == MarketDataType.Bid)
{
rows = bidRows;
// Due to race conditions, it is possible the first event is an Update operation instead of an Insert. When this happens, populate your Lists via e.MarketDepth first.
if (firstBidEvent)
{
if (e.Operation == Operation.Update)
{
// Lock the MarketDepth collection to prevent modification to the collection while we are still processing it
lock (e.Instrument.MarketDepth.Bids)
{
for (int idx = 0; idx < e.Instrument.MarketDepth.Bids.Count; idx++)
rows.Add(new LadderRow(e.Instrument.MarketDepth.Bids[idx].Price, e.Instrument.MarketDepth.Bids[idx].Volume, e.Instrument.MarketDepth.Bids[idx].MarketMaker));
}
}
firstBidEvent = false;
}
}
if (rows == null)
return;
// Checks to see if the action taken was an insertion into the ladder
if (e.Operation == Operation.Add)
{
// Add a new row at the end if the designated position is greater than our current ladder size
if (e.Position >= rows.Count)
rows.Add(new LadderRow(e.Price, e.Volume, e.MarketMaker));
// Insert a new row into our ladder at the designated position
else
rows.Insert(e.Position, new LadderRow(e.Price, e.Volume, e.MarketMaker));
}
/* Checks to see if the action taken was a removal of itself from the ladder
Note: Due to the multi threaded architecture of the NT core, race conditions could occur
-> check if e.Position is within valid range */
else if (e.Operation == Operation.Remove && e.Position < rows.Count)
rows.RemoveAt(e.Position);
/* Checks to see if the action taken was to update a data already on the ladder
Note: Due to the multi threaded architecture of the NT core, race conditions could occur
-> check if e.Position is within valid range */
else if (e.Operation == Operation.Update && e.Position < rows.Count)
{
rows[e.Position].MarketMaker = e.MarketMaker;
rows[e.Position].Price = e.Price;
rows[e.Position].Volume = e.Volume;
}
}

Comment