thinkScript study guide

Probability Technical Indicator in thinkScript

A probability technical indicator in thinkScript can measure how often price rose over a chosen bar horizon inside a rolling historical sample. The study below reports empirical frequency. It does not calculate the chance of a future gain.

ProbabilityGridStudyexcerpt
script UpRate {
    input horizon = 5;
    input sampleSize = 100;
    def upMove = if close > close[horizon]
        then 1 else 0;
    plot rate = Average(upMove, sampleSize);
}

def up5 = UpRate(horizon = 5);
AddLabel(yes, "5 BAR UP " + AsPercent(up5));
5 BAR UP
10 BAR UP
20 BAR UP
Statistic
Rolling historical up rate
Grid horizons
5, 10, and 20 bars
Output
Lower plot and chart labels

Direct answer

What this probability study measures

For each bar, the study asks whether the close is above the close from a fixed number of bars earlier. It converts that result to 1 or 0, then uses a rolling average. A value of 0.57 means that 57 percent of the observations in the current sample were positive for that bar horizon.

The unit is bars, so the chart aggregation changes the time represented by each horizon. On a daily chart, 5 bars is roughly one trading week. On a 15-minute chart, it is 75 minutes of chart time.

Complete source

thinkScript probability grid code

This single-file study uses one helper for every horizon. The lower plot follows the selected horizon, while the label row keeps the 5, 10, and 20-bar readings visible together.

ProbabilityGridStudy
Review in the thinkScript Editor
declare lower;

input sampleSize = 100;
input selectedHorizon = 10;
input showProbabilityGrid = yes;

script UpRate {
    input price = close;
    input horizon = 5;
    input sampleSize = 100;

    def upMove = if price > price[horizon] then 1 else 0;
    plot rate = Average(upMove, sampleSize);
}

def up5 = UpRate(
    price = close,
    horizon = 5,
    sampleSize = sampleSize
);
def up10 = UpRate(
    price = close,
    horizon = 10,
    sampleSize = sampleSize
);
def up20 = UpRate(
    price = close,
    horizon = 20,
    sampleSize = sampleSize
);
def selectedUpRate = UpRate(
    price = close,
    horizon = selectedHorizon,
    sampleSize = sampleSize
);

plot UpProbability = 100 * selectedUpRate;
plot ReferenceLine = 50;

UpProbability.SetDefaultColor(Color.CYAN);
UpProbability.SetLineWeight(2);
ReferenceLine.SetDefaultColor(Color.GRAY);

AddCloud(
    UpProbability,
    ReferenceLine,
    Color.DARK_GREEN,
    Color.DARK_RED
);

AddLabel(
    showProbabilityGrid,
    "5 BAR UP " + AsPercent(up5),
    if up5 >= 0.5 then Color.GREEN else Color.GRAY
);
AddLabel(
    showProbabilityGrid,
    "10 BAR UP " + AsPercent(up10),
    if up10 >= 0.5 then Color.GREEN else Color.GRAY
);
AddLabel(
    showProbabilityGrid,
    "20 BAR UP " + AsPercent(up20),
    if up20 >= 0.5 then Color.GREEN else Color.GRAY
);
Keep sampleSize larger than the horizons you compare. The first part of the chart may have fewer usable observations because the script needs both the horizon offset and the rolling sample.

Calculation

How the indicator works

  1. 01

    Classify each return

    The helper compares the current close with the close at the requested historical offset. A positive horizon return becomes 1; every other observation becomes 0.

  2. 02

    Average the sample

    Average converts the binary series into a rolling rate. The plot multiplies the rate by 100 so the axis reads as a percentage.

  3. 03

    Display the grid

    AddLabel formats the 5, 10, and 20-bar rates with AsPercent. The selected horizon remains visible as a lower-chart series.

Two different tools

Custom probability grid vs. ProbabilityOfExpiringCone

The built-in study and the code on this page answer different questions. Use the built-in cone when you want its expiration-based forecast range. Use the custom grid when you want to inspect the historical direction rate across fixed bar horizons.

DimensionCustom probability gridProbabilityOfExpiringCone
Question answeredHow often was the horizon return positive in this rolling sample?What future price range corresponds to the selected probability setting?
OutputA lower plot plus 5, 10, and 20-bar labelsUpper and lower forecast cone plots into expiration Fridays
Main inputsSample size and bar horizonForecast period and probability range
InterpretationHistorical directional frequencyStatistical forecast range

Platform steps

Add the study to thinkorswim

  1. 1

    Open the thinkorswim desktop Charts tab, then choose Studies and Edit Studies.

  2. 2

    Click Create, remove the default plot, and paste the complete source from this page.

  3. 3

    Let the thinkScript Editor parse the source. Review any messages before saving the study.

  4. 4

    Add the study to a chart, then compare symbols, aggregations, sample sizes, and horizons.

Frequently asked questions

Adapt the study to your own rules

Use the thinkorswim AI Coding Agent to change the horizons, sample logic, labels, filters, alerts, or plots in one editable thinkScript file. Review the result in Pineify, then check it in the thinkScript Editor and on relevant charts.

Open the thinkScript AI agent

This page is for educational and informational purposes. It does not provide investment advice, predict future prices, or guarantee trading results.