PINE LIBRARY
Aggiornato

StatMetrics

75
Library "StatMetrics"
A utility library for common statistical indicators and ratios used in technical analysis.
Includes Z-Score, correlation, PLF, SRI, Sharpe, Sortino, Omega ratios, and normalization tools.

zscore(src, len)
  Calculates the Z-score of a series
  Parameters:
    src (float): The input price or series (e.g., close)
    len (simple int): The lookback period for mean and standard deviation
  Returns: Z-score: number of standard deviations the input is from the mean

corr(x, y, len)
  Computes Pearson correlation coefficient between two series
  Parameters:
    x (float): First series
    y (float): Second series
    len (simple int): Lookback period
  Returns: Correlation coefficient between -1 and 1

plf(src, longLen, shortLen, smoothLen)
  Calculates the Price Lag Factor (PLF) as the difference between long and short Z-scores, normalized and smoothed
  Parameters:
    src (float): Source series (e.g., close)
    longLen (simple int): Long Z-score period
    shortLen (simple int): Short Z-score period
    smoothLen (simple int): Hull MA smoothing length
  Returns: Smoothed and normalized PLF oscillator

sri(signal, len)
  Computes the Statistical Reliability Index (SRI) based on trend persistence
  Parameters:
    signal (float): A price or signal series (e.g., smoothed PLF)
    len (simple int): Lookback period for smoothing and deviation
  Returns: Normalized trend reliability score

sharpe(src, len)
  Calculates the Sharpe Ratio over a period
  Parameters:
    src (float): Price series (e.g., close)
    len (simple int): Lookback period
  Returns: Sharpe ratio value

sortino(src, len)
  Calculates the Sortino Ratio over a period, using only downside volatility
  Parameters:
    src (float): Price series
    len (simple int): Lookback period
  Returns: Sortino ratio value

omega(src, len)
  Calculates the Omega Ratio as the ratio of upside to downside return area
  Parameters:
    src (float): Price series
    len (simple int): Lookback period
  Returns: Omega ratio value

beta(asset, benchmark, len)
  Calculates beta coefficient of asset vs benchmark using rolling covariance
  Parameters:
    asset (float): Series of the asset (e.g., close)
    benchmark (float): Series of the benchmark (e.g., SPX close)
    len (simple int): Lookback window
  Returns: Beta value (slope of linear regression)

alpha(asset, benchmark, len)
  Calculates rolling alpha of an asset relative to a benchmark
  Parameters:
    asset (float): Series of the asset (e.g., close)
    benchmark (float): Series of the benchmark (e.g., SPX close)
    len (simple int): Lookback window
  Returns: Alpha value (excess return not explained by Beta exposure)

skew(x, len)
  Computes skewness of a return series
  Parameters:
    x (float): Input series (e.g., returns)
    len (simple int): Lookback period
  Returns: Skewness value

kurtosis(x, len)
  Computes kurtosis of a return series
  Parameters:
    x (float): Input series (e.g., returns)
    len (simple int): Lookback period
  Returns: Kurtosis value

cv(x, len)
  Calculates Coefficient of Variation
  Parameters:
    x (float): Input series (e.g., returns or prices)
    len (simple int): Lookback period
  Returns: CV value

autocorr(x, len)
  Calculates autocorrelation with 1-lag
  Parameters:
    x (float): Series to test
    len (simple int): Lookback window
  Returns: Autocorrelation at lag 1

stderr(x, len)
  Calculates rolling standard error of a series
  Parameters:
    x (float): Input series
    len (simple int): Lookback window
  Returns: Standard error (std dev / sqrt(n))

info_ratio(asset, benchmark, len)
  Calculates the Information Ratio
  Parameters:
    asset (float): Asset price series
    benchmark (float): Benchmark price series
    len (simple int): Lookback period
  Returns: Information ratio (alpha / tracking error)

tracking_error(asset, benchmark, len)
  Measures deviation from benchmark (Tracking Error)
  Parameters:
    asset (float): Asset return series
    benchmark (float): Benchmark return series
    len (simple int): Lookback window
  Returns: Tracking error value

max_drawdown(x, len)
  Computes maximum drawdown over a rolling window
  Parameters:
    x (float): Price series
    len (simple int): Lookback window
  Returns: Rolling max drawdown percentage (as a negative value)

zscore_signal(z, ob, os)
  Converts Z-score into a 3-level signal
  Parameters:
    z (float): Z-score series
    ob (float): Overbought threshold
    os (float): Oversold threshold
  Returns: -1, 0, or 1 depending on signal state

r_squared(x, y, len)
  Calculates rolling R-squared (coefficient of determination)
  Parameters:
    x (float): Asset returns
    y (float): Benchmark returns
    len (simple int): Lookback window
  Returns: R-squared value (0 to 1)

entropy(x, len)
  Approximates Shannon entropy using log returns
  Parameters:
    x (float): Price series
    len (simple int): Lookback period
  Returns: Approximate entropy

zreversal(z)
  Detects Z-score reversals to the mean
  Parameters:
    z (float): Z-score series
  Returns: +1 on upward reversal, -1 on downward

momentum_rank(x, len)
  Calculates relative momentum strength
  Parameters:
    x (float): Price series
    len (simple int): Lookback window
  Returns: Proportion of lookback where current price is higher

normalize(x, len)
  Normalizes a series to a 0–1 range over a period
  Parameters:
    x (float): The input series
    len (simple int): Lookback period
  Returns: Normalized value between 0 and 1

composite_score(score1, score2, score3)
  Combines multiple normalized scores into a composite score
  Parameters:
    score1 (float)
    score2 (float)
    score3 (float)
  Returns: Average composite score
Note di rilascio
v2

Added:
hurst(x, len)
  Estimates the Hurst Exponent (simplified)
  Parameters:
    x (float): Price or return series
    len (simple int): Lookback window
  Returns: Hurst exponent approximation

mad(x, len)
  Computes Mean Absolute Deviation
  Parameters:
    x (float): Input series (e.g., price or return)
    len (simple int): Lookback period
  Returns: MAD value

cdf_score(x, len)
  Approximates quantile rank of the current value in history
  Parameters:
    x (float): Input series
    len (simple int): Lookback window
  Returns: CDF-like score between 0 and 1

cvar(x, len, alpha)
  Approximates Conditional Value at Risk (CVaR)
  Parameters:
    x (float): Return series
    len (simple int): Lookback period
    alpha (float): Tail percentile (e.g., 0.05 for 5% worst-case)
  Returns: CVaR value (average of worst returns)

seasonality_index(x, period)
  Computes a basic seasonality index
  Parameters:
    x (float): Return series
    period (simple int): Seasonal period (e.g., 24 for hourly, 7 for daily)
  Returns: Value indicating typical seasonal effect

Declinazione di responsabilità

Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.