Library · Pine Script v5 · Library

MathTrigonometry

by ricardosantos79· code credited to RicardoSantos

This Pine Script v5 library exports trigonometric helpers and draws nothing on the chart.

View on GitHub

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

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

MathTrigonometry.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 Trigonometric methods.
library("MathTrigonometry")
// reference:
//      https://www.brighthubeducation.com/homework-math-help/72270-trigonometry-functions-a-trig-functions-list/
//      https://github.com/mathnet/mathnet-numerics/blob/a50d68d52def605a53d129cc60ea3371a2e97548/src/Numerics/Trigonometry.cs

// @function Normalized sinc function.
// @param value float, value.
// @returns float.
export sinc (float value) => //{
    float _z = math.pi * value
    if math.abs(_z) <= 1.0e-15 // almost equal
        1.0 
    else
        math.sin(_z) / _z
//}

//######################################

// @function Cotangent of value.
// @param value float, value.
// @returns float.
export cot (float value) => //{
    1.0 / math.tan(value)
//}

// @function Cosecant of value.
// @param value float, value.
// @returns float.
export csc (float value) => //{
    1.0 / math.sin(value)
//}

// @function Secant of value.
// @param value float, value.
// @returns float.
export sec (float value) => //{
    1.0 / math.cos(value)
//}

///#################

// @function Arc cotangent of value.
// @param value float, adjacent value.
// @returns float.
export acot (float value) => //{
    math.atan(1.0 / value)
//}

// @function Arc secant of value.
// @param value float, hypotenuse value.
// @returns float.
export asec (float value) => //{
    math.acos(1.0 / value)
//}

// @function Arc cosecant of value.
// @param value float, hipotenuse value.
// @returns float.
export acsc (float value) => //{
    math.atan(1.0 / value)
//}

//################

// @function Hyperbolic sine of angle.
// @param angle float, value.
// @returns float.
export sinh (float angle) => //{
    (math.exp(angle) - math.exp(-angle)) / 2.0
//}

// @function Hyperbolic cosine of angle.
// @param angle float, value.
// @returns float.
export cosh (float angle) => //{
    (math.exp(angle) + math.exp(-angle)) / 2.0
//}

// @function Hyperbolic tangent of angle.
// @param angle float, value.
// @returns float.
export tanh (float angle) => //{
    if angle > 19.1
        1.0
    else if angle -19.1
        -1.0
    else
        float _e1 = math.exp(angle)
        float _e2 = math.exp(-angle)
        (_e1 - _e2) / (_e1 + _e2)
//}

// @function Hyperbolic cotangent of angle.
// @param angle float, value.
// @returns float.
export coth (float angle) => //{
    if angle > 19.1
        1.0
    else if angle -19.1
        -1.0
    else
        float _e1 = math.exp(angle)
        float _e2 = math.exp(-angle)
        (_e1 + _e2) / (_e1 - _e2)
//}

// @function Hyperbolic secant of angle.
// @param angle float, value.
// @returns float.
export sech (float angle) => //{
    1.0 / cosh(angle)
//}

// @function Hyperbolic cosecant of angle.
// @param angle float, value.
// @returns float.
export csch (float angle) => //{
    1.0 / sinh(angle)
//}

//#############################

// @function Hyperbolic area sine.
// @param value float, value.
// @returns float.
export asinh (float value) => //{
    //https://github.com/mathnet/mathnet-numerics/blob/a50d68d52def605a53d129cc60ea3371a2e97548/src/Numerics/Trigonometry.cs#L711
    float _absv = math.abs(value)
    if _absv >= 268435456.0
        math.sign(value) * (math.log(_absv) + math.log(2.0))
    else 
        math.sign(value) * math.log(_absv + math.sqrt(1.0 + math.pow(value, 2.0)))
//}

// @function Hyperbolic area cosine.
// @param value float, value.
// @returns float.
export acosh (float value) => //{
    //https://github.com/mathnet/mathnet-numerics/blob/a50d68d52def605a53d129cc60ea3371a2e97548/src/Numerics/Trigonometry.cs#L737
    float _absv = math.abs(value)
    if _absv >= 268435456.0
        math.log(value) + math.log(2.0)
    else 
        math.log(value + (math.sqrt(value -1.0) * math.sqrt(value + 1.0)))
//}

// @function Hyperbolic area tangent.
// @param value float, value.
// @returns float.
export atanh (float value) => //{
    0.5 * math.log((value + 1.0) / (value - 1.0))
//}

// @function Hyperbolic area cotangent.
// @param value float, value.
// @returns float.
export acoth (float value) => //{
    0.5 * math.log((value + 1.0) / (value - 1.0))
//}

// @function Hyperbolic area secant.
// @param value float, value.
// @returns float.
export asech (float value) => //{
    acosh(1.0 / value)
//}

// @function Hyperbolic area cosecant.
// @param value float, value.
// @returns float.
export acsch (float value) => //{
    asinh(1.0 / value)
//}

Overview

MathTrigonometry provides a normalized sinc function plus cotangent, cosecant, and secant, with matching inverse functions arc cotangent, arc secant, and arc cosecant. Hyperbolic sine, cosine, tangent, cotangent, secant, and cosecant are included, along with hyperbolic area sine. Large-angle cases clamp hyperbolic tangent and cotangent, and large values use a log form for asinh. There are no inputs, plots, or trading signals.

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