Correlation Heatmap█ OVERVIEW
This indicator creates a correlation matrix for a user-specified list of symbols based on their time-aligned weekly or monthly price returns. It calculates the Pearson correlation coefficient for each possible symbol pair, and it displays the results in a symmetric table with heatmap-colored cells. This format provides an intuitive view of the linear relationships between various symbols' price movements over a specific time range.
█ CONCEPTS
Correlation
Correlation typically refers to an observable statistical relationship between two datasets. In a financial time series context, it usually represents the extent to which sampled values from a pair of datasets, such as two series of price returns, vary jointly over time. More specifically, in this context, correlation describes the strength and direction of the relationship between the samples from both series.
If two separate time series tend to rise and fall together proportionally, they might be highly correlated. Likewise, if the series often vary in opposite directions, they might have a strong anticorrelation . If the two series do not exhibit a clear relationship, they might be uncorrelated .
Traders frequently analyze asset correlations to help optimize portfolios, assess market behaviors, identify potential risks, and support trading decisions. For instance, correlation often plays a key role in diversification . When two instruments exhibit a strong correlation in their returns, it might indicate that buying or selling both carries elevated unsystematic risk . Therefore, traders often aim to create balanced portfolios of relatively uncorrelated or anticorrelated assets to help promote investment diversity and potentially offset some of the risks.
When using correlation analysis to support investment decisions, it is crucial to understand the following caveats:
• Correlation does not imply causation . Two assets might vary jointly over an analyzed range, resulting in high correlation or anticorrelation in their returns, but that does not indicate that either instrument directly influences the other. Joint variability between assets might occur because of shared sensitivities to external factors, such as interest rates or global sentiment, or it might be entirely coincidental. In other words, correlation does not provide sufficient information to identify cause-and-effect relationships.
• Correlation does not predict the future relationship between two assets. It only reflects the estimated strength and direction of the relationship between the current analyzed samples. Financial time series are ever-changing. A strong trend between two assets can weaken or reverse in the future.
Correlation coefficient
A correlation coefficient is a numeric measure of correlation. Several coefficients exist, each quantifying different types of relationships between two datasets. The most common and widely known measure is the Pearson product-moment correlation coefficient , also known as the Pearson correlation coefficient or Pearson's r . Usually, when the term "correlation coefficient" is used without context, it refers to this correlation measure.
The Pearson correlation coefficient quantifies the strength and direction of the linear relationship between two variables. In other words, it indicates how consistently variables' values move together or in opposite directions in a proportional, linear manner. Its formula is as follows:
𝑟(𝑥, 𝑦) = cov(𝑥, 𝑦) / (𝜎𝑥 * 𝜎𝑦)
Where:
• 𝑥 is the first variable, and 𝑦 is the second variable.
• cov(𝑥, 𝑦) is the covariance between 𝑥 and 𝑦.
• 𝜎𝑥 is the standard deviation of 𝑥.
• 𝜎𝑦 is the standard deviation of 𝑦.
In essence, the correlation coefficient measures the covariance between two variables, normalized by the product of their standard deviations. The coefficient's value ranges from -1 to 1, allowing a more straightforward interpretation of the relationship between two datasets than what covariance alone provides:
• A value of 1 indicates a perfect positive correlation over the analyzed sample. As one variable's value changes, the other variable's value changes proportionally in the same direction .
• A value of -1 indicates a perfect negative correlation (anticorrelation). As one variable's value increases, the other variable's value decreases proportionally.
• A value of 0 indicates no linear relationship between the variables over the analyzed sample.
Aligning returns across instruments
In a financial time series, each data point (i.e., bar) in a sample represents information collected in periodic intervals. For instance, on a "1D" chart, bars form at specific times as successive days elapse.
However, the times of the data points for a symbol's standard dataset depend on its active sessions , and sessions vary across instrument types. For example, the daily session for NYSE stocks is 09:30 - 16:00 UTC-4/-5 on weekdays, Forex instruments have 24-hour sessions that span from 17:00 UTC-4/-5 on one weekday to 17:00 on the next, and new daily sessions for cryptocurrencies start at 00:00 UTC every day because crypto markets are consistently open.
Therefore, comparing the standard datasets for different asset types to identify correlations presents a challenge. If two symbols' datasets have bars that form at unaligned times, their correlation coefficient does not accurately describe their relationship. When calculating correlations between the returns for two assets, both datasets must maintain consistent time alignment in their values and cover identical ranges for meaningful results.
To address the issue of time alignment across instruments, this indicator requests confirmed weekly or monthly data from spread tickers constructed from the chart's ticker and another specified ticker. The datasets for spreads are derived from lower-timeframe data to ensure the values from all symbols come from aligned points in time, allowing a fair comparison between different instrument types. Additionally, each spread ticker ID includes necessary modifiers, such as extended hours and adjustments.
In this indicator, we use the following process to retrieve time-aligned returns for correlation calculations:
1. Request the current and previous prices from a spread representing the sum of the chart symbol and another symbol ( "chartSymbol + anotherSymbol" ).
2. Request the prices from another spread representing the difference between the two symbols ( "chartSymbol - anotherSymbol" ).
3. Calculate half of the difference between the values from both spreads ( 0.5 * (requestedSum - requestedDifference) ). The results represent the symbol's prices at times aligned with the sample points on the current chart.
4. Calculate the arithmetic return of the retrieved prices: (currentPrice - previousPrice) / previousPrice
5. Repeat steps 1-4 for each symbol requiring analysis.
It's crucial to note that because this process retrieves prices for a symbol at times consistent with periodic points on the current chart, the values can represent prices from before or after the closing time of the symbol's usual session.
Additionally, note that the maximum number of weeks or months in the correlation calculations depends on the chart's range and the largest time range common to all the requested symbols. To maximize the amount of data available for the calculations, we recommend setting the chart to use a daily or higher timeframe and specifying a chart symbol that covers a sufficient time range for your needs.
█ FEATURES
This indicator analyzes the correlations between several pairs of user-specified symbols to provide a structured, intuitive view of the relationships in their returns. Below are the indicator's key features:
Requesting a list of securities
The "Symbol list" text box in the indicator's "Settings/Inputs" tab accepts a comma-separated list of symbols or ticker identifiers with optional spaces (e.g., "XOM, MSFT, BITSTAMP:BTCUSD"). The indicator dynamically requests returns for each symbol in the list, then calculates the correlation between each pair of return series for its heatmap display.
Each item in the list must represent a valid symbol or ticker ID. If the list includes an invalid symbol, the script raises a runtime error.
To specify a broker/exchange for a symbol, include its name as a prefix with a colon in the "EXCHANGE:SYMBOL" format. If a symbol in the list does not specify an exchange prefix, the indicator selects the most commonly used exchange when requesting the data.
Note that the number of symbols allowed in the list depends on the user's plan. Users with non-professional plans can compare up to 20 symbols with this indicator, and users with professional plans can compare up to 32 symbols.
Timeframe and data length selection
The "Returns timeframe" input specifies whether the indicator uses weekly or monthly returns in its calculations. By default, its value is "1M", meaning the indicator analyzes monthly returns. Note that this script requires a chart timeframe lower than or equal to "1M". If the chart uses a higher timeframe, it causes a runtime error.
To customize the length of the data used in the correlation calculations, use the "Max periods" input. When enabled, the indicator limits the calculation window to the number of periods specified in the input field. Otherwise, it uses the chart's time range as the limit. The top-left corner of the table shows the number of confirmed weeks or months used in the calculations.
It's important to note that the number of confirmed periods in the correlation calculations is limited to the largest time range common to all the requested datasets, because a meaningful correlation matrix requires analyzing each symbol's returns under the same market conditions. Therefore, the correlation matrix can show different results for the same symbol pair if another listed symbol restricts the aligned data to a shorter time range.
Heatmap display
This indicator displays the correlations for each symbol pair in a heatmap-styled table representing a symmetric correlation matrix. Each row and column corresponds to a specific symbol, and the cells at their intersections correspond to symbol pairs . For example, the cell at the "AAPL" row and "MSFT" column shows the weekly or monthly correlation between those two symbols' returns. Likewise, the cell at the "MSFT" row and "AAPL" column shows the same value.
Note that the main diagonal cells in the display, where the row and column refer to the same symbol, all show a value of 1 because any series of non-na data is always perfectly correlated with itself.
The background of each correlation cell uses a gradient color based on the correlation value. By default, the gradient uses blue hues for positive correlation, orange hues for negative correlation, and white for no correlation. The intensity of each blue or orange hue corresponds to the strength of the measured correlation or anticorrelation. Users can customize the gradient's base colors using the inputs in the "Color gradient" section of the "Settings/Inputs" tab.
█ FOR Pine Script® CODERS
• This script uses the `getArrayFromString()` function from our ValueAtTime library to process the input list of symbols. The function splits the "string" value by its commas, then constructs an array of non-empty strings without leading or trailing whitespaces. Additionally, it uses the str.upper() function to convert each symbol's characters to uppercase.
• The script's `getAlignedReturns()` function requests time-aligned prices with two request.security() calls that use spread tickers based on the chart's symbol and another symbol. Then, it calculates the arithmetic return using the `changePercent()` function from the ta library. The `collectReturns()` function uses `getAlignedReturns()` within a loop and stores the data from each call within a matrix . The script calls the `arrayCorrelation()` function on pairs of rows from the returned matrix to calculate the correlation values.
• For consistency, the `getAlignedReturns()` function includes extended hours and dividend adjustment modifiers in its data requests. Additionally, it includes other settings inherited from the chart's context, such as "settlement-as-close" preferences.
• A Pine script can execute up to 40 or 64 unique `request.*()` function calls, depending on the user's plan. The maximum number of symbols this script compares is half the plan's limit, because `getAlignedReturns()` uses two request.security() calls.
• This script can use the request.security() function within a loop because all scripts in Pine v6 enable dynamic requests by default. Refer to the Dynamic requests section of the Other timeframes and data page to learn more about this feature, and see our v6 migration guide to learn what's new in Pine v6.
• The script's table uses two distinct color.from_gradient() calls in a switch structure to determine the cell colors for positive and negative correlation values. One call calculates the color for values from -1 to 0 based on the first and second input colors, and the other calculates the colors for values from 0 to 1 based on the second and third input colors.
Look first. Then leap.
Diversification
Lead-Lag Market Detector [CryptoSea]The Lead-Lag Market Detector is an advanced tool designed to help traders identify leading and lagging assets within a chosen market. This indicator leverages correlation analysis to rank assets based on their influence, making it ideal for traders seeking to optimise their portfolio or spot key market trends.
Key Features
Dynamic Asset Ranking: Utilises real-time correlation calculations to rank assets by their influence on the market, helping traders identify market leaders and laggers.
Customisable Parameters: Includes adjustable lookback periods and correlation thresholds to adapt the analysis to different market conditions and trading styles.
Comprehensive Asset Coverage: Supports up to 30 assets, offering broad market insights across cryptocurrencies, stocks, or other markets.
Gradient-Enhanced Table Display: Presents results in a colour-coded table, where assets are ranked dynamically with influence scores, aiding in quick visual analysis.
In the example below, the ranking highlights how assets tend to move in groups. For instance, BTCUSDT, ETHUSDT, BNBUSDT, SOLUSDT, and LTCUSDT are highly correlated and moving together as a group. Similarly, another group of correlated assets includes XRPUSDT, FILUSDT, APEUSDT, XTZUSDT, THETAUSDT, and CAKEUSDT. This grouping of assets provides valuable insights for traders to diversify or spread exposure.
If you believe one asset in a group is likely to perform well, you can spread your exposure into other correlated assets within the same group to capitalise on their collective movement. Additionally, assets like AVAXUSDT and ZECUSDT, which appear less correlated or uncorrelated with the rest, may offer opportunities to act as potential hedges in your trading strategy.
How it Works
Correlation-Based Scoring: Calculates pairwise correlations between assets over a user-defined lookback period, identifying assets with high influence scores as market leaders.
Customisable Thresholds: Allows traders to define a correlation threshold, ensuring the analysis focuses only on significant relationships between assets.
Dynamic Score Calculation: Scores are updated dynamically based on the timeframe and input settings, providing real-time insights into market behaviour.
Colour-Enhanced Results: The table display uses gradients to visually distinguish between leading and lagging assets, simplifying data interpretation.
Application
Portfolio Optimisation: Identifies influential assets to help traders allocate their portfolio effectively and reduce exposure to lagging assets.
Market Trend Identification: Highlights leading assets that may signal broader market trends, aiding in strategic decision-making.
Customised Trading Strategies: Adapts to various trading styles through extensive input settings, ensuring the analysis meets the specific needs of each trader.
The Lead-Lag Market Detector by is an essential tool for traders aiming to uncover market leaders and laggers, navigate complex market dynamics, and optimise their trading strategies with precision and insight.
GDP BreakdownProvides an easy way for viewing the sub sections that make up a country's total GDP. Not all countries provide data for each subsector (Agriculture, Construction, Manufacturing, Mining, Public Administration, Services, Utilities). Only countries that provide complete data are able to be selected in the settings. If I've missed any please let me know in the comment section so they can be added. This is much easier than having to individually selecting each ticker for each country when looking to compare how diversified an economy is.
Diversified Investment EMA Cross Strategy SimulatorThis simulating indicator proves that even if you use a simple strategy, you can reduce your risk by diversifying your investments.
The strategy itself is simple.(only long)
Buy when 50 days EMA crosses over 200 days EMA.
Sell when 50 days EMA crosses under 200 days EMA.
Or, stop loss when the asset falls by 2% (eg).
Using this simple strategy on an asset is just a test of your luck.
However, this capital change graph shows that risk can be reduced by diversifying investment into eight assets rather than one asset.
Options
Total Assets Capital Change represents the sum of capital changes for 8 assets. The gray line is the initial capital.
Each Asset Capital Change represents all eight asset capital changes. In this case, the gray line is displayed as the initial capital divided by 8.
The rest of the options show a graph of capital change for each asset, showing when buys and sells occurred.
And set the start date, initial capital, stop loss %, and commission.
And select the 8 assets you want to invest in and you are ready to go. To effectively reduce risk, uncoupled assets would be better if possible.
The table in the lower right shows the selected asset and color.
Please enjoy the simulation.
Coefficient of Variation - EMA and SMA StDevYet another way to try and measure volatility. An alternative to using ATR is Standard Deviation, it can be used to measure volatility or what is also known as risk. SD measures how dispersed or far away the data is from the mean. It's commonly seen in risk management formulas or portfolio diversification formulas. The problem however is that the numbers that ATR and SD give off from one equity might not be relative to others or its own past. For example, SPY can give a large number despite not being as volatile as other equities while others being compared to can have smaller volatility numbers and still be more volatile looking.
A solution I thought of is to use percentages that are relatable to different equities. I found out another name for this idea comes from statistics and is known as coefficient of variation, also known as relative standard deviation. This helps see the volatility as a percentage and not just a number that only relates to what is being seen at the moment. I put in a border line on the zero level to see where zero is at but also to edit in case there is such a thing as a percentage number that can be too high or too low for volatility to be looked at if needed. The average and standard deviation formulas can use either simple moving average or exponential moving average.