Indicator · Pine Script v4 · Own pane

Correlation with P-Value & Confidence Interval

by Anderson-ALGO· code credited to balipour

This indicator plots the Pearson correlation between the chart source and a user-chosen comparison symbol in a separate pane.

View on GitHub

GitHub stars9on the repository
Pine versionv4older, still runs
Lines of code126
Settings12inputs
LicenceNone statedfile: MPL-2.0
Last update5 years ago30 Nov 2020

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 indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

126 lines of Pine Script v4 from Pine/Pine Wizards/Balipur/Correlation with P-Value & Confidence Interval.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

Correlation with P-Value & Confidence Interval.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © balipour

//@version=4
study("Correlation with P-Value & Confidence Interval [pig]", "BA🐷 CC", false, format.price, 3)

var invisible = color(na)
bgcolor(#000000c0)

var cPI = 2.0 * asin(1.0) // 3.1415926536 Constant


//===== Functions =====//
cc(x, y, len) => // Correlation Coefficent function
    lenMinusOne = len - 1
    meanx = 0.0, meany = 0.0
    for i=0.0 to lenMinusOne
        meanx := meanx + nz(x[i])
        meany := meany + nz(y[i])
    meanx := meanx / len
    meany := meany / len
    sumxy=0.0, sumx=0.0, sumy=0.0
    for i=0  to lenMinusOne
        sumxy := sumxy +    (nz(x[i]) - meanx) * (nz(y[i]) - meany)
        sumx  := sumx  + pow(nz(x[i]) - meanx, 2)
        sumy  := sumy  + pow(nz(y[i]) - meany, 2)
    sumxy / sqrt(sumy * sumx)

adj(r, n) => // Unbiased Adjusted R Estimation Approximate Function
    (1 + (1 - pow(r, 2)) / (2 * n)) * r

Round(src, digits) => // Round Function
    p = pow(10, digits)
    round(abs(src) * p) / p * sign(src)

xp(offset) => // Offset 
    time + round(change(time) * offset)

_label(offset,P, T, s, color_PnL) => // Info Panel
    label PnL_Label = na
    label.delete(PnL_Label[1])
    PnL_Label := label.new(xp(offset), P, text=T, color=color_PnL, textcolor=color.white, style=s, yloc=yloc.price, xloc=xloc.bar_time, size=size.normal)

//===== Inputs =====//
src    = input(       close,  "========= Source =========", input.source )
sec1in = input(       "SPX",         "Comparison Security", input.symbol , confirm=true   )
mode   = input("Adjusted R",       "Correlation Variants ", input.string , options=["R", "Adjusted R"])
len    = input(          20, "Correlation Lookback Length", input.integer,  minval=2)

//Stats Settings
sc   = input(true, "Show Confidence Interval for Population", input.bool   )
csd  = input(1.96,       "Confidence Interval SD Multiplier", input.float  , minval=0.1, step=0.1)  //Default 95% 
sp   = input(true,                           "Show P-Values", input.bool   )
cp   = input(0.05,    "P-Value Significant Confidence Level", input.float  , minval=0.0, step=0.01) //Default = 1- 0.05 =  95%
pan  = input(true,                 " Show Information Panel", input.bool   )
rlen = input(  50,                      "  R Squared Length", input.integer, minval=2)
os   = input(  40,                 "  Panel Position Offset", input.integer, minval=0)
lT   = input(   1,                  "--- Line Thickness ---", input.integer, options=[1,2,3])

sec1 = security(sec1in, timeframe.period, close)


//===== Calculations =====//
R    =  cc(src, sec1, len) // Traditional Pearson
adjr = adj(  R,       len) // Adjusted R

float r = na
if(mode == "R")
    r := R
if(mode == "Adjusted R")
    r := adjr

R2    = pow(    cc(src, sec1, rlen)       , 2) // R Squared
adjR2 = pow(adj(cc(src, sec1, rlen), rlen), 2) // R Sqaured Based on Adjusted R

float r2 = na
if(mode == "R")
    r2 := R2
if(mode == "Adjusted R")
    r2 := adjR2

// Fisher Transform
z  = 0.5 * log((r + 1.0) / (1.0 - r)) // Fisher 
se = 1.0 / sqrt(len - 3)              // Standard Error
zl = z - csd * se                     // Lower Limit for fisher z
zu = z + csd * se                     // Upper Limit for fisher z 95% confidence

// Inverse Fisher Transform to Transform Back to r
rl = (exp(2.0 * zl) - 1.0) / (exp(2.0 * zl) + 1.0)   // Lower limit for r
ru = (exp(2.0 * zu) - 1.0) / (exp(2.0 * zu) + 1.0)   // Upper limit for r

// P Test
t      = sqrt((1.0 - pow(r    , 2)) / (len - 2))            // T value based on t distribution degrees of freedom 2 
Pvalue =  exp(-0.5 * pow(r / t, 2)) / (t * sqrt(2.0 * cPI)) // P Value
pro    = Pvalue > 1.0 ? 1.0 : Pvalue                        // Limit P value overshoot 1


//===== Plotting =====//
colorCC = (pro < cp and r < 0.0) ? #FF0000ff :
          (pro < cp and r > 0.0) ? #00FF00ff :
                                   #FFFFFFff
colorP  = (pro < cp) ? #FFFF00ff : #C0C0C040

plot(       sp ? pro : na, color=colorP, title="P Value", style=plot.style_columns)
plotUpper = plot(      rl, color=sc ? color.new(#00C0FFff,100) : invisible, style=plot.style_linebr, title="Confidence Interval Lower" )
plotLower = plot(      ru, color=sc ? color.new(#00C0FFff,100) : invisible, style=plot.style_linebr, title="Confidence Interval Higher")
fill(plotUpper, plotLower, color=sc ? color.new(#00C0FFff, 85) : invisible)
plot(  r,  linewidth=lT  , color=colorCC                                  , style=plot.style_linebr, title="🐷 Correlation")

plot(sp ? cp : na, color=color.new(#C0C0C0ff,30), trackprice=true, show_last=1, title="P value Threshold", style=plot.style_linebr)
plot(sp ? na :  0, color=color.new(#C0C0C0ff,30), trackprice=true, show_last=1, title="Zero Line")
hline(        1.0, color=color.new(#00FFFFff,30))
hline(       -1.0, color=color.new(#FF00FFff,30))

// Information Panel
sig() =>
    return = (pro < cp) and r > 0 ? "Significant Positive Correlation" :
             (pro < cp) and r < 0 ? "Significant Negative Correlation" :
                                           "Insignificant Correlation"

if(pan)
    txt =         "R : " + tostring(Round(r  ,3)) +
     "\n\n R Squared : " + tostring(Round(r2 ,4)) +
       "\n\n P Value : " + tostring(Round(pro,4)) +
                  "\n\n" + sig() 
    _label(os, 0, txt, label.style_label_center, color.new(#000000,45))

Overview

Correlation with P-Value & Confidence Interval offers raw R or an adjusted R estimate over a lookback length, then uses a Fisher transform and its inverse to draw a confidence band around that value. Optional p-values mark significance against a chosen level, and a label panel shows R-squared over a separate length. There are no trade entries or exits.

Details

Published by

Anderson-ALGO on GitHub

Code credit

balipour, as written in the script's header

Type

Indicator, Pine Script v4

Draws

Separate pane below the chart

Category

Screeners & dashboards

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.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
========= Source =========sourceclosesrc
Comparison SecuritysymbolSPXsec1in
Correlation Variants stringAdjusted Rmode
Correlation Lookback Lengthint20len
Show Confidence Interval for Populationbooltruesc
Confidence Interval SD Multiplierfloat1.96csd
Show P-Valuesbooltruesp
P-Value Significant Confidence Levelfloat0.05cp
Show Information Panelbooltruepan
  R Squared Lengthint50rlen
  Panel Position Offsetint40os
--- Line Thickness ---int1lT
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

948 stars

This indicator measures the chart symbol’s percentage change from the opening price at the start of each US presidential term and plots those values as histogram columns in a separate pane. Columns are offset so each administration appears separately, with a s

Indicator v6 Screeners & dashboards
Updated 10 days ago · GPL-3.0 GitHub repository of Presidents Performance

This indicator scans a pasted list of symbols for RSI directional momentum and shows matching long and short signals in a table in a separate pane. The user chooses continuous break, continuous retracement, flip break, flip retracement, divergence, or hidden d

Indicator v6 Oscillators & momentumDivergence
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup - RSI Directional Momentum Scanner

Tables-001

by ricardosantos79
261 stars

This indicator builds a TradingView table in a separate pane and fills its cells with fixed placeholder text according to whether each row and column index is even or odd. Users set the table position, the number of rows and columns, and the background, frame,

Indicator v4 Screeners & dashboards
Updated 4 years ago · None stated GitHub repository of Tables-001
257 stars

Donald SPX - Options Trading is an indicator designed to assist SPX index option traders by tracking session benchmarks and multi-timeframe moving average data. It calculates a 200-period weighted moving average (WMA) and requests multi-timeframe bar data from

Indicator v5 Moving averagesScreeners & dashboards Repaints
Updated 6 weeks ago · None stated GitHub repository of Donald SPX - Options Trading

Index Member Distribution

by TradersPost
218 stars

Calculates and plots a weighted histogram of constituent returns for major market indices (NDX, SPX, DJIA, and RUT) in a separate pane. Each histogram bin reflects the summed weights of members within that percentage return bracket. The script renders vertical

Indicator v6 Screeners & dashboards
Updated 3 months ago · None stated GitHub repository of Index Member Distribution

KamaFama

by Mastaaa1987
141 stars

KamaFama is an indicator that translates a Freqtrade trading strategy into Pine Script for visualization. It combines Kaufman Adaptive Moving Average (KAMA), MAMA/FAMA calculations derived via Hilbert Transform, Williams %R, and correlation trend filters to ev

Indicator v6 Moving averagesOscillators & momentum
Updated 6 weeks ago · MIT GitHub repository of KamaFama

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers