Correlation ( IDataHolder data1, IDataHolder data2, int length);

Default values:

length: 10


Returns the correlation coefficient between the data1 and data2 variables for the last length bars.

General Information

Correlation defines the relation between two variables. See the following example to learn how the coefficient is calculated.

Input parameters

Parameter Default value Description
data1 - Defines the first of the two variables for which the correlation is calculated.
data2 - Defines the second of the two variables for which the correlation is calculated.
length 10 Defines the period on which the correlation is calculated.


script correlationTS {
input data1 = close;
input data2 = close;
input length = 12;
plot CorrelationTS = Covariance(data1, data2, length) / ( StDev(data1, length) * StDev(data2, length) );

declare lower;
input length = 10;
input secondSymbol = "SPX";
plot Correlation1 = Correlation(close, close(secondSymbol), length);
plot Correlation2 = CorrelationTS(close, close(secondSymbol), length);

The example draws two plots that show the correlation for the close price of the current and the specified symbol on the defined period. The first plot is drawn based on the built-in Correlation function, the second plot is based on its thinkScript® implementation. Both plots are equal on the entire interval. For more information about the covariance function, see the Covariance function in this section.