PINE LIBRARY
已更新 StatMetrics

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
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
發行說明
v2Added:
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
發行說明
v3Added:
ulcer_index(x, len)
Computes Ulcer Index (only downside deviations)
Parameters:
x (float): Price series
len (simple int): Lookback period
Returns: Ulcer Index value
tail_ratio(x, len)
Computes Tail Ratio (95th percentile / 5th percentile)
Parameters:
x (float): Return series (e.g., log returns)
len (simple int): Lookback period
Returns: Tail Ratio
regression_slope(x, len)
Computes rolling linear regression slope
Parameters:
x (float): Series (e.g., returns or price)
len (simple int): Lookback period
Returns: Slope value
cagr(x, len)
Computes rolling CAGR
Parameters:
x (float): Price series
len (simple int): Lookback window
Returns: CAGR over window (not annualized)
mar_ratio(x, len)
Computes MAR Ratio (CAGR / Max Drawdown)
Parameters:
x (float): Price series
len (simple int): Lookback window
Returns: MAR ratio
Pine腳本庫
秉持 TradingView 一貫的共享精神,作者將此 Pine 程式碼發佈為開源庫,讓社群中的其他 Pine 程式設計師能夠重複使用。向作者致敬!您可以在私人專案或其他開源發佈中使用此庫,但在公開發佈中重複使用該程式碼需遵守社群規範。
免責聲明
這些資訊和出版物並不意味著也不構成TradingView提供或認可的金融、投資、交易或其他類型的意見或建議。請在使用條款閱讀更多資訊。
Pine腳本庫
秉持 TradingView 一貫的共享精神,作者將此 Pine 程式碼發佈為開源庫,讓社群中的其他 Pine 程式設計師能夠重複使用。向作者致敬!您可以在私人專案或其他開源發佈中使用此庫,但在公開發佈中重複使用該程式碼需遵守社群規範。
免責聲明
這些資訊和出版物並不意味著也不構成TradingView提供或認可的金融、投資、交易或其他類型的意見或建議。請在使用條款閱讀更多資訊。