Library · Pine Script v5 · Library

MathConstants

by ricardosantos79· code credited to RicardoSantos

This Pine Script v5 library exports frequently used mathematical constants as functions that return fixed numeric values.

View on GitHub

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

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

MathConstants.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 Mathematical Constants
library(title='MathConstants')

//  reference:
//      https://github.com/mathnet/mathnet-numerics/blob/master/src/Numerics/Constants.cs


// A collection of frequently used mathematical constants.
    
// Mathematical Constants {

// @function The number e
export E () => //{
    2.7182818284590452353602874713526624977572470937000
//}

// @function The number log[2](e)
export Log2E () => //{
    1.4426950408889634073599246810018921374266459541530
//}

// @function The number log[10](e)
export Log10E () => //{
    0.43429448190325182765112891891660508229439700580366
//}

// @function The number log[e](2)
export Ln2 () => //{
    0.69314718055994530941723212145817656807550013436026
//}

// @function The number log[e](10)
export Ln10 () => //{
    2.3025850929940456840179914546843642076011014886288
//}

// @function The number log[e](pi)
export LnPi () => //{
    1.1447298858494001741434273513530587116472948129153
//}

// @function The number log[e](2*pi)/2
export Ln2PiOver2 () => //{
    0.91893853320467274178032973640561763986139747363780
//}

// @function The number 1/e
export InvE () => //{
    0.36787944117144232159552377016146086744581113103176
//}

// @function The number sqrt(e)
export SqrtE () => //{
    1.6487212707001281468486507878141635716537761007101
//}

// @function The number sqrt(2)
export Sqrt2 () => //{
    1.4142135623730950488016887242096980785696718753769
//}

// @function The number sqrt(3)
export Sqrt3 () => //{
    1.7320508075688772935274463415058723669428052538104
//}

// @function The number sqrt(1/2) = 1/sqrt(2) = sqrt(2)/2
export Sqrt1Over2 () => //{
    0.70710678118654752440084436210484903928483593768845
//}

// @function The number sqrt(3)/2
export HalfSqrt3 () => //{
    0.86602540378443864676372317075293618347140262690520
//}

// @function The number pi
export Pi () => //{
    3.1415926535897932384626433832795028841971693993751
//}

// @function The number pi*2
export Pi2 () => //{
    6.2831853071795864769252867665590057683943387987502
//}

// @function The number pi/2
export PiOver2 () => //{
    1.5707963267948966192313216916397514420985846996876
//}

// @function The number pi*3/2
export Pi3Over2 () => //{
    4.71238898038468985769396507491925432629575409906266
//}

// @function The number pi/4
export PiOver4 () => //{
    0.78539816339744830961566084581987572104929234984378
//}

// @function The number sqrt(pi)
export SqrtPi () => //{
    1.7724538509055160272981674833411451827975494561224
//}

// @function The number sqrt(2pi)
export Sqrt2Pi () => //{
    2.5066282746310005024157652848110452530069867406099
//}

// @function The number sqrt(pi/2)
export SqrtPiOver2 () => //{
    1.2533141373155002512078826424055226265034933703050
//}

// @function The number sqrt(2*pi*e)
export Sqrt2PiE () => //{
    4.1327313541224929384693918842998526494455219169913
//}

// @function The number log(sqrt(2*pi))
export LogSqrt2Pi () => //{
    0.91893853320467274178032973640561763986139747363778
//}

// @function The number log(sqrt(2*pi*e))
export LogSqrt2PiE () => //{
    1.4189385332046727417803297364056176398613974736378
//}

// @function The number log(2 * sqrt(e / pi))
export LogTwoSqrtEOverPi () => //{
    0.6207822376352452223455184457816472122518527279025978
//}

// @function The number 1/pi
export InvPi () => //{
    0.31830988618379067153776752674502872406891929148091
//}

// @function The number 2/pi
export TwoInvPi () => //{
    0.63661977236758134307553505349005744813783858296182
//}

// @function The number 1/sqrt(pi)
export InvSqrtPi () => //{
    0.56418958354775628694807945156077258584405062932899
//}

// @function The number 1/sqrt(2pi)
export InvSqrt2Pi () => //{
    0.39894228040143267793994605993438186847585863116492
//}

// @function The number 2/sqrt(pi)
export TwoInvSqrtPi () => //{
    1.1283791670955125738961589031215451716881012586580
//}

// @function The number 2 * sqrt(e / pi)
export TwoSqrtEOverPi () => //{
    1.8603827342052657173362492472666631120594218414085755
//}

// @function The number (pi)/180 - factor to convert from Degree (deg) to Radians (rad).
export Degree () => //{
    0.017453292519943295769236907684886127134428718885417
//}

// @function The number (pi)/200 - factor to convert from NewGrad (grad) to Radians (rad).
export Grad () => //{
    0.015707963267948966192313216916397514420985846996876
//}

// @function The number ln(10)/20 - factor to convert from Power Decibel (dB) to Neper (Np). Use this version when the Decibel represent a power gain but the compared values are not powers (e.g. amplitude, current, voltage).
export PowerDecibel () => //{
    0.11512925464970228420089957273421821038005507443144
//}

// @function The number ln(10)/10 - factor to convert from Neutral Decibel (dB) to Neper (Np). Use this version when either both or neither of the Decibel and the compared values represent powers.
export NeutralDecibel () => //{
    0.23025850929940456840179914546843642076011014886288
//}

// @function The Catalan constant
// Sum(k=0 -> inf){ (-1)^k/(2*k + 1)2 }
export Catalan () => //{
    0.9159655941772190150546035149323841107741493742816721342664981196217630197762547694794
//}

// @function The Euler-Mascheroni constant
// lim(n -> inf){ Sum(k=1 -> n) { 1/k - log(n) } }
export EulerMascheroni () => //{
    0.5772156649015328606065120900824024310421593359399235988057672348849
//}

// @function The number (1+sqrt(5))/2, also known as the golden ratio
export GoldenRatio () => //{
    1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072
//}

// @function The Glaisher constant
// e^(1/12 - Zeta(-1))
export Glaisher () => //{
    1.2824271291006226368753425688697917277676889273250011920637400217404063088588264611297
//}

// @function The Khinchin constant
// prod(k=1 -> inf){1+1/(k*(k+2))^log(k,2)}
export Khinchin () => //{
    2.6854520010653064453097148354817956938203822939944629530511523455572188595371520028011
//}

//}

Overview

MathConstants includes e, pi, natural and base-10 logarithms of e, 2, 10 and pi, square roots of e, 2, 3 and pi, and related products and inverses such as 2*pi, pi/2, sqrt(2*pi) and 1/pi. It draws nothing and has no inputs, signals, entries or exits. Other scripts must import it and call the exported functions.

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