PINE LIBRARY

LibWght

64
Library "LibWght"
This is a library of mathematical and statistical functions
designed for quantitative analysis in Pine Script. Its core
principle is the integration of a custom weighting series
(e.g., volume) into a wide array of standard technical
analysis calculations.

Key Capabilities:

1. **Universal Weighting:** All exported functions accept a `weight`
parameter. This allows standard calculations (like moving
averages, RSI, and standard deviation) to be influenced by an
external data series, such as volume or tick count.

2. **Weighted Averages and Indicators:** Includes a comprehensive
collection of weighted functions:
- **Moving Averages:** `wSma`, `wEma`, `wWma`, `wRma` (Wilder's),
`wHma` (Hull), and `wLSma` (Least Squares / Linear Regression).
- **Oscillators & Ranges:** `wRsi`, `wAtr` (Average True Range),
`wTr` (True Range), and `wR` (High-Low Range).

3. **Volatility Decomposition:** Provides functions to decompose
total variance into distinct components for market analysis.
- **Two-Way Decomposition (`wTotVar`):** Separates variance into
**between-bar** (directional) and **within-bar** (noise)
components.
- **Three-Way Decomposition (`wLRTotVar`):** Decomposes variance
relative to a linear regression into **Trend** (explained by
the LR slope), **Residual** (mean-reversion around the
LR line), and **Within-Bar** (noise) components.
- **Local Volatility (`wLRLocTotStdDev`):** Measures the total
"noise" (within-bar + residual) around the trend line.

4. **Weighted Statistics and Regression:** Provides a robust
function for Weighted Linear Regression (`wLinReg`) and a
full suite of related statistical measures:
- **Between-Bar Stats:** `wBtwVar`, `wBtwStdDev`, `wBtwStdErr`.
- **Residual Stats:** `wResVar`, `wResStdDev`, `wResStdErr`.

5. **Fallback Mechanism:** All functions are designed for reliability.
If the total weight over the lookback period is zero (e.g., in
a no-volume period), the algorithms automatically fall back to
their unweighted, uniform-weight equivalents (e.g., `wSma`
becomes a standard `ta.sma`), preventing errors and ensuring
continuous calculation.

---

**DISCLAIMER**

This library is provided "AS IS" and for informational and
educational purposes only. It does not constitute financial,
investment, or trading advice.

The author assumes no liability for any errors, inaccuracies,
or omissions in the code. Using this library to build
trading indicators or strategies is entirely at your own risk.

As a developer using this library, you are solely responsible
for the rigorous testing, validation, and performance of any
scripts you create based on these functions. The author shall
not be held liable for any financial losses incurred directly
or indirectly from the use of this library or any scripts
derived from it.

wSma(source, weight, length)
  Weighted Simple Moving Average (linear kernel).
  Parameters:
    source (float): series float Data to average.
    weight (float): series float Weight series.
    length (int): series int Look-back length ≥ 1.
  Returns: series float Linear-kernel weighted mean; falls back to
the arithmetic mean if Σweight = 0.

wEma(source, weight, length)
  Weighted EMA (exponential kernel).
  Parameters:
    source (float): series float Data to average.
    weight (float): series float Weight series.
    length (simple int): simple int Look-back length ≥ 1.
  Returns: series float Exponential-kernel weighted mean; falls
back to classic EMA if Σweight = 0.

wWma(source, weight, length)
  Weighted WMA (linear kernel).
  Parameters:
    source (float): series float Data to average.
    weight (float): series float Weight series.
    length (int): series int Look-back length ≥ 1.
  Returns: series float Linear-kernel weighted mean; falls back to
classic WMA if Σweight = 0.

wRma(source, weight, length)
  Weighted RMA (Wilder kernel, α = 1/len).
  Parameters:
    source (float): series float Data to average.
    weight (float): series float Weight series.
    length (simple int): simple int Look-back length ≥ 1.
  Returns: series float Wilder-kernel weighted mean; falls back to
classic RMA if Σweight = 0.

wHma(source, weight, length)
  Weighted HMA (linear kernel).
  Parameters:
    source (float): series float Data to average.
    weight (float): series float Weight series.
    length (int): series int Look-back length ≥ 1.
  Returns: series float Linear-kernel weighted mean; falls back to
classic HMA if Σweight = 0.

wRsi(source, weight, length)
  Weighted Relative Strength Index.
  Parameters:
    source (float): series float Price series.
    weight (float): series float Weight series.
    length (simple int): simple int Look-back length ≥ 1.
  Returns: series float Weighted RSI; uniform if Σw = 0.

wAtr(tr, weight, length)
  Weighted ATR (Average True Range).
Implemented as WRMA on *true range*.
  Parameters:
    tr (float): series float True Range series.
    weight (float): series float Weight series.
    length (simple int): simple int Look-back length ≥ 1.
  Returns: series float Weighted ATR; uniform weights if Σw = 0.

wTr(tr, weight, length)
  Weighted True Range over a window.
  Parameters:
    tr (float): series float True Range series.
    weight (float): series float Weight series.
    length (int): series int Look-back length ≥ 1.
  Returns: series float Weighted mean of TR; uniform if Σw = 0.

wR(r, weight, length)
  Weighted High-Low Range over a window.
  Parameters:
    r (float): series float High-Low per bar.
    weight (float): series float Weight series.
    length (int): series int Look-back length ≥ 1.
  Returns: series float Weighted mean of range; uniform if Σw = 0.

wBtwVar(source, weight, length, biased)
  Weighted Between Variance (biased/unbiased).
  Parameters:
    source (float): series float Data series.
    weight (float): series float Weight series.
    length (int): series int Look-back length ≥ 2.
    biased (bool): series bool true → population (biased); false → sample.
  Returns: [variance, sumW, sumW2]
variance series float The calculated between-bar variance (σ²btw), either biased or unbiased.
sumW series float The sum of weights over the lookback period (Σw).
sumW2 series float The sum of squared weights over the lookback period (Σw²).

wBtwStdDev(source, weight, length, biased)
  Weighted Between Standard Deviation.
  Parameters:
    source (float): series float Data series.
    weight (float): series float Weight series.
    length (int): series int Look-back length ≥ 2.
    biased (bool): series bool true → population (biased); false → sample.
  Returns: series float σbtw uniform if Σw = 0.

wBtwStdErr(source, weight, length, biased)
  Weighted Between Standard Error.
  Parameters:
    source (float): series float Data series.
    weight (float): series float Weight series.
    length (int): series int Look-back length ≥ 2.
    biased (bool): series bool true → population (biased); false → sample.
  Returns: series float √(σ²btw / N_eff) uniform if Σw = 0.

wTotVar(mu, sigma, weight, length, biased)
  Weighted Total Variance (= between-group + within-group).
Useful when each bar represents an aggregate with its own
mean* and pre-estimated σ (e.g., second-level ranges inside a
1-minute bar). Assumes the *weight* series applies to both the
group means and their σ estimates.
  Parameters:
    mu (float): series float Group means (e.g., HL2 of 1-second bars).
    sigma (float): series float Pre-estimated σ of each group (same basis).
    weight (float): series float Weight series (volume, ticks, …).
    length (int): series int Look-back length ≥ 2.
    biased (bool): series bool true → population (biased); false → sample.
  Returns: [varBtw, varWtn, sumW, sumW2]
varBtw series float The between-bar variance component (σ²btw).
varWtn series float The within-bar variance component (σ²wtn).
sumW series float The sum of weights over the lookback period (Σw).
sumW2 series float The sum of squared weights over the lookback period (Σw²).

wTotStdDev(mu, sigma, weight, length, biased)
  Weighted Total Standard Deviation.
  Parameters:
    mu (float): series float Group means (e.g., HL2 of 1-second bars).
    sigma (float): series float Pre-estimated σ of each group (same basis).
    weight (float): series float Weight series (volume, ticks, …).
    length (int): series int Look-back length ≥ 2.
    biased (bool): series bool true → population (biased); false → sample.
  Returns: series float σtot.

wTotStdErr(mu, sigma, weight, length, biased)
  Weighted Total Standard Error.
SE = √( total variance / N_eff ) with the same effective sample
size logic as `wster()`.
  Parameters:
    mu (float): series float Group means (e.g., HL2 of 1-second bars).
    sigma (float): series float Pre-estimated σ of each group (same basis).
    weight (float): series float Weight series (volume, ticks, …).
    length (int): series int Look-back length ≥ 2.
    biased (bool): series bool true → population (biased); false → sample.
  Returns: series float √(σ²tot / N_eff).

wLinReg(source, weight, length)
  Weighted Linear Regression.
  Parameters:
    source (float): series float Data series.
    weight (float): series float Weight series.
    length (int): series int Look-back length ≥ 2.
  Returns: [mid, slope, intercept]
mid series float The estimated value of the regression line at the most recent bar.
slope series float The slope of the regression line.
intercept series float The intercept of the regression line.

wResVar(source, weight, midLine, slope, length, biased)
  Weighted Residual Variance.
linear regression – optionally biased (population) or
unbiased (sample).
  Parameters:
    source (float): series float Data series.
    weight (float): series float Weighting series (volume, etc.).
    midLine (float): series float Regression value at the last bar.
    slope (float): series float Slope per bar.
    length (int): series int Look-back length ≥ 2.
    biased (bool): series bool true → population variance (σ²_P), denominator ≈ N_eff.
false → sample variance (σ²_S), denominator ≈ N_eff - 2.
(Adjusts for 2 degrees of freedom lost to the regression).
  Returns: [variance, sumW, sumW2]
variance series float The calculated residual variance (σ²res), either biased or unbiased.
sumW series float The sum of weights over the lookback period (Σw).
sumW2 series float The sum of squared weights over the lookback period (Σw²).

wResStdDev(source, weight, midLine, slope, length, biased)
  Weighted Residual Standard Deviation.
  Parameters:
    source (float): series float Data series.
    weight (float): series float Weight series.
    midLine (float): series float Regression value at the last bar.
    slope (float): series float Slope per bar.
    length (int): series int Look-back length ≥ 2.
    biased (bool): series bool true → population (biased); false → sample.
  Returns: series float σres; uniform if Σw = 0.

wResStdErr(source, weight, midLine, slope, length, biased)
  Weighted Residual Standard Error.
  Parameters:
    source (float): series float Data series.
    weight (float): series float Weight series.
    midLine (float): series float Regression value at the last bar.
    slope (float): series float Slope per bar.
    length (int): series int Look-back length ≥ 2.
    biased (bool): series bool true → population (biased); false → sample.
  Returns: series float √(σ²res / N_eff); uniform if Σw = 0.

wLRTotVar(mu, sigma, weight, midLine, slope, length, biased)
  Weighted Linear-Regression Total Variance **around the
window’s weighted mean μ**.

σ²_tot = E_w[σ_i²] ⟶ *within-group variance*
+ Var_w[r_i] ⟶ *residual variance*
+ Var_w[ŷ_i] ⟶ *trend variance*

where each bar i in the look-back window contributes
m_i = *mean* (e.g. 1-sec HL2)
σ_i = *sigma* (pre-estimated intrabar σ)
w_i = *weight* (volume, ticks, …)
ŷ_i = b₀ + b₁·x (value of the weighted LR line)
r_i = m_i − ŷ_i (orthogonal residual)
  Parameters:
    mu (float): series float Per-bar mean m_i.
    sigma (float): series float Pre-estimated σ_i of each bar.
    weight (float): series float Weight series w_i (≥ 0).
    midLine (float): series float Regression value at the latest bar (ŷₙ₋₁).
    slope (float): series float Slope b₁ of the regression line.
    length (int): series int Look-back length ≥ 2.
    biased (bool): series bool true → population; false → sample.

  Returns: [varRes, varWtn, varTrd, sumW, sumW2]
varRes series float The residual variance component (σ²res).
varWtn series float The within-bar variance component (σ²wtn).
varTrd series float The trend variance component (σ²trd), explained by the linear regression.
sumW series float The sum of weights over the lookback period (Σw).
sumW2 series float The sum of squared weights over the lookback period (Σw²).

wLRTotStdDev(mu, sigma, weight, midLine, slope, length, biased)
  Weighted Linear-Regression Total Standard Deviation.
  Parameters:
    mu (float): series float Per-bar mean m_i.
    sigma (float): series float Pre-estimated σ_i of each bar.
    weight (float): series float Weight series w_i (≥ 0).
    midLine (float): series float Regression value at the latest bar (ŷₙ₋₁).
    slope (float): series float Slope b₁ of the regression line.
    length (int): series int Look-back length ≥ 2.
    biased (bool): series bool true → population; false → sample.
  Returns: series float √(σ²tot).

wLRTotStdErr(mu, sigma, weight, midLine, slope, length, biased)
  Weighted Linear-Regression Total Standard Error.
SE = √( σ²_tot / N_eff ) with N_eff = Σw² / Σw² (like in wster()).
  Parameters:
    mu (float): series float Per-bar mean m_i.
    sigma (float): series float Pre-estimated σ_i of each bar.
    weight (float): series float Weight series w_i (≥ 0).
    midLine (float): series float Regression value at the latest bar (ŷₙ₋₁).
    slope (float): series float Slope b₁ of the regression line.
    length (int): series int Look-back length ≥ 2.
    biased (bool): series bool true → population; false → sample.
  Returns: series float √((σ²res, σ²wtn, σ²trd) / N_eff).

wLRLocTotStdDev(mu, sigma, weight, midLine, slope, length, biased)
  Weighted Linear-Regression Local Total Standard Deviation.
Measures the total "noise" (within-bar + residual) around the trend.
  Parameters:
    mu (float): series float Per-bar mean m_i.
    sigma (float): series float Pre-estimated σ_i of each bar.
    weight (float): series float Weight series w_i (≥ 0).
    midLine (float): series float Regression value at the latest bar (ŷₙ₋₁).
    slope (float): series float Slope b₁ of the regression line.
    length (int): series int Look-back length ≥ 2.
    biased (bool): series bool true → population; false → sample.
  Returns: series float √(σ²wtn + σ²res).

wLRLocTotStdErr(mu, sigma, weight, midLine, slope, length, biased)
  Weighted Linear-Regression Local Total Standard Error.
  Parameters:
    mu (float): series float Per-bar mean m_i.
    sigma (float): series float Pre-estimated σ_i of each bar.
    weight (float): series float Weight series w_i (≥ 0).
    midLine (float): series float Regression value at the latest bar (ŷₙ₋₁).
    slope (float): series float Slope b₁ of the regression line.
    length (int): series int Look-back length ≥ 2.
    biased (bool): series bool true → population; false → sample.
  Returns: series float √((σ²wtn + σ²res) / N_eff).

wLSma(source, weight, length)
  Weighted Least Square Moving Average.
  Parameters:
    source (float): series float Data series.
    weight (float): series float Weight series.
    length (int): series int Look-back length ≥ 2.
  Returns: series float Least square weighted mean. Falls back
to unweighted regression if Σw = 0.

คำจำกัดสิทธิ์ความรับผิดชอบ

ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมที่ ข้อกำหนดการใช้งาน