Library · Pine Script v5 · Library

MathExtension

by ricardosantos79· code credited to RicardoSantos

MathExtension is a Pine Script v5 library of exported math helpers and does not plot or trade.

View on GitHub

GitHub stars261on the repository
Pine versionv5runs as is
Lines of code190
Settings0inputs
LicenceNone statedfile: MPL-2.0
Last update4 years ago13 Nov 2021

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

190 lines of Pine Script v5 from beta/libs/Math/MathExtension.pinescript, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

MathExtension.pinescript
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RicardoSantos

//@version=5

// @description Math Extension.
library("MathExtension")
// reference:
//      https://github.com/python/cpython/blob/4a42cebf6dd769e2fa4e234a9e91093b3ad1cb63/Modules/mathmodule.c

// @function calculate log base 2
// @param _value float, number.
// @returns float, base 2 logarithm of value.
export log2(float value)=>//{
    math.log(value) / 0.693147180559945//math.log(2) aproximation
//}

// @function float remainder of x divided by y.
// @param numerator float, division numerator.
// @param denominator float, division denuminator.
// @returns float
export fmod (float numerator, float denominator) => //{
    numerator - (denominator * math.floor(numerator / denominator))
//{ usage:
// float a = 20.156, float b = 0.5
// float c = fmod(numerator=a, denominator=b)
// plot(c)
//}}

// @function computes the fractional part of the argument value.
// @param value float, value to compute.
// @returns float, fractional part.
export fractional (float value) => //{<<
    value - math.floor(value)
//}

// @function Find the integral of value.
// @param value float, value.
// @returns float.
export integral(float value) => //{
    float _new_value = math.abs(value)
    while _new_value - math.floor(_new_value) != 0
        _new_value := _new_value * 10
    _new_value
//{ usage:
// float input_value = input.float(1.2345), plot(integral(input_value))
//}}

// @function Approximation to atan2 calculation, arc tangent of y/ x in the range (-pi,pi) radians.
// @param value_x float, value x.
// @param value_y float, value y.
// @returns float, value with angle in radians. (negative if quadrante 3 or 4)
export atan2 (float value_x, float value_y) => //{<<
    float _one_qtr_pi = 0.25 * math.pi
    float _three_qtr_pi = 3.0 * _one_qtr_pi
    float _abs_y = math.abs(value_y) + 1e-12 // kludge to void zero division
    float _r = 0.0, float _angle = 0.0
    if value_x < 0.0
        _r := (value_x + _abs_y) / (_abs_y - value_x)
        _angle := _three_qtr_pi
    else
        _r := (value_x - _abs_y) / (value_x + _abs_y)
        _angle := _one_qtr_pi
    _angle := _angle + (0.1963 * _r * _r - 0.9817) * _r
    math.sign(value_y) * _angle
//}

// @function Multidimensional euclidean distance from the origin to a point.
// @param value_x float, value x.
// @param value_y float, value y.
// @returns float
export hypotenuse (float value_x, float value_y) => //{
    math.sqrt(value_x * value_x + value_y * value_y)
// usage: 
//      the hypotenuse of a right triangle (3:4:5)
//      hypot(value_x=3.0, value_y=4.0) // 5.0
// reference: 
//      https://github.com/python/cpython/blob/4a42cebf6dd769e2fa4e234a9e91093b3ad1cb63/Modules/mathmodule.c
//}

// @function Determine whether two floating point numbers are near in value.
// @param value_a float, value to compare with.
// @param value_b float, value to be compared against.
// @param relative_tolerance float, default (1.0e-09).
// @param absolute_tolerance float, default (0.0).
// @returns bool
export near_equal (float value_a, float value_b, float relative_tolerance=1.0e-09, float absolute_tolerance=0.0) => //{
    if relative_tolerance < 0 or absolute_tolerance < 0
        bool(na)
    else if value_a == value_b
        true
    else
        float _diff = math.abs(value_b - value_a)
        _diff <= math.abs(relative_tolerance * value_b) or _diff <= math.abs(relative_tolerance * value_a) or _diff <= absolute_tolerance
// usage: 
//      plot(is_close(value_a=1.0, value_b=1.0 + 1.0e-10) ? 1 : 0)
// reference: 
//      https://github.com/python/cpython/blob/4a42cebf6dd769e2fa4e234a9e91093b3ad1cb63/Modules/mathmodule.c
//}

// @function Factorize a number.
// @param value int, positive number.
// @returns int
export factorize (int value) => //{
    if value == 0 or value == 1
        1
    else if value < 0 or na(value)
        int(na)
    else
        int _value = 1
        for _i = 2 to value
            _value *= _i
        int(_value)
// usage:
//      plot(factorize(int(na))) // na
//      plot(factorize(-3)) // na
//      plot(factorize(0)) // 1
//      plot(factorize(1)) // 1
//      plot(factorize(2)) // 2
//      plot(factorize(3)) // 6
//      plot(factorize(5)) // 120
//      plot(factorize(10)) // 3628800
//      plot(factorize(20)) // 2432902008176640000
//
// reference:
//      https://www.freecodecamp.org/news/how-to-factorialize-a-number-in-javascript-9263c89a4b38/
//}

// @function Number of ways to choose k items from n items without repetition and with order.
// @param options_size int, number of items to pool from
// @param combo_size int, number of items to be chosen
// @returns int
export permutations (int options_size, int combo_size) => //{
    if na(combo_size)
        factorize(value=options_size)
    else if na(options_size)
        int(na) // must have items to select from
    else if options_size < 0 or combo_size < 0
        int(na) // must be positive
    else if options_size < combo_size
        0
    else
        _factor = options_size
        _result = options_size
        for _i = combo_size to 2
            _factor -= 1
            _result *= _factor
        _result
// usage:
//      plot(permutations(52, 5)) // 311875200
//      plot(permutations(10, 2)) // 90
//      plot(permutations(5, 3)) // 60
//      plot(permutations(3, 5)) // 0 // k > n
//      plot(permutations(5, int(na))) // 120
//}

// @function Find the total number of possibilities to choose k things from n items
// @param options_size int, number of items to pool from
// @param combo_size int, number of items to be chosen
// @returns int
export combinations (int options_size, int combo_size) => //{
    if na(combo_size)
        int(na)
    else if na(options_size)
        int(na) // must have items to select from
    else if options_size < 0 or combo_size < 0
        int(na) // must be positive
    else 
        int _temp = options_size - combo_size
        if _temp < 0
            0
        else if combo_size == 1
            1
        else
            int _k = math.min(combo_size, options_size - combo_size)
            
            _factor = options_size
            _result = options_size
            for _i = _k to 2
                _factor -= 1
                _result *= _factor
                _result := math.floor(_result / _i + 1)
            _result - 1
// usage:
//      plot(combinations(7, 5)) // 21
//      plot(combinations(10, 2)) // 45
//      plot(combinations(5, 3)) // 10
//      plot(combinations(3, 5)) // 0
//      plot(combinations(5, int(na))) // n/a
//}

Overview

MathExtension supplies base-2 log, floating-point remainder, fractional part, a digit-scaling integral helper, an atan2 approximation in radians, Euclidean hypotenuse, and a near-equal float comparison with relative and absolute tolerances. Callers pass the numeric arguments; the library has no user inputs and draws nothing on the chart or in a pane. It is a utility library, not a signal or strategy.

Details

Published by

ricardosantos79 on GitHub

Code credit

RicardoSantos, as written in the script's header

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

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

Zignaly2Beta

by ruckard
13 stars

This library provides helper functions to format and build JSON webhook alert payloads for the Zignaly automated trading platform. It constructs formatted messages containing parameters such as trading pair, exchange, account type, leverage, margin mode, hedge

Library v5
Updated 1 year ago · None stated GitHub repository of Zignaly2Beta

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