OPEN-SOURCE SCRIPT
Telah dikemas kini

Correlation Table

782
This indicator displays a vertical table that shows the correlation between the asset currently loaded on the chart and up to 32 selected trading pairs. It offers the following features:

Chart-Based Correlation: Correlations are calculated based on the asset you have loaded in your chart, providing relevant insights for your current market focus.

Configurable Pairs: Choose from a list of 32 symbols (e.g., AUDUSD, EURUSD, GBPUSD, etc.) with individual checkboxes to include or exclude each pair in the correlation analysis.

Custom Correlation Length: Adjust the lookback period for the correlation calculation to suit your analysis needs.

Optional EMA Smoothing: Enable an Exponential Moving Average (EMA) on the price data, with a configurable EMA length, to smooth the series before calculating correlations.

Color-Coded Output: The table cells change color based on the correlation strength and direction—neutral, bullish (green), or bearish (red)—making it easy to interpret at a glance.

Clear Table Layout: The indicator outputs a neatly organized vertical table with headers for "Pair" and "Correlation," ensuring the information is displayed cleanly and is easy to understand.

Ideal for traders who want a quick visual overview of how different instruments correlate with their current asset, this tool supports informed multi-asset analysis

ITALIANO:
Questo indicatore visualizza una tabella verticale che mostra la correlazione tra l'asset attualmente caricato sul grafico e fino a 32 coppie di trading selezionate. Offre le seguenti funzionalità:

Correlazione basata sul grafico: le correlazioni vengono calcolate in base all'asset caricato nel grafico, fornendo informazioni pertinenti per il tuo attuale focus di mercato.

Coppie configurabili: scegli da un elenco di 32 simboli (ad esempio, AUDUSD, EURUSD, GBPUSD, ecc.) con caselle di controllo individuali per includere o escludere ciascuna coppia nell'analisi della correlazione.

Lunghezza di correlazione personalizzata: regola il periodo di lookback per il calcolo della correlazione in base alle tue esigenze di analisi.

Smoothing EMA opzionale: abilita una media mobile esponenziale (EMA) sui dati dei prezzi, con una lunghezza EMA configurabile, per smussare la serie prima di calcolare le correlazioni.

Output codificato a colori: le celle della tabella cambiano colore in base alla forza e alla direzione della correlazione, neutra, rialzista (verde) o ribassista (rosso), rendendola facile da interpretare a colpo d'occhio.

Clear Table Layout: l'indicatore genera una tabella verticale ordinatamente organizzata con intestazioni per "Coppia" e "Correlazione", assicurando che le informazioni siano visualizzate in modo chiaro e siano facili da comprendere.

Ideale per i trader che desiderano una rapida panoramica visiva di come diversi strumenti siano correlati con il loro asset corrente, questo strumento supporta un'analisi multi-asset informata
Nota Keluaran
Here is a summary of the improvements made to the correlation indicator:

Optimized Default Pairs:
Set 11 major forex pairs as active by default:
-EURUSD
-USDCHF
-USDCAD
-NZDJPY
-AUDJPY
-NZDUSD
-AUDUSD
-GBPJPY
-GBPUSD
-USDJPY
-CADJPY

Other pairs available but disabled by default

Sort System:
Added the ability to sort correlations in:
-Ascending order
-Descending order
-Custom order (default)
-Fixed the ConcurrentModificationException issue in sorting

Improved Documentation:
-Added bilingual tooltip (Italian/English) for the Correlation Length parameter
-Clear explanation of how the correlation calculation works
-Practical example with timeframes (e.g. M5 with 50 periods = 250 minutes)

User Interface:
-Cleaner and more organized layout
-View options customizable for each pair
-Adjustable table size (Large, Medium, Small)

These improvements make the indicator more user-friendly and functional for analyzing correlations between forex pairs.
Nota Keluaran
The main changes are:
- Moved array declaration outside the "if" block
- Added "array.clear()" to clear arrays before filling them
- Removed "var" from array declaration inside the block
Nota Keluaran
I made several key changes to make the code more stable, particularly in sorting management. Here are the main modifications:

1. **New Sorting Approach**:
Pine Script®
sort_pairs(pairs) => if sortType != 'Personalizzato' n = array.size(pairs) sorted = array.new<PairData>() // Create a new array instead of modifying the existing one


// First copy all values
for i = 0 to n - 1
array.push(sorted, array.get(pairs, i))

- Instead of modifying the original array, we create a new `sorted` array
- first copy all values into this new array
- This prevents modifications to the array while we're reading it

2. **Improved Sorting Algorithm**:
Pine Script®
// Selection sort instead of bubble sort for i = 0 to n - 2 minIdx = i for j = i + 1 to n - 1 curr = array.get(sorted, minIdx) next = array.get(sorted, j) shouldSwap = sortType == 'Crescente' ? curr.correlation > next.correlation : curr.correlation < next.correlation if shouldSwap minIdx := j


- implemented selection sort instead of bubble sort
- This algorithm is more stable because it makes fewer swaps
- keep track of the minimum index instead of continuously swapping elements

3. **Safer Swap Management**:
Pine Script®
if minIdx != i temp = array.get(sorted, i) array.set(sorted, i, array.get(sorted, minIdx)) array.set(sorted, minIdx, temp)


- Swaps are only made when necessary (minIdx != i)
- use a temporary variable for swapping
- This makes the operation safer and prevents data corruption

4. **Clearer Data Flow**:
Pine Script®
if barstate.islast pairs = get_pairs_data() // 1. Get data sorted_pairs = sort_pairs(pairs) // 2. Sort data safely update_table(sorted_pairs) // 3. Update table with sorted data


- made the data flow clearer
- Each operation has a specific and well-defined purpose
- The original array remains intact while we work on a sorted copy

These changes have made the code more stable because:
- We avoid concurrent modifications to arrays
- We use a more robust sorting algorithm
- We maintain a clear separation between original and sorted data
- We reduce the risk of data corruption during sorting
Nota Keluaran
Renamed the indicator correctly

Penafian

Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.