TradingView
ZenAndTheArtOfTrading
3. Mrz. 2021 00:40

Correlation Meter 

Apple Inc.NASDAQ

Beschreibung

This script calculates the covariance and correlation coefficient between two markets using arrays.

Lookback: How many bars to perform the calculation on.
Source: Price source to calculate the correlation on.
Reference Market: The reference market to compare to the current market.

It's a simple indicator, but very useful for determining how correlated your preferred markets to trade are.

A correlation reading of +1.0 means the markets are perfectly positively correlated, a reading of -1.0 means they are perfectly negatively correlated.

If you're not sure what correlation & covariance are then Google the terms with "Investopedia" added to the end - they have some great definitions and examples.

For traders this can be useful for deciding how much risk to spread across two markets that have a high correlation, or how to hedge existing positions by trading a negatively correlated market.

For investors this can be useful for building a truly diversified portfolio.

If a market has a high positive correlation, the black line will stay above zero most of the time. If a market has a high negative correlation, the black line will stay below zero most of the time.

A market with no or little correlation will bounce between the two or hover around zero most of the time.

The example market above is comparing Apple's weekly price action to the S&P500's over the past 20 weeks. It has a high positive correlation as the black line is above zero most of the time.

Good luck with your trading!

Versionshinweise

Updated to Pine Script v5
Kommentare
ASilk
Love it, I have been trying to alter the script to have the reference be another source without success (both source and reference become limited to O/H/L/C etc rather than all values present in the pane.

Any tips?
name3trades
This is quite useful. I wish however the colored scale could be made to read more than a stack of 0's, (say) scaled to read -100 to 0 to 100. Thanks for considering.
avv2010
A helpful script and YouTube video!

I have been attempting to re-code the above so that the referenceArray's elements can be fixed (such as the last 20 returns from the latest closing bar now) as we move back through time, whilst the currentArray adds and removes elements as it moves back through time (as per your code above). I tried array.unshift and other permutations with no success.

Would you consider adding this as an option in your code above? Or indeed be able to advise on the required amendment? Thank you.
tuckerleiske94
Well done!
Unknown_TracerBUNNY
very usefull
Mehr