Library · Pine Script v5 · Library

Interpolation

by Electrified-Trading

Interpolation is a Pine Script utility library providing mathematical interpolation and easing functions, including linear and quadratic transitions.

View on GitHub

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

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

Interpolation.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)
// Original Easing Functions: (c) 2001 Robert Penner http://robertpenner.com/easing/

// @version=5
// @description Functions for interpolating values. Can be useful in signal processing or applied as a sigmoid function.
library('Interpolation')

clamp(float k, bool unbound) =>
    unbound ? k : k < 0 ? 0 : k > 1 ? 1 : k

///////////////////////////////////////////////////
// @function Returns the linear adjusted value.
// @param k A number (float) from 0 to 1 representing where the on the line the value is.
// @param delta The amount the value should change as k reaches 1.
// @param offset The start value.
// @param unbound When true, k values less than 0 or greater than 1 are still calculated. When false (default), k values less than 0 will return the offset value and values greater than 1 will return (offset + delta). 
export linear(
  float k,
  float delta = 1,
  float offset = 0,
  simple bool unbound = false) =>
    m = clamp(k, unbound) 
    offset + delta * m
///////////////////////////////////////////////////


///////////////////////////////////////////////////
// @function Returns the quadratic (easing-in) adjusted value.
// @param k A number (float) from 0 to 1 representing where the on the curve the value is.
// @param delta The amount the value should change as k reaches 1.
// @param offset The start value.
// @param unbound When true, k values less than 0 or greater than 1 are still calculated. When false (default), k values less than 0 will return the offset value and values greater than 1 will return (offset + delta). 
export quadIn(
  float k,
  float delta = 1,
  float offset = 0,
  simple bool unbound = false) =>
    m = clamp(k, unbound)   
    offset + delta * m * m
///////////////////////////////////////////////////

///////////////////////////////////////////////////
// @function Returns the quadratic (easing-out) adjusted value.
// @param k A number (float) from 0 to 1 representing where the on the curve the value is.
// @param delta The amount the value should change as k reaches 1.
// @param offset The start value.
// @param unbound When true, k values less than 0 or greater than 1 are still calculated. When false (default), k values less than 0 will return the offset value and values greater than 1 will return (offset + delta). 
export quadOut(
  float k,
  float delta = 1,
  float offset = 0,
  simple bool unbound = false) =>
    m = clamp(k, unbound) 
    offset + delta * m * (2 - m)
///////////////////////////////////////////////////

///////////////////////////////////////////////////
// @function Returns the quadratic (easing-in-out) adjusted value.
// @param k A number (float) from 0 to 1 representing where the on the curve the value is.
// @param delta The amount the value should change as k reaches 1.
// @param offset The start value.
// @param unbound When true, k values less than 0 or greater than 1 are still calculated. When false (default), k values less than 0 will return the offset value and values greater than 1 will return (offset + delta). 
export quadInOut(
  float k,
  float delta = 1,
  float offset = 0,
  simple bool unbound = false) =>  
    m = clamp(k, unbound) * 2
    f = if m <= 1
        0.5 * m * m
    else
        m -= 1
        -0.5 * (m * (m - 2) - 1)
    offset + delta * f
///////////////////////////////////////////////////


///////////////////////////////////////////////////
// @function Returns the cubic (easing-in) adjusted value.
// @param k A number (float) from 0 to 1 representing where the on the curve the value is.
// @param delta The amount the value should change as k reaches 1.
// @param offset The start value.
// @param unbound When true, k values less than 0 or greater than 1 are still calculated. When false (default), k values less than 0 will return the offset value and values greater than 1 will return (offset + delta). 
export cubicIn(
  float k,
  float delta = 1,
  float offset = 0,
  simple bool unbound = false) =>
    m = clamp(k, unbound) 
    offset + delta * m * m * m
///////////////////////////////////////////////////

///////////////////////////////////////////////////
// @function Returns the cubic (easing-out) adjusted value.
// @param k A number (float) from 0 to 1 representing where the on the curve the value is.
// @param delta The amount the value should change as k reaches 1.
// @param offset The start value.
// @param unbound When true, k values less than 0 or greater than 1 are still calculated. When false (default), k values less than 0 will return the offset value and values greater than 1 will return (offset + delta). 
export cubicOut(
  float k,
  float delta = 1,
  float offset = 0,
  simple bool unbound = false) =>
    m = clamp(k, unbound) - 1
    m := m * m * m + 1
    offset + delta * m
///////////////////////////////////////////////////

///////////////////////////////////////////////////
// @function Returns the cubic (easing-in-out) adjusted value.
// @param k A number (float) from 0 to 1 representing where the on the curve the value is.
// @param delta The amount the value should change as k reaches 1.
// @param offset The start value.
// @param unbound When true, k values less than 0 or greater than 1 are still calculated. When false (default), k values less than 0 will return the offset value and values greater than 1 will return (offset + delta). 
export cubicInOut(
  float k,
  float delta = 1,
  float offset = 0,
  simple bool unbound = false) =>
    m = clamp(k, unbound) * 2
    f = if m <= 1
        0.5 * m * m * m
    else
        m -= 2
        0.5 * m * m * m + 2
    offset + delta * f
///////////////////////////////////////////////////


///////////////////////////////////////////////////
// @function Returns the exponential (easing-in) adjusted value.
// @param k A number (float) from 0 to 1 representing where the on the curve the value is.
// @param delta The amount the value should change as k reaches 1.
// @param offset The start value.
// @param unbound When true, k values less than 0 or greater than 1 are still calculated. When false (default), k values less than 0 will return the offset value and values greater than 1 will return (offset + delta). 
export expoIn(
  float k,
  float delta = 1,
  float offset = 0,
  simple bool unbound = false) =>
    m = clamp(k, unbound)
    switch m
        0 => offset
        1 => offset + delta
        => offset + delta * math.pow(1024, m - 1)
///////////////////////////////////////////////////

///////////////////////////////////////////////////
// @function Returns the exponential (easing-out) adjusted value.
// @param k A number (float) from 0 to 1 representing where the on the curve the value is.
// @param delta The amount the value should change as k reaches 1.
// @param offset The start value.
// @param unbound When true, k values less than 0 or greater than 1 are still calculated. When false (default), k values less than 0 will return the offset value and values greater than 1 will return (offset + delta). 
export expoOut(
  float k,
  float delta = 1,
  float offset = 0,
  simple bool unbound = false) =>
    m = clamp(k, unbound)
    switch m
        0 => offset
        1 => offset + delta
        => offset + delta * (1 - math.pow(2, -10*m))    
///////////////////////////////////////////////////

///////////////////////////////////////////////////
// @function Returns the exponential (easing-in-out) adjusted value.
// @param k A number (float) from 0 to 1 representing where the on the curve the value is.
// @param delta The amount the value should change as k reaches 1.
// @param offset The start value.
// @param unbound When true, k values less than 0 or greater than 1 are still calculated. When false (default), k values less than 0 will return the offset value and values greater than 1 will return (offset + delta). 
export expoInOut(
  float k,
  float delta = 1,
  float offset = 0,
  simple bool unbound = false) =>
    m = clamp(k, unbound)
    switch m
        0 => offset
        1 => offset + delta
        =>
            m *= 2
            f = m<=1 ?
              math.pow(1024, m - 1) :
              (-math.pow(2, -10*(m - 1)) + 2)
            offset + delta * f * 0.5          
///////////////////////////////////////////////////

///////////////////////////////////////////////////
// @function Returns the adjusted value by function name.
// @param fn The name of the function.  Allowed values: linear, quadIn, quadOut, quadInOut, cubicIn, cubicOut, cubicInOut, expoIn, expoOut, expoInOut.
// @param k A number (float) from 0 to 1 representing where the on the curve the value is.
// @param delta The amount the value should change as k reaches 1.
// @param offset The start value.
// @param unbound When true, k values less than 0 or greater than 1 are still calculated. When false (default), k values less than 0 will return the offset value and values greater than 1 will return (offset + delta). 
export using(
  simple string fn,
  float k,
  float delta = 1,
  float offset = 0,
  simple bool unbound = false) =>
    switch fn
        "linear"      => linear(k, delta, offset, unbound)
        "quadIn"      => quadIn(k, delta, offset, unbound)
        "quadOut"     => quadOut(k, delta, offset, unbound)
        "quadInOut"   => quadInOut(k, delta, offset, unbound)
        "cubicIn"     => cubicIn(k, delta, offset, unbound)
        "cubicOut"    => cubicOut(k, delta, offset, unbound)
        "cubicInOut"  => cubicInOut(k, delta, offset, unbound)
        "expoIn"      => expoIn(k, delta, offset, unbound)
        "expoOut"     => expoOut(k, delta, offset, unbound)
        "expoInOut"   => expoInOut(k, delta, offset, unbound)
        =>
            runtime.error("Unknown function name.")
            na
///////////////////////////////////////////////////


///////////////////////////////////////////////////
// Demo

var float bar = na
day = time("D")
if day != day[1]
    bar := 0
else
    bar += 1

float end = 390 / 2 / timeframe.multiplier // half bars per day
float pos = bar / end

fn = input.string("expoInOut", "Function", options=["linear", "quadIn", "quadOut", "quadInOut", "cubicIn", "cubicOut", "cubicInOut", "expoIn", "expoOut", "expoInOut"])

hline(0)
hline(0.5)
hline(1)
plot(using(fn, pos))

Overview

Designed for signal processing, custom smoothing algorithms, and sigmoid transformations, each exported function computes an interpolated value from an input ratio, change amount, and baseline offset. An unbound setting allows calculations to either clamp the input factor between zero and one or extend past boundaries. As a mathematical utility, it renders no visual plots on the chart.

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.

Settings

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

SettingTypeDefaultVariable
FunctionstringexpoInOutfn
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