Indicator · Pine Script v6 · Own pane

Jarque-Bera Test (JB)

by mihakralj

Jarque-Bera Test evaluates whether price data follows a normal distribution.

View on GitHub

GitHub stars99on the repository
Pine versionv6current
Lines of code82
Settings2inputs
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

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

jb.pine
// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
indicator("Jarque-Bera Test (JB)", "JB", overlay=false, precision=4)

// Helper function to get a window of series data into an array
_get_window_array(series float source, simple int length) =>
    float[] arr = array.new_float(length)
    for i = 0 to length - 1
        array.set(arr, i, source[length - 1 - i]) // Oldest to newest
    arr

// Helper function to calculate the k-th central moment
// m_k = sum((x_i - mean)^k) / n
_central_moment(float[] arr, int moment_order, float mean_val) =>
    n = array.size(arr)
    if n == 0
        float(na)
    else
        sum_pow_diff = 0.0
        for i = 0 to n - 1
            sum_pow_diff += math.pow(array.get(arr, i) - mean_val, moment_order)
        sum_pow_diff / n

//@function Calculates the Jarque-Bera statistic.
//@param source series float The input series.
//@param length simple int The lookback period (sample size). Min 10.
//@returns series float The Jarque-Bera statistic. Higher values suggest deviation from normality.
jb_stat(series float source, simple int length) => // Renamed function for clarity jb_stat -> jb
    if length < 10 // Need sufficient sample size
        float(na)
    else
        float[] window_arr = _get_window_array(source, length)
        
        float mean_val = array.avg(window_arr) // Pine Script built-in array average
        
        if na(mean_val)
            float(na)
        else
            // Calculate variance (2nd central moment)
            float m2 = _central_moment(window_arr, 2, mean_val)
            
            if na(m2) or m2 < 1e-10 // Avoid division by zero or near-zero std dev
                float(na) // Or 0 if data is truly constant, but JB is ill-defined
            else
                float stddev_val = math.sqrt(m2)
                
                // Calculate 3rd central moment for skewness
                float m3 = _central_moment(window_arr, 3, mean_val)
                float s = m3 / math.pow(stddev_val, 3) // Skewness
                
                // Calculate 4th central moment for kurtosis
                float m4 = _central_moment(window_arr, 4, mean_val)
                float k_raw = m4 / math.pow(stddev_val, 4) // Raw Kurtosis
                float ek = k_raw - 3.0 // Excess Kurtosis
                
                if na(s) or na(ek)
                    float(na)
                else
                    // Jarque-Bera statistic formula: JB = (n/6) * (S^2 + (EK^2)/4)
                    float jb_value_calc = (length / 6.0) * (s * s + (ek * ek) / 4.0) // Renamed internal variable
                    jb_value_calc

// Inputs
i_source = input.source(close, title="Source")
i_length = input.int(20, title="Lookback Period (Sample Size)", minval=10, maxval=200, tooltip="Number of bars for calculation. Min 10. Max 200 due to array processing. Higher values provide more stable estimates but lag more.")

// Calculation
jb_value = jb_stat(i_source, i_length) // Call renamed function

// Plot
plot(jb_value, "Jarque-Bera Statistic", color=color.teal, linewidth=2)

// Critical values for Chi-squared distribution with 2 degrees of freedom (approximate):
// Significance Level | Critical Value
// 10% (0.10)         | 4.605
// 5%  (0.05)         | 5.991
// 1%  (0.01)         | 9.210
// These lines can help interpret the JB statistic. If JB > critical value, reject null hypothesis of normality.
hline(4.605, "Critical Value (10%)", color.gray, linestyle=hline.style_dotted, linewidth=1)
hline(5.991, "Critical Value (5%)", color.orange, linestyle=hline.style_dashed, linewidth=1)
hline(9.210, "Critical Value (1%)", color.red, linestyle=hline.style_solid, linewidth=1)

Overview

Jarque-Bera Test (JB) constructs a rolling array of recent bars to compute sample mean, variance, skewness, and excess kurtosis through central statistical moments. The calculated statistic quantifies departure from normality and plots in a separate indicator pane alongside statistical critical thresholds. Higher values signify strong deviation from a standard Gaussian distribution. Users can adjust the input price source and lookback period.

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
Lookback Period (Sample Size)int20i_length
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