Kerbal_Lib

Kerbal Indicators Shared Library - Core utilities for TradingView indicators
This library provides reusable functions for ATR calculations, multi-timeframe analysis,
confluence detection, clustering, regime classification, and more.
atrTolerance(length, multiplier)
Calculate ATR-based tolerance value
Parameters:
length (simple int): The ATR calculation length
multiplier (float): The ATR multiplier for tolerance
Returns: The tolerance value (ATR * multiplier)
pricesWithinATR(price1, price2, atrMultiplier, atrLength)
Check if two prices are within ATR tolerance
Parameters:
price1 (float): First price level
price2 (float): Second price level
atrMultiplier (float): ATR multiplier for tolerance
atrLength (simple int): ATR calculation length
Returns: True if prices are within tolerance
atrPercentile(atrLength, lookback, percentile)
Get ATR percentile for compression detection
Parameters:
atrLength (simple int): The ATR calculation length
lookback (int): Lookback period for percentile
percentile (simple float): The percentile to calculate (0-100)
Returns: ATR percentile value
isHigherTimeframe(htfPeriod)
Check if a given timeframe is higher than current chart timeframe
Parameters:
htfPeriod (string): Higher timeframe period string
Returns: True if HTF is genuinely higher than chart TF
confluenceCount(signal1, signal2, signal3, signal4, signal5)
Count how many boolean signals are true
Parameters:
signal1 (bool): First signal
signal2 (bool): Second signal
signal3 (bool): Third signal
signal4 (bool): Fourth signal
signal5 (bool): Fifth signal
Returns: Count of true signals
confluenceScore(signal1, signal2, signal3, signal4, signal5, weight1, weight2, weight3, weight4, weight5)
Calculate weighted confluence score from multiple signals
Parameters:
signal1 (bool): First signal
signal2 (bool): Second signal
signal3 (bool): Third signal
signal4 (bool): Fourth signal
signal5 (bool): Fifth signal
weight1 (float): Weight for first signal
weight2 (float): Weight for second signal
weight3 (float): Weight for third signal
weight4 (float): Weight for fourth signal
weight5 (float): Weight for fifth signal
Returns: Weighted score (0-100)
clusterPrices(prices, weights, tolerance)
Cluster prices within tolerance using weighted averaging
Parameters:
prices (array<float>): Array of price levels to cluster
weights (array<float>): Array of weights for each price
tolerance (float): Distance tolerance for grouping (typically ATR-based)
Returns: Tuple of [clusterCenters, clusterWeights] arrays
clusterSort(centers, weights, maxClusters)
Sort clusters by weight and return top N
Parameters:
centers (array<float>): Array of cluster center prices
weights (array<float>): Array of cluster weights
maxClusters (int): Maximum number of clusters to return
Returns: Tuple of [sortedCenters, sortedWeights] arrays (descending by weight)
regimeClassify(hurst, thresholdHi, thresholdLo)
Classify market regime based on Hurst exponent
Parameters:
hurst (float): The Hurst exponent value (0-1)
thresholdHi (float): Upper threshold for trending regime
thresholdLo (float): Lower threshold for mean-reverting regime
Returns: Regime string: "TREND", "REVERT", or "MIXED"
volatilityRegime(atrLength, lookback, percentile)
Classify volatility regime based on ATR percentile
Parameters:
atrLength (simple int): ATR calculation length
lookback (int): Lookback period for percentile
percentile (simple float): Compression threshold percentile
Returns: Volatility regime: "COMPRESSED", "NORMAL", or "EXPANDED"
normalizedSlope(current, previous, atrLength)
Calculate normalized slope using ATR
Parameters:
current (float): Current value
previous (float): Previous value
atrLength (simple int): ATR length for normalization
Returns: Normalized slope value
slopeColor(slope, threshold, upColor, downColor, flatColor)
Get color based on slope direction
Parameters:
slope (float): The slope value to evaluate
threshold (float): Threshold for flat detection
upColor (color): Color for rising slope
downColor (color): Color for falling slope
flatColor (color): Color for flat slope
Returns: Color based on slope direction
slopeDirection(current, previous, threshold)
Determine slope direction as string
Parameters:
current (float): Current value
previous (float): Previous value
threshold (float): Threshold for flat detection
Returns: Direction string: "RISING", "FALLING", or "FLAT"
dojiMid(openPrice, closePrice)
Calculate doji midpoint (fair value for doji candles)
Parameters:
openPrice (float): Open price
closePrice (float): Close price
Returns: Midpoint between open and close
vwap(sumPriceVolume, sumVolume)
Calculate VWAP from cumulative values
Parameters:
sumPriceVolume (float): Cumulative sum of price * volume
sumVolume (float): Cumulative sum of volume
Returns: VWAP value
isNewSession(tf)
Check if a new session has started for given timeframe
Parameters:
tf (string): Timeframe string (e.g., "1D", "1W")
Returns: True if new session started
isIntraday()
Check if current timeframe is intraday
Returns: True if intraday timeframe
relativeVolume(length)
Calculate relative volume using median baseline
Parameters:
length (int): Lookback length for median calculation
Returns: Relative volume (current volume / median volume)
volumeCategory(relVol, lowThreshold, highThreshold, extremeThreshold)
Categorize volume level
Parameters:
relVol (float): Relative volume value
lowThreshold (float): Threshold for low volume
highThreshold (float): Threshold for high volume
extremeThreshold (float): Threshold for extreme volume
Returns: Volume category: "EXTREME", "HIGH", "NORMAL", or "LOW"
volumeTrend(shortLength, longLength)
Calculate volume trend (fast median vs slow median)
Parameters:
shortLength (int): Short median length
longLength (int): Long median length
Returns: Volume trend ratio
volumeMultiplier(relVol, lowThreshold, highThreshold, extremeThreshold)
Get volume multiplier for calculations based on relative volume
Parameters:
relVol (float): Relative volume value
lowThreshold (float): Threshold for low volume multiplier
highThreshold (float): Threshold for high volume multiplier
extremeThreshold (float): Threshold for extreme volume multiplier
Returns: Multiplier value (0.5 for low, 1.0 for normal, 1.5 for high, 2.0 for extreme)
Perpustakaan Pine
Dalam semangat TradingView sebenar, penulis telah menerbitkan kod Pine ini sebagai perpustakaan sumber terbuka supaya pengaturcara Pine lain dari komuniti kami boleh menggunakannya semula. Sorakan kepada penulis! Anda boleh menggunakan perpustakaan ini secara peribadi atau dalam penerbitan sumber terbuka lain, tetapi penggunaan semula kod ini dalam penerbitan adalah dikawal selia oleh Peraturan Dalaman.
Penafian
Perpustakaan Pine
Dalam semangat TradingView sebenar, penulis telah menerbitkan kod Pine ini sebagai perpustakaan sumber terbuka supaya pengaturcara Pine lain dari komuniti kami boleh menggunakannya semula. Sorakan kepada penulis! Anda boleh menggunakan perpustakaan ini secara peribadi atau dalam penerbitan sumber terbuka lain, tetapi penggunaan semula kod ini dalam penerbitan adalah dikawal selia oleh Peraturan Dalaman.