OPEN-SOURCE SCRIPT
Granger Causality Flow Indicator

Granger Causality Flow Indicator
█ OVERVIEW
The Granger Causality Flow Indicator is a statistical analysis tool designed to identify predictive relationships between two assets (Symbol X and Symbol Y). In econometrics, "Granger Causality" does not test for actual physical causation (e.g., rain causes mud); rather, it tests for predictive causality.
This script is designed to answer a specific question for traders: "Does the past price action of Asset X provide statistically significant information about the future price of Asset Y, beyond what is already contained in the past prices of Asset Y itself?"
This tool is particularly useful for Pairs Traders, Arbitrageurs, and Macro Analysts looking to identify lead-lag relationships between correlated assets (e.g., BTC vs. ETH, NASDAQ vs. SPY, or Gold vs. Silver).
█ CONCEPTS & CALCULATIONS
To determine if Symbol X "Granger-causes" Symbol Y, this script utilizes a variance-reduction approach based on Auto-Regressive (AR) models. Due to the runtime constraints of Pine Script™, we employ an optimized proxy for the standard Granger test using an AR(1) logic (looking back 1 period).
The calculation performs a comparative test over a rolling window (Default: 50 bars):
█ HOW TO USE
This indicator is not a simple Buy/Sell signal generator; it is a context filter for cross-asset analysis.
1. Setup
2. Interpreting the Visuals
The script changes the background color and displays a table to indicate the current flow of causality:
3. Trading Application
█ SETTINGS
█ DISCLAIMER
This tool provides statistical analysis of historical price data and does not guarantee future performance. Granger Causality is a measure of predictive capability, not necessarily fundamental causation. Always use appropriate risk management.
█ OVERVIEW
The Granger Causality Flow Indicator is a statistical analysis tool designed to identify predictive relationships between two assets (Symbol X and Symbol Y). In econometrics, "Granger Causality" does not test for actual physical causation (e.g., rain causes mud); rather, it tests for predictive causality.
This script is designed to answer a specific question for traders: "Does the past price action of Asset X provide statistically significant information about the future price of Asset Y, beyond what is already contained in the past prices of Asset Y itself?"
This tool is particularly useful for Pairs Traders, Arbitrageurs, and Macro Analysts looking to identify lead-lag relationships between correlated assets (e.g., BTC vs. ETH, NASDAQ vs. SPY, or Gold vs. Silver).
█ CONCEPTS & CALCULATIONS
To determine if Symbol X "Granger-causes" Symbol Y, this script utilizes a variance-reduction approach based on Auto-Regressive (AR) models. Due to the runtime constraints of Pine Script™, we employ an optimized proxy for the standard Granger test using an AR(1) logic (looking back 1 period).
The calculation performs a comparative test over a rolling window (Default: 50 bars):
- The Restricted Model (Baseline):
We attempts to predict the current value of Y using only the previous value of Y (Auto-Regression). We measure the error of this prediction (the "Residuals") and calculate the Variance of the Restricted Model (Var_R). - The Unrestricted Model (Proxy):
We then test if the past value of X can explain the errors made by the Restricted Model. If X contains predictive power, including it should reduce the error variance. We calculate the remaining Variance of the Unrestricted Model (Var_UR). - The GC Score:
The script calculates a score based on the ratio of variance reduction:
Score = 1 - (Var_UR / Var_R)
- If the Score is High (> 0): It implies that including X significantly reduced the prediction error for Y. Therefore, X "Granger-causes" Y.
- If the Score is Low or 0: It implies X added no predictive value.
█ HOW TO USE
This indicator is not a simple Buy/Sell signal generator; it is a context filter for cross-asset analysis.
1. Setup
- Symbol 1 (X): The potential "Leader" (e.g., BINANCE:BTCUSDT).
- Symbol 2 (Y): The potential "Follower" (e.g., BINANCE:ETHUSDT).
- Differencing: Enabled by default. This checks the changes in price rather than absolute price, which is crucial for statistical stationarity.
2. Interpreting the Visuals
The script changes the background color and displays a table to indicate the current flow of causality:
- Green Background (X → Y): Symbol 1 is leading Symbol 2. Price moves in Symbol 1 are statistically likely to foreshadow moves in Symbol 2.
- Orange Background (Y → X): Symbol 2 is leading Symbol 1. The relationship has inverted.
- Blue Background (Bidirectional): Both assets are predicting each other (tight coupling or feedback loop).
- Gray/No Color: No statistically significant relationship detected.
3. Trading Application
- Trend Confirmation: If you trade Symbol Y, wait for the background to turn Green. This indicates that the "Leader" (Symbol X) is currently exerting predictive influence, potentially making trend-following setups on Symbol Y more reliable.
- Divergence Warning: If you are trading a correlation pair and the causality breaks (turns Gray), the correlation may be weakening, signaling a higher risk of divergence.
█ SETTINGS
- Symbol 1 (X) & Symbol 2 (Y): The two tickers to analyze.
- Use Differencing: (Default: True) Converts prices to price-changes. Highly recommended for accurate statistical results to avoid spurious regression.
- Calculation Window: The number of bars used to compute the variance and coefficients. Larger windows provide smoother, more stable signals but react slower to regime changes.
- Significance Threshold: (0.01 - 0.99) The minimum variance reduction score required to trigger a causal signal.
█ DISCLAIMER
This tool provides statistical analysis of historical price data and does not guarantee future performance. Granger Causality is a measure of predictive capability, not necessarily fundamental causation. Always use appropriate risk management.
نص برمجي مفتوح المصدر
بروح TradingView الحقيقية، قام مبتكر هذا النص البرمجي بجعله مفتوح المصدر، بحيث يمكن للمتداولين مراجعة وظائفه والتحقق منها. شكرا للمؤلف! بينما يمكنك استخدامه مجانًا، تذكر أن إعادة نشر الكود يخضع لقواعد الموقع الخاصة بنا.
mastertop
إخلاء المسؤولية
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.
نص برمجي مفتوح المصدر
بروح TradingView الحقيقية، قام مبتكر هذا النص البرمجي بجعله مفتوح المصدر، بحيث يمكن للمتداولين مراجعة وظائفه والتحقق منها. شكرا للمؤلف! بينما يمكنك استخدامه مجانًا، تذكر أن إعادة نشر الكود يخضع لقواعد الموقع الخاصة بنا.
mastertop
إخلاء المسؤولية
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.