Indicator · Pine Script v6 · Own pane

Mean Squared Error (MSE)

by mihakralj

The Mean Squared Error indicator calculates the mean of squared differences between a source series and its EMA, averaged via a rolling buffer acting as SMA.

View on GitHub

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

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

mse.pine
// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
indicator("Mean Squared Error (MSE)", "MSE")


//@function Calculates Mean Squared Error between two sources using SMA for averaging
//@param source1 First series to compare
//@param source2 Second series to compare
//@param period Lookback period for error averaging
//@returns MSE value averaged over the specified period using SMA
mse(series float source1, series float source2, simple int period) =>
    squared_error = math.pow(source1 - source2, 2)
    if period <= 0
        runtime.error("Period must be greater than 0")
    int p = math.min(math.max(1, period), 4000)
    var float[] buffer = array.new_float(p, na)
    var int head = 0
    var float sum = 0.0
    var int valid_count = 0
    float oldest = array.get(buffer, head)
    if not na(oldest)
        sum := sum - oldest
        valid_count := valid_count - 1
    if not na(squared_error)
        sum := sum + squared_error
        valid_count := valid_count + 1
    array.set(buffer, head, squared_error)
    head := (head + 1) % p
    valid_count > 0 ? sum / valid_count : squared_error

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

// Inputs
i_source1 = input.source(close, "Source")
i_period = input.int(100, "Period", minval=1)
i_source2 = ta.ema(i_source1, i_period)

// Calculation
error = mse(i_source1, i_source2, i_period)

// Plot
plot(error, "MSE", color=color.new(color.red, 60), linewidth=2, style = plot.style_area)
plot(i_source2, "EMA", color=color.yellow, linewidth=1, style = plot.style_line, force_overlay = true)

Overview

This error metric plots as a filled red area on a separate pane, while the EMA overlays as a yellow line on the price chart. Inputs allow source selection and period adjustment. It measures price deviations from smoothed trend for volatility assessment. The tool applies to any market and timeframe with no exit signals.

Details

Published by

mihakralj on GitHub

Type

Indicator, Pine Script v6

Draws

Separate pane below the chart

Category

Volatility & bands, Mean reversion

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_source1
Periodint100i_period
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

261 stars

This indicator calculates standardized z-scores of price data across four different lookback periods (6, 14, 26, and 100). Standardizing data subtracts the moving average from the price and divides by the standard deviation. It applies a Double Exponential Mov

Indicator v5 Mean reversionVolatility & bands
114 stars

This indicator implements a dual-direction variation of the Williams Vix Fix oscillator in a separate pane. It normalizes price data using min-max scaling to compute both upward and downward synthetic volatility metrics across two selectable calculation modes

Indicator v4 Volatility & bandsMean reversion
Updated 1 year ago · None stated GitHub repository of Combo Williams Vix Fix (Twin)

This indicator classifies market behavior into momentum, mean-reversion, noise, or mixed regimes. It fuses four statistical predictability sensors—Variance Ratio across multiple horizons, return autocorrelation, Hurst exponent approximation, and fractal effici

Indicator v6 TrendMean reversion
Updated yesterday · MIT GitHub repository of Predictability Regime Index [WavesUnchained]

Variance Ratio Test

by Anderson-ALGO
9 stars

This indicator calculates the Variance Ratio Test statistic to evaluate random walk behavior across multiple sampling periods. It computes log returns, base period variance over a specified lookback window, and scaled multi-period variances to determine test Z

Indicator v4 Volatility & bandsMean reversion
Updated 5 years ago · None stated GitHub repository of Variance Ratio Test

This indicator builds a channel from a Hull moving average of the source and an ATR scaled by a band multiplier, then normalizes price inside that channel into a 0 to 100 oscillator. It smooths the result with a weighted moving average and plots the line in a

Indicator v6 Mean reversionVolatility & bands
Updated 3 months ago · None stated GitHub repository of Radiant Mean Reversion Channels [Pineify]

TR Basis Monitor

by Enesp4rl4k
7 stars

This indicator calculates the cost-of-carry fair value between futures and spot prices, measuring the basis and fair value deviation in basis points. Operating in a separate pane, it computes a rolling Z-score of deviation using moving averages and standard de

Indicator v6 Mean reversionVolatility & bands
Updated 5 days ago · MIT GitHub repository of TR Basis Monitor

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