Advanced Trading Tutorial — ProRealTime ProBuilder indicator development
A market that repeatedly closes beyond recent trading boundaries is exhibiting a different sequence of events from one whose closes remain inside those boundaries. But even when escapes are frequent, they may be predominantly upward, predominantly downward, or nearly balanced. A simple breakout flag cannot describe both frequency and directional composition. The Range Escape Direction Balance indicator addresses this by maintaining two complementary measurements: the direction balance of qualifying closes and the proportion of observed bars that qualify. This tutorial develops the complete calculation in ProBuilder using completed bars, with a deliberately simple reference range and no order-execution logic.
Define an escape relative to information already available
For every candidate candle, define a reference range using the highest high and lowest low of the preceding M candles. The default M is five. Exclude the candidate itself from both calculations, because including its high or low would allow its own price action to alter the boundary it is being tested against. In ProBuilder, the expressions Highest[referencePeriod](high[1]) and Lowest[referencePeriod](low[1]) produce the intended lagged extrema.
Classify the candidate as an upper escape when its closing price is strictly greater than the upper reference boundary. Classify it as a lower escape when its closing price is strictly less than the lower boundary. Otherwise it is neutral. A close that equals either boundary is neutral. The classification concerns closing prices, not whether the intrabar high or low touched or crossed the reference. Consecutive qualifying candles count individually, rather than as a single prolonged breakout episode.
Two measurements instead of one
Within a window of N completed candidate candles, count the number of upper escapes U and lower escapes D. The default N is twenty. When at least one escape exists, calculate the signed balance as 100 times (U minus D) divided by (U plus D). If no escapes exist, define the balance as zero. Positive values indicate a greater number of upper events, negative values indicate a greater number of lower events, and zero indicates equal counts or no events. The mathematical range is minus one hundred to plus one hundred.
Calculate a second series, escape activity, as 100 times (U plus D) divided by N. Its range is zero to one hundred. This companion measurement resolves an important ambiguity: a balance of +100 can describe a window with one upper escape or a window in which every candle escaped upward. Activity distinguishes those situations. Likewise, a balance of zero accompanied by high activity represents a different event sequence from a zero balance accompanied by no activity. Neither statistic measures the absolute size of price movements.
Worked example
For illustration, shorten N to six. Assume the six completed candidates are classified in chronological order as upper, neutral, upper, lower, neutral and upper. Each classification has already been tested against its own preceding reference range. There are three upper escapes, one lower escape and two neutral candles. The balance is 100 times (3 minus 1) divided by (3 plus 1), which equals +50. The activity is 100 times four divided by six, or approximately 66.67%. The green and red symbols in the figure identify the direction of each qualifying observation; gray denotes a neutral candidate.

Compare this with a window containing just one upper escape and five neutral bars. The balance is +100, but activity is only 16.67%. It would therefore be misleading to rank the second window as universally stronger based solely on its balance. This example demonstrates why the denominator used to normalize the direction count must remain visible in the analysis.
Complete native ProBuilder source
// Range Escape Direction Balance - ProBuilder
// Set the observation and reference periods here.
window = 20
referencePeriod = 5
// Exclude each candidate from its own reference range.
upperBound = Highest[referencePeriod](high[1])
lowerBound = Lowest[referencePeriod](low[1])
escapeUp = 0
escapeDown = 0
IF BarIndex >= referencePeriod THEN
IF close > upperBound THEN
escapeUp = 1
ELSIF close < lowerBound THEN
escapeDown = 1
ENDIF
ENDIF
balance = 0
activity = 0
// Read only completed candidates, starting at index 1.
IF BarIndex >= referencePeriod + window THEN
upCount = Summation[window](escapeUp[1])
downCount = Summation[window](escapeDown[1])
totalCount = upCount + downCount
IF totalCount > 0 THEN
balance = 100 * (upCount - downCount) / totalCount
ENDIF
activity = 100 * totalCount / window
ENDIF
RETURN balance COLOURED(45,95,190) AS "Escape Balance", activity COLOURED(35,145,100) AS "Escape Activity", 0 COLOURED(130,130,130) AS "Zero"
Installation and output interpretation
Open a ProRealTime chart, enter the indicator creation interface, and create a new ProBuilder indicator. Name it Range Escape Direction Balance, paste the complete listing, validate the syntax and add the indicator to a separate chart pane. The blue line shows signed balance, the green line shows activity and the gray line marks zero. To change the periods, edit the two assignments at the top of the source. Use positive integer values; for an initial experiment, choose a reference period of five and an observation window of twenty. To reproduce the illustration, use an observation window of six. The code has not been compiled in a running ProRealTime terminal for this tutorial, so verify the syntax and chart output in your own environment.
Completed-bar alignment and warm-up
The classifications are calculated for each chart bar, but the final sums use escapeUp[1] and escapeDown[1]. Therefore, the forming bar is not counted in the displayed result. The current plot position represents the immediately preceding completed-candidate window. If the platform recalculates on incoming price changes, the displayed measurement should remain unchanged until a new bar starts, assuming the earlier input data are unchanged. This is a conservative alignment convention, not a centered or forward-looking calculation.
BarIndex starts at zero. The first usable candidate requires M preceding bars. The first usable output requires N completed candidates as well as the earlier reference data. Before that condition is met, both outputs are initialized to zero. These warm-up zeros are placeholders and must not be interpreted as measured inactivity. A downstream application should ignore them explicitly. Because neutral observations remain inside the denominator for activity, the code never extends its window to search for additional escape events. This preserves a fixed observation horizon.
Choosing the two periods
The reference period defines the difficulty of the local escape. Shorter reference ranges generally create more opportunities for a qualifying close, whereas longer ranges impose boundaries drawn from more historical observations. The observation window controls the persistence of the count. A short observation window responds quickly when new events enter or old ones leave, but its readings can change substantially on a single bar. A longer window can make the balance and activity less sensitive to isolated observations while retaining older market behavior longer.
Changing either input alters the feature's distribution. Comparing results across instruments requires consistent chart construction, session treatment and period definitions. Neither input has been optimized for a particular instrument. If testing a decision rule based on balance and activity, select parameters using a development dataset and evaluate them independently on unseen periods. Avoid treating the number of parameter combinations tested as independent evidence of a robust effect.
Validation checklist
- Manually compare each candidate close against the preceding M highs and lows.
- Verify that a close exactly on a reference boundary is classified as neutral.
- Reproduce the six-candidate example: +50 balance and 66.67% activity.
- Test all upper, all lower, equal upper and lower, and entirely neutral windows.
- Check that a forming candle crossing a boundary does not change the completed-window outputs.
- Exclude the warm-up placeholders and compare values after reloading identical chart data.
Limitations and further development
An escape count does not measure the amount by which a boundary was exceeded. A small qualifying close and a large displacement receive identical weight. Session gaps and historical price corrections can affect the classifications, and synthetic chart types may create price relationships that differ from conventional time bars. The outputs do not estimate future breakout probability or account for commissions, spreads, liquidity or market impact. For a minimum-distance filter, independent session logic or integration into a customized trading study, contact us through our Custom ProRealTime indicator Development Services.
Trading involves risk of loss. The examples are hypothetical and the source requires local verification. Neither balance nor activity establishes that a trading rule will be profitable. Validate all calculations, market data and execution assumptions before using real capital.