Statistics
Z-Score Calculator (Price vs Moving Average)
Paste closing prices and this computes the z-score of the latest close against its simple moving average and standard deviation. It expresses how far price sits from its recent mean in standard deviation units.
What the z-score is
A z-score, also called a standard score, states how many standard deviations a value lies from the mean of its group: z = (x - mean) / standard deviation. A z-score of 0 means the value equals the mean, +1 means one standard deviation above it, and -1 means one below.
Applied to a price series, the mean is the simple moving average over the lookback and the deviation is the standard deviation of the same closes. The result measures the distance between the latest close and its moving average in units that adjust to how volatile the window has been, so the same price gap reads differently in a quiet window than in a noisy one.
Why the z-score is used
Many mean-reversion indicators are built on this idea, since it puts distance from the average on a common scale across instruments and timeframes. It is closely related to Bollinger Bands, where the bands sit a chosen number of standard deviations around the basis, so a close on the upper band has a z-score equal to that multiplier.
A z-score is a descriptive statistic. It says nothing on its own about what price will do next, and price distributions are not guaranteed to be normal. How you read it is a matter for your own analysis.
What are TradingView and Pine Script?
TradingView is one of the most widely used charting and market-analysis platforms, where traders and analysts study price movement across stocks, crypto, forex, and futures on interactive charts. Pine Script is TradingView's own lightweight programming language, created so anyone can build custom tools that run directly on those charts.
People use Pine Script to build four main kinds of tools. Indicators calculate and plot values on the chart, exactly like the calculation above, but recomputed automatically on every bar. Strategies add explicit entry and exit rules and can be backtested against historical data in TradingView's Strategy Tester to see how they would have behaved. Screeners scan many symbols at once for conditions you define. Alerts notify you the moment a condition you specified occurs, so you do not have to watch the screen.
The value is precision and automation. Instead of eyeballing a chart, you describe exactly what you want measured, visualized, or notified about, and TradingView runs it consistently across any market and timeframe. That is why traders, analysts, and developers write Pine Script: it turns a manual charting idea into a repeatable tool. These tools are for tracking, visualizing, and testing market ideas; they do not tell you what to trade, and that decision always remains yours.
Writing that code by hand means learning Pine Script's syntax, its type system, and the exact names of hundreds of built-in functions. It is a real programming language, and small mistakes stop a script from compiling in the Pine Editor.
Turn this into Pine Script
This calculator gives you the z-score at one moment. On a live TradingView chart you want it recalculated on every bar in its own pane. In Pine Script the core is (close - ta.sma(close, 20)) / ta.stdev(close, 20), but handling a zero deviation, adding threshold lines, alerts, or strategy logic is where most of the work lives.
//@version=6
indicator("Z-Score (price vs SMA)", overlay = false)
length = input.int(20, "Length", minval = 2)
src = input.source(close, "Source")
basis = ta.sma(src, length)
dev = ta.stdev(src, length) // population standard deviation, divides by N
// Guard against a zero deviation on perfectly flat prices
z = dev == 0 ? na : (src - basis) / dev
plot(z, "Z-Score", color = color.purple)
hline(0, "Mean", color = color.gray)
hline(2, "+2")
hline(-2, "-2")PineScripter is an AI built specifically for Pine Script. You describe what you want in plain English and it writes TradingView-ready v6 code. Because it is specialized on the Pine Script language and its exact function signatures, it tends to produce code that compiles far more reliably than general-purpose models like ChatGPT, which often invent functions that do not exist in Pine Script.
Related free calculators
From the blog
PineScripter is an AI developer tool that helps you write Pine Script code. It is not a financial advisor and will never offer financial, investment, or trading advice. Everything on this page, including the calculator and the explanations, is provided purely for educational and informational purposes. Any decision about how to interpret an indicator or trade a market is entirely your own. See our full disclaimer for more.