Library · Pine Script v5 · Library

MathComplexCore

by ricardosantos79· code credited to RicardoSantos

This library provides core functions to handle complex numbers in Pine Script v5.

View on GitHub

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

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

MathComplexCore.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 Core functions to handle complex numbers.
library(title='MathComplexCore')

// references:
//      https://en.wikipedia.org/wiki/Complex_number
//      js: https://rosettacode.org/wiki/Fast_Fourier_transform
//      https://github.com/trekhleb/javascript-algorithms/blob/477f30b0bdac6024874d2976de1eca7afe0176dd/src/algorithms/math/complex-number/ComplexNumber.js#L3
//      https://github.com/infusion/Complex.js/
//      https://github.com/mathnet/mathnet-numerics/blob/a50d68d52def605a53d129cc60ea3371a2e97548/src/Numerics/Complex32.cs

// imports:
import RicardoSantos/DebugConsole/1 as console
// console.init(), console.queue(), console.update()
[__T, __C] = console.init(20) //initiate console configuration variables.
//

// @function Set the real part of complex_number.
// @param complex_number float array, pseudo complex number in the form of a array [real, imaginary].
// @param real float, value to replace real value of complex_number.
// @returns Void, Modifies complex_number.
export set_real (float[] complex_number, float real) => //{
    array.set(id=complex_number, index=0, value=real)
//}

// @function Set the imaginary part of complex_number.
// @param complex_number float array, pseudo complex number in the form of a array [real, imaginary].
// @param imaginary float, value to replace imaginary value of complex_number.
// @returns Void, Modifies complex_number.
export set_imaginary (float[] complex_number, float imaginary=0.0) => //{
    array.set(id=complex_number, index=1, value=imaginary)
//}

// @function Creates a prototype array to handle complex numbers.
// @param real float, real value of the complex number. default=0.
// @param imaginary float, imaginary number of the complex number. default=0.
// @return float array, pseudo complex number in the form of a array [real, imaginary].
export new (float real=0.0, float imaginary=0.0) => //{
    array.from(real, imaginary)
//}

// @function complex number "0+0i".
// @return float array, pseudo complex number in the form of a array [real, imaginary].
export zero () => //{
    array.new_float(2, 0.0)
//}

// @function complex number "1+0i".
// @return float array, pseudo complex number in the form of a array [real, imaginary].
export one () => //{
    array.from(1.0, 0.0)
//}

// @function complex number "0+1i".
// @return float array, pseudo complex number in the form of a array [real, imaginary].
export imaginary_one () => //{
    array.from(0.0, 1.0)
//}

// @function complex number "0+1i".
// @return float array, pseudo complex number in the form of a array [real, imaginary].
export nan () => //{
    array.new_float(2, na)
//}

// @function Create a complex number from a point's polar coordinates.
// @param magnitude float, default=0.0, The magnitude, which is the distance from the origin (the intersection of the x-axis and the y-axis) to the number.
// @param phase float, default=0.0, The phase, which is the angle from the line to the horizontal axis, measured in radians.
// @return float array, pseudo complex number in the form of a array [real, imaginary].
export from_polar_coordinates (float magnitude=0.0, float phase=0.0) => //{
    array.from(magnitude * math.cos(phase), magnitude * math.sin(phase))
//}

// @function Get the real part of complex_number.
// @param complex_number pseudo complex number in the form of a array [real, imaginary].
// @returns float, Real part of the complex_number.
export get_real (float[] complex_number) => //{
    array.get(id=complex_number, index=0)
//}

// @function Get the imaginary part of complex_number.
// @param complex_number pseudo complex number in the form of a array [real, imaginary].
// @returns float, Imaginary part of the complex number.
export get_imaginary (float[] complex_number) => //{
    array.get(id=complex_number, index=1)
//}

// @function Checks that its a valid complex_number.
// @param complex_number pseudo complex number in the form of a array [real, imaginary].
// @returns bool.
export is_complex (float[] complex_number) => //{
    array.size(complex_number) == 2
//}

// @function Checks that its empty "na" complex_number.
// @param complex_number pseudo complex number in the form of a array [real, imaginary].
// @returns bool.
export is_nan (float[] complex_number) => //{
    if not is_complex(complex_number)
        runtime.error('MathComplex -> is_nan(): "complex_number" has the wrong size.')
        bool(na)
    else
        na(get_real(complex_number)) or na(get_imaginary(complex_number))
//}

// @function Checks that the complex_number is real.
// @param complex_number pseudo complex number in the form of a array [real, imaginary].
// @returns bool.
export is_real (float[] complex_number) => //{
    if not is_complex(complex_number)
        runtime.error('MathComplex -> is_real(): "complex_number" has the wrong size.')
        bool(na)
    else
        get_imaginary(complex_number) == 0.0
//}

// @function Checks that the complex_number is real and not negative.
// @param complex_number pseudo complex number in the form of a array [real, imaginary].
// @returns bool.
export is_real_non_negative (float[] complex_number) => //{
    if not is_complex(complex_number)
        runtime.error('MathComplex -> is_real_non_negative(): "complex_number" has the wrong size.')
        bool(na)
    else
        get_real(complex_number) >= 0.0 and get_imaginary(complex_number) == 0
//}

// @function Checks that the complex_number is zero.
// @param complex_number pseudo complex number in the form of a array [real, imaginary].
// @returns bool.
export is_zero (float[] complex_number) => //{
    if not is_complex(complex_number)
        runtime.error('MathComplex -> is_real(): "complex_number" has the wrong size.')
        bool(na)
    else
        (get_real(complex_number) + get_imaginary(complex_number)) == 0.0
//}

// @function Compares two complex numbers: 
// @param complex_number_a float array, pseudo complex number in the form of a array [real, imaginary].
// @param complex_number_b float array, pseudo complex number in the form of a array [real, imaginary].
// @returns boolean value representing the equality.
export equals (float[] complex_number_a, float[] complex_number_b) => //{
    float _epsilon = 1e-15
    math.abs(get_real(complex_number_b) - get_real(complex_number_a)) <= _epsilon and math.abs(get_imaginary(complex_number_b) - get_imaginary(complex_number_a)) <= _epsilon
//}

// @function Converts complex_number to a string format, in the form: "a+bi" 
// @param complex pseudo complex number in the form of a array [real, imaginary].
// @param format string, formating to apply.
// @returns a string in "a+bi" format
export to_string (float[] complex, string format='#.##########') => //{
    if not is_complex(complex)
        runtime.error('MathComplex -> to_string(): "complex" has the wrong size.')
        string(na)
    else
        float _re = get_real(complex)
        float _im = get_imaginary(complex)
        string _str = ''
        if na(_re)
            _str := str.tostring(_im, format) + 'i'
        else if _im < 0
            _str := str.tostring(_re, format) + '' + str.tostring(_im, format) + 'i'
        else
            _str := str.tostring(_re, format) + '+' + str.tostring(_im, format) + 'i'
        _str
//{ usage:
// console.queue_one(__C, str.format('to_string conversion of complex number "5.2+3.0i": {0}', to_string(new(5.2, 3.0), '#.##'))) // 
//}

if barstate.islastconfirmedhistory
    float _i0 = 1.0
    float _i1 = 2.0
    float _i2 = 3.0
    float _i3 = 4.0
    A = new(0.0, 0.0)
    B = one()
    console.queue_one(__C, str.format('input parameters: {0}, {1}, {2}, {3}', _i0, _i1, _i2, _i3))
    console.queue_one(__C, str.format('##   Create      ##', ''))
    console.queue_one(__C, str.format('new complex number A: {0}', to_string(A, '#.##')))
    console.queue_one(__C, str.format('new complex number B: {0}', to_string(A, '#.##')))
    console.queue_one(__C, str.format('new complex number zero(): {0}', to_string(zero(), '#.##')))
    console.queue_one(__C, str.format('new complex number one(): {0}', to_string(one(), '#.##')))
    console.queue_one(__C, str.format('new complex number imaginary_one(): {0}', to_string(imaginary_one(), '#.##')))
    console.queue_one(__C, str.format('new complex number nan(): {0}', to_string(nan(), '#.##')))
    console.queue_one(__C, str.format('new complex number from_polar_coordinates({0}, {1}): {2}', _i2, _i3, to_string(from_polar_coordinates(_i2, _i3), '#.##')))
    console.queue_one(__C, str.format('##   Access      ##', ''))
    set_real(A, _i0), set_imaginary(A, _i1)
    console.queue_one(__C, str.format('set real and imaginary parts of A with inputs({0}, {1}): {2}', _i0, _i1, to_string(A, '#.##')))
    console.queue_one(__C, str.format('set real and imaginary parts of B with inputs({0}, {1}): {2}', _i2, _i3, to_string(B, '#.##')))
    console.queue_one(__C, str.format('get real and imaginary parts of B:({0}, {1})', get_real(B), get_imaginary(B)))
    console.queue_one(__C, str.format('##   Test        ##', ''))
    console.queue_one(__C, str.format('Test: is A complex number: {0}', is_complex(A)))
    console.queue_one(__C, str.format('Test: is A NaN: {0}', is_nan(A)))
    console.queue_one(__C, str.format('Test: is A real: {0}', is_real(A)))
    console.queue_one(__C, str.format('Test: is A real non negative: {0}', is_real_non_negative(A)))
    console.queue_one(__C, str.format('Test: is A zero: {0}', is_zero(A)))


// update console
console.update(__T, __C)

Overview

Complex numbers are represented as float arrays containing real and imaginary parts. Functions set the real or imaginary component of a number, create new complex numbers from given values, and supply constants for zero, one, imaginary one, and NaN. A function converts from polar coordinates using magnitude and phase. It includes in-place modification and array creation utilities. This utility library draws nothing on charts and contains no trading signals, entries, exits, stop loss, or take profit rules. It serves as a math helper for other scripts without user inputs or settings.

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