Library · Pine Script v5 · Library

Momentum

by Electrified-Trading

This Pine Script library calculates various momentum metrics and oscillator values for other scripts to use.

View on GitHub

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

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

Momentum.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 Contains utilities varying algorithms for measuring momentum.
library('Momentum')
import Electrified/MovingAverages/10 as MA
import Electrified/DataCleaner/7 as Data

///////////////////////////////////////////////////
// @function Returns the 'change' (current - previous) in value normalized by standard deviation measured by the provided length.
// @param src The series to measure changes.
// @param len The number of bars to measure the standard deviation.
export changeNormalized(series float src, int len) =>
    Data.normalize(ta.change(src) / src, len)
///////////////////////////////////////////////////

///////////////////////////////////////////////////
// @function Derives momentum from two moving averages of different lengths.
// @param fast The length of the fast moving average.
// @param slow The length of the slow moving average.
// @param src The series to measure from. Default is 'close'.
// @param fastType The type of moving average the fast should use. Values allowed are: SMA, EMA, WMA, VWMA and VAWMA.
// @param slowType The type of moving average the slow should use. Values allowed are: SMA, EMA, WMA, VWMA and VAWMA.
export simple(
  simple int fast,
  simple int slow,
  series float src = close,
  simple string fastType = "SMA",
  simple string slowType = "SMA") =>
    if fast > slow
        runtime.error("A fast length that is greater than the slow length will cause inverted momentum results.")
    MA.get(fastType, fast, src) - MA.get(slowType, slow, src)
///////////////////////////////////////////////////

///////////////////////////////////////////////////
// @function Returns the K and D values of a Stochastic RSI. Allows for different moving averages to produce the K value.
// @param smoothK The length to average the stochastic.
// @param smoothD The length to smooth out K and produce D.
// @param rsiLen The length of the RSI.
// @param stochLen The length of stochastic.
// @param src The series to measure from. Default is 'close'.
// @param kmode The type of moving average to generate. Values allowed are: SMA, EMA, WMA, VWMA and VAWMA.
// @returns [K, D]
export stochRSI(
  simple int smoothK,
  simple int smoothD,
  simple int rsiLen,
  simple int stochLen,
  series float src = close,
  simple string kmode = "SMA") =>
    rsi1 = ta.rsi(src, rsiLen)
    stoch = ta.stoch(rsi1, rsi1, rsi1, stochLen)
    k = MA.get(kmode, smoothK, stoch)
    d = ta.sma(k, smoothD)
    [k, d]
///////////////////////////////////////////////////

///////////////////////////////////////////////////
// @function Same as well-known MACD formula but allows for different moving averages types to be used.
// @param fast The length of the fast moving average.
// @param slow The length of the slow moving average.
// @param signal The length of average to applied to smooth out the signal.
// @param src The series to measure from. Default is 'close'.
// @param fastType The type of moving average the fast should use. Values allowed are: SMA, EMA, WMA, VWMA and VAWMA.
// @param slowType The type of moving average the slow should use. Values allowed are: SMA, EMA, WMA, VWMA and VAWMA.
// @param slowType The type of moving average the signal should use. Values allowed are: SMA, EMA, WMA, VWMA and VAWMA.
// @returns [macd, signal, histogram]
export macd(
  simple int fast = 12,
  simple int slow = 26,
  simple int signal = 9,
  series float src = close,
  simple string fastType = "EMA",
  simple string slowType = "EMA",
  simple string signalType = "SMA") =>
    macdValue = simple(fast, slow, src, fastType, slowType)    
    signalValue = MA.get(signalType, signal, macdValue)
    hist = macdValue - signalValue
    [macdValue, signalValue, hist]
///////////////////////////////////////////////////

// Example:
[m, s, h] = macd()
plot(m, "MACD", color.blue)
plot(s, "Signal", color.yellow)
plot(h, "Histogram", color.gray, style=plot.style_histogram)

Overview

Momentum provides functions for standard-deviation-normalized price change, dual-moving-average momentum, Stochastic RSI, and a customizable MACD. Function parameters let callers adjust calculation lengths, input sources, and moving average types including SMA, EMA, WMA, VWMA, and VAWMA. Because this is a library, it draws nothing on price charts or separate panes and generates no direct trade signals. Users must import the library into an indicator or strategy to display plots or execute trades.

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.

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

MirPapa_Lib_trend

by alvinng214
2 stars

MirPapa_Lib_trend is a Pine Script utility library providing trend and momentum calculation functions for other scripts. It features implementations for several moving average types, including the Hull Moving Average (HMA), Exponential Hull Moving Average (EHM

Library v6 TrendMoving averages
Updated 2 months ago · None stated GitHub repository of MirPapa_Lib_trend

ta

by ivansoft
1 stars

Provides a collection of technical analysis calculation functions for use across Pine scripts. It exports implementations for various indicators and mathematical formulas, including oscillators like the Awesome Oscillator, Aroon, and Schaff Trend Cycle, moving

Library v5 TrendOscillators & momentum
Updated 1 year ago · None stated GitHub repository of ta

FunctionArrayMovingAverage

by ricardosantos79
261 stars

This Pine Script v5 library provides an exported function to calculate moving averages directly over float arrays. Users can supply a sample array, set the smoothing length, choose whether to omit initial values prior to reaching the lookback window, and speci

Library v5 Moving averages
Updated 4 years ago · None stated GitHub repository of FunctionArrayMovingAverage
29 stars

This library exports one function, custom_ma, which returns a 14-period exponential moving average of a supplied source series. It declares no inputs, so the length stays fixed at 14 and the caller chooses the series. It plots nothing and does not generate ent

Library v5 Moving averages
Updated 11 months ago · Custom licence GitHub repository of Exponential Trend [AlgoAlpha]

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
3 stars

This Pine Script v5 library exports detectYinYangCrown, which builds a standard MACD from close using caller-supplied fast, slow, and signal lengths. It tracks histogram color and a small state machine: a green histogram that rises to a peak, then declines, th

Library v5 Oscillators & momentum
Updated 7 weeks ago · None stated GitHub repository of YZM YinYang Crown Library

More Oscillators & momentum 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