EntropyLib

Entropy Library - Composite entropy calculation for oscillators with binary and ternary modes
f_clamp(x, lo, hi)
Clamp value to range
Parameters:
x (float): Value to clamp
lo (float): Lower bound
hi (float): Upper bound
Returns: Clamped value
f_binary_entropy(x, length)
Calculate Shannon binary entropy from a series
Parameters:
x (float): Input series (oscillator, price changes, etc.)
length (int): Lookback period for entropy calculation
Returns: Binary entropy value [0.0, 1.0] where 0=deterministic, 1=maximum uncertainty
f_ternary_entropy(x, length, flatPct)
Calculate ternary entropy (up/down/flat states)
Parameters:
x (float): Input series
length (int): Lookback period for entropy calculation
flatPct (simple float): Percentile threshold for flat zone (e.g., 55.0 means middle 55% is "flat")
Returns: Ternary entropy value [0.0, 1.0]
f_composite_entropy(osc, price, volume, length, osc_weight, price_weight, vol_weight, mode, flatPct)
Calculate composite entropy from oscillator, price, and volume components
Parameters:
osc (float): Primary oscillator series
price (float): Price series (typically close)
volume (float): Volume series
length (int): Lookback period for entropy calculation
osc_weight (float): Weight for oscillator entropy component (e.g., 0.4)
price_weight (float): Weight for price entropy component (e.g., 0.4)
vol_weight (float): Weight for volume entropy component (e.g., 0.2)
mode (string): Entropy mode: "binary" or "ternary"
flatPct (simple float): Percentile for ternary flat zone (only used if mode="ternary", default 55.0)
Returns: Composite entropy [0.0, 1.0]
f_composite_entropy_simple(osc, length, mode, flatPct)
Simplified composite entropy using only oscillator
Parameters:
osc (float): Oscillator series
length (int): Lookback period
mode (string): Entropy mode: "binary" or "ternary"
flatPct (simple float): Percentile for ternary flat zone (default 55.0)
Returns: Composite entropy [0.0, 1.0]
f_composite_entropy_standard(osc, price, length, mode, flatPct)
Standard composite entropy with oscillator + price (50/50 split, no volume)
Parameters:
osc (float): Oscillator series
price (float): Price series
length (int): Lookback period
mode (string): Entropy mode: "binary" or "ternary"
flatPct (simple float): Percentile for ternary flat zone (default 55.0)
Returns: Composite entropy [0.0, 1.0]
f_entropy_weighted_signal(signal, entropy)
Apply entropy weighting to a signal (REOS-style formula)
Parameters:
signal (float): Input signal/oscillator
entropy (float): Entropy value [0.0, 1.0]
Returns: Entropy-weighted signal: sign(signal) * |signal| * (1 - entropy)
f_detect_true_pivots(osc_series, price_series, pivot_left, pivot_right, entropy_len, max_entropy)
Detects true structural pivots by filtering out high-entropy noise
Parameters:
osc_series (float): The oscillator series to evaluate
price_series (float): The underlying price series
pivot_left (int): Bars to the left of the pivot
pivot_right (int): Bars to the right of the pivot
entropy_len (int): Lookback window for entropy calculation (usually 8-14)
max_entropy (float): The maximum allowed joint entropy for a valid pivot (e.g., 0.4)
Returns: [is_true_high, is_true_low, ph_val, pl_val] - True pivot flags and pivot values
f_detect_spring(osc_series, price_series, chaos_len, osc_overbought, osc_oversold, min_chaos)
Detects extreme market compression (chaos) at oscillator extremes
Parameters:
osc_series (float): The oscillator series to evaluate
price_series (float): The underlying price series
chaos_len (int): Ultra-short lookback window (e.g., 3, 4, or 5 bars)
osc_overbought (float): The upper extreme threshold (e.g., 90)
osc_oversold (float): The lower extreme threshold (e.g., 10)
min_chaos (float): The minimum joint entropy required to signal compression (e.g., 0.8)
Returns: [is_spring_compressed, joint_chaos_index] - Spring flag and chaos intensity
f_optimize_thresholds(osc_series, base_ob, base_os, entropy_len, expansion_factor)
Dynamically adjusts OB/OS levels based on background entropy
Parameters:
osc_series (float): The oscillator series
base_ob (float): The baseline overbought level (e.g., 80)
base_os (float): The baseline oversold level (e.g., 20)
entropy_len (int): Lookback for the background entropy evaluation (e.g., 30)
expansion_factor (float): How much the bands can expand/contract (e.g., 10.0 points)
Returns: [dynamic_ob, dynamic_os] - Adaptive overbought and oversold thresholds
Added:
f_detect_lagless_pivots(osc_series, price_series, ob_threshold, os_threshold, entropy_len)
Detects true structural pivots with minimal lag (1 bar) using Joint Entropy
Parameters:
osc_series (float): The oscillator series to evaluate (e.g., REOS or RSI)
price_series (float): The underlying price series for momentum (e.g., close)
ob_threshold (float): Overbought threshold series/float (e.g., bearThresh or 80)
os_threshold (float): Oversold threshold series/float (e.g., bullThresh or 20)
entropy_len (int): Lookback window for Shannon entropy calculation (e.g., 10 to 14)
Returns: [is_pivot_high, is_pivot_low] - Booleans triggering exactly 1 bar after the extreme
Updated:
f_detect_lagless_pivots(osc_series, price_series, ob_threshold, os_threshold, entropy_len)
Detects true structural pivots and calculates entropy concentration for sizing
Parameters:
osc_series (float): The oscillator series to evaluate (e.g., REOS or RSI)
price_series (float): The underlying price series for momentum (e.g., close)
ob_threshold (float): Overbought threshold series/float (e.g., bearThresh or 80)
os_threshold (float): Oversold threshold series/float (e.g., bullThresh or 20)
entropy_len (int): Lookback window for Shannon entropy calculation (e.g., 10 to 14)
Returns: [is_pivot_high, is_pivot_low, concentration] - Pivot flags and order concentration [0.0, 1.0]
Updated:
f_detect_lagless_pivots(osc_series, price_series, ob_threshold, os_threshold, entropy_len, combination_method)
Detects true structural pivots and calculates entropy concentration for sizing
Parameters:
osc_series (float): The oscillator series to evaluate (e.g., REOS or RSI)
price_series (float): The underlying price series for momentum (e.g., close)
ob_threshold (float): Overbought threshold series/float (e.g., bearThresh or 80)
os_threshold (float): Oversold threshold series/float (e.g., bullThresh or 20)
entropy_len (int): Lookback window for Shannon entropy calculation (e.g., 10 to 14)
combination_method (string): Method to combine H_price and H_osc: "multiply", "harmonic", "euclidean"
Returns: [is_pivot_high, is_pivot_low, concentration] - Pivot flags and order concentration [0.0, 1.0]
Updated:
f_detect_lagless_pivots(osc_series, price_series, ob_threshold, os_threshold, entropy_len, combination_method, compression_floor)
Detects true structural pivots and calculates entropy concentration for sizing
Parameters:
osc_series (float): The oscillator series to evaluate (e.g., REOS or RSI)
price_series (float): The underlying price series for momentum (e.g., close)
ob_threshold (float): Overbought threshold series/float (e.g., bearThresh or 80)
os_threshold (float): Oversold threshold series/float (e.g., bullThresh or 20)
entropy_len (int): Lookback window for Shannon entropy calculation (e.g., 10 to 14)
combination_method (string): Method to combine H_price and H_osc: "multiply", "harmonic", "euclidean"
compression_floor (float): Threshold for "rising from compression" detection (0.55-0.70, default 0.60)
Returns: [is_pivot_high, is_pivot_low, concentration] - Pivot flags and order concentration [0.0, 1.0]
Updated:
f_detect_lagless_pivots(osc_series, price_series, ob_threshold, os_threshold, entropy_len, combination_method, floor_pct)
Detects true structural pivots and calculates entropy concentration for sizing
Parameters:
osc_series (float): The oscillator series to evaluate (e.g., REOS or RSI)
price_series (float): The underlying price series for momentum (e.g., close)
ob_threshold (float): Overbought threshold series/float (e.g., bearThresh or 80)
os_threshold (float): Oversold threshold series/float (e.g., bullThresh or 20)
entropy_len (int): Lookback window for Shannon entropy calculation (e.g., 5 to 20)
combination_method (string): Method to combine H_price and H_osc: "osc_only", "mutual_info", "multiply", "harmonic" (REMOVED: "euclidean" - mathematically broken)
floor_pct (simple float): Adaptive percentile for compression floor (70-80, default 75) - replaces fixed compression_floor
Returns: [is_pivot_high, is_pivot_low, concentration] - Pivot flags and order concentration [0.0, 1.0]
Updated:
f_detect_lagless_pivots(osc_series, price_series, ob_threshold, os_threshold, entropy_len, combination_method, floor_pct, entropy_mode, flat_pct)
Detects true structural pivots and calculates entropy concentration for sizing
Parameters:
osc_series (float): The oscillator series to evaluate (e.g., REOS or RSI)
price_series (float): The underlying price series for momentum (e.g., close)
ob_threshold (float): Overbought threshold series/float (e.g., bearThresh or 80)
os_threshold (float): Oversold threshold series/float (e.g., bullThresh or 20)
entropy_len (int): Lookback window for Shannon entropy calculation (e.g., 5 to 20)
combination_method (string): Method to combine H_price and H_osc: "osc_only", "mutual_info", "multiply", "harmonic" (REMOVED: "euclidean" - mathematically broken)
floor_pct (simple float): Adaptive percentile for compression floor (70-80, default 75) - replaces fixed compression_floor
entropy_mode (string): Entropy calculation mode: "binary" (up/down) or "ternary" (up/down/flat)
flat_pct (simple float): Percentile threshold for ternary flat zone (only used if entropy_mode="ternary", default 55.0)
Returns: [is_pivot_high, is_pivot_low, concentration] - Pivot flags and order concentration [0.0, 1.0]
Updated:
f_detect_lagless_pivots(osc_series, price_series, ob_threshold, os_threshold, entropy_len, combination_method, floor_pct, entropy_mode, flat_pct, use_relative_thresholds, trend_lookback, trend_os_pct, trend_ob_pct)
Detects true structural pivots and calculates entropy concentration for sizing
Parameters:
osc_series (float): The oscillator series to evaluate (e.g., REOS or RSI)
price_series (float): The underlying price series for momentum (e.g., close)
ob_threshold (float): Overbought threshold series/float (e.g., bearThresh or 80) - used only if use_relative_thresholds=false
os_threshold (float): Oversold threshold series/float (e.g., bullThresh or 20) - used only if use_relative_thresholds=false
entropy_len (int): Lookback window for Shannon entropy calculation (e.g., 5 to 20)
combination_method (string): Method to combine H_price and H_osc: "osc_only", "mutual_info", "multiply", "harmonic" (REMOVED: "euclidean" - mathematically broken)
floor_pct (simple float): Adaptive percentile for compression floor (70-80, default 75) - replaces fixed compression_floor
entropy_mode (string): Entropy calculation mode: "binary" (up/down) or "ternary" (up/down/flat)
flat_pct (simple float): Percentile threshold for ternary flat zone (only used if entropy_mode="ternary", default 55.0)
use_relative_thresholds (bool): Use percentile-based gates instead of absolute OB/OS (detects trending pullbacks/divergences)
trend_lookback (int): Lookback window for relative percentile calculation (e.g., 60 bars = 4h on 15m)
trend_os_pct (simple float): Lower percentile for relative oversold (e.g., 25 = bottom 25% of recent RSI range)
trend_ob_pct (simple float): Upper percentile for relative overbought (e.g., 75 = top 25% of recent RSI range)
Returns: [is_pivot_high, is_pivot_low, concentration] - Pivot flags and order concentration [0.0, 1.0]
Added:
f_detect_regime(series, regime_len, regime_thresh_pct, regime_lookback)
Detects market regime using long-window Shannon entropy
Parameters:
series (float): Input series (typically close price)
regime_len (int): Long lookback window for regime detection (e.g., 200 bars = 50h on 15m = 2 trading days)
regime_thresh_pct (simple float): Percentile threshold for regime classification (e.g., 70 = top 30% entropy = ranging)
regime_lookback (int): Lookback for percentile calculation of regime threshold (e.g., 500 bars)
Returns: [h_regime, is_ranging, is_trending, is_reversal] - Regime entropy, ranging flag, trending flag, reversal signal
Updated:
f_detect_regime(series, regime_len, norm_lookback, ranging_thresh, trending_thresh)
Detects market regime using long-window Shannon entropy
Parameters:
series (float): Input series (typically close price)
regime_len (int): Long lookback window for regime detection (e.g., 200 bars = 50h on 15m = 2 trading days)
norm_lookback (int): Lookback for min/max normalization (e.g., 100 bars)
ranging_thresh (float): Normalized threshold for ranging regime (e.g., 0.75 = top 25% of local H range)
trending_thresh (float): Normalized threshold for trending regime (e.g., 0.40 = bottom 60% of local H range)
Returns: [h_regime, h_normalized, is_ranging, is_trending, is_reversal] - Raw entropy, normalized entropy, regime flags, reversal signal
Added:
f_silverman_bandwidth(src, lookback)
Silverman's rule of thumb for bandwidth estimation in kernel density estimation
Parameters:
src (float): Source series for bandwidth calculation
lookback (int): Lookback window for standard deviation calculation
Returns: Optimal bandwidth h = 1.06 * σ * n^(-1/5) where σ is sample std dev, n is sample size
f_nis_test(innov, S, dof)
Normalized Innovation Squared (NIS) test for anomaly detection
Parameters:
innov (float): Innovation (measurement residual) from Kalman filter
S (float): Innovation covariance (variance of innovation)
dof (int): Degrees of freedom (typically 1 for scalar, 3 for 3D)
Returns: NIS statistic ~ χ²(dof) under null hypothesis. High NIS = anomaly/changepoint
f_cusum_changepoint(nis_series, dof, threshold)
CUSUM (Cumulative Sum) accumulator for changepoint detection
Parameters:
nis_series (float): NIS statistic series from f_nis_test
dof (int): Degrees of freedom for drift parameter k
threshold (float): CUSUM threshold for changepoint alarm (e.g., 5.0 for quick detection)
Returns: [cusum, is_changepoint] - CUSUM statistic and changepoint flag
f_bocpd_update(innov, S, hazard_rate)
Bayesian Online Changepoint Detection (BOCPD) - simplified log-evidence update
Parameters:
innov (float): Innovation from Kalman filter
S (float): Innovation covariance
hazard_rate (float): Hazard rate (probability of changepoint at each step, e.g., 1/100 = 0.01)
Returns: [run_length, changepoint_prob] - Current run length since last changepoint and changepoint probability
f_kalman_reset_gate(P, P0, is_changepoint)
Kalman filter reset gate - reinitialize covariance after changepoint
Parameters:
P (float): Current state covariance
P0 (float): Initial state covariance (reset value)
is_changepoint (bool): Changepoint detection flag
Returns: P_reset - Covariance after potential reset
f_adaptive_Q_from_innov(innov, S, window)
Adaptive Q estimation from innovation sequence (process noise covariance)
Parameters:
innov (float): Innovation series
S (float): Innovation covariance series
window (int): Lookback window for variance estimation
Returns: Q_adaptive - Estimated process noise covariance
f_adaptive_R_from_outliers(innov, percentile, window)
Adaptive R estimation from innovation outliers (measurement noise covariance)
Parameters:
innov (float): Innovation series
percentile (simple float): Percentile for outlier threshold (e.g., 95.0)
window (int): Lookback window
Returns: R_adaptive - Estimated measurement noise covariance
f_kalman_changepoint_detector(innov, S, P, P0, dof, cusum_threshold)
Integrated Kalman Changepoint Detector (combines NIS + CUSUM + reset)
Parameters:
innov (float): Innovation from Kalman filter
S (float): Innovation covariance
P (float): Current state covariance
P0 (float): Initial state covariance for reset
dof (int): Degrees of freedom (1 for scalar, 3 for 3D)
cusum_threshold (float): CUSUM threshold for changepoint (default 5.0)
Returns: [nis, cusum, is_changepoint, P_reset] - NIS statistic, CUSUM, changepoint flag, reset covariance
f_mahalanobis_3d(x1, x2, x3, mean1, mean2, mean3, std1, std2, std3)
Mahalanobis distance for 3D state vector (multivariate anomaly detection)
Parameters:
x1 (float): First component of state vector
x2 (float): Second component
x3 (float): Third component
mean1 (float): Mean of first component
mean2 (float): Mean of second component
mean3 (float): Mean of third component
std1 (float): Standard deviation of first component
std2 (float): Standard deviation of second component
std3 (float): Standard deviation of third component
Returns: Mahalanobis distance (assuming diagonal covariance)
f_sequential_kalman_update(x_prior, P_prior, y, H, R, Q)
Sequential Kalman update (scalar case)
Parameters:
x_prior (float): Prior state estimate
P_prior (float): Prior error covariance
y (float): Measurement
H (float): Measurement matrix (scalar)
R (float): Measurement noise covariance
Q (float): Process noise covariance
Returns: [x_post, P_post, innov, S, K] - Posterior state, covariance, innovation, innovation covariance, Kalman gain
f_state_sanitize(x, x_min, x_max, x_default)
State sanitize - clamp state to valid range and reset on extreme values
Parameters:
x (float): State estimate
x_min (float): Minimum valid state value
x_max (float): Maximum valid state value
x_default (float): Default value for reset
Returns: x_sanitized - Sanitized state
Updated:
f_detect_lagless_pivots(osc_series, price_series, ob_threshold, os_threshold, entropy_len, combination_method, floor_pct, entropy_mode, flat_pct, use_relative, trend_lookback, trend_os_pct, trend_ob_pct)
Detects lagless pivots using entropy compression with dual-mode gating
Parameters:
osc_series (float): Oscillator series (e.g., RSI, Stochastic)
price_series (float): Price series (typically close)
ob_threshold (float): Absolute overbought threshold (used when use_relative=false)
os_threshold (float): Absolute oversold threshold (used when use_relative=false)
entropy_len (int): Lookback period for entropy calculation
combination_method (string): Method to combine price and oscillator entropy: "osc_only", "mutual_info", "harmonic", "multiply"
floor_pct (simple float): Adaptive floor percentile (e.g., 70.0 = bottom 30% entropy = compression)
entropy_mode (string): Entropy calculation mode: "binary" or "ternary"
flat_pct (simple float): Flat zone percentile for ternary mode (default 55.0)
use_relative (bool): Enable relative percentile-based gates for trending markets
trend_lookback (int): Lookback window for relative percentile calculation
trend_os_pct (simple float): Relative oversold percentile (e.g., 25.0 = bottom 25%)
trend_ob_pct (simple float): Relative overbought percentile (e.g., 75.0 = top 25%)
Returns: [is_pivot_high, is_pivot_low, concentration] - Pivot flags and order concentration
Removed:
f_clamp(x, lo, hi)
Clamp value to range
f_composite_entropy(osc, price, volume, length, osc_weight, price_weight, vol_weight, mode, flatPct)
Calculate composite entropy from oscillator, price, and volume components
f_composite_entropy_simple(osc, length, mode, flatPct)
Simplified composite entropy using only oscillator
f_composite_entropy_standard(osc, price, length, mode, flatPct)
Standard composite entropy with oscillator + price (50/50 split, no volume)
f_entropy_weighted_signal(signal, entropy)
Apply entropy weighting to a signal (REOS-style formula)
f_detect_regime(series, regime_len, norm_lookback, ranging_thresh, trending_thresh)
Detects market regime using long-window Shannon entropy
f_detect_true_pivots(osc_series, price_series, pivot_left, pivot_right, entropy_len, max_entropy)
Detects true structural pivots by filtering out high-entropy noise
f_detect_spring(osc_series, price_series, chaos_len, osc_overbought, osc_oversold, min_chaos)
Detects extreme market compression (chaos) at oscillator extremes
f_optimize_thresholds(osc_series, base_ob, base_os, entropy_len, expansion_factor)
Dynamically adjusts OB/OS levels based on background entropy
f_silverman_bandwidth(src, lookback)
Silverman's rule of thumb for bandwidth estimation in kernel density estimation
f_nis_test(innov, S, dof)
Normalized Innovation Squared (NIS) test for anomaly detection
f_cusum_changepoint(nis_series, dof, threshold)
CUSUM (Cumulative Sum) accumulator for changepoint detection
f_bocpd_update(innov, S, hazard_rate)
Bayesian Online Changepoint Detection (BOCPD) - simplified log-evidence update
f_kalman_reset_gate(P, P0, is_changepoint)
Kalman filter reset gate - reinitialize covariance after changepoint
f_adaptive_Q_from_innov(innov, S, window)
Adaptive Q estimation from innovation sequence (process noise covariance)
f_adaptive_R_from_outliers(innov, percentile, window)
Adaptive R estimation from innovation outliers (measurement noise covariance)
f_kalman_changepoint_detector(innov, S, P, P0, dof, cusum_threshold)
Integrated Kalman Changepoint Detector (combines NIS + CUSUM + reset)
f_mahalanobis_3d(x1, x2, x3, mean1, mean2, mean3, std1, std2, std3)
Mahalanobis distance for 3D state vector (multivariate anomaly detection)
f_sequential_kalman_update(x_prior, P_prior, y, H, R, Q)
Sequential Kalman update (scalar case)
f_state_sanitize(x, x_min, x_max, x_default)
State sanitize - clamp state to valid range and reset on extreme values
Added:
f_clamp(x, lo, hi)
Clamp value to range
Parameters:
x (float): Value to clamp
lo (float): Lower bound
hi (float): Upper bound
Returns: Clamped value
f_composite_entropy(osc, price, volume, length, osc_weight, price_weight, vol_weight, mode, flatPct)
Calculate composite entropy from oscillator, price, and volume components
Parameters:
osc (float): Primary oscillator series
price (float): Price series (typically close)
volume (float): Volume series
length (int): Lookback period for entropy calculation
osc_weight (float): Weight for oscillator entropy component (e.g., 0.4)
price_weight (float): Weight for price entropy component (e.g., 0.4)
vol_weight (float): Weight for volume entropy component (e.g., 0.2)
mode (string): Entropy mode: "binary" or "ternary"
flatPct (simple float): Percentile for ternary flat zone (only used if mode="ternary", default 55.0)
Returns: Composite entropy [0.0, 1.0]
f_composite_entropy_simple(osc, length, mode, flatPct)
Simplified composite entropy using only oscillator
Parameters:
osc (float): Oscillator series
length (int): Lookback period
mode (string): Entropy mode: "binary" or "ternary"
flatPct (simple float): Percentile for ternary flat zone (default 55.0)
Returns: Composite entropy [0.0, 1.0]
f_composite_entropy_standard(osc, price, length, mode, flatPct)
Standard composite entropy with oscillator + price (50/50 split, no volume)
Parameters:
osc (float): Oscillator series
price (float): Price series
length (int): Lookback period
mode (string): Entropy mode: "binary" or "ternary"
flatPct (simple float): Percentile for ternary flat zone (default 55.0)
Returns: Composite entropy [0.0, 1.0]
f_entropy_weighted_signal(signal, entropy)
Apply entropy weighting to a signal (REOS-style formula)
Parameters:
signal (float): Input signal/oscillator
entropy (float): Entropy value [0.0, 1.0]
Returns: Entropy-weighted signal: sign(signal) * |signal| * (1 - entropy)
f_detect_regime(series, regime_len, norm_lookback, ranging_thresh, trending_thresh)
Detects market regime using long-window Shannon entropy
Parameters:
series (float): Input series (typically close price)
regime_len (int): Long lookback window for regime detection (e.g., 200 bars = 50h on 15m = 2 trading days)
norm_lookback (int): Lookback for min/max normalization (e.g., 100 bars)
ranging_thresh (float): Normalized threshold for ranging regime (e.g., 0.75 = top 25% of local H range)
trending_thresh (float): Normalized threshold for trending regime (e.g., 0.40 = bottom 60% of local H range)
Returns: [h_regime, h_normalized, is_ranging, is_trending, is_reversal] - Raw entropy, normalized entropy, regime flags, reversal signal
f_detect_true_pivots(osc_series, price_series, pivot_left, pivot_right, entropy_len, max_entropy)
Detects true structural pivots by filtering out high-entropy noise
Parameters:
osc_series (float): The oscillator series to evaluate
price_series (float): The underlying price series
pivot_left (int): Bars to the left of the pivot
pivot_right (int): Bars to the right of the pivot
entropy_len (int): Lookback window for entropy calculation (usually 8-14)
max_entropy (float): The maximum allowed joint entropy for a valid pivot (e.g., 0.4)
Returns: [is_true_high, is_true_low, ph_val, pl_val] - True pivot flags and pivot values
Updated:
f_detect_lagless_pivots(osc_series, price_series, ob_threshold, os_threshold, entropy_len, combination_method, floor_pct, entropy_mode, flat_pct, use_relative_thresholds, trend_lookback, trend_os_pct, trend_ob_pct, gamma_val, basis_val)
Detects true structural pivots and calculates entropy concentration for sizing
Parameters:
osc_series (float): The oscillator series to evaluate (e.g., REOS or RSI)
price_series (float): The underlying price series for momentum (e.g., close)
ob_threshold (float): Overbought threshold series/float (e.g., bearThresh or 80) - used only if use_relative_thresholds=false
os_threshold (float): Oversold threshold series/float (e.g., bullThresh or 20) - used only if use_relative_thresholds=false
entropy_len (int): Lookback window for Shannon entropy calculation (e.g., 5 to 20)
combination_method (string): Method to combine H_price and H_osc: "osc_only", "mutual_info", "multiply", "harmonic" (REMOVED: "euclidean" - mathematically broken)
floor_pct (simple float): Adaptive percentile for compression floor (70-80, default 75) - replaces fixed compression_floor
entropy_mode (string): Entropy calculation mode: "binary" (up/down) or "ternary" (up/down/flat)
flat_pct (simple float): Percentile threshold for ternary flat zone (only used if entropy_mode="ternary", default 55.0)
use_relative_thresholds (bool): Use percentile-based gates instead of absolute OB/OS (detects trending pullbacks/divergences)
trend_lookback (int): Lookback window for relative percentile calculation (e.g., 60 bars = 4h on 15m)
trend_os_pct (simple float): Lower percentile for relative oversold (e.g., 25 = bottom 25% of recent RSI range)
trend_ob_pct (simple float): Upper percentile for relative overbought (e.g., 75 = top 25% of recent RSI range)
gamma_val (float): Gamma from probability bands (1/sqrt(1+phi^2)) - band compression indicator
basis_val (float): Basis (EMA) from probability bands - for proximity calculation
Returns: [is_pivot_high, is_pivot_low, concentration] - Pivot flags and order concentration [0.0, 1.0]
Added:
f_detect_lagless_pivots_unbounded(unbounded_osc, price_series, entropy_len, combination_method, floor_pct, entropy_mode, flat_pct)
Lagless pivot detection for unbounded oscillators (e.g., proj_sign_bal)
description Wrapper around f_detect_lagless_pivots with z-score normalization for unbounded series
Parameters:
unbounded_osc (float): Unbounded oscillator series (e.g., proj_sign_bal from KNN pool)
price_series (float): Price series for mutual_info combination method
entropy_len (int): Entropy window (5-20)
combination_method (string): "osc_only", "mutual_info", "multiply", "harmonic"
floor_pct (simple float): Adaptive floor percentile (70-80)
entropy_mode (string): "binary" or "ternary"
flat_pct (simple float): Ternary flat zone % (only if entropy_mode="ternary")
Returns: [is_pivot_high, is_pivot_low, concentration, normalized_osc, upper_band, lower_band]
Added:
f_combo_entropy(x, length, flatPct)
Calculate binary AND ternary entropy in single pass (30% faster than separate calls)
Parameters:
x (float): Input series
length (int): Lookback period
flatPct (simple float): Percentile threshold for ternary flat zone
Returns: [binary_entropy, ternary_entropy]
Updated:
f_clamp(x, lo, hi)
Clamp value to range [lo, hi]
Parameters:
x (float): Value to clamp
lo (float): Lower bound
hi (float): Upper bound
Returns: Clamped value
f_binary_entropy(x, length)
Calculate Shannon binary entropy from a series
Parameters:
x (float): Input series (up/down states)
length (simple int): Lookback period for entropy calculation
Returns: Binary entropy value [0.0, 1.0] where 0=deterministic, 1=maximum uncertainty
f_ternary_entropy(x, length, flatPct)
Calculate ternary entropy (up/down/flat states)
Parameters:
x (float): Input series
length (simple int): Lookback period for entropy calculation
flatPct (simple float): Percentile threshold for flat zone (e.g., 55.0 means middle 55% is "flat")
Returns: Ternary entropy value [0.0, 1.0]
Removed:
f_combo_entropy(x, length, flatPct)
Calculate binary AND ternary entropy in single pass (30% faster than separate calls)
f_composite_entropy(osc, price, volume, length, osc_weight, price_weight, vol_weight, mode, flatPct)
Calculate composite entropy from oscillator, price, and volume components
f_composite_entropy_simple(osc, length, mode, flatPct)
Simplified composite entropy using only oscillator
f_composite_entropy_standard(osc, price, length, mode, flatPct)
Standard composite entropy with oscillator + price (50/50 split, no volume)
f_entropy_weighted_signal(signal, entropy)
Apply entropy weighting to a signal (REOS-style formula)
f_detect_regime(series, regime_len, norm_lookback, ranging_thresh, trending_thresh)
Detects market regime using long-window Shannon entropy
f_detect_true_pivots(osc_series, price_series, pivot_left, pivot_right, entropy_len, max_entropy)
Detects true structural pivots by filtering out high-entropy noise
f_detect_spring(osc_series, price_series, chaos_len, osc_overbought, osc_oversold, min_chaos)
Detects extreme market compression (chaos) at oscillator extremes
f_optimize_thresholds(osc_series, base_ob, base_os, entropy_len, expansion_factor)
Dynamically adjusts OB/OS levels based on background entropy
f_detect_lagless_pivots(osc_series, price_series, ob_threshold, os_threshold, entropy_len, combination_method, floor_pct, entropy_mode, flat_pct, use_relative_thresholds, trend_lookback, trend_os_pct, trend_ob_pct, gamma_val, basis_val)
Detects true structural pivots and calculates entropy concentration for sizing
f_silverman_bandwidth(src, lookback)
Silverman's rule of thumb for bandwidth estimation in kernel density estimation
f_nis_test(innov, S, dof)
Normalized Innovation Squared (NIS) test for anomaly detection
f_cusum_changepoint(nis_series, dof, threshold)
CUSUM (Cumulative Sum) accumulator for changepoint detection
f_bocpd_update(innov, S, hazard_rate)
Bayesian Online Changepoint Detection (BOCPD) - simplified log-evidence update
f_kalman_reset_gate(P, P0, is_changepoint)
Kalman filter reset gate - reinitialize covariance after changepoint
f_adaptive_Q_from_innov(innov, S, window)
Adaptive Q estimation from innovation sequence (process noise covariance)
f_adaptive_R_from_outliers(innov, percentile, window)
Adaptive R estimation from innovation outliers (measurement noise covariance)
f_kalman_changepoint_detector(innov, S, P, P0, dof, cusum_threshold)
Integrated Kalman Changepoint Detector (combines NIS + CUSUM + reset)
f_sequential_kalman_update(x_prior, P_prior, y, H, R, Q)
Sequential Kalman update (scalar case)
f_state_sanitize(x, x_min, x_max, x_default)
State sanitize - clamp state to valid range and reset on extreme values
f_detect_lagless_pivots_unbounded(unbounded_osc, price_series, entropy_len, combination_method, floor_pct, entropy_mode, flat_pct)
Lagless pivot detection for unbounded oscillators (e.g., proj_sign_bal)
description Wrapper around f_detect_lagless_pivots with z-score normalization for unbounded series
f_compute_csd_oscillator(osc, sigma, ewma_alpha, shrinkage)
Compute Cauchy-Schwarz Divergence for oscillator state space
description Streaming EWMA-based CSD using [osc, velocity, acceleration] state.
Uses tri-packed covariance and KalmanEngineLib.f_mahalanobis_3d().
Detects regime transitions via Mahalanobis distance from streaming centroid.
Perpustakaan pine
Dengan semangat TradingView yang sesungguhnya, penulis telah menerbitkan kode Pine ini sebagai pustaka sumber terbuka agar programmer Pine lain dari komunitas kami dapat menggunakannya kembali. Salut untuk penulis! Anda dapat menggunakan pustaka ini secara pribadi atau dalam publikasi sumber terbuka lainnya, tetapi penggunaan kembali kode ini dalam publikasi diatur oleh Tata Tertib.
Pernyataan Penyangkalan
Perpustakaan pine
Dengan semangat TradingView yang sesungguhnya, penulis telah menerbitkan kode Pine ini sebagai pustaka sumber terbuka agar programmer Pine lain dari komunitas kami dapat menggunakannya kembali. Salut untuk penulis! Anda dapat menggunakan pustaka ini secara pribadi atau dalam publikasi sumber terbuka lainnya, tetapi penggunaan kembali kode ini dalam publikasi diatur oleh Tata Tertib.