OPEN-SOURCE SCRIPT
Spread Trading Z-Score

Indicator: Z-Score Spread Indicator
Description
The "Z-Score Spread Indicator" is a powerful tool for traders employing mean-reversion strategies on the spread between two financial assets (e.g., futures contracts like MNQ and MES). This indicator calculates and plots the Z-score of the price spread, indicating how far the current spread deviates from its historical mean. It features customizable entry and exit thresholds with adjustable offsets, along with an estimated p-value displayed in a table to assess statistical significance.
Key Features
Asset Selection: Allows users to select two asset symbols (e.g., CME_MINI:MNQ1! and CME_MINI:MES1!) via customizable inputs.
Z-Score Calculation: Computes the Z-score based on the spread’s simple moving average and standard deviation over a user-defined lookback period.
Customizable Thresholds with Offset: Offers adjustable base entry and exit thresholds, with an optional offset to fine-tune trading levels, plotted as horizontal lines.
P-Value Estimation: Provides an approximate p-value to evaluate the statistical significance of the Z-score, displayed in a table anchored to the top-left corner.
Visual Representation: Plots the Z-score with a zero line and threshold lines for intuitive interpretation.
Adjustable Parameters
Asset A Symbol: Symbol for Asset A (default: CME_MINI:MNQ1!).
Asset B Symbol: Symbol for Asset B (default: CME_MINI:MES1!).
Z-Score Lookback: Lookback period for Z-score calculation (default: 40, minimum 2).
Base Entry Threshold: Threshold for entry signals (default: 1.8, adjustable with a step of 0.1).
Base Exit Threshold: Threshold for exit signals (default: 0.5, adjustable with a step of 0.1).
Threshold Offset (+/-): Offset to adjust entry and exit thresholds symmetrically (default: 0.0, range -5.0 to 5.0, step 0.1).
Usage
Add the indicator to your chart via the "Indicators" tab.
Customize the parameters based on your preferred assets and trading strategy (lookback period, thresholds, offset).
Observe the Z-score plot and threshold lines (red for short entry, green for long entry, orange dotted for exits) to identify potential trade setups.
Check the p-value table in the top-left corner to assess the statistical significance of the current Z-score.
Use this data to inform mean-reversion trading decisions, ideally in conjunction with other indicators.
Notes
A Z-score above the entry threshold (positive) or below the negative entry threshold suggests a potential short or long entry, respectively. Exits are signaled when the Z-score crosses the exit thresholds.
The p-value is an approximation based on the normal distribution; a value below 0.05 typically indicates statistical significance, but further validation is recommended.
The indicator uses a simple spread (Asset A - Asset B) without volatility adjustments; consider pairing it with a lots calculator for hedging.
Limitations
The p-value is an approximation and may not reflect advanced statistical tests (e.g., ADF) due to Pine Script constraints.
No automatic trading signals are generated; it provides data for manual analysis.
Author
Developed by grogusama, October 15, 2025, 07:29 PM CEST.
Description
The "Z-Score Spread Indicator" is a powerful tool for traders employing mean-reversion strategies on the spread between two financial assets (e.g., futures contracts like MNQ and MES). This indicator calculates and plots the Z-score of the price spread, indicating how far the current spread deviates from its historical mean. It features customizable entry and exit thresholds with adjustable offsets, along with an estimated p-value displayed in a table to assess statistical significance.
Key Features
Asset Selection: Allows users to select two asset symbols (e.g., CME_MINI:MNQ1! and CME_MINI:MES1!) via customizable inputs.
Z-Score Calculation: Computes the Z-score based on the spread’s simple moving average and standard deviation over a user-defined lookback period.
Customizable Thresholds with Offset: Offers adjustable base entry and exit thresholds, with an optional offset to fine-tune trading levels, plotted as horizontal lines.
P-Value Estimation: Provides an approximate p-value to evaluate the statistical significance of the Z-score, displayed in a table anchored to the top-left corner.
Visual Representation: Plots the Z-score with a zero line and threshold lines for intuitive interpretation.
Adjustable Parameters
Asset A Symbol: Symbol for Asset A (default: CME_MINI:MNQ1!).
Asset B Symbol: Symbol for Asset B (default: CME_MINI:MES1!).
Z-Score Lookback: Lookback period for Z-score calculation (default: 40, minimum 2).
Base Entry Threshold: Threshold for entry signals (default: 1.8, adjustable with a step of 0.1).
Base Exit Threshold: Threshold for exit signals (default: 0.5, adjustable with a step of 0.1).
Threshold Offset (+/-): Offset to adjust entry and exit thresholds symmetrically (default: 0.0, range -5.0 to 5.0, step 0.1).
Usage
Add the indicator to your chart via the "Indicators" tab.
Customize the parameters based on your preferred assets and trading strategy (lookback period, thresholds, offset).
Observe the Z-score plot and threshold lines (red for short entry, green for long entry, orange dotted for exits) to identify potential trade setups.
Check the p-value table in the top-left corner to assess the statistical significance of the current Z-score.
Use this data to inform mean-reversion trading decisions, ideally in conjunction with other indicators.
Notes
A Z-score above the entry threshold (positive) or below the negative entry threshold suggests a potential short or long entry, respectively. Exits are signaled when the Z-score crosses the exit thresholds.
The p-value is an approximation based on the normal distribution; a value below 0.05 typically indicates statistical significance, but further validation is recommended.
The indicator uses a simple spread (Asset A - Asset B) without volatility adjustments; consider pairing it with a lots calculator for hedging.
Limitations
The p-value is an approximation and may not reflect advanced statistical tests (e.g., ADF) due to Pine Script constraints.
No automatic trading signals are generated; it provides data for manual analysis.
Author
Developed by grogusama, October 15, 2025, 07:29 PM CEST.
오픈 소스 스크립트
진정한 트레이딩뷰 정신에 따라 이 스크립트 작성자는 트레이더가 기능을 검토하고 검증할 수 있도록 오픈소스로 공개했습니다. 작성자에게 찬사를 보냅니다! 무료로 사용할 수 있지만 코드를 다시 게시할 경우 하우스 룰이 적용된다는 점을 기억하세요.
면책사항
이 정보와 게시물은 TradingView에서 제공하거나 보증하는 금융, 투자, 거래 또는 기타 유형의 조언이나 권고 사항을 의미하거나 구성하지 않습니다. 자세한 내용은 이용 약관을 참고하세요.
오픈 소스 스크립트
진정한 트레이딩뷰 정신에 따라 이 스크립트 작성자는 트레이더가 기능을 검토하고 검증할 수 있도록 오픈소스로 공개했습니다. 작성자에게 찬사를 보냅니다! 무료로 사용할 수 있지만 코드를 다시 게시할 경우 하우스 룰이 적용된다는 점을 기억하세요.
면책사항
이 정보와 게시물은 TradingView에서 제공하거나 보증하는 금융, 투자, 거래 또는 기타 유형의 조언이나 권고 사항을 의미하거나 구성하지 않습니다. 자세한 내용은 이용 약관을 참고하세요.