OPEN-SOURCE SCRIPT
cd_correlation_analys_Cx

cd_correlation_analys_Cx
General:
This indicator is designed for correlation analysis by classifying stocks (487 in total) and indices (14 in total) traded on Borsa İstanbul (BIST) on a sectoral basis.
Tradingview's sector classifications (20) have been strictly adhered to for sector grouping.
Depending on user preference, the analysis can be performed within sectors, between sectors, or manually (single asset).
Let me express my gratitude to the code author, fikira, beforehand; you will find the reason for my thanks in the context.
Details:
First, let's briefly mention how this indicator could have been prepared using the classic method before going into details.
Classically, assets could be divided into groups of forty (40), and the analysis could be performed using the built-in function:
ta.correlation(source1, source2, length) → series float.
I chose sectoral classification because I believe there would be a higher probability of assets moving together, rather than using fixed-number classes.
In this case, 21 arrays were formed with the following number of elements:
(3, 11, 21, 60, 29, 20, 12, 3, 31, 5, 10, 11, 6, 48, 73, 62, 16, 19, 13, 34 and indices (14)).
However, you might have noticed that some arrays have more than 40 elements. This is exactly where fikira's indicator came to the rescue. When I examined their excellent indicator, I saw that it could process 120 assets in a single operation. (I believe this was the first limit overrun; thanks again.)
It was amazing to see that data for 3 pairs could be called in a single request using a special method.
You can find the details here:

When I adapted it for BIST, I found it sufficient to call data for 2 pairs instead of 3 in a single go. Since asset prices are regular and have 2 decimal places, I used a fixed multiplier of $10^8$ and a fixed decimal count of 2 in Fikira's formulas.
With this method, the (high, low, open, close) values became accessible for each asset.
The summary up to this point is that instead of the ready-made formula + groups of 40, I used variable-sized groups and the method I will detail now.
Correlation/harmony/co-movement between assets provides advantages to market participants. Coherent assets are expected to rise or fall simultaneously.
Therefore, to convert co-movement into a mathematical value, I defined the possible movements of the current candle relative to the previous candle bar over a certain period (user-defined). These are:
Up := high > high[1] and low > low[1]
Down := high < high[1] and low < low[1]
Inside := high <= high[1] and low >= low[1]
Outside := high >= high[1] and low <= low[1] and NOT Inside.
Ignore := high = low = open = close
If both assets performed the same movement, 1 was added to the tracking counter.
If (Up-Up), (Down-Down), (Inside-Inside), or (Outside-Outside), then counter := counter + 1.
If the period length is 100 and the counter is 75, it means there is 75% co-movement.
Corr = counter / period ($75/100$)
Average = ta.sma(Corr, 100) is obtained.
The highest coefficients recorded in the array are presented to the user in a table.
From the user menu options, the user can choose to compare:
• With assets in its own sector
• With assets in the selected sector
• By activating the confirmation box and manually entering a single asset for comparison.
Table display options can be adjusted from the Settings tab.

In the attached examples:
Results for AKBNK stock from the Finance sector compared with GARAN stock from the same sector:
Timeframe: Daily, Period: 50 => Harmony 76% (They performed the same movement in 38 out of 50 bars)
Comment: Opposite movements at swing high and low levels may indicate a change in the direction of the price flow (SMT).

Looking at ASELS from the Electronic Technology sector over the last 30 daily candles, they performed the same movements by 40% with XU100, 73.3% (22/30) with XUTEK (Technology Index), and 86.9% according to the averages.
Comment: It is more appropriate to follow ASELS stock with XUTEK (Technology index) instead of the general index (XU100). Opposite movements at swing high and low levels may indicate a change in the direction of the price flow (SMT).

Again, when ASELS stock is taken on H1 instead of daily, and the length is 100 instead of 30, the harmony rate is seen to be 87%.

Please share your thoughts and criticisms regarding the indicator, which I prepared with a bit of an educational purpose specifically for BIST.
Happy trading.
General:
This indicator is designed for correlation analysis by classifying stocks (487 in total) and indices (14 in total) traded on Borsa İstanbul (BIST) on a sectoral basis.
Tradingview's sector classifications (20) have been strictly adhered to for sector grouping.
Depending on user preference, the analysis can be performed within sectors, between sectors, or manually (single asset).
Let me express my gratitude to the code author, fikira, beforehand; you will find the reason for my thanks in the context.
Details:
First, let's briefly mention how this indicator could have been prepared using the classic method before going into details.
Classically, assets could be divided into groups of forty (40), and the analysis could be performed using the built-in function:
ta.correlation(source1, source2, length) → series float.
I chose sectoral classification because I believe there would be a higher probability of assets moving together, rather than using fixed-number classes.
In this case, 21 arrays were formed with the following number of elements:
(3, 11, 21, 60, 29, 20, 12, 3, 31, 5, 10, 11, 6, 48, 73, 62, 16, 19, 13, 34 and indices (14)).
However, you might have noticed that some arrays have more than 40 elements. This is exactly where fikira's indicator came to the rescue. When I examined their excellent indicator, I saw that it could process 120 assets in a single operation. (I believe this was the first limit overrun; thanks again.)
It was amazing to see that data for 3 pairs could be called in a single request using a special method.
You can find the details here:

When I adapted it for BIST, I found it sufficient to call data for 2 pairs instead of 3 in a single go. Since asset prices are regular and have 2 decimal places, I used a fixed multiplier of $10^8$ and a fixed decimal count of 2 in Fikira's formulas.
With this method, the (high, low, open, close) values became accessible for each asset.
The summary up to this point is that instead of the ready-made formula + groups of 40, I used variable-sized groups and the method I will detail now.
Correlation/harmony/co-movement between assets provides advantages to market participants. Coherent assets are expected to rise or fall simultaneously.
Therefore, to convert co-movement into a mathematical value, I defined the possible movements of the current candle relative to the previous candle bar over a certain period (user-defined). These are:
Up := high > high[1] and low > low[1]
Down := high < high[1] and low < low[1]
Inside := high <= high[1] and low >= low[1]
Outside := high >= high[1] and low <= low[1] and NOT Inside.
Ignore := high = low = open = close
If both assets performed the same movement, 1 was added to the tracking counter.
If (Up-Up), (Down-Down), (Inside-Inside), or (Outside-Outside), then counter := counter + 1.
If the period length is 100 and the counter is 75, it means there is 75% co-movement.
Corr = counter / period ($75/100$)
Average = ta.sma(Corr, 100) is obtained.
The highest coefficients recorded in the array are presented to the user in a table.
From the user menu options, the user can choose to compare:
• With assets in its own sector
• With assets in the selected sector
• By activating the confirmation box and manually entering a single asset for comparison.
Table display options can be adjusted from the Settings tab.
In the attached examples:
Results for AKBNK stock from the Finance sector compared with GARAN stock from the same sector:
Timeframe: Daily, Period: 50 => Harmony 76% (They performed the same movement in 38 out of 50 bars)
Comment: Opposite movements at swing high and low levels may indicate a change in the direction of the price flow (SMT).
Looking at ASELS from the Electronic Technology sector over the last 30 daily candles, they performed the same movements by 40% with XU100, 73.3% (22/30) with XUTEK (Technology Index), and 86.9% according to the averages.
Comment: It is more appropriate to follow ASELS stock with XUTEK (Technology index) instead of the general index (XU100). Opposite movements at swing high and low levels may indicate a change in the direction of the price flow (SMT).
Again, when ASELS stock is taken on H1 instead of daily, and the length is 100 instead of 30, the harmony rate is seen to be 87%.
Please share your thoughts and criticisms regarding the indicator, which I prepared with a bit of an educational purpose specifically for BIST.
Happy trading.
Skrypt open-source
W duchu TradingView twórca tego skryptu udostępnił go jako open-source, aby traderzy mogli analizować i weryfikować jego funkcjonalność. Brawo dla autora! Możesz korzystać z niego za darmo, ale pamiętaj, że ponowna publikacja kodu podlega naszym Zasadom Społeczności.
Wyłączenie odpowiedzialności
Informacje i publikacje przygotowane przez TradingView lub jego użytkowników, prezentowane na tej stronie, nie stanowią rekomendacji ani porad handlowych, inwestycyjnych i finansowych i nie powinny być w ten sposób traktowane ani wykorzystywane. Więcej informacji na ten temat znajdziesz w naszym Regulaminie.
Skrypt open-source
W duchu TradingView twórca tego skryptu udostępnił go jako open-source, aby traderzy mogli analizować i weryfikować jego funkcjonalność. Brawo dla autora! Możesz korzystać z niego za darmo, ale pamiętaj, że ponowna publikacja kodu podlega naszym Zasadom Społeczności.
Wyłączenie odpowiedzialności
Informacje i publikacje przygotowane przez TradingView lub jego użytkowników, prezentowane na tej stronie, nie stanowią rekomendacji ani porad handlowych, inwestycyjnych i finansowych i nie powinny być w ten sposób traktowane ani wykorzystywane. Więcej informacji na ten temat znajdziesz w naszym Regulaminie.