Library · Pine Script v5 · Library

Daily Deviation

by Electrified-Trading· code credited to Electrified

This library calculates arrays of daily price deviations from the open to the high, low, and close over a user-specified number of days.

View on GitHub

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code101
Settings2inputs
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

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

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

// @version=5
// @description Helps in determining the relative deviation from the open of the day compared to the high or low values.
library("DailyDeviation")
import Electrified/SessionInfo/10 as Session
import Electrified/DailyLevels/9 as Daily
import Electrified/DataCleaner/5 as Data

//////////////////////////////////////////////////
// @function Returns a set of arrays representing the daily deviation of price for a given number of days.
// @param daysPrior Number of days back to get the close from.
// @param maxDeviation Maximum deviation before a value is considered an outlier. A value of 0 will not filter results.
// @param spec session.regular (default), session.extended or other time spec.
// @param res The resolution (default = '1440').
// @returns [OH, OL, OC] Where OH = Open vs High, OL = Open vs Low, and OC = Open vs Close
export hlcDeltaArrays(
  simple int days,
  simple float maxDeviation = 3,
  simple string spec = session.regular,
  simple string res = '1440') =>
    Dopen  = Daily.openArray(days, spec, res)
    Dhigh  = Daily.highArray(days, spec, res)
    Dlow   = Daily.lowArray(days, spec, res)
    Dclose = Daily.closeArray(days, spec, res)    
    OH = array.new_float(days + 1)
    OL = array.new_float(days + 1)
    OC = array.new_float(days + 1)

    int d = 0
    while d < days
        d += 1
        o = array.get(Dopen, d)
        h = array.get(Dhigh, d)
        l = array.get(Dlow, d)
        c = array.get(Dclose, d)
        array.set(OH, d, h - o)
        array.set(OL, d, o - l)
        array.set(OC, d, c - o)

    if maxDeviation == 0
        [OH, OL, OC]
    else
        [Data.cleanArray(OH, maxDeviation), Data.cleanArray(OL, maxDeviation), Data.cleanArray(OC, maxDeviation)]  
//////////////////////////////////////////////////

//////////////////////////////////////////////////
// @function Returns a value representing the deviation from the open (to the high or low) of the current day given number of days to measure from. 
// @param daysPrior Number of days back to get the close from.
// @param maxDeviation Maximum deviation before a value is considered an outlier. A value of 0 will not filter results.
// @param comparison The value use in comparison to the current open for the day.
// @param spec session.regular (default), session.extended or other time spec.
// @param res The resolution (default = '1440').
export fromOpen(
  simple int days,
  simple float maxDeviation = 3,
  float comparison = close,
  simple string spec = session.regular,
  simple string res = '1440') =>
    if na(comparison) or not Session.isIn(spec, res)
        na
    else
        oD = Daily.O()
        [OH, OL, OC] = hlcDeltaArrays(days, maxDeviation, spec, res)
        if comparison > oD
            OHbase = oD + array.avg(OH)
            Hstdev = array.stdev(OH)
            d = OHbase + Hstdev
            if comparison < d
                (comparison - oD) / (d - oD)
            else
                d2 = d + Hstdev
                1 + (comparison - d) / Hstdev

        else if comparison < oD
            OLbase = oD - array.avg(OL)
            Lstdev = array.stdev(OL)
            d = OLbase - Lstdev
            if comparison > d
                (comparison - oD) / (oD - d)
            else
                d2 = d - Lstdev
                (comparison - d) / Lstdev - 1

        else
            0
//////////////////////////////////////////////////

days = input.int(50, "Days to Measure Deviation", minval=2)
max = input.float(3, "Data Cleaning: Maximum Deviation", minval=0)

dev = fromOpen(days, max)
plot(dev, "Deviation From Open", dev<0 ? color.blue : color.yellow, style=plot.style_area)

hline(1)
hline(2)
hline(3)
hline(-1)
hline(-2)
hline(-3)

Overview

Daily Deviation optionally filters outliers using a maximum deviation threshold. The fromOpen function returns a normalized value showing how far the current comparison price (defaulting to close) is from the day's open, based on historical deviation statistics. It draws nothing and is intended for use within other scripts on the price chart. The main settings are the number of days to measure and the maximum deviation for outlier cleaning. The library relies on daily levels and session information to determine relevant data.

Details

Published by

Electrified-Trading on GitHub

Code credit

Electrified, as written in the script's header

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
Days to Measure Deviationint50days
Data Cleaning: Maximum Deviationfloat3max
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

SFA FlowLiquidity v1

by genesisgrubio39
1 stars

The SFA FlowLiquidity library computes structural flow and liquidity events from price data. It detects sweep ups and downs with associated scores, identifies liquidity levels, and applies session and regime filters. Persistent arrays track liquidity prices, b

Library v6 SMC / ICT / price actionSupport & resistance
Updated 10 months ago · None stated GitHub repository of SFA FlowLiquidity v1
3 stars

This indicator calculates and plots the NYSE Cumulative Tick Index as colored candles in a separate pane, with green indicating upward tick trends and red downward trends. Overbought and oversold zones are marked using smoothed averages of high and low tick va

Indicator v5 Oscillators & momentumVolatility & bands
Updated 5 months ago · None stated GitHub repository of CSW Tick Internals

AmiComStrategy

by amico1234
1 stars

AmiComStrategy is an indicator that calculates correlations between securities using the Corell indicator over specified timeframes such as 5D. It filters trading opportunities based on session times like 1300-1700 and security up and down times such as 3H. Th

Indicator v4 Sessions & timeVolatility & bands
Updated 2 years ago · None stated GitHub repository of AmiComStrategy

Daily Bias + FVG Strategy

by TradersPost
228 stars

This overlay strategy plots previous-day high and low, the daily open, and fair value gap zones on the price chart. It takes entries on a selectable timeframe when the FVG requirement and an optional session window allow them, then uses an ATR or fixed pip sto

Strategy v6 SMC / ICT / price actionSupport & resistance Repaints
Updated 3 months ago · None stated GitHub repository of Daily Bias + FVG Strategy
56 stars

This indicator marks forex trading sessions and opening ranges on the price chart. It can draw Sydney, Tokyo, Frankfurt, London, and New York session boxes or five- and fifteen-minute opening ranges, plus an optional opening channel and a day-open range. Day r

Indicator v5 Sessions & timeSupport & resistance Repaints
Updated 2 months ago · None stated GitHub repository of Day Trading Booster by DGT

More Volatility & bands scripts

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