Advanced Trading Tutorial — Thinkorswim Stock Hacker
A stock that briefly trades above its recent high is not necessarily a stock that finishes the session above that high. Even when both conditions are true, the size of the retreat from the intrabar peak can differ considerably. A conventional breakout scan treats these observations as a binary event. The breakout retention scan developed here adds a quantitative question: how much of the excursion beyond the previous ceiling was still present at the close? The purpose is to produce a reproducible research filter rather than to predict the next candle or issue an order.
Constructing a reference that cannot move during the candidate bar
Use the last completed candle as the candidate and the twenty completed candles immediately preceding it as the reference window. The reference ceiling is their highest high. Exclude the candidate from the reference, because otherwise its own high would raise the ceiling and erase the excursion being measured. Also exclude the current forming candle. In thinkScript, high[1] is the candidate and Highest(high[2], lookback) selects the preceding window. With the default twenty-bar setting, the oldest input is high[21]. The scan needs twenty-one completed candles in addition to the current bar.
Let C be the reference ceiling, H the candidate high and P the candidate close. The extension is H minus C, and the retained extension is P minus C. If the extension is positive, the retention fraction is (P minus C) divided by (H minus C). Require the close to remain strictly above the ceiling, and require the fraction to be at least 0.70. To avoid accepting tiny excursions, also require the extension to be at least two minimum price ticks. These are separate conditions: a strong fraction on a one-tick move is not sufficient under the defaults.
Worked example: distinguishing a retreat from a retained breakout
Suppose the reference ceiling is 102, the candidate reaches 106 and closes at 105. The extension is four price units. Three units remain at the close, giving a retention fraction of 75%. With a hypothetical tick size of 0.25, the minimum two-tick extension is 0.50, so this candidate passes both filters. If the same high is followed by a close at 103, the fraction falls to 25% and the candidate fails. A close at 102 also fails because the close must be strictly above the ceiling. These examples are hypothetical and do not indicate the likelihood of a subsequent price increase.

Complete native thinkScript scan
# Breakout Retention - Stock Hacker
input lookback = 20;
input minRetention = 0.70;
input minExtensionTicks = 2;
# Candidate is the last completed bar.
def ceiling = Highest(high[2], lookback);
def extension = high[1] - ceiling;
def retained = close[1] - ceiling;
def fraction = if extension > 0
then retained / extension
else 0;
def ready = !IsNaN(high[lookback + 1])
and !IsNaN(close[1]);
# Exactly one Boolean scan output.
plot scan = ready
and extension > 0
and extension >= minExtensionTicks * TickSize()
and retained > 0
and fraction >= minRetention;
Installation in Stock Hacker
Open the Scan tab in Thinkorswim and select Stock Hacker. Add a Study filter, choose Custom, and open the thinkScript editor. Replace the existing expression with the supplied source. Select the aggregation period appropriate for the research question and choose the universe to scan. The code contains exactly one plot, which is a Boolean result. It does not use secondary aggregation or chart drawing functions. Save the scan under a descriptive name. The source has not been compiled in a live terminal for this tutorial; verify it in your own platform before relying on it.
Parameter design and tradeoffs
A shorter reference window makes the ceiling easier to exceed, but the reference may represent only a small local fluctuation. A longer window can identify a more substantial historical boundary while reducing the number of qualifying candidates. The retention threshold controls the amount of retreat allowed after the high. A threshold near one favors candles that finish close to their high; a lower threshold admits greater pullback. Keep the fraction between zero and one, use a positive integer lookback, and choose a nonnegative tick requirement. The script assumes valid inputs. Do not optimize these settings solely against the period used to discover the idea.
The minimum tick filter is useful for eliminating very small excursions, yet two ticks are not economically equivalent across instruments. A thinly traded stock can move several ticks with little available liquidity, while a liquid stock can trade substantial volume within a narrow interval. A separate liquidity filter may be appropriate. If volatility normalization is desired, an independently validated extension threshold based on past volatility can replace the tick rule, but that modification changes the definition of the scanner.
Timing, edge cases and reproducibility
The scan deliberately evaluates the previous bar rather than the current one. On a daily aggregation this means the latest completed daily candle relative to the current bar, and on an intraday aggregation it means the previous intraday candle. The result becomes available when the next bar exists. A scan run after a session ends may still treat the last available candle as index zero; the code does not attempt to detect session closure. Consequently it may report the preceding completed candle until a new bar appears. This conservative convention avoids intrabar changes in the candidate itself. Historical data corrections and aggregation or session-setting changes can still alter results.
A nonpositive extension is handled without division. A candidate that closes exactly at the ceiling is rejected. A gap above the ceiling can pass even if no trade occurred at the ceiling itself: the measurement describes price geometry, not a continuous execution path. If insufficient reference history exists, the readiness test rejects the symbol. Missing or inconsistent market data should be investigated separately.
Validation checklist
- Compare the ceiling with the highest high of the exact twenty bars preceding the candidate.
- Test the 75% example, the 25% rejection and the zero-extension case.
- Test equality at the retention threshold and at the minimum tick extension.
- Confirm that a forming candle does not replace the candidate before a new bar appears.
- Compare results under regular and extended trading hours using matching chart settings.
- Review gaps, corporate actions, illiquid securities and different tick sizes.
Limitations and next steps
Retention is not a probability of continuation. It omits volume, order-book liquidity, transaction costs, volatility regime and broader market context. A high score can occur immediately before a reversal. Treat the result as a candidate list for research, not an entry signal. If you want to adapt this free scan to another reference level, add independent filters or integrate it into a custom workflow, contact us through our Custom Thinkorswim indicator Development Services.
Trading involves risk of loss. No profitability claim or live compilation result is implied. Validate the scan and all trading assumptions independently.