Indicator · Pine Script v6 · Own pane

Student's t-Distribution CDF (TDIST)

by mihakralj

This indicator plots the cumulative distribution function of Student's t-distribution in a separate pane.

View on GitHub

GitHub stars99on the repository
Pine versionv6current
Lines of code142
Settings3inputs
LicenceApache-2.0
Last update4 weeks ago27 Sep 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

142 lines of Pine Script v6 from lib/numerics/tdist/tdist.pine, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

tdist.pine
// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
indicator("Student's t-Distribution CDF (TDIST)", "TDIST", overlay=false, precision=6)

//@function Natural log of the Gamma function via Lanczos approximation (g=7, 9 coefficients)
//@param z Argument (must be > 0)
//@returns ln(Γ(z))
lnGamma(float z) =>
    float g = 7.0
    array<float> c = array.from(
         0.99999999999980993,
       676.5203681218851,
     -1259.1392167224028,
       771.32342877765313,
      -176.61502916214059,
        12.507343278686905,
        -0.13857109526572012,
         9.9843695780195716e-6,
         1.5056327351493116e-7)
    float zz = z < 0.5 ? 1.0 - z : z - 1.0
    float x = array.get(c, 0)
    for i = 1 to 8
        x += array.get(c, i) / (zz + float(i))
    float t = zz + g + 0.5
    float logSqrt2Pi = 0.9189385332046727
    float lnG = logSqrt2Pi + math.log(t) * (zz + 0.5) - t + math.log(x)
    z < 0.5 ? math.log(math.pi / math.sin(math.pi * z)) - lnG : lnG

//@function Regularized incomplete beta function I_x(a, b) via Lentz continued fraction
//@param x Upper integration limit in [0, 1]
//@param a First shape parameter (> 0)
//@param b Second shape parameter (> 0)
//@returns I_x(a, b) in [0, 1]
betaReg(float x, float a, float b) =>
    int MAXITER = 200
    float EPS   = 1e-10
    float TINY  = 1e-30

    float result = 0.0
    if x <= 0.0
        result := 0.0
    else if x >= 1.0
        result := 1.0
    else
        bool flipped = x > (a + 1.0) / (a + b + 2.0)
        float xx = flipped ? 1.0 - x : x
        float aa = flipped ? b : a
        float bb = flipped ? a : b

        float logPfx = aa * math.log(xx) + bb * math.log(1.0 - xx)
                     - math.log(aa)
                     - lnGamma(aa) - lnGamma(bb) + lnGamma(aa + bb)
        float pfx = math.exp(logPfx)

        float f = 1.0 + TINY
        float C = f
        float D = 0.0
        for m = 1 to MAXITER
            float m2 = 2.0 * float(m)
            float numEven = float(m) * (bb - float(m)) * xx /
                           ((aa + m2 - 1.0) * (aa + m2))
            D := 1.0 + numEven * D
            D := math.abs(D) < TINY ? TINY : D
            D := 1.0 / D
            C := 1.0 + numEven / C
            C := math.abs(C) < TINY ? TINY : C
            f *= C * D
            float numOdd = -(aa + float(m)) * (aa + bb + float(m)) * xx /
                          ((aa + m2) * (aa + m2 + 1.0))
            D := 1.0 + numOdd * D
            D := math.abs(D) < TINY ? TINY : D
            D := 1.0 / D
            C := 1.0 + numOdd / C
            C := math.abs(C) < TINY ? TINY : C
            float delta = C * D
            f *= delta
            if math.abs(delta - 1.0) < EPS
                break

        float raw = pfx * f
        result := flipped ? 1.0 - raw : raw
    result

//@function Calculates Student's t-Distribution CDF
//@param source Series to evaluate (typically close)
//@param period Lookback period for min-max normalization
//@param df Degrees of freedom (ν > 0)
//@returns CDF value P(T ≤ t) in [0, 1]
//@description The Student's t-distribution CDF is computed via the relation:
//    CDF(t; ν) = 1 − 0.5 × I(ν/(ν+t²), ν/2, 1/2)  if t ≥ 0
//    CDF(t; ν) = 0.5 × I(ν/(ν+t²), ν/2, 1/2)        if t < 0
//  where I is the regularized incomplete beta function (Lentz CF).
//  The source is min-max normalized over the lookback period, then mapped
//  to a t-statistic via linear transform: t = (x − 0.5) × tScale where
//  tScale = 6.0 maps the [0,1] range to approximately [−3, +3].
//  Reuses lnGamma (Lanczos 9-coeff) and betaReg (Lentz CF with symmetry flip)
//  from BETADIST/FDIST. Stateless pure function — no var state.
//  df=1 → Cauchy (heavy tails), df=5 → moderate tails, df→∞ → normal.
//  Trading interpretation: CDF near 1.0 = price at top of recent range
//  (assuming large df, approaches normal behavior). Heavy tails (low df) make
//  the CDF less extreme, reflecting uncertainty about outlier moves.
tdist(series float source, simple int period, simple float df) =>
    if period <= 0
        runtime.error("Period must be greater than 0")
    if df <= 0.0
        runtime.error("Degrees of freedom must be greater than 0")

    float src = nz(source)
    float hi = src
    float lo = src
    for i = 1 to period - 1
        float v = nz(source[i])
        hi := math.max(hi, v)
        lo := math.min(lo, v)
    float range = hi - lo
    float x = range == 0.0 ? 0.5 : (src - lo) / range

    float tScale = 6.0
    float t = (x - 0.5) * tScale

    float t2 = t * t
    float bx = df / (df + t2)
    float ibeta = betaReg(bx, df / 2.0, 0.5)

    t >= 0.0 ? 1.0 - 0.5 * ibeta : 0.5 * ibeta

// ---------- Main loop ----------

// Inputs
i_source = input.source(close, "Source")
i_period = input.int(50, "Period", minval=1)
i_df     = input.float(5.0, "Degrees of Freedom", minval=0.1, step=0.1)

// Calculation
tdist_value = tdist(i_source, i_period, i_df)

// Plot
plot(tdist_value, "TDIST", color=color.yellow, linewidth=2)
hline(0.5, "Midline", color=color.gray, linestyle=hline.style_dotted)
hline(0.95, "Upper", color=color.red, linestyle=hline.style_dashed)
hline(0.05, "Lower", color=color.green, linestyle=hline.style_dashed)

Overview

Student's t-Distribution CDF (TDIST) min-max normalizes the chosen source over a lookback period, maps the result to a t-statistic, and evaluates the CDF through the regularized incomplete beta function and a Lanczos log-gamma approximation. Source, period, and degrees of freedom change the input series, the normalization window, and the distribution shape. It does not define trade entries, exits, or position sizing.

Details

Published by

mihakralj on GitHub

Type

Indicator, Pine Script v6

Draws

Separate pane below the chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

algorithmic-tradingcsharpdotnetpinescriptpythonquantitative-financesimdta-libtechnical-analysistechnical-indicators

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Sourcesourceclosei_source
Periodint50i_period
Degrees of Freedomfloat5.0i_df
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Asymmetrical RSI

by everget
948 stars

Asymmetrical RSI is an indicator that computes a custom relative strength index from the rate of change of a user-selected source. It plots the resulting ARSI line in a separate pane with color coding that turns green above the overbought level and red below t

Indicator v4 Oscillators & momentum
Updated 10 days ago · GPL-3.0 GitHub repository of Asymmetrical RSI

This indicator calculates momentum setups based on the Relative Strength Index (RSI) and plots in a separate pane. It offers multiple scanning modes such as Continuous Break, Continuous Retracement, Flip Break, Flip Retracement, and both regular and hidden div

Indicator v6 Oscillators & momentumDivergence Repaints
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup - RSI Directional Momentum Indicator
549 stars

Range Volume Change compares volume inside a chosen session window with the same window on the previous day and plots that change as a percentage. It accumulates volume during the window and, when enabled, volume outside it, drawing growing columns in a separa

Indicator v5 VolumeSessions & time Repaints
Updated 10 months ago · None stated GitHub repository of Range Volume Change

Beep Boop

by magic8bot
409 stars

This indicator calculates a modified MACD oscillator and signal line alongside a trend EMA filter, plotting the output as a stylized column histogram in a separate pane. Instead of plotting true MACD distance, it quantifies positive and negative histogram mome

Indicator v4 Oscillators & momentumTrend
Updated 3 years ago · MIT GitHub repository of Beep Boop

Mojito v3 Strategy

by namuan
318 stars

The Mojito v3 Strategy indicator evaluates volatility term structure by calculating the ratio between front-month and second-month VIX futures contracts. It smooths this ratio using a configurable median filter and compares the result against user-defined uppe

Indicator v5 Volatility & bands
Updated 2 months ago · MIT GitHub repository of Mojito v3 Strategy

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers