Minim volum and Price

I already know how to get the Maximum Volume price efficiently:

long pocVol = vi.GetMaximumVolume(null, out pocPrice);

However, I need the price level with the minimum volume.

I know I can iterate through all price levels and find the minimum manually, but I would like to avoid the additional CPU cost.

Is there any built-in method similar to GetMaximumVolume() that directly returns the minimum volume price level?

Thanks.

No. The Order Flow Volumetric Bars page of the help guide lists what a Volumes[i] entry exposes: the per-price getters (GetAskVolumeForPrice, GetBidVolumeForPrice, GetTotalVolumeForPrice, GetDeltaForPrice), the delta values, the bar totals, and GetMaximumVolume, the only one that hands back a price. Nothing on that page returns the least-traded price, so you write the loop yourself.

On the CPU side, what matters is how often the loop runs and how wide the bar is, not the loop itself. A bar with a 40-tick range is 41 calls to GetTotalVolumeForPrice: once per bar, when it closes, that is very little. On every tick with Calculate.OnEachTick it grows with the number of trades and the bar’s width, and with Tick Replay on it also runs for every historical tick while the chart loads. What hurts is redoing the loop for many bars on every update, for example for each visible bar inside OnRender, which runs far more often than OnBarUpdate. So compute it once per completed bar and keep the result in plots, which keep every bar, or in two Series of doubles created with MaximumBarsLookBack.Infinite (by default a Series keeps only the last 256 values).

The one thing the loop makes you decide is what “minimum” means:

  • Empty levels. With Ticks per level at 1, a bar that moved fast has prices inside its range where nothing traded. Those prices read 0, so the raw minimum is 0 at the first gap. If you want the thinnest traded level, skip the zeros.
  • The extremes. The thinnest traded level is often just the tip of the bar’s high or low, but not always: a bar that reversed on heavy volume can have its POC there, and a fast bar leaves thin levels in the middle. If what you want is a low-volume node, the thin spot between two heavy areas, one bar’s minimum is not it: sum GetTotalVolumeForPrice per price over several bars, then look for the trough between two volume peaks.
  • Ticks per level above 1. The loop below is written for Ticks per level 1. Above that, the help guide does not say what the per-price getters return inside a cell, so Print a few bars next to GetMaximumVolume before relying on it.

Put this method inside your indicator or strategy class, next to OnBarUpdate:

// Lowest-volume price level of one Volumetric bar, written for Ticks per level 1.
// askVolume: true = ask, false = bid, null = combined, as in GetMaximumVolume.
// Returns the volume and sets price; ties go to the lowest price; price is -1 when no level qualified.
private long GetMinimumVolume(NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsType barsType, int barIndex, double low, double high, bool? askVolume, bool skipEmptyLevels, out double price)
{
	var bar = barsType.Volumes[barIndex];
	long minVolume = long.MaxValue;
	price = -1;

	int levels = (int)Math.Round((high - low) / TickSize) + 1;

	for (int i = 0; i < levels; i++)
	{
		// Build each price from the low and round it: adding TickSize step by step collects rounding error on 0.01 instruments
		double p = Instrument.MasterInstrument.RoundToTickSize(low + i * TickSize);

		long v = askVolume == null ? bar.GetTotalVolumeForPrice(p)
		       : askVolume == true ? bar.GetAskVolumeForPrice(p)
		       : bar.GetBidVolumeForPrice(p);

		if (skipEmptyLevels && v == 0)
			continue;

		if (v < minVolume)
		{
			minVolume = v;
			price = p;
		}
	}

	return minVolume == long.MaxValue ? 0 : minVolume;
}

The call, once per completed bar, next to your POC line (with Calculate.OnBarClose):

protected override void OnBarUpdate()
{
	var barsType = Bars.BarsSeries.BarsType as NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsType;
	if (barsType == null)
		return;

	// Volumes is indexed by bar index (CurrentBar), not by BarsAgo
	double pocPrice, minPrice;
	long pocVol = barsType.Volumes[CurrentBar].GetMaximumVolume(null, out pocPrice);
	long minVol = GetMinimumVolume(barsType, CurrentBar, Low[0], High[0], null, true, out minPrice);

	Print(string.Format("{0}  POC {1} ({2})  Min {3} ({4})", Time[0], pocPrice, pocVol, minPrice, minVol));
}

Two checks on your own instrument. With askVolume null and Ticks per level 1, a running total of v inside the loop should match bar.TotalVolume; that shows the loop reaches every level and the rounding holds on 0.01-tick instruments like CL. And if your Volumetric series was added with AddVolumetric on the same instrument rather than being the chart’s series, read BarsArray[1].BarsType, CurrentBars[1], Lows[1][0] and Highs[1][0] inside BarsInProgress == 1, so the loop covers the Volumetric bar’s range, not the primary bar’s.

With Calculate.OnEachTick, do the same on IsFirstTickOfBar once CurrentBar is at least 1, with CurrentBar - 1, Low[1] and High[1], the bar that has just completed.

If you want the version that finds the thin spot between two heavy areas rather than one bar’s thinnest print, say so and I will sketch it.