I need help with translating pine correlation function to python, I've already translated stdev and swma functions, but this one is a bit confusing for me.
I've also found this explanation but didn't quite understand how to implement it:
in python try using
pandas
with.rolling(window).corr
wherewindow
is the correlation coefficient period,pandas
allow you to compute any rolling statistic by usingrolling()
. The correlation coefficient from pine is calculated with :sma(y*x,length) - sma(y,length)*sma(x,length)
divided bystdev(length*y,length)*stdev(length*x,length)
wherestdev
is based on the naïve algorithm.
Pine documentation for this func:
> Correlation coefficient. Describes the degree to which two series tend
> to deviate from their sma values. correlation(source_a, source_b,
> length) → series[float] RETURNS Correlation coefficient.
ARGUMENTS
source_a
(series) Source series.
source_b
(series) Target series.
length
(integer) Length (number of bars back).
Using pandas is indeed the best option, TA-Lib also has a CORREL
function. In order for you to get a better idea of how the correlation
function in pine is implemented here is a python code making use of numpy, note that this is not an efficient solution.
import numpy as np
from matplotlib import pyplot as plt
def sma(src,m):
coef = np.ones(m)/m
return np.convolve(src,coef,mode="valid")
def stdev(src,m):
a = sma(src*src,m)
b = np.power(sma(src,m),2)
return np.sqrt(a-b)
def correlation(x,y,m):
cov = sma(x*y,m) - sma(x,m)*sma(y,m)
den = stdev(x,m)*stdev(y,m)
return cov/den
ts = np.random.normal(size=500).cumsum()
n = np.linspace(0,1,len(ts))
cor = correlation(ts,n,14)
plt.subplot(2,1,1)
plt.plot(ts)
plt.subplot(2,1,2)
plt.plot(cor)
plt.show()