Covariance ( int length);

Default values:

length: 10


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

General Information

Covariance defines whether two variables have the same trend or not. If the covariance is positive, then the two values move in the same direction, if negative the two values move inversely. The covariance formula is provided in the following example.

Input parameters

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


script covarianceTS {
input data1 = close;
input data2 = close;
input length = 12;
plot CovarianceTS = Average(data1 * data2, length) - Average(data1, length) * Average(data2, length);

declare lower;
input length = 10;
input secondSymbol = "SPX";
plot Covariance1 = Covariance(close, close(secondSymbol), length);
plot Covariance2 = CovarianceTS(close, close(secondSymbol), length);

The code draws two plots that show the covariance for the close price of the current and the specified symbol on the defined period. The Covariance1 plot is based on the built-in function, the Covariance2 plot is based on its thinkScript® implementation. These two plots coincide with each other forming a single plot. For more information about the Average function, see the Average function in the Technical Analysis section.