OPEN-SOURCE SCRIPT

Linear Dominion

1537


### Indicator Note: Linear Dominion

**Overview:**
The "Linear Dominion" indicator is a Pine Script v6 implementation of a Linear Regression Channel, designed to overlay on price charts in TradingView. It calculates a linear regression line based on a user-defined length and source, then constructs upper and lower channel boundaries around this line using either standard deviation or maximum price deviations. The indicator provides visual trend analysis, statistical correlation (Pearson's R), and alert conditions for price movements.

**Key Components:**
1. **Linear Regression Line (Base Line):**
- Calculated using the least squares method over a specified `Length` period.
- Represents the best-fit straight line through the price data (default source: close price).
- Displayed in red (solid line, no transparency).

2. **Upper and Lower Channels:**
- **Upper Channel**: Plotted above the base line (blue, semi-transparent).
- **Lower Channel**: Plotted below the base line (red, semi-transparent).
- Channel width can be set using:
- Standard deviation multiplied by a user-defined factor (`Upper Deviation` and `Lower Deviation`).
- Maximum price deviation from the regression line (if deviation multipliers are disabled).

3. **Inputs:**
- **Length**: Number of bars used for regression calculation (default: 100, range: 1-5000).
- **Source**: Price data to analyze (default: close).
- **Channel Settings**:
- `Upper Deviation`: Toggle and multiplier for upper channel width (default: true, 2.0).
- `Lower Deviation`: Toggle and multiplier for lower channel width (default: true, 2.0).
- **Display Settings**:
- `Show Pearson's R`: Displays correlation coefficient (default: true).
- `Extend Lines Left`: Extends channel lines to the left (default: false).
- `Extend Lines Right`: Extends channel lines to the right (default: true).
- **Color Settings**:
- Upper channel: Light blue (85% transparency).
- Lower channel: Light red (85% transparency).

4. **Statistical Features:**
- **Pearson's R**: Measures the linear correlation between price and time, displayed as a label below the lower channel when enabled.
- Range: -1 to 1 (1 = perfect positive correlation, -1 = perfect negative correlation, 0 = no correlation).

5. **Alerts:**
- **Dominion Channel Exited**: Triggers when price crosses above the upper channel or below the lower channel.
- **Switched to Uptrend**: Triggers when the trend changes from downtrend to uptrend (base line slope becomes positive).
- **Switched to Downtrend**: Triggers when the trend changes from uptrend to downtrend (base line slope becomes negative).

**How It Works:**
- The `calcSlope` function computes the slope, average, and intercept of the regression line using the formula: \( slope = \frac{n\sum(xy) - \sum x \sum y}{n\sum(x^2) - (\sum x)^2} \).
- The `calcDev` function calculates:
- Standard deviation of price from the regression line.
- Maximum upward and downward deviations (based on high/low prices).
- Pearson’s R correlation coefficient.
- Lines are dynamically updated each bar:
- Base line connects the start price (intercept + slope * (length-1)) to the end price (intercept).
- Upper/lower channels are offset from the base line based on the chosen deviation method.
- The area between lines is filled with semi-transparent colors for visual clarity.

**Usage:**
- **Trend Identification**: The slope of the base line indicates the overall trend direction.
- **Support/Resistance**: Upper and lower channels act as dynamic levels where price might reverse or break out.
- **Volatility Analysis**: Wider channels indicate higher volatility; narrower channels suggest consolidation.
- **Correlation Insight**: Pearson's R helps assess how well price follows a linear trend.

**Notes:**
- The lower channel line currently uses `colorUpper` (blue) instead of `colorLower` (red), which may be a bug (should be fixed to `colorLower` for consistency).
- The indicator updates only on the last bar (`barstate.islast`) to optimize performance.
- Maximum bars back is set to 5000, which should match or exceed the maximum `Length` input.

**Potential Improvements:**
- Fix the lower line color to use `colorLower`.
- Add options for line width customization.
- Include additional statistical metrics (e.g., R-squared).

免責聲明

這些資訊和出版物並不意味著也不構成TradingView提供或認可的金融、投資、交易或其他類型的意見或建議。請在使用條款閱讀更多資訊。