Introduction to array.covariance
The array.covariance
function in Pine Script is used to calculate the covariance between two arrays. Covariance is a measure of the relationship between two variables. A positive covariance means that the two variables tend to move in the same direction, while a negative covariance means that they tend to move in opposite directions.
The syntax for the array.covariance
function is as follows:
array.covariance(array1, array2) where: array1 and array2 are the two arrays for which we want to calculate the covariance
The array.covariance
function returns a single value, which is the covariance between the two arrays.
Example: Calculating Covariance between Two Pairs
In this example, we will use the **array.covariance
**function to calculate the covariance between two currency pairs “NZDJPY” and “USDJPY”. We will then use this information to make a trading decision.
//@version=5 indicator(title='USDJPY and NZDJPY Covariance', overlay=false) // Define the two arrays and initialize them with empty values usdjpy_array = array.new_float() nzdjpy_array = array.new_float() // Define the two currency pairs usdjpy = request.security('FX:USDJPY', timeframe.period, close) nzdjpy = request.security('FX:NZDJPY', timeframe.period, close) // Append the currency pair values to the arrays using a for loop for i = 0 to 99 by 1 array.push(usdjpy_array, usdjpy[i]) array.push(nzdjpy_array, nzdjpy[i]) // Calculate the covariance between the two arrays cov = array.covariance(usdjpy_array, nzdjpy_array) // Plot the covariance plot(cov, color=color.new(color.red, 0), linewidth=2) l = label.new(bar_index, cov, text=str.tostring(cov), size=size.huge, textcolor=color.white) label.delete(l[1])
Here’s a breakdown of each section of the code:
- The
//@version=5
directive specifies that the script is written in Pine Script version 5. - The
indicator
function is used to define the indicator’s title and overlay settings. - The
array.new_float()
function is used to define the two empty arraysusdjpy_array
andnzdjpy_array
. - The
request.security
function is used to get the closing prices of the USDJPY and NZDJPY pairs. TheFX:
prefix is used to indicate that these are forex pairs. - A
for
loop is used to append the USDJPY and NZDJPY pair values to their respective arrays. The loop runs 100 times, which is the length of the arrays we want to use for the covariance calculation. - The
array.covariance()
function is used to calculate the covariance between the two arrays. - The
plot
function is used to plot the covariance. Thecolor.new()
function is used to make the line transparent. This is done to make it easier to see the labels below the line. - The
label.new
function is used to create a label that displays the value of the covariance. Thesize.huge
parameter is used to make the label large and easy to read. Thetextcolor
parameter is used to set the text color to white. - The
label.delete
function is used to delete the previous label to prevent it from stacking up on top of the current label.
This code can be used to gain insights into the relationship between the USDJPY and NZDJPY pairs and make informed trading decisions based on the results.
Key Takeaways
- The
array.covariance
function in Pine Script is used to calculate the covariance between two arrays. - Covariance is a measure of the relationship between two variables. A positive covariance means that the two variables tend to move in the same direction, while a negative covariance means that they tend to move in opposite directions.
- The
array.covariance
function takes two arrays and a length as inputs, and returns a single value, which is the covariance between the two arrays. array.covariance
function can be used in a variety of applications, such as trading strategies or data analysis, to gain insights into the relationship between two variables.
Conclusion
In this tutorial, we have learned about the array.covariance
function in Pine Script. We have seen how to use it to calculate the covariance between two arrays and how to make trading decisions based on the results. We have also seen two different examples of how to use the function in practice.
Covariance is a useful measure of the relationship between two variables, and the array.covariance
function is a powerful tool for calculating it in Pine Script. Whether you are a trader or a data analyst, understanding covariance can help you make better decisions and gain valuable insights into your data.