Chatterjee Correlation
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HeWhoMustNotBeNamed

//   __    __            __       __  __                  __       __                        __      __    __              __      _______             __    __                                          __ 
//  /  |  /  |          /  |  _  /  |/  |                /  \     /  |                      /  |    /  \  /  |            /  |    /       \           /  \  /  |                                        /  |
//  $$ |  $$ |  ______  $$ | / \ $$ |$$ |____    ______  $$  \   /$$ | __    __   _______  _$$ |_   $$  \ $$ |  ______   _$$ |_   $$$$$$$  |  ______  $$  \ $$ |  ______   _____  ____    ______    ____$$ |
//  $$ |__$$ | /      \ $$ |/$  \$$ |$$      \  /      \ $$$  \ /$$$ |/  |  /  | /       |/ $$   |  $$$  \$$ | /      \ / $$   |  $$ |__$$ | /      \ $$$  \$$ | /      \ /     \/    \  /      \  /    $$ |
//  $$    $$ |/$$$$$$  |$$ /$$$  $$ |$$$$$$$  |/$$$$$$  |$$$$  /$$$$ |$$ |  $$ |/$$$$$$$/ $$$$$$/   $$$$  $$ |/$$$$$$  |$$$$$$/   $$    $$< /$$$$$$  |$$$$  $$ | $$$$$$  |$$$$$$ $$$$  |/$$$$$$  |/$$$$$$$ |
//  $$$$$$$$ |$$    $$ |$$ $$/$$ $$ |$$ |  $$ |$$ |  $$ |$$ $$ $$/$$ |$$ |  $$ |$$      \   $$ | __ $$ $$ $$ |$$ |  $$ |  $$ | __ $$$$$$$  |$$    $$ |$$ $$ $$ | /    $$ |$$ | $$ | $$ |$$    $$ |$$ |  $$ |
//  $$ |  $$ |$$$$$$$$/ $$$$/  $$$$ |$$ |  $$ |$$ \__$$ |$$ |$$$/ $$ |$$ \__$$ | $$$$$$  |  $$ |/  |$$ |$$$$ |$$ \__$$ |  $$ |/  |$$ |__$$ |$$$$$$$$/ $$ |$$$$ |/$$$$$$$ |$$ | $$ | $$ |$$$$$$$$/ $$ \__$$ |
//  $$ |  $$ |$$       |$$$/    $$$ |$$ |  $$ |$$    $$/ $$ | $/  $$ |$$    $$/ /     $$/   $$  $$/ $$ | $$$ |$$    $$/   $$  $$/ $$    $$/ $$       |$$ | $$$ |$$    $$ |$$ | $$ | $$ |$$       |$$    $$ |
//  $$/   $$/  $$$$$$$/ $$/      $$/ $$/   $$/  $$$$$$/  $$/      $$/  $$$$$$/  $$$$$$$/     $$$$/  $$/   $$/  $$$$$$/     $$$$/  $$$$$$$/   $$$$$$$/ $$/   $$/  $$$$$$$/ $$/  $$/  $$/  $$$$$$$/  $$$$$$$/ 
//                                                                                                                                                                                                          
//                                                                                                                                                                                                          
//
//@version=5
indicator("Chatterjee Correlation", precision=4)
import HeWhoMustNotBeNamed/BinaryInsertionSort/1 as bis

source = input.string('volume', 'Source', options = ['volume', 'volatility'])
loopback = input.int(100, 'Loopback', minval=10, step=20)
sampleSize = input.int(20000, "Sample Size", minval=5000, maxval = 30000, step=5000)
plotSize = input.int(500, "Plot Last X bars", minval = 100, maxval=5000, step=100)


averageVolume = ta.sma(volume, loopback)
atr = ta.atr(loopback)
float x = source == 'volume'? averageVolume : atr
float y = close - ta.sma(close, loopback)

float correlation = na
float correlationAnton = na

if(bar_index >= last_bar_index-sampleSize) 
    [xArray, xSortedArray, xSortIndices] = bis.get_sorted_arrays(x)
    [yArray, ySortedArray, ySortIndices] = bis.get_sorted_arrays(y)
    coefficient = 0.0
    n = array.size(xArray)

    if(bar_index >= last_bar_index-plotSize)
        index = array.get(xSortIndices, 0)
        for i=1 to n > 1? n -1 : na
            indexNext = array.get(xSortIndices, i)
            coefficient := coefficient + math.abs(array.get(ySortIndices, indexNext)-array.get(ySortIndices, index))
            index := indexNext

        correlation := 1 - 3*coefficient/(math.pow(n,2)-1)

plot(correlation, "Chatterjee Correlation", color=color.blue)
