Trading Tips — Measuring Upper Range Breach Failure in MultiCharts
A candle that trades above a recent high does not necessarily close above it. In some market environments, intrabar excursions beyond a reference level frequently end with a close back inside the previous trading range. In other environments, those excursions more often coincide with closes outside the range. A simple count of failed breakouts does not distinguish a busy market with many opportunities from a quiet market with very few. This tutorial develops a PowerLanguage indicator that measures the proportion of upper-range breaches that finish back inside their earlier reference interval. It is a descriptive research statistic rather than a prediction of future reversals.
Define the event and its denominator
For every completed candidate candle, calculate the highest high and lowest low of the RangeLength candles immediately preceding it. Call these levels U and L. Exclude the candidate from both calculations so that its own high cannot move the reference level. Ignore a reference interval whose width is zero. An upper breach occurs when the candidate high is strictly greater than U. Classify that breach as a failure only if the final close is between L and U, including the boundaries. A candle closing below L after an upper breach remains in the denominator but is not counted as a return inside the interval under this particular definition.
Over the latest SampleLength completed candles, count all eligible upper breaches and all failures. Divide failures by breaches and multiply by 100. The denominator is the number of events, not the number of observed candles. This distinction matters: if two failures occur during thirty candles but only four candles breach their respective upper levels, the rate is 50%, not 6.67%. If no breaches occur, there is no meaningful conditional estimate, so the study leaves the primary output unplotted.

Walk through the calculations
Suppose the preceding reference interval is 100 to 110. Candidate A has open 106, high 113, low 104 and close 108. Its high exceeds 110, while its close returns inside the interval. Count one breach and one failure. Candidate B has open 108, high 114, low 106 and close 112. Count one breach but no failure. Candidate C has open 104, high 109, low 102 and close 107. Count neither. These three independent examples use a common hypothetical reference level for clarity. The actual study recalculates the reference separately for every candidate, so the boundaries generally change over time.
Boundary cases deserve attention. A high exactly equal to U is not a breach. A high above U followed by a close exactly equal to U qualifies as a failure. A candle that reaches above U but closes below L is a breach without an inside-range failure, as defined here. Some researchers may prefer to classify all closes at or below U as failures; that alternative is valid but must be named and tested as a different event definition. The study makes the chosen condition explicit rather than mixing these interpretations.
Complete native PowerLanguage indicator
Inputs:
RangeLength(20),
SampleLength(30);
Variables:
UpperLevel(0),
LowerLevel(0),
BreachCount(0),
FailureCount(0),
Offset(0),
RefOffset(0);
// Set Maximum Bars Back to at least
// RangeLength + SampleLength.
// Study uses Data1 and completed bars only.
If RangeLength >= 1 and SampleLength >= 1 then begin
If BarStatus(1) = 2 then begin
BreachCount = 0;
FailureCount = 0;
// Examine each candidate in the sample.
For Offset = 0 to SampleLength - 1 begin
UpperLevel = High[Offset + 1];
LowerLevel = Low[Offset + 1];
// Exclude the candidate from its reference.
For RefOffset = Offset + 2 to Offset + RangeLength begin
UpperLevel = MaxList(UpperLevel, High[RefOffset]);
LowerLevel = MinList(LowerLevel, Low[RefOffset]);
end;
// Count upper breaches only.
If UpperLevel > LowerLevel and
High[Offset] > UpperLevel then begin
BreachCount = BreachCount + 1;
// A close inside the old interval is a failure.
If Close[Offset] <= UpperLevel and
Close[Offset] >= LowerLevel then
FailureCount = FailureCount + 1;
end;
end;
If BreachCount > 0 then
Plot1(100.0 * FailureCount / BreachCount, "FailRate")
else
NoPlot(1);
Plot2(50, "Midpoint");
end;
end;
Understand the historical offsets
PowerLanguage uses zero for the current calculation bar and positive historical offsets for earlier bars. On a closing update, candidate Offset equals zero for the most recent completed bar. Increasing Offset moves backward through the sample. For each candidate, the reference starts at Offset+1 and ends at Offset+RangeLength. The two inner variables are initialized from the nearest preceding candle, then expanded by the reference loop. When RangeLength is one, that initial candle is already the complete reference. No future price is consulted.
The implementation uses direct nested loops rather than a recursive running total. Each calculation reconstructs the two counts from the requested windows. This makes the event definition easy to audit and avoids accidental accumulation on repeated callbacks. Its cost is proportional to the product of the sample and reference lengths, so extremely long inputs or many simultaneous charts may require optimization. A production variant could calculate each breach and failure flag once and maintain rolling event counts, provided that initialization and historical recalculation are handled correctly.
Installation and required history
Open the classic MultiCharts PowerLanguage Editor, create a new Indicator named UpperRangeBreachFailureRate, paste the full source and compile it. Add the indicator to a single-data-series chart in a separate subchart. Use positive integer values for both inputs. Set Maximum Bars Back manually to at least RangeLength plus SampleLength; increase it whenever either input increases. This conservative setting ensures the oldest candidate has enough preceding reference candles. Disable intrabar indicator updates for a closed-bar workflow. BarStatus(1)=2 additionally restricts the calculation to closing updates of Data1. The source has not been compiled or executed in MultiCharts here, so verify compatibility in your installed version before deployment.
Closed-bar behavior and repainting
The study uses a completed candidate high and close and historical reference values. It does not use future data or centered calculations, and it does not intentionally repaint confirmed observations. An intrabar move beyond a previous high should not be interpreted as a completed failure event before the candle closes. The next calculation can produce a different rate as candidates leave and enter the sample. That is expected rolling-window behavior, not retrospective rewriting of previous values. Historical corrections, changes to sessions and reconstructed synthetic bars can still alter the historical output.
Build a validation checklist
Use a small RangeLength and SampleLength and mark the relevant reference high and low for each candidate. Independently count breaches and failures, then compare the percentage with the plot. Test an exact touch, a close on each boundary, a close below the lower boundary, and a window with no qualifying breach. Test a zero-width reference and confirm it is excluded. Change the inputs, reload the chart and confirm sufficient historical data. Observe a forming candle that exceeds a reference high and then reverses; the statistic should only incorporate that candidate after the closing calculation.
Interpretation and limitations
A high percentage indicates that a greater share of observed upper breaches closed inside their respective reference intervals. It does not establish that short positions would have been profitable. One failure out of one breach and twenty failures out of twenty breaches both give 100%, but the evidence is not equally informative. A useful extension is to expose BreachCount as a separate sample-size plot and require a minimum count. The indicator does not distinguish whether the excursion was one tick or a large price shock, and ignores liquidity, volume, spread and transaction costs. OHLC data alone cannot establish the exact intrabar order of price movements. Separate upper and lower breach statistics may also help describe asymmetric market behavior. Validate extensions on independent data instead of choosing settings purely for historical results.
For sample-size filters, lower-boundary variants, alerts or optimized rolling calculations, contact our Custom MultiCharts indicator Development Services.
Risk disclosure: educational material only. Trading involves risk of loss. Hypothetical examples do not establish profitability.