OPEN-SOURCE SCRIPT
Correlation & Mean Reversion - Paired Stocks/ETFs Trading

This indicator is designed to analyze pair trading opportunities between two assets by evaluating:
🔧 Core Features
1. Symbol Pair Analysis
Takes two input symbols (Symbol A, Symbol B).
Calculates the price ratio (A/B) and compares it to a user-defined targetRatio.
2. Z-Score of Ratio
Computes Z-Score of the ratio over a user-defined lookback window to measure how far the ratio deviates from its mean.
This gauges whether the ratio is likely to revert to the mean.
3. Reversion Probability
Applies a normal CDF approximation to Z-Score to estimate the probability of mean reversion.
Flags moderate (80%+) and strong (95%+) reversion signals.
4. Correlation
Calculates the rolling correlation of log returns between Symbol A and Symbol B.
Useful to confirm if the assets historically move together.
🔧 Core Features
1. Symbol Pair Analysis
Takes two input symbols (Symbol A, Symbol B).
Calculates the price ratio (A/B) and compares it to a user-defined targetRatio.
2. Z-Score of Ratio
Computes Z-Score of the ratio over a user-defined lookback window to measure how far the ratio deviates from its mean.
This gauges whether the ratio is likely to revert to the mean.
3. Reversion Probability
Applies a normal CDF approximation to Z-Score to estimate the probability of mean reversion.
Flags moderate (80%+) and strong (95%+) reversion signals.
4. Correlation
Calculates the rolling correlation of log returns between Symbol A and Symbol B.
Useful to confirm if the assets historically move together.
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
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.
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
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.