Library · Pine Script v5 · Library

DataCleaner

by Electrified-Trading

DataCleaner is a Pine Script utility library providing functions to calculate outlier levels and generate cleaned versions of input data series.

View on GitHub

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code212
Settings3inputs
LicenceMPL-2.0
Last update1 year ago7 Jul 2025

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 library, select all of the template text and paste. Press Save and give it a name.
  4. 4Publish or import itA library draws nothing. Publish it from the editor, then import its functions in your own scripts.

Source Code

212 lines of Pine Script v5 from DataCleaner.pine, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.

DataCleaner.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Electrified (electrifiedtrading)

// @version=5
// @description Functions for acquiring outlier levels and deriving a cleaned version of a series.
library("DataCleaner")

//////////////////////////////////////////////////
// @type A struct of the result of calculating the average and the standard deviation.
// @field average The average.
// @field stdev The standard deviation.
export type Deviation
    float average
    float stdev
//////////////////////////////////////////////////

//////////////////////////////////////////////////
// @function Gets the average and standard deviation for a given series.
// @param src The source series
// @param len The The number of bars to measure. 
export calcDeviation(
  series float src,
  simple int len) =>
    avg = ta.sma(src, len)
    std = ta.stdev(src, len)
    Deviation.new(avg, std)
//////////////////////////////////////////////////

//////////////////////////////////////////////////
// @function Gets the (standard deviation) outlier level for a given series.
// @param src The series to average and add a multiple of the standard deviation to.
// @param len The The number of bars to measure. 
// @param level The positive or negative multiple of the standard deviation to apply to the average. A positive number will be the upper boundary and a negative number will be the lower boundary. 
// @returns The average of the series plus the multiple of the standard deviation. 
export outlierLevel(
  series float src,
  simple int len,
  simple float level) =>
    if level == 0
        runtime.error('Passing a level of (0) zero to "level" is simply the average.')
    d = calcDeviation(src, len)
    d.average + d.stdev * level
//////////////////////////////////////////////////

//////////////////////////////////////////////////
// @function Returns only values that are within the maximum deviation.
// @param src The series to filter results from.
// @param len The The number of bars to measure. 
// @param maxDeviation The maximum deviation before considered an outlier. 
export naOutliers(
  series float src,
  simple int len,
  simple float maxDeviation = 4) =>
    if maxDeviation == 0
        runtime.error('Passing a level of (0) zero to "maxDeviation" is simply the average.')
    prev = src[1] // ignore current in measurment as it could throw off result.
    avg = ta.sma(prev, len)
    dev = ta.stdev(prev, len) * maxDeviation
    src > avg + dev or src < avg - dev ? na : src
//////////////////////////////////////////////////

//////////////////////////////////////////////////
// @function Returns the source value adjusted by its standard deviation.
// @param src The series to measure.
// @param len The number of bars to measure the standard deviation.
// @param maxDeviation The maximum deviation before considered an outlier.
// @param baseline The value considered to be at center.  Typically zero.
export normalize(
  series float src,
  simple int len,
  simple int maxDeviation = 4,
  float baseline = 0) =>
    cleaned = naOutliers(src, len, maxDeviation)
    d = calcDeviation(cleaned, len)
    src / (src >= baseline ? (d.average + d.stdev) : math.abs(d.average - d.stdev))
//////////////////////////////////////////////////

//////////////////////////////////////////////////
// @function Returns an array representing the result series with (outliers provided by the source) removed.
// @param src The source series to read from.
// @param result The result series.
// @param len The maximum size of the resultant array.
// @param maxDeviation The positive or negative multiple of the standard deviation to apply to the average. A positive number will be the upper boundary and a negative number will be the lower boundary. 
// @returns An array containing the cleaned series. 
export cleanUsing(
  series float src,
  series float result,
  simple int len,
  simple float maxDeviation) =>

    var cleaned = array.new_float() 

    if maxDeviation == 0
        runtime.error('Cannot clean a series with a "maxDeviation" of (0) zero.')
        cleaned

    else
        // Hold the previous value so it's not included in the set yet.
        var raw = array.new_float()

        var level = outlierLevel(src, len, maxDeviation)

        // Is the value in bounds?  Add it to the series.
        if(maxDeviation > 0 and src < level or maxDeviation < 0 and src > level)
            array.push(cleaned, result)

        // Maxed out results?
        if(array.size(cleaned) > len)
            array.shift(cleaned)

        level := outlierLevel(src, len, maxDeviation)

        cleaned    
//////////////////////////////////////////////////

//////////////////////////////////////////////////
// @function Returns an array representing the source series with outliers removed.
// @param src The source series to read from.
// @param len The maximum size of the resultant array.
// @param maxDeviation The positive or negative multiple of the standard deviation to apply to the average. A positive number will be the upper boundary and a negative number will be the lower boundary. 
// @returns An array containing the cleaned series. 
export clean(
  series float src,
  simple int len,
  simple float maxDeviation) =>
    cleanUsing(src, src, len, maxDeviation)
//////////////////////////////////////////////////

//////////////////////////////////////////////////
// @function Returns an array representing the source array with outliers removed.
// @param src The source series to read from.
// @param maxDeviation The positive or negative multiple of the standard deviation to apply to the average. A positive number will be the upper boundary and a negative number will be the lower boundary. 
// @returns An array containing the cleaned series. 
export cleanArray(
  float[] src,
  simple float maxDeviation) =>
    if array.size(src)==0
        src
    else
        avg = array.avg(src)
        stdev = array.stdev(src)
        cleaned = array.new_float()
        last = array.size(src) - 1
        level = stdev * maxDeviation
        hi = avg + level
        lo = avg - level
        for i = 0 to last
            v = array.get(src, i)
            if lo < v and v < hi
                array.push(cleaned, v)
        cleaned 
//////////////////////////////////////////////////

//////////////////////////////////////////////////
// @function Returns an array representing the source array with outliers removed.
// @param src The array to set outliers to N/A.
// @param maxDeviation The maximum deviation before considered an outlier.
// @returns True if there were any outliers; otherwise false.
export naArrayOutliers(
  float[] src,
  simple float maxDeviation) =>
    if array.size(src)==0
        false
    else
        avg = array.avg(src)
        stdev = array.stdev(src)
        last = array.size(src) - 1
        level = stdev * maxDeviation
        hi = avg + level
        lo = avg - level
        bool modified = false
        for i = 0 to last
            v = array.get(src, i)
            if lo > v or v > hi
                array.set(src, i, na)
                modified := true
        modified 
//////////////////////////////////////////////////

//////////////////////////////////////////////////
// @function Gets the (standard deviation) outlier level for a given series after a single pass of removing any outliers.
// @param src The series to average and add a multiple of the standard deviation to.
// @param len The The number of bars to measure. 
// @param level The positive or negative multiple of the standard deviation to apply to the average. A positive number will be the upper boundary and a negative number will be the lower boundary. 
// @param maxDeviation The optional standard deviation level to use when cleaning the series.  The default is the value of the provided level. 
// @returns The average of the series plus the multiple of the standard deviation. 
export outlierLevelAdjusted(
  series float src,
  simple int len,
  simple float level,
  simple float maxDeviation = 0)=>
    if level == 0
        runtime.error('Passing a level of (0) zero to "level" is simply the average.')
    cleaned = clean(src, len, maxDeviation==0 ? level : maxDeviation)
    avg = array.avg(cleaned)
    std = array.stdev(cleaned)  
    avg + std * level
//////////////////////////////////////////////////

len = input.int(200, "Length")
maxDev = input.float(2, "Max Deviation", minval=1)
level = input.float(4, "Max Deviation After Cleaning", minval=1)
//plot(ta.atr(len), "ATR", color.red)

candleHeight = high - low
plot(candleHeight, "Candle Height", color.yellow)

before = outlierLevel(candleHeight, len, maxDev)
plot(before, "Deviation Before Adjustment", color.orange)

after = outlierLevelAdjusted(candleHeight, len, level)
plot(after, "Deviation After Adjustment", color.green)

Overview

DataCleaner utilizes moving averages and standard deviations to detect extreme statistical deviations, replace outlier values with non-available values, and normalize data relative to a customizable baseline. Designed as a developer library, it draws nothing on the price chart directly and is intended for implementation within custom indicators or trading strategies requiring sanitized data inputs.

Details

Published by

Electrified-Trading on GitHub

Draws

Library (no plots)

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.

Settings

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

SettingTypeDefaultVariable
Lengthint200len
Max Deviationfloat2maxDev
Max Deviation After Cleaningfloat4level
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

AIActivationFunctionsBipolarSigmoid is a Pine Script library that provides mathematical implementations of the bipolar sigmoid activation function and its derivatives for machine learning algorithms. It calculates bipolar sigmoid values bounded between -1 and

Library v5 AI / machine learning
Updated 4 years ago · None stated GitHub repository of AIActivationFunctionsBipolarSigmoid

KernelFunctions

by artificial-intelligence-edge
75 stars

KernelFunctions is a Pine Script v5 library that exports kernel weight functions for Nadaraya-Watson style estimation. It calculates a Rational Quadratic kernel, a Gaussian kernel based on a radial basis function, and a periodic kernel that models repeating be

Library v5
Updated 4 days ago · MIT GitHub repository of KernelFunctions

Ta

by nersent
29 stars

This Pine Script v5 library exports thin wrappers around built-in technical analysis functions. It covers moving averages, momentum measures such as RSI and CCI, volatility tools including ATR and standard deviation, range extremes, cross detection, and runnin

Library v5
Updated 11 months ago · Custom licence GitHub repository of Ta

Bands

by heyphat
26 stars

This library exports a Band type and functions that build a simple-moving-average basis with upper and lower envelopes from standard deviation. It classifies a source as above, below, or inside those envelopes, measures band width, and scales an imported z-sco

Library v6 Volatility & bandsMoving averages
Updated 2 months ago · AGPL-3.0 GitHub repository of Bands

Mylib

by ferranbt
21 stars

This script serves as a minimal utility library written in Pine Script v6. It provides an exported helper function that calculates the sum of two numeric arguments. As a reusable code library, it produces no visual plots, charts, or trading signals, and it con

Library v6
Updated 7 weeks ago · MPL-2.0 GitHub repository of Mylib

HLevel

by stupenkov
14 stars

HLevel is a Pine Script v5 utility library designed to manage horizontal price levels for cryptocurrency charts. It defines custom types for pivots and levels, along with methods to initialize, add, track, and expire price levels based on pivot high and pivot

Library v5
Updated 3 years ago · None stated GitHub repository of HLevel

More libraries

Related on ForexR

Trade Your Charts With a Regulated Broker

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

Compare brokers