Library · Pine Script v5 · Library

LocalLimit

by Electrified-Trading

LocalLimit is a Pine Script library designed to calculate local upper and local lower limits for price or indicator series.

View on GitHub

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

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

LocalLimit.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 Calculates the local upper or local lower limit for a given series.  Applying multiple passes produces what appears like support or resistance levels.
library('LocalLimit', true)

export type Limit
    float value
    int trend

export type Level
    Limit upper
    Limit lower

///////////////////////////////////////////////////
// @function Produces the recent local upper limit for a given series.
// @param src The source series to derive from.
// @param padding Gives some extra room when lowering the level.
export upper(
  series float src,
  float padding = 0) =>
    value = src[0]
    valuePrev = src[1]
    var limit = value
    var int trend = 0
    var int phase = 0

    phase := if not na(value) and (value > limit or na(limit))
        trend := +1
        limit := value
        0
    else
        switch phase
            0 => 1
            1 => value > valuePrev ? 2 : 1
            2 =>
                if value < valuePrev
                    trend := -1
                    limit := math.min(limit, valuePrev + nz(padding))
                    0
                else
                    2
    limit
///////////////////////////////////////////////////

///////////////////////////////////////////////////
// @function Produces the recent local lower limit for a given series.
// @param src The source series to derive from.
// @param padding Gives some extra room when raising the level.
export lower(
  series float src,
  float padding = 0) =>
    value = src[0]
    valuePrev = src[1]
    var limit = value
    var int trend = 0
    var int phase = 0

    phase := if not na(value) and (value < limit or na(limit))
        trend := -1
        limit := value
        0
    else
        switch phase
            0 => 1
            1 => value < valuePrev ? 2 : 1
            2 =>
                if value > valuePrev
                    trend := +1
                    limit := math.max(limit, valuePrev - nz(padding))
                    0
                else
                    2
    limit
///////////////////////////////////////////////////

///////////////////////////////////////////////////
// @function Determines the local trend of a series that and persists the trend if the value is unchanged.
// @param src The source series to derive from.
// @return +1 when the trend is up; -1 when the trend is down; 0 if the trend is not yet established.
export trend(series float src) =>
    var s = 0
    if src > src[1]
        s := +1
    if src < src[1]
        s := -1
    s
///////////////////////////////////////////////////

///////////////////////////////////////////////////
// @function Creates a Limit object that contains the value and the trend.
// @param src The source series to derive from.
export valueAndTrend(series float src) =>
    Limit.new(src, trend(src))
///////////////////////////////////////////////////

///////////////////////////////////////////////////
// @function Creates a Level object representing the local upper and lower limits.
// @param hiSrc The source to discover the upper limit from.
// @param loSrc The source to discover the lower limit from.
// @param padding Gives some extra room when raising the lower level and lowering the upper level.
export of(
  series float hiSrc = high,
  series float loSrc = low,
  float padding = 0) =>
    hi = valueAndTrend(upper(hiSrc, padding))
    lo = valueAndTrend(lower(loSrc, padding))
    Level.new(hi, lo)
///////////////////////////////////////////////////

///////////////////////////////////////////////////
// @function Creates a Level object representing the local upper and lower limits.
// @param level The source levels to expand of.
// @param padding Gives some extra room when raising the lower level and lowering the upper level.
export of(
  series Level level,
  float padding = 0) =>
    hi = valueAndTrend(upper(level.upper.value, padding))
    lo = valueAndTrend(lower(level.lower.value, padding))
    Level.new(hi, lo)
///////////////////////////////////////////////////

limit0 = of()
limit1 = of(limit0, 0.1)
limit2 = of(limit1, 0.5)

hColor = color.yellow
lColor = color.blue

plot(limit0.upper.value, "H0", hColor, 1)
plot(limit1.upper.value, "H1", hColor, 2)
plot(limit2.upper.value, "H2", hColor, 3)

plot(limit0.lower.value, "L0", lColor, 1)
plot(limit1.lower.value, "L1", lColor, 2)
plot(limit2.lower.value, "L3", lColor, 3)

Overview

By tracking local price extrema through multi-phase state logic, it helps scripts construct dynamic support and resistance boundaries. The library exports custom types and functions to compute upper limits, lower limits, persistence trends, and paired upper and lower level objects. Users can pass custom source series and optional padding parameters to adjust offset spacing. Designed as a developer library, it renders no direct chart drawings.

Details

Published by

Electrified-Trading on GitHub

Type

Library, Pine Script v5

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.

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 2 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

Trade Your Charts With a Regulated Broker

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

Compare brokers