Sidechair

array.covariance()

Browse the reference

Returns the covariance of two equally long numeric arrays: how much their items move together.

Try it in the editor

A positive result means the two arrays tend to rise and fall together, a negative one means they move opposite ways. Pairs where either side is na are skipped.

By default it divides by the number of pairs; pass biased = false for the sample form with one less.

Syntax

Pine Script
array.covariance(id1: array<int/float>, id2: array<int/float>, biased: series bool (optional)) -> series float
array.covariance(id2: array<int/float>, biased: series bool (optional)) -> series float

Arguments

NameTypeDefaultDescription
id1array<int/float>requiredThe array that is read, or that receives the result.
id2array<int/float>requiredThe second numeric array. It must hold as many items as the first one.
biasedseries booltruetrue divides by the count (population); false divides by the count minus one (sample).

Returns

series float: The covariance of the paired items.

Remarks

  • The arrays must have the same size, or the script stops with an error that quotes both sizes.

Example

Pine ScriptPrice against volumeOpen in editor
//@version=6
indicator("array.covariance: price and volume", overlay = false)
var px = array.new_float()
var vx = array.new_float()
array.push(px, close)
array.push(vx, volume)
if array.size(px) > 20
    array.shift(px)
    array.shift(vx)
plot(array.covariance(px, vx), "Covariance")

See also

Runs on Sidechair