(Actually) trim the bids / asks to configured number of levels

This commit is contained in:
moshferatu 2024-04-23 02:16:42 -07:00
parent 5603353cc5
commit 479bbfd791

View File

@ -97,6 +97,7 @@ namespace NinjaTrader.NinjaScript.Indicators
}
ProcessMarketDepthMessage(message.Trim());
TrimMarketDepth(maxLevels);
}
}
}
@ -158,6 +159,42 @@ namespace NinjaTrader.NinjaScript.Indicators
}
}
}
static void TrimMarketDepth(int maxLevels)
{
if (maxLevels <= 0)
return;
if (Asks.Count > maxLevels)
{
var sortedLevels = Asks.Keys.OrderBy(x => x).ToList();
if (sortedLevels.Count > maxLevels)
{
// Remove the highest asks.
var levelsToRemove = sortedLevels.Skip(maxLevels).ToList();
foreach (var level in levelsToRemove)
{
int value;
Asks.TryRemove(level, out value);
}
}
}
if (Bids.Count > maxLevels)
{
var sortedLevels = Bids.Keys.OrderBy(x => x).ToList();
if (sortedLevels.Count > maxLevels)
{
// Remove the lowest bids.
var levelsToRemove = sortedLevels.Take(sortedLevels.Count - maxLevels).ToList();
foreach (var level in levelsToRemove)
{
int value;
Bids.TryRemove(level, out value);
}
}
}
}
}
public class MarketDepth : Indicator