Beginner Trading Tutorial — Confirmed Inside Bar Range Ratio for MultiCharts .NET
Inside bars are a familiar way to describe a contraction in price activity. A binary inside-bar indicator tells us whether one candle is contained within another, but does not distinguish a very narrow candle from one that almost fills its reference range. This tutorial develops a native C# indicator for MultiCharts .NET that combines strict containment with a normalized range measurement. It is designed as an introductory exercise in historical indexing, conditional calculations and plotting.
Define containment before measuring contraction
Use the high and low of the evaluated candle and the high and low of the immediately preceding reference candle. Strict containment requires the evaluated high to be below the reference high and the evaluated low to be above the reference low. Equal highs or equal lows are deliberately excluded. This definition is independent of candle color, body size and closing direction. The reference is always the immediately preceding candle, not an older mother bar retained across a sequence of nested candles.
When both containment conditions are satisfied and the reference range is positive, divide the evaluated high minus low by the reference high minus low and multiply by 100. The result expresses the evaluated range as a percentage of its reference. A value of 40 means that the evaluated candle spans 40 percent of the preceding candle's full range. A valid strict inside bar has a ratio below 100. A zero-range candle can qualify if its single price lies strictly inside the reference. Nonqualifying observations are represented by the sentinel −1, which is deliberately distinct from a valid zero.

Calculate the example manually
The reference candle has a high of 110 and low of 100, giving a range of 10. The first candidate has a high of 107 and low of 103. Both boundaries are strictly inside the reference and its range is four. The output is 100 × 4 / 10 = 40%. The second candidate has a high of 105 and low of 99. Its range is six, but its low breaks the reference boundary, so the indicator returns −1 rather than 60%. These are independent hypothetical candidates using the same reference candle, not a consecutive three-bar sequence.
Consider another candidate with high 110 and low 104. Its range is also six, but its high equals the reference high. It fails the strict rule and receives −1. If you prefer inclusive containment, the comparisons must be changed deliberately and the interpretation of a 100% reading must be documented. Do not silently mix the two definitions in a research dataset.
Complete native C# source
using System;
using System.Drawing;
using PowerLanguage;
namespace PowerLanguage.Indicator
{
public class ConfirmedInsideRangeRatio : IndicatorObject
{
private IPlotObject ratio;
public ConfirmedInsideRangeRatio(object ctx)
: base(ctx) { }
protected override void Create()
{
ratio = AddPlot(new PlotAttributes(
"InsideRatio", EPlotShapes.Histogram,
Color.SteelBlue));
}
protected override void StartCalc()
{
}
protected override void CalcBar()
{
// Evaluate the previous completed bar, not bar zero.
// The reference is one bar older still.
if (Bars.CurrentBar < 3)
{
ratio.Set(-1.0);
return;
}
double h = Bars.High[1];
double l = Bars.Low[1];
double ph = Bars.High[2];
double pl = Bars.Low[2];
double referenceRange = ph - pl;
// -1 denotes no valid inside-bar observation.
if (referenceRange <= 0 || h < l ||
h >= ph || l <= pl)
{
ratio.Set(-1.0);
return;
}
// Strict containment; output is a percentage.
ratio.Set(100.0 * (h - l) / referenceRange);
}
}
}
Implementation details
The indicator derives from IndicatorObject and creates its histogram plot in Create. StartCalc is included as a lifecycle method but no persistent calculation state is required. CalcBar reads the price series through Bars.High and Bars.Low. In this API, bracketed offsets refer to bars relative to the bar being calculated. Offset zero is the current calculation bar; offset one is the preceding bar; offset two is the bar before that.
The example deliberately evaluates offset one against offset two. The plotted value is assigned to the current calculation bar, not moved backward to the candle being measured. This one-bar alignment is a feature: the chart displays the result where it can first be known using this implementation. The initial guard is conservative and skips the first two calculation bars. Ensure the study has at least two bars of historical lookback available in its settings. The platform's calculation start and MaxBarsBack configuration can affect how much initial history is skipped.
The sentinel −1 is a visible negative histogram value, not a missing-data instruction. It distinguishes a failed containment test from a valid zero-range inside candle. A downstream signal should check for a nonnegative value before applying a compression threshold. The code avoids division by zero and rejects inverted evaluated ranges. It requires no external libraries beyond the platform and standard .NET assemblies.
Installation in MultiCharts .NET
Open the PowerLanguage .NET Editor, create a new C# indicator named ConfirmedInsideRangeRatio and replace the generated source with the example. Compile it using the editor and resolve any compiler messages. Insert the study into a chart and configure a separate subchart if necessary. Set the maximum number of bars referenced to at least two, or use automatic detection if supported by your version. The code has not been compiled or executed in the platform here; confirm API compatibility and output alignment in your installed version before relying on it.
Closed-bar behavior and repainting
The indicator never uses the current bar's high or low. It reads the preceding completed candle and its reference, so the result does not change simply because the current candle expands. With intrabar updating enabled, the same confirmed value can be calculated repeatedly during the new candle. With bar-close-only updating, the value may not be delivered until the current calculation candle closes. This is an important distinction between the data used in the formula and the time at which the platform actually executes the study.
No future price data is used, and the code does not intentionally rewrite historical values. Changes to historical quotes, chart construction or study settings may nevertheless change the output. A chart label or downstream strategy must respect the one-bar alignment rather than treating the plotted result as a measurement of the candle directly above it. For non-time-based charts, verify the platform's bar completion and reconstruction behavior separately.
Testing and practical limitations
Compare the example with a spreadsheet. Test a strictly contained candle, equal high, equal low, a candle extending beyond either boundary, a zero reference range and a zero-range candidate inside a positive reference. Confirm that the current candle's intrabar changes do not alter the result and that the next candle advances the reference correctly. Compare the chart output after reloading identical historical data. Check both intrabar and bar-close calculation settings to understand when the result is available.
Inside bars describe range containment, not future direction. A small ratio can appear in quiet markets, during temporary pauses in trends or in illiquid conditions. It does not guarantee a subsequent breakout, and the ratio ignores volume, spread and execution costs. A threshold such as 50% is a research choice, not a universal trading rule. Evaluate any proposed use on independent data and across different market conditions.
For inclusive containment, nested mother-bar tracking, alerts or automated strategy integration, contact our Custom MultiCharts .NET indicator Development Services.
Risk disclosure: educational material only. Trading involves risk of loss. Hypothetical examples do not establish profitability.