Library · Pine Script v5 · Library

CorrelationExtendsStatistics

by ricardosantos79· code credited to RicardoSantos

This Pine Script v5 library exposes numerical helpers for correlation and special-function work and draws nothing on a chart.

View on GitHub

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

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

CorrelationExtendsStatistics.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 TODO: add library description here
library(title='CorrelationExtendsStatistics')

// reference:
//      https://people.sc.fsu.edu/~jburkardt/f_src/correlation/correlation.html
//      https://people.sc.fsu.edu/~jburkardt/f_src/correlation/correlation.f90

// @function real machine-dependent constants.
// @param _i int, input value (1 > _i > 5).
f_r8_mach (int _i) => //{
    if _i < 1
        0.0
    else if _i == 1
        4.450147717014403e-308
    else if _i == 2
        8.988465674311579e+307
    else if _i == 3
        1.110223024625157e-016
    else if _i == 4
        2.220446049250313e-016
    else if _i == 5
        0.301029995663981
    else if 5 < _i
        0.0
// reference:
//      https://people.sc.fsu.edu/~jburkardt/f_src/fn/fn.f90 line: 25905
//{
// !  Discussion:
// !
// !    R8_MACH can be used to obtain machine-dependent parameters
// !    for the local machine environment.  It is a function
// !    with one input argument, and can be called as follows:
// !
// !      D = R8_MACH ( I )
// !
// !    where I=1,...,5.  The output value of D above is
// !    determined by the input value of I:.
// !
// !    R8_MACH ( 1) = B^(EMIN-1), the smallest positive magnitude.
// !    R8_MACH ( 2) = B^EMAX*(1 - B^(-T)), the largest magnitude.
// !    R8_MACH ( 3) = B^(-T), the smallest relative spacing.
// !    R8_MACH ( 4) = B^(1-T), the largest relative spacing.
// !    R8_MACH ( 5) = LOG10(B)
//}//}

// @function initializes a Chebyshev series.
// @param DOS float array, the Chebyshev coefficients.
// @param NOS int, the number of coefficients.
// @param ETA float, the desired accuracy.
f_r8_inits (float[] dos, int nos, float eta) =>//{
    int _value = 0
    if nos < 1
        // number of coefficients < 1
        int(na)
    else if eta < array.get(id=dos, index=nos)
        // ETA may be too small
        _value := nos
    else
        float _err = 0.0
        for _i = 0 to 999999
            _value += 1
            _err += math.abs(array.get(id=dos, index=_i))
            if eta < _err
                break
        _value
// reference:
//      https://people.sc.fsu.edu/~jburkardt/f_src/fn/fn.f90 line:24371
//}

// @function evaluates a Chebyshev series.
// @param eval_point float, evaluation point, -1.1 < x < +1.1 .
// @param a float array, the Chebyshev coefficients.
// @param c_index int, the index of Chebyshev coefficient. 0 < c_index < 20
f_r8_csevl (float eval_point, float[] cseries, int c_index) => //{
    if c_index < 1
        // number of terms <= 0
        float(na)
    else if 20 < c_index
        // 20 < bumber of terms
        float(na)
    else if eval_point < -1.1 or 1.1 < eval_point
        // eval_point outside input range
        float(na)
    else
        float _twox = 2.0 * eval_point
        float _b2 = 0.0
        float _b1 = 0.0
        float _b0 = 0.0
        for _i = c_index to 1
            _b2 := _b1
            _b1 := _b0
            _b0 := _twox * _b1 - _b2 + array.get(id=cseries, index=_i)
        5.0 * (_b0 - _b2)
// usage:
// float[] tt = array.from(+0.00000000000000000000000000000000e+00, 
//      +0.10025416196893913701073127264074e+00,
//      -0.66522300776440513177678757831124e+00,
//      +0.24898370349828131370460468726680e+00,
//      -0.33252723170035769653884341503854e-01,
//      +0.23114179304694015462904924117729e-02,
//      -0.99112774199508092339048519336549e-04,
//      +0.28916708643998808884733903747078e-05,
//      -0.61210858663032635057818407481516e-07,
//      +0.98386507938567841324768748636415e-09,
//      -0.12423551597301765145515897006836e-10,
//      +0.12654336302559045797915827210363e-12,
//      -0.10619456495287244546914817512959e-14,
//      +0.74706210758024567437098915584000e-17,
//      -0.44697032274412780547627007999999e-19,
//      +0.23024281584337436200523093333333e-21,
//      -0.10319144794166698148522666666666e-23,
//      +0.40608178274873322700800000000000e-26,
//      -0.14143836005240913919999999999999e-28,
//      +0.43910905496698880000000000000000e-31 )
// plot(f_r8_csevl(1.1, tt, 15))
//
// reference:
//      https://people.sc.fsu.edu/~jburkardt/f_src/fn/fn.f90 line: 21643
//}

// @function evaluates the modulus and phase for the Bessel J0 and Y0 functions.
// @param x float, the argument. 4.0 > x > +inf
// @returns 
//      ampl
//      theta
f_r8_b0mp (float x) => //{
    //{
    float[] _bm0cs = array.from( 0.0, 
         +0.9211656246827742712573767730182e-01,
         -0.1050590997271905102480716371755e-02,
         +0.1470159840768759754056392850952e-04,
         -0.5058557606038554223347929327702e-06,
         +0.2787254538632444176630356137881e-07,
         -0.2062363611780914802618841018973e-08,
         +0.1870214313138879675138172596261e-09,
         -0.1969330971135636200241730777825e-10,
         +0.2325973793999275444012508818052e-11,
         -0.3009520344938250272851224734482e-12,
         +0.4194521333850669181471206768646e-13,
         -0.6219449312188445825973267429564e-14,
         +0.9718260411336068469601765885269e-15,
         -0.1588478585701075207366635966937e-15,
         +0.2700072193671308890086217324458e-16,
         -0.4750092365234008992477504786773e-17,
         +0.8615128162604370873191703746560e-18,
         -0.1605608686956144815745602703359e-18,
         +0.3066513987314482975188539801599e-19,
         -0.5987764223193956430696505617066e-20,
         +0.1192971253748248306489069841066e-20,
         -0.2420969142044805489484682581333e-21,
         +0.4996751760510616453371002879999e-22,
         -0.1047493639351158510095040511999e-22,
         +0.2227786843797468101048183466666e-23,
         -0.4801813239398162862370542933333e-24,
         +0.1047962723470959956476996266666e-24,
         -0.2313858165678615325101260800000e-25,
         +0.5164823088462674211635199999999e-26,
         -0.1164691191850065389525401599999e-26,
         +0.2651788486043319282958336000000e-27,
         -0.6092559503825728497691306666666e-28,
         +0.1411804686144259308038826666666e-28,
         -0.3298094961231737245750613333333e-29,
         +0.7763931143074065031714133333333e-30,
         -0.1841031343661458478421333333333e-30,
         +0.4395880138594310737100799999999e-31)
    
    float[] _bth0cs = array.from( 0.0, 
         -0.24901780862128936717709793789967e+00,
         +0.48550299609623749241048615535485e-03,
         -0.54511837345017204950656273563505e-05,
         +0.13558673059405964054377445929903e-06,
         -0.55691398902227626227583218414920e-08,
         +0.32609031824994335304004205719468e-09,
         -0.24918807862461341125237903877993e-10,
         +0.23449377420882520554352413564891e-11,
         -0.26096534444310387762177574766136e-12,
         +0.33353140420097395105869955014923e-13,
         -0.47890000440572684646750770557409e-14,
         +0.75956178436192215972642568545248e-15,
         -0.13131556016891440382773397487633e-15,
         +0.24483618345240857495426820738355e-16,
         -0.48805729810618777683256761918331e-17,
         +0.10327285029786316149223756361204e-17,
         -0.23057633815057217157004744527025e-18,
         +0.54044443001892693993017108483765e-19,
         -0.13240695194366572724155032882385e-19,
         +0.33780795621371970203424792124722e-20,
         -0.89457629157111779003026926292299e-21,
         +0.24519906889219317090899908651405e-21,
         -0.69388422876866318680139933157657e-22,
         +0.20228278714890138392946303337791e-22,
         -0.60628500002335483105794195371764e-23,
         +0.18649748964037635381823788396270e-23,
         -0.58783732384849894560245036530867e-24,
         +0.18958591447999563485531179503513e-24,
         -0.62481979372258858959291620728565e-25,
         +0.21017901684551024686638633529074e-25,
         -0.72084300935209253690813933992446e-26,
         +0.25181363892474240867156405976746e-26,
         -0.89518042258785778806143945953643e-27,
         +0.32357237479762298533256235868587e-27,
         -0.11883010519855353657047144113796e-27,
         +0.44306286907358104820579231941731e-28,
         -0.16761009648834829495792010135681e-28,
         +0.64292946921207466972532393966088e-29,
         -0.24992261166978652421207213682763e-29,
         +0.98399794299521955672828260355318e-30,
         -0.39220375242408016397989131626158e-30,
         +0.15818107030056522138590618845692e-30,
         -0.64525506144890715944344098365426e-31,
         +0.26611111369199356137177018346367e-31)
    float[] _bm02cs = array.from( 0.0, 
         +0.9500415145228381369330861335560e-01,
         -0.3801864682365670991748081566851e-03,
         +0.2258339301031481192951829927224e-05,
         -0.3895725802372228764730621412605e-07,
         +0.1246886416512081697930990529725e-08,
         -0.6065949022102503779803835058387e-10,
         +0.4008461651421746991015275971045e-11,
         -0.3350998183398094218467298794574e-12,
         +0.3377119716517417367063264341996e-13,
         -0.3964585901635012700569356295823e-14,
         +0.5286111503883857217387939744735e-15,
         -0.7852519083450852313654640243493e-16,
         +0.1280300573386682201011634073449e-16,
         -0.2263996296391429776287099244884e-17,
         +0.4300496929656790388646410290477e-18,
         -0.8705749805132587079747535451455e-19,
         +0.1865862713962095141181442772050e-19,
         -0.4210482486093065457345086972301e-20,
         +0.9956676964228400991581627417842e-21,
         -0.2457357442805313359605921478547e-21,
         +0.6307692160762031568087353707059e-22,
         -0.1678773691440740142693331172388e-22,
         +0.4620259064673904433770878136087e-23,
         -0.1311782266860308732237693402496e-23,
         +0.3834087564116302827747922440276e-24,
         -0.1151459324077741271072613293576e-24,
         +0.3547210007523338523076971345213e-25,
         -0.1119218385815004646264355942176e-25,
         +0.3611879427629837831698404994257e-26,
         -0.1190687765913333150092641762463e-26,
         +0.4005094059403968131802476449536e-27,
         -0.1373169422452212390595193916017e-27,
         +0.4794199088742531585996491526437e-28,
         -0.1702965627624109584006994476452e-28,
         +0.6149512428936330071503575161324e-29,
         -0.2255766896581828349944300237242e-29,
         +0.8399707509294299486061658353200e-30,
         -0.3172997595562602355567423936152e-30,
         +0.1215205298881298554583333026514e-30,
         -0.4715852749754438693013210568045e-31)
    float[] _bt02cs = array.from( 0.0, 
         -0.24548295213424597462050467249324e+00,
         +0.12544121039084615780785331778299e-02,
         -0.31253950414871522854973446709571e-04,
         +0.14709778249940831164453426969314e-05,
         -0.99543488937950033643468850351158e-07,
         +0.85493166733203041247578711397751e-08,
         -0.86989759526554334557985512179192e-09,
         +0.10052099533559791084540101082153e-09,
         -0.12828230601708892903483623685544e-10,
         +0.17731700781805131705655750451023e-11,
         -0.26174574569485577488636284180925e-12,
         +0.40828351389972059621966481221103e-13,
         -0.66751668239742720054606749554261e-14,
         +0.11365761393071629448392469549951e-14,
         -0.20051189620647160250559266412117e-15,
         +0.36497978794766269635720591464106e-16,
         -0.68309637564582303169355843788800e-17,
         +0.13107583145670756620057104267946e-17,
         -0.25723363101850607778757130649599e-18,
         +0.51521657441863959925267780949333e-19,
         -0.10513017563758802637940741461333e-19,
         +0.21820381991194813847301084501333e-20,
         -0.46004701210362160577225905493333e-21,
         +0.98407006925466818520953651199999e-22,
         -0.21334038035728375844735986346666e-22,
         +0.46831036423973365296066286933333e-23,
         -0.10400213691985747236513382399999e-23,
         +0.23349105677301510051777740800000e-24,
         -0.52956825323318615788049749333333e-25,
         +0.12126341952959756829196287999999e-25,
         -0.28018897082289428760275626666666e-26,
         +0.65292678987012873342593706666666e-27,
         -0.15337980061873346427835733333333e-27,
         +0.36305884306364536682359466666666e-28,
         -0.86560755713629122479172266666666e-29,
         +0.20779909972536284571238399999999e-29,
         -0.50211170221417221674325333333333e-30,
         +0.12208360279441714184191999999999e-30,
         -0.29860056267039913454250666666666e-31)
    //}
    
    float _pi4 = 0.785398163397448309615660845819876

    float _eta = 0.1 * f_r8_mach(3)
    int _nbm0 = f_r8_inits(_bm0cs, 37, _eta)
    int _nbt02 = f_r8_inits(_bt02cs, 39, _eta)
    int _nbm02 = f_r8_inits(_bm02cs, 40, _eta)
    int _nbth0 = f_r8_inits(_bth0cs, 44, _eta)
    float _xmax = 1.0 / f_r8_mach(4)

    if x < 4.0
        // x under limit
        [float(na), float(na)]
    else
        if x <= 8.0
            float _z = (128.0 / x / x - 5.0) / 3.0
            _ampl = (0.75 + f_r8_csevl(_z, _bm0cs, _nbm0)) / math.sqrt(x)
            _theta = x - _pi4 + f_r8_csevl(_z, _bt02cs, _nbt02) / x
            [_ampl, _theta]
        else
            float _z = 128.0 / x / x - 1.0
            _ampl = (0.75 + f_r8_csevl(_z, _bm02cs, _nbm02)) / math.sqrt(x)
            _theta = x - _pi4 + f_r8_csevl(_z, _bth0cs, _nbth0) / x
            [_ampl, _theta]
// usage:
// [aa, bb] = f_r8_b0mp(math.pi*2)
// plot(aa), plot(bb)
//}


// @function a
f_r8_besj0 (float _x) => //{
    //{
    float[] _bj0cs = array.from(
         +0.00000000000000000000000000000000e+00, 
         +0.10025416196893913701073127264074e+00,
         -0.66522300776440513177678757831124e+00,
         +0.24898370349828131370460468726680e+00,
         -0.33252723170035769653884341503854e-01,
         +0.23114179304694015462904924117729e-02,
         -0.99112774199508092339048519336549e-04,
         +0.28916708643998808884733903747078e-05,
         -0.61210858663032635057818407481516e-07,
         +0.98386507938567841324768748636415e-09,
         -0.12423551597301765145515897006836e-10,
         +0.12654336302559045797915827210363e-12,
         -0.10619456495287244546914817512959e-14,
         +0.74706210758024567437098915584000e-17,
         -0.44697032274412780547627007999999e-19,
         +0.23024281584337436200523093333333e-21,
         -0.10319144794166698148522666666666e-23,
         +0.40608178274873322700800000000000e-26,
         -0.14143836005240913919999999999999e-28,
         +0.43910905496698880000000000000000e-31 )
    //}
    int _ntj0 = 0
    _xsml = 0.0
    if ( _ntj0 == 0 )
        _ntj0 := f_r8_inits(_bj0cs, 19, 0.1 * f_r8_mach(3))
        _xsml := math.sqrt(4.0 * f_r8_mach(3))
    
    _y = math.abs(_x)
    if _y <= _xsml
        1.0
    else if _y <= 4.0
        f_r8_csevl(0.125 * _y * _y - 1.0, _bj0cs, _ntj0)
    else
        [_ampl, _theta] = f_r8_b0mp(_y)
        _ampl * math.cos(_theta)
// usage:
// plot(f_r8_besj0(0.2))
// reference:
//      https://people.sc.fsu.edu/~jburkardt/f_src/fn/fn.f90 line: 17498
//}

// @function Evaluates the Bessel J. correlation method.
// @param sample_a float array, sample data.
// @param correlation_length int, correlation length.
// @returns float array with correlations.
export correlation_bessel_j(float[] sample_a, int correlation_length) => //{
    int _size_a = array.size(id=sample_a)
    if _size_a > 0
        float[] _corr = array.new_float(size=_size_a)
        for _i = 0 to _size_a-1
            float _y = math.abs(array.get(id=sample_a, index=_i)) / correlation_length
            array.set(id=_corr, index=_i, value=f_r8_besj0(_y))
        _corr
if barstate.islast
    _arr = array.from(0.2, 0.5, 0.0, 0.23, 0.56, 0.70, 0.3, 0.8, 0.3, 0.9)
    _t = correlation_bessel_j(_arr, 2)
    label.new(bar_index, 0.0, str.format('{0}', _t))
    
//}

Overview

CorrelationExtendsStatistics returns machine-dependent floating-point constants, truncates a Chebyshev coefficient array to a requested accuracy, and evaluates a Chebyshev series at a point inside a narrow valid range. There are no inputs, signals, entries, exits, or position rules. The library header still lacks a description, and the published excerpt ends mid-example.

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