OPEN-SOURCE SCRIPT

Market Gravity: Relative Value Engine

2 737
---

**Relative Value Model (Enhanced)**

This indicator calculates a statistically-derived fair value for your asset by modeling its historical relationship with a reference symbol (default: USDT Dominance). It supports three model types — Linear Range, Linear Regression, and Logarithmic Range — and outputs a prediction line, residual bands, Z-Score signals, and a full metrics dashboard.

---

**What Does This Indicator Do?**

This indicator builds a statistical price model output by comparing your current chart's price to a reference symbol. It calculates what your asset's price *should* be based on the historical relationship between the two symbols, then measures how far the actual price deviates from that prediction. In short: it answers the question — *"Is this asset overvalued or undervalued relative to a correlated market driver?"*

---

**How the Three Models Work**

**Linear Range**
Maps the reference symbol's high/low range to the current chart's high/low range using a simple slope/intercept formula. Best for assets that move in proportion to the reference linearly.

**Linear Regression (Correlation)** *(Default)*
Uses Pearson correlation, standard deviation, and rolling means to calculate a beta (slope) and alpha (intercept) — essentially a rolling OLS regression. This is the most statistically rigorous method and handles non-proportional relationships better.

**Logarithmic Range**
Performs the same range mapping as Linear Range but in log space, then exponentiates back. Ideal for assets with exponential price behavior (e.g., crypto over long timeframes).

---

**Dashboard Panel — What Each Row Means**

**Regime** — Whether price is above (Premium) or below (Discount) the model. Green = overpriced vs model; Red = underpriced.

**Diff %** — Percentage deviation of actual price from predicted price. +5% means price is 5% above the model estimate.

**Prediction** — The model's estimated fair price for the current bar. Your fair value anchor.

**Z-Score** — How many standard deviations price is from the prediction. Above +2 or below -2 is considered statistically extreme.

**Z-Score State** — Normal or Extreme based on your threshold setting. Extreme signals a potential mean-reversion setup.

**R²** — R-squared, goodness of fit from 0 to 1. ≥0.6 = Strong, 0.35–0.6 = Moderate, below 0.35 = Weak. Only trust the model when quality is at least Moderate.

**Model Quality** — Text label derived from R². Use signals only when this reads Moderate or Strong.

**MAE** — Mean Absolute Error in price units. Lower = model tracks price more closely.

**MAPE %** — Mean Absolute Percentage Error. Lower percentage = more accurate model overall.

**Smoothing** — Shows the active moving average type and length applied to the prediction line.

---

**Visual Elements on the Chart**

- Yellow line — The predicted fair-value price line
- Aqua bands — Upper and lower residual bands (predicted ± multiplier × standard deviation). Price outside these bands is statistically stretched.
- Green fill — Premium zone, price is above the model
- Red fill — Discount zone, price is below the model
- LONG label — Price crossed above the prediction line, bullish regime shift
- SHORT label — Price crossed below the prediction line, bearish regime shift
- Z+ triangle — Z-Score crossed above the extreme threshold, potential reversal warning
- Z- triangle — Z-Score crossed below the negative threshold, potential reversal warning

---

**Settings Guide**

*Data and Model*
- Reference Symbol: Default is USDT Dominance. Try DXY, SPX, BTC, or any asset correlated to your chart.
- Reference Timeframe: Leave blank to match your chart, or set a higher timeframe for macro-level signals.
- Model Type: Start with Linear Regression. Switch to Logarithmic Range for crypto on long-term charts.
- Lookback Period: Use 10–20 on lower timeframes (1H, 4H), and 50–100 on daily or weekly charts.

*Smoothing*
- Smoothing Type: EMA reacts faster, RMA is smoother, None gives the raw model output.
- Smoothing Length: Increase to reduce noise, decrease for more responsiveness.

*Signals and Risk*
- Residual Band Multiplier: Default 1.5. Try 1.0–2.5. Higher = wider bands, fewer signals.
- Z-Score Threshold: Default 2.0. Use 1.5 for more frequent signals, 2.5 for rarer and stronger ones.

*Visualization*
- All colors, band visibility, and panel on/off are fully customizable. Auto theme detects your chart background automatically.

---

**Built-in Alerts**

Three alert conditions are included:
1. Z-Score Premium Threshold Crossed — price enters extreme overvalued territory above the model.
2. Z-Score Discount Threshold Crossed — price enters extreme undervalued territory below the model.
3. Price Crossed Model — price crosses the prediction line in either direction, possible regime change.

To activate, go to Alerts → Create Alert → Condition → select this indicator.

---

*This indicator is for educational and analytical purposes only. Not financial advice.*

Declinazione di responsabilità

Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.