SpatialIndexYou can start using this now by inserthing this at the top of your indicator/strategy/library.
import ArunaReborn/SpatialIndex/1 as SI
Overview
SpatialIndex is a high-performance Pine Script library that implements price-bucketed spatial indexing for efficient proximity queries on large datasets. Instead of scanning through hundreds or thousands of items linearly (O(n)), this library provides O(k) bucket lookup where k is typically just a handful of buckets, dramatically improving performance for price-based filtering operations.
This library works with any data type through index-based references, making it universally applicable for support/resistance levels, pivot points, order zones, pattern detection points, Fair Value Gaps, and any other price-based data that needs frequent proximity queries.
Why This Library Exists
The Problem
When building advanced technical indicators that track large numbers of price levels (support/resistance zones, pivot points, order blocks, etc.), you often need to answer questions like:
- *"Which levels are within 5% of the current price?"*
- *"What zones overlap with this price range?"*
- *"Are there any significant levels near my entry point?"*
The naive approach is to loop through every single item and check its price. For 500 levels across multiple timeframes, this means 500 comparisons every bar . On instruments with thousands of historical bars, this quickly becomes a performance bottleneck that can cause scripts to time out or lag.
The Solution
SpatialIndex solves this by organizing items into price buckets —like filing cabinets organized by price range. When you query for items near $50,000, the library only looks in the relevant buckets (e.g., $49,000-$51,000 range), ignoring all other price regions entirely.
Performance Example:
- Linear scan: Check 500 items = 500 comparisons per query
- Spatial index: Check 3-5 buckets with ~10 items each = 30-50 comparisons per query
- Result: 10-16x faster queries
Key Features
Core Capabilities
- ✅ Generic Design : Works with any data type via index references
- ✅ Multiple Index Strategies : Fixed bucket size or ATR-based dynamic sizing
- ✅ Range Support : Index items that span price ranges (zones, gaps, channels)
- ✅ Efficient Queries : O(k) bucket lookup instead of O(n) linear scan
- ✅ Multiple Query Types : Proximity percentage, fixed range, exact price with tolerance
- ✅ Dynamic Updates : Add, remove, update items in O(1) time
- ✅ Batch Operations : Efficient bulk removal and reindexing
- ✅ Query Caching : Optional caching for repeated queries within same bar
- ✅ Statistics & Debugging : Built-in stats and diagnostic functions
### Advanced Features
- ATR-Based Bucketing : Automatically adjusts bucket sizes based on volatility
- Multi-Bucket Spanning : Items that span ranges are indexed in all overlapping buckets
- Reindexing Support : Handles array removals with automatic index shifting
- Cache Management : Configurable query caching with automatic invalidation
- Empty Bucket Cleanup : Automatically removes empty buckets to minimize memory
How It Works
The Bucketing Concept
Think of price space as divided into discrete buckets, like a histogram:
```
Price Range: $98-$100 $100-$102 $102-$104 $104-$106 $106-$108
Bucket Key: 49 50 51 52 53
Items:
```
When you query for items near $103:
1. Calculate which buckets overlap the $101.50-$104.50 range (keys 50, 51, 52)
2. Return items from only those buckets:
3. Never check items in buckets 49 or 53
Bucket Size Selection
Fixed Size Mode:
```pine
var SI.SpatialBucket index = SI.newSpatialBucket(2.0) // $2 per bucket
```
- Good for: Instruments with stable price ranges
- Example: For stocks trading at $100, 2.0 = 2% increments
ATR-Based Mode:
```pine
float atr = ta.atr(14)
var SI.SpatialBucket index = SI.newSpatialBucketATR(1.0, atr) // 1x ATR per bucket
SI.updateATR(index, atr) // Update each bar
```
- Good for: Instruments with varying volatility
- Adapts automatically to market conditions
- 1.0 multiplier = one bucket spans one ATR unit
Optimal Bucket Size:
The library includes a helper function to calculate optimal size:
```pine
float optimalSize = SI.calculateOptimalBucketSize(close, 5.0) // For 5% proximity queries
```
This ensures queries span approximately 3 buckets for optimal performance.
Index-Based Architecture
The library doesn't store your actual data—it only stores indices that point to your external arrays:
```pine
// Your data
var array levels = array.new()
var array types = array.new()
var array ages = array.new()
// Your index
var SI.SpatialBucket index = SI.newSpatialBucket(2.0)
// Add a level
array.push(levels, 50000.0)
array.push(types, "support")
array.push(ages, 0)
SI.add(index, array.size(levels) - 1, 50000.0) // Store index 0
// Query near current price
SI.QueryResult result = SI.queryProximity(index, close, 5.0)
for idx in result.indices
float level = array.get(levels, idx)
string type = array.get(types, idx)
// Work with your actual data
```
This design means:
- ✅ Works with any data structure you define
- ✅ No data duplication
- ✅ Minimal memory footprint
- ✅ Full control over your data
---
Usage Guide
Basic Setup
```pine
// Import library
import username/SpatialIndex/1 as SI
// Create index
var SI.SpatialBucket index = SI.newSpatialBucket(2.0)
// Your data arrays
var array supportLevels = array.new()
var array touchCounts = array.new()
```
Adding Items
Single Price Point:
```pine
// Add a support level at $50,000
array.push(supportLevels, 50000.0)
array.push(touchCounts, 1)
int levelIdx = array.size(supportLevels) - 1
SI.add(index, levelIdx, 50000.0)
```
Price Range (Zones/Gaps):
```pine
// Add a resistance zone from $51,000 to $52,000
array.push(zoneBottoms, 51000.0)
array.push(zoneTops, 52000.0)
int zoneIdx = array.size(zoneBottoms) - 1
SI.addRange(index, zoneIdx, 51000.0, 52000.0) // Indexed in all overlapping buckets
```
Querying Items
Proximity Query (Percentage):
```pine
// Find all levels within 5% of current price
SI.QueryResult result = SI.queryProximity(index, close, 5.0)
if array.size(result.indices) > 0
for idx in result.indices
float level = array.get(supportLevels, idx)
// Process nearby level
```
Fixed Range Query:
```pine
// Find all items between $49,000 and $51,000
SI.QueryResult result = SI.queryRange(index, 49000.0, 51000.0)
```
Exact Price with Tolerance:
```pine
// Find items at exactly $50,000 +/- $100
SI.QueryResult result = SI.queryAt(index, 50000.0, 100.0)
```
Removing Items
Safe Removal Pattern:
```pine
SI.QueryResult result = SI.queryProximity(index, close, 5.0)
if array.size(result.indices) > 0
// IMPORTANT: Sort descending to safely remove from arrays
array sorted = SI.sortIndicesDescending(result)
for idx in sorted
// Remove from index
SI.remove(index, idx)
// Remove from your data arrays
array.remove(supportLevels, idx)
array.remove(touchCounts, idx)
// Reindex to maintain consistency
SI.reindexAfterRemoval(index, idx)
```
Batch Removal (More Efficient):
```pine
// Collect indices to remove
array toRemove = array.new()
for i = 0 to array.size(supportLevels) - 1
if array.get(touchCounts, i) > 10 // Remove old levels
array.push(toRemove, i)
// Remove in descending order from data arrays
array sorted = array.copy(toRemove)
array.sort(sorted, order.descending)
for idx in sorted
SI.remove(index, idx)
array.remove(supportLevels, idx)
array.remove(touchCounts, idx)
// Batch reindex (much faster than individual reindexing)
SI.reindexAfterBatchRemoval(index, toRemove)
```
Updating Items
```pine
// Update a level's price (e.g., after refinement)
float newPrice = 50100.0
SI.update(index, levelIdx, newPrice)
array.set(supportLevels, levelIdx, newPrice)
// Update a zone's range
SI.updateRange(index, zoneIdx, 51000.0, 52500.0)
array.set(zoneBottoms, zoneIdx, 51000.0)
array.set(zoneTops, zoneIdx, 52500.0)
```
Query Caching
For repeated queries within the same bar:
```pine
// Create cache (persistent)
var SI.CachedQuery cache = SI.newCachedQuery()
// Cached query (returns cached result if parameters match)
SI.QueryResult result = SI.queryProximityCached(
index,
cache,
close,
5.0, // proximity%
1 // cache duration in bars
)
// Invalidate cache when index changes significantly
if bigChangeDetected
SI.invalidateCache(cache)
```
---
Practical Examples
Example 1: Support/Resistance Finder
```pine
//@version=6
indicator("S/R with Spatial Index", overlay=true)
import username/SpatialIndex/1 as SI
// Data storage
var array levels = array.new()
var array types = array.new() // "support" or "resistance"
var array touches = array.new()
var array ages = array.new()
// Spatial index
var SI.SpatialBucket index = SI.newSpatialBucket(close * 0.02) // 2% buckets
// Detect pivots
bool isPivotHigh = ta.pivothigh(high, 5, 5)
bool isPivotLow = ta.pivotlow(low, 5, 5)
// Add new levels
if isPivotHigh
array.push(levels, high )
array.push(types, "resistance")
array.push(touches, 1)
array.push(ages, 0)
SI.add(index, array.size(levels) - 1, high )
if isPivotLow
array.push(levels, low )
array.push(types, "support")
array.push(touches, 1)
array.push(ages, 0)
SI.add(index, array.size(levels) - 1, low )
// Find nearby levels (fast!)
SI.QueryResult nearby = SI.queryProximity(index, close, 3.0) // Within 3%
// Process nearby levels
for idx in nearby.indices
float level = array.get(levels, idx)
string type = array.get(types, idx)
// Check for touch
if type == "support" and low <= level and low > level
array.set(touches, idx, array.get(touches, idx) + 1)
else if type == "resistance" and high >= level and high < level
array.set(touches, idx, array.get(touches, idx) + 1)
// Age and cleanup old levels
for i = array.size(ages) - 1 to 0
array.set(ages, i, array.get(ages, i) + 1)
// Remove levels older than 500 bars or with 5+ touches
if array.get(ages, i) > 500 or array.get(touches, i) >= 5
SI.remove(index, i)
array.remove(levels, i)
array.remove(types, i)
array.remove(touches, i)
array.remove(ages, i)
SI.reindexAfterRemoval(index, i)
// Visualization
for idx in nearby.indices
line.new(bar_index, array.get(levels, idx), bar_index + 10, array.get(levels, idx),
color=array.get(types, idx) == "support" ? color.green : color.red)
```
Example 2: Multi-Timeframe Zone Detector
```pine
//@version=6
indicator("MTF Zones", overlay=true)
import username/SpatialIndex/1 as SI
// Store zones from multiple timeframes
var array zoneBottoms = array.new()
var array zoneTops = array.new()
var array zoneTimeframes = array.new()
// ATR-based spatial index for adaptive bucketing
var SI.SpatialBucket index = SI.newSpatialBucketATR(1.0, ta.atr(14))
SI.updateATR(index, ta.atr(14)) // Update bucket size with volatility
// Request higher timeframe data
= request.security(syminfo.tickerid, "240", )
// Detect HTF zones
if not na(htf_high) and not na(htf_low)
float zoneTop = htf_high
float zoneBottom = htf_low * 0.995 // 0.5% zone thickness
// Check if zone already exists nearby
SI.QueryResult existing = SI.queryRange(index, zoneBottom, zoneTop)
if array.size(existing.indices) == 0 // No overlapping zones
// Add new zone
array.push(zoneBottoms, zoneBottom)
array.push(zoneTops, zoneTop)
array.push(zoneTimeframes, "4H")
int idx = array.size(zoneBottoms) - 1
SI.addRange(index, idx, zoneBottom, zoneTop)
// Query zones near current price
SI.QueryResult nearbyZones = SI.queryProximity(index, close, 2.0) // Within 2%
// Highlight nearby zones
for idx in nearbyZones.indices
box.new(bar_index - 50, array.get(zoneBottoms, idx),
bar_index, array.get(zoneTops, idx),
bgcolor=color.new(color.blue, 90))
```
### Example 3: Performance Comparison
```pine
//@version=6
indicator("Spatial Index Performance Test")
import username/SpatialIndex/1 as SI
// Generate 500 random levels
var array levels = array.new()
var SI.SpatialBucket index = SI.newSpatialBucket(close * 0.02)
if bar_index == 0
for i = 0 to 499
float randomLevel = close * (0.9 + math.random() * 0.2) // +/- 10%
array.push(levels, randomLevel)
SI.add(index, i, randomLevel)
// Method 1: Linear scan (naive approach)
int linearCount = 0
float proximityPct = 5.0
float lowBand = close * (1 - proximityPct/100)
float highBand = close * (1 + proximityPct/100)
for i = 0 to array.size(levels) - 1
float level = array.get(levels, i)
if level >= lowBand and level <= highBand
linearCount += 1
// Method 2: Spatial index query
SI.QueryResult result = SI.queryProximity(index, close, proximityPct)
int spatialCount = array.size(result.indices)
// Compare performance
plot(result.queryCount, "Items Examined (Spatial)", color=color.green)
plot(linearCount, "Items Examined (Linear)", color=color.red)
plot(spatialCount, "Results Found", color=color.blue)
// Spatial index typically examines 10-50 items vs 500 for linear scan!
```
API Reference Summary
Initialization
- `newSpatialBucket(bucketSize)` - Fixed bucket size
- `newSpatialBucketATR(atrMultiplier, atrValue)` - ATR-based buckets
- `updateATR(sb, newATR)` - Update ATR for dynamic sizing
Adding Items
- `add(sb, itemIndex, price)` - Add item at single price point
- `addRange(sb, itemIndex, priceBottom, priceTop)` - Add item spanning range
Querying
- `queryProximity(sb, refPrice, proximityPercent)` - Query by percentage
- `queryRange(sb, priceBottom, priceTop)` - Query fixed range
- `queryAt(sb, price, tolerance)` - Query exact price with tolerance
- `queryProximityCached(sb, cache, refPrice, pct, duration)` - Cached query
Removing & Updating
- `remove(sb, itemIndex)` - Remove item
- `update(sb, itemIndex, newPrice)` - Update item price
- `updateRange(sb, itemIndex, newBottom, newTop)` - Update item range
- `reindexAfterRemoval(sb, removedIndex)` - Reindex after single removal
- `reindexAfterBatchRemoval(sb, removedIndices)` - Batch reindex
- `clear(sb)` - Remove all items
Utilities
- `size(sb)` - Get item count
- `isEmpty(sb)` - Check if empty
- `contains(sb, itemIndex)` - Check if item exists
- `getStats(sb)` - Get debug statistics string
- `calculateOptimalBucketSize(price, pct)` - Calculate optimal bucket size
- `sortIndicesDescending(result)` - Sort for safe removal
- `sortIndicesAscending(result)` - Sort ascending
Performance Characteristics
Time Complexity
- Add : O(1) for single point, O(m) for range spanning m buckets
- Remove : O(1) lookup + O(b) bucket cleanup where b = buckets item spans
- Query : O(k) where k = buckets in range (typically 3-5) vs O(n) linear scan
- Update : O(1) removal + O(1) addition = O(1) total
Space Complexity
- Memory per item : ~8 bytes for index reference + map overhead
- Bucket overhead : Proportional to price range coverage
- Typical usage : For 500 items with 50 active buckets ≈ 4-8KB total
Scalability
- ✅ 100 items : ~5-10x faster than linear scan
- ✅ 500 items : ~10-15x faster
- ✅ 1000+ items : ~15-20x faster
- ⚠️ Performance degrades if bucket size is too small (too many buckets)
- ⚠️ Performance degrades if bucket size is too large (too many items per bucket)
Best Practices
Bucket Size Selection
1. Start with 2-5% of asset price for percentage-based queries
2. Use ATR-based mode for volatile assets or multi-symbol scripts
3. Test bucket size using `calculateOptimalBucketSize()` function
4. Monitor with `getStats()` to ensure reasonable bucket count
Memory Management
1. Clear old items regularly to prevent unbounded growth
2. Use age tracking to remove stale data
3. Set maximum item limits based on your needs
4. Batch removals are more efficient than individual removals
Query Optimization
1. Use caching for repeated queries within same bar
2. Invalidate cache when index changes significantly
3. Sort results descending before removal iteration
4. Batch operations when possible (reindexing, removal)
Data Consistency
1. Always reindex after removal to maintain index alignment
2. Remove from arrays in descending order to avoid index shifting issues
3. Use batch reindex for multiple simultaneous removals
4. Keep external arrays and index in sync at all times
Limitations & Caveats
Known Limitations
- Not suitable for exact price matching : Use tolerance with `queryAt()`
- Bucket size affects performance : Too small = many buckets, too large = many items per bucket
- Memory usage : Scales with price range coverage and item count
- Reindexing overhead : Removing items mid-array requires index shifting
When NOT to Use
- ❌ Datasets with < 50 items (linear scan is simpler)
- ❌ Items that change price every bar (constant reindexing overhead)
- ❌ When you need ALL items every time (no benefit over arrays)
- ❌ Exact price level matching without tolerance (use maps instead)
When TO Use
- ✅ Large datasets (100+ items) with occasional queries
- ✅ Proximity-based filtering (% of price, ATR-based ranges)
- ✅ Multi-timeframe level tracking
- ✅ Zone/range overlap detection
- ✅ Price-based spatial filtering
---
Technical Details
Bucketing Algorithm
Items are assigned to buckets using integer division:
```
bucketKey = floor((price - basePrice) / bucketSize)
```
For ATR-based mode:
```
effectiveBucketSize = atrValue × atrMultiplier
bucketKey = floor((price - basePrice) / effectiveBucketSize)
```
Range Indexing
Items spanning price ranges are indexed in all overlapping buckets to ensure accurate range queries. The midpoint bucket is designated as the "primary bucket" for removal operations.
Index Consistency
The library maintains two maps:
1. `buckets`: Maps bucket keys → IntArray wrappers containing item indices
2. `itemToBucket`: Maps item indices → primary bucket key (for O(1) removal)
This dual-mapping ensures both fast queries and fast removal while maintaining consistency.
Implementation Note: Pine Script doesn't allow nested collections (map containing arrays directly), so the library uses an `IntArray` wrapper type to hold arrays within the map structure. This is an internal implementation detail that doesn't affect usage.
---
Version History
Version 1.0
- Initial release
Credits & License
License : Mozilla Public License 2.0 (TradingView default)
Library Type : Open-source educational resource
This library is designed as a public domain utility for the Pine Script community. As per TradingView library rules, this code can be freely reused by other authors. If you use this library in your scripts, please provide appropriate credit as required by House Rules.
Summary
SpatialIndex is a specialized library that solves a specific problem: fast proximity queries on large price-based datasets . If you're building indicators that track hundreds of levels, zones, or price points and need to frequently filter by proximity to current price, this library can provide 10-20x performance improvements over naive linear scanning.
The index-based architecture makes it universally applicable to any data type, and the ATR-based bucketing ensures it adapts to market conditions automatically. Combined with query caching and batch operations, it provides a complete solution for spatial data management in Pine Script.
Use this library when speed matters and your dataset is large.
Arrays
MLMatrixLibOverview
MLMatrixLib is a comprehensive Pine Script v6 library implementing machine learning algorithms using native matrix operations. This library provides traders and developers with a toolkit of statistical and ML methods for building quantitative trading systems, performing data analysis, and creating adaptive indicators.
How It Works
The library leverages Pine Script's native matrix type to perform efficient linear algebra operations. Each algorithm is implemented from first principles, using matrix decomposition, iterative optimization, and statistical estimation techniques. All functions are designed for numerical stability with careful handling of edge cases.
---
Library Contents (34 Sections)
Section 1: Utility Functions & Matrix Operations
Core building blocks including:
• identity(n) - Creates n×n identity matrix
• diagonal(values) - Creates diagonal matrix from array
• ones(rows, cols) / zeros(rows, cols) - Matrix constructors
• frobeniusNorm(m) / l1Norm(m) - Matrix norm calculations
• hadamard(m1, m2) - Element-wise multiplication
• columnMeans(m) / rowMeans(m) - Statistical aggregations
• standardize(m) - Z-score normalization (zero mean, unit variance)
• minMaxNormalize(m) - Scale values to range
• fitStandardScaler(m) / fitMinMaxScaler(m) - Reusable scaler parameters
• addBiasColumn(m) - Prepend column of ones for regression
• arrayMedian(arr) / arrayPercentile(arr, p) - Array statistics
Section 2: Activation Functions
Numerically stable implementations:
• sigmoid(x) / sigmoidMatrix(m) - Logistic function with overflow protection
• tanhActivation(x) / tanhMatrix(m) - Hyperbolic tangent
• relu(x) / reluMatrix(m) - Rectified Linear Unit
• leakyRelu(x, alpha) - Leaky ReLU with configurable slope
• elu(x, alpha) - Exponential Linear Unit
• Derivatives for backpropagation: sigmoidDerivative, tanhDerivative, reluDerivative
Section 3: Linear Regression (OLS)
Ordinary Least Squares implementation using the normal equation (X'X)⁻¹X'y:
• fitLinearRegression(X, y) - Fits model, returns coefficients, R², standard error
• fitSimpleLinearRegression(x, y) - Single-variable regression
• predictLinear(model, X) - Generate predictions
• predictionInterval(model, X, confidence) - Confidence intervals using t-distribution
• Model type stores: coefficients, R-squared, residuals, standard error
Section 4: Weighted Linear Regression
Generalized least squares with observation weights:
• fitWeightedLinearRegression(X, y, weights) - Solves (X'WX)⁻¹X'Wy
• Useful for downweighting outliers or emphasizing recent data
Section 5: Polynomial Regression
Fits polynomials of arbitrary degree:
• fitPolynomialRegression(x, y, degree) - Constructs Vandermonde matrix
• predictPolynomial(model, x) - Evaluate polynomial at points
Section 6: Ridge Regression (L2 Regularization)
Adds penalty term λ||β||² to prevent overfitting:
• fitRidgeRegression(X, y, lambda) - Solves (X'X + λI)⁻¹X'y
• Lambda parameter controls regularization strength
Section 7: LASSO Regression (L1 Regularization)
Coordinate descent algorithm for sparse solutions:
• fitLassoRegression(X, y, lambda, maxIter, tolerance) - Iterative soft-thresholding
• Produces sparse coefficients by driving some to exactly zero
• softThreshold(x, lambda) - Core shrinkage operator
Section 8: Elastic Net (L1 + L2 Regularization)
Combines LASSO and Ridge penalties:
• fitElasticNet(X, y, lambda, alpha, maxIter, tolerance)
• Alpha balances L1 vs L2: alpha=1 is LASSO, alpha=0 is Ridge
Section 9: Huber Robust Regression
Iteratively Reweighted Least Squares (IRLS) for outlier resistance:
• fitHuberRegression(X, y, delta, maxIter, tolerance)
• Delta parameter defines transition between L1 and L2 loss
• Downweights observations with large residuals
Section 10: Quantile Regression
Estimates conditional quantiles using linear programming approximation:
• fitQuantileRegression(X, y, tau, maxIter, tolerance)
• Tau specifies quantile (0.5 = median, 0.25 = lower quartile, etc.)
Section 11: Logistic Regression (Binary Classification)
Gradient descent optimization of cross-entropy loss:
• fitLogisticRegression(X, y, learningRate, maxIter, tolerance)
• predictProbability(model, X) - Returns probabilities
• predictClass(model, X, threshold) - Returns binary predictions
Section 12: Linear SVM (Support Vector Machine)
Sub-gradient descent with hinge loss:
• fitLinearSVM(X, y, C, learningRate, maxIter)
• C parameter controls regularization (higher = harder margin)
• predictSVM(model, X) - Returns class predictions
Section 13: Recursive Least Squares (RLS)
Online learning with exponential forgetting:
• createRLSState(nFeatures, lambda, delta) - Initialize state
• updateRLS(state, x, y) - Update with new observation
• Lambda is forgetting factor (0.95-0.99 typical)
• Useful for adaptive indicators that update incrementally
Section 14: Covariance and Correlation
Matrix statistics:
• covarianceMatrix(m) - Sample covariance
• correlationMatrix(m) - Pearson correlations
• pearsonCorrelation(x, y) - Single correlation coefficient
• spearmanCorrelation(x, y) - Rank-based correlation
Section 15: Principal Component Analysis (PCA)
Dimensionality reduction via eigendecomposition:
• fitPCA(X, nComponents) - Power iteration method
• transformPCA(X, model) - Project data onto principal components
• Returns components, explained variance, and mean
Section 16: K-Means Clustering
Lloyd's algorithm with k-means++ initialization:
• fitKMeans(X, k, maxIter, tolerance) - Cluster data points
• predictCluster(model, X) - Assign new points to clusters
• withinClusterVariance(model) - Measure cluster compactness
Section 17: Gaussian Mixture Model (GMM)
Expectation-Maximization algorithm:
• fitGMM(X, k, maxIter, tolerance) - Soft clustering with probabilities
• predictProbaGMM(model, X) - Returns membership probabilities
• Models data as mixture of Gaussian distributions
Section 18: Kalman Filter
Linear state estimation:
• createKalman1D(processNoise, measurementNoise, ...) - 1D filter
• createKalman2D(processNoise, measurementNoise) - Position + velocity tracking
• kalmanStep(state, measurement) - Predict-update cycle
• Optimal filtering for noisy measurements
Section 19: K-Nearest Neighbors (KNN)
Instance-based learning:
• fitKNN(X, y) - Store training data
• predictKNN(model, X, k) - Classify by majority vote
• predictKNNRegression(model, X, k) - Average of k neighbors
• predictKNNWeighted(model, X, k) - Distance-weighted voting
Section 20: Neural Network (Feedforward)
Multi-layer perceptron:
• createNeuralNetwork(architecture) - Define layer sizes
• trainNeuralNetwork(nn, X, y, learningRate, epochs) - Backpropagation
• predictNN(nn, X) - Forward pass
• Supports configurable hidden layers
Section 21: Naive Bayes Classifier
Gaussian Naive Bayes:
• fitNaiveBayes(X, y) - Estimate class-conditional distributions
• predictNaiveBayes(model, X) - Maximum a posteriori classification
• Assumes feature independence given class
Section 22: Anomaly Detection
Statistical outlier detection:
• fitAnomalyDetector(X, contamination) - Mahalanobis distance-based
• detectAnomalies(model, X) - Returns anomaly scores
• isAnomaly(model, X, threshold) - Binary classification
Section 23: Dynamic Time Warping (DTW)
Time series similarity:
• dtw(series1, series2) - Compute DTW distance
• Handles sequences of different lengths
• Useful for pattern matching
Section 24: Markov Chain / Regime Detection
Discrete state transitions:
• fitMarkovChain(states, nStates) - Estimate transition matrix
• predictNextState(transitionMatrix, currentState) - Most likely next state
• stationaryDistribution(transitionMatrix) - Long-run probabilities
Section 25: Hidden Markov Model (Simple)
Baum-Welch algorithm:
• fitHMM(observations, nStates, maxIter) - EM training
• viterbi(model, observations) - Most likely state sequence
• Useful for regime detection
Section 26: Exponential Smoothing & Holt-Winters
Time series smoothing:
• exponentialSmooth(data, alpha) - Simple exponential smoothing
• holtWinters(data, alpha, beta, gamma, seasonLength) - Triple smoothing
• Captures trend and seasonality
Section 27: Entropy and Information Theory
Information measures:
• entropy(probabilities) - Shannon entropy in bits
• conditionalEntropy(jointProbs, marginalProbs) - H(X|Y)
• mutualInformation(probsX, probsY, jointProbs) - I(X;Y)
• kldivergence(p, q) - Kullback-Leibler divergence
Section 28: Hurst Exponent
Long-range dependence measure:
• hurstExponent(data) - R/S analysis
• H < 0.5: mean-reverting, H = 0.5: random walk, H > 0.5: trending
Section 29: Change Detection (CUSUM)
Cumulative sum control chart:
• cusumChangeDetection(data, threshold, drift) - Detect regime changes
• cusumOnline(value, prevCusumPos, prevCusumNeg, target, drift) - Streaming version
Section 30: Autocorrelation
Serial dependence analysis:
• autocorrelation(data, maxLag) - ACF for all lags
• partialAutocorrelation(data, maxLag) - PACF via Durbin-Levinson
• Useful for time series model identification
Section 31: Ensemble Methods
Model combination:
• baggingPredict(models, X) - Average predictions
• votingClassify(models, X) - Majority vote
• Improves robustness through aggregation
Section 32: Model Evaluation Metrics
Performance assessment:
• mse(actual, predicted) / rmse / mae / mape - Regression metrics
• accuracy(actual, predicted) - Classification accuracy
• precision / recall / f1Score - Binary classification metrics
• confusionMatrix(actual, predicted, nClasses) - Multi-class evaluation
• rSquared(actual, predicted) / adjustedRSquared - Goodness of fit
Section 33: Cross-Validation
Model validation:
• trainTestSplit(X, y, trainRatio) - Random split
• Foundation for walk-forward validation
Section 34: Trading Convenience Functions
Trading-specific utilities:
• priceMatrix(length) - OHLC data as matrix
• logReturns(length) - Log return series
• rollingSlope(src, length) - Linear trend strength
• kalmanFilter(src, processNoise, measurementNoise) - Filtered price
• kalmanFilter2D(src, ...) - Price with velocity estimate
• adaptiveMA(src, sensitivity) - Kalman-based adaptive moving average
• volAdjMomentum(src, length) - Volatility-normalized momentum
• detectSRLevels(length, nLevels) - K-means based S/R detection
• buildFeatures(src, lengths) - Multi-timeframe feature construction
• technicalFeatures(length) - Standard indicator feature set (RSI, MACD, BB, ATR, etc.)
• lagFeatures(src, lags) - Time-lagged features
• sharpeRatio(returns) - Risk-adjusted return measure
• sortinoRatio(returns) - Downside risk-adjusted return
• maxDrawdown(equity) - Maximum peak-to-trough decline
• calmarRatio(returns, equity) - Return/drawdown ratio
• kellyCriterion(winRate, avgWin, avgLoss) - Optimal position sizing
• fractionalKelly(...) - Conservative Kelly sizing
• rollingBeta(assetReturns, benchmarkReturns) - Market exposure
• fractalDimension(data) - Market complexity measure
---
Usage Example
```
import YourUsername/MLMatrixLib/1 as ml
// Create feature matrix
matrix X = ml.priceMatrix(50)
X := ml.standardize(X)
// Fit linear regression
ml.LinearRegressionModel model = ml.fitLinearRegression(X, y)
float prediction = ml.predictLinear(model, X_new)
// Kalman filter for smoothing
float smoothedPrice = ml.kalmanFilter(close, 0.01, 1.0)
// Detect support/resistance levels
array levels = ml.detectSRLevels(100, 3)
// K-means clustering for regime detection
ml.KMeansModel km = ml.fitKMeans(features, 3)
int cluster = ml.predictCluster(km, newFeature)
```
---
Technical Notes
• All matrix operations use Pine Script's native matrix type
• Numerical stability ensured through:
- Clamping exponential arguments to prevent overflow
- Division by zero protection with epsilon thresholds
- Iterative algorithms with convergence tolerance
• Designed for bar-by-bar execution in Pine Script's event-driven model
• Compatible with Pine Script v6
---
Disclaimer
This library provides mathematical tools for quantitative analysis. It does not constitute financial advice. Past performance of any algorithm does not guarantee future results. Users are responsible for validating models on their specific use cases and understanding the limitations of each method.
ZigZag forceLibrary "ZigZag"
method lastPivot(this)
Retrieves the last `Pivot` object's reference from a `ZigZag` object's `pivots`
array if it contains at least one element, or `na` if the array is empty.
Callable as a method or a function.
Namespace types: ZigZag
Parameters:
this (ZigZag) : (series ZigZag) The `ZigZag` object's reference.
Returns: (Pivot) The reference of the last `Pivot` instance in the `ZigZag` object's
`pivots` array, or `na` if the array is empty.
method update(this)
Updates a `ZigZag` object's pivot information, volume data, lines, and
labels when it detects new pivot points.
NOTE: This function requires a single execution on each bar for accurate
calculations.
Callable as a method or a function.
Namespace types: ZigZag
Parameters:
this (ZigZag) : (series ZigZag) The `ZigZag` object's reference.
Returns: (bool) `true` if the function detects a new pivot point and updates the
`ZigZag` object's data, `false` otherwise.
newInstance(settings)
Creates a new `ZigZag` instance with optional settings.
Parameters:
settings (Settings) : (series Settings) Optional. A `Settings` object's reference for the new
`ZigZag` instance's `settings` field. If `na`, the `ZigZag` instance
uses a new `Settings` object with default properties. The default is `na`.
Returns: (ZigZag) A new `ZigZag` object's reference.
Settings
A structure for objects that store calculation and display properties for `ZigZag` instances.
Fields:
devThreshold (series float) : The minimum percentage deviation from a previous pivot point required to change the Zig Zag's direction.
depth (series int) : The number of bars required for pivot point detection.
lineColor (series color) : The color of each line in the Zig Zag drawing.
extendLast (series bool) : Specifies whether the Zig Zag drawing includes a line connecting the most recent pivot point to the latest bar's `close`.
displayReversalPrice (series bool) : Specifies whether the Zig Zag drawing shows pivot prices in its labels.
displayCumulativeVolume (series bool) : Specifies whether the Zig Zag drawing shows the cumulative volume between pivot points in its labels.
displayReversalPriceChange (series bool) : Specifies whether the Zig Zag drawing shows the reversal amount from the previous pivot point in each label.
differencePriceMode (series string) : The reversal amount display mode. Possible values: `"Absolute"` for price change or `"Percent"` for percentage change.
draw (series bool) : Specifies whether the Zig Zag drawing displays its lines and labels.
allowZigZagOnOneBar (series bool) : Specifies whether the Zig Zag calculation can register a pivot high *and* pivot low on the same bar.
Pivot
A structure for objects that store chart point references, drawing references, and volume information for `ZigZag` instances.
Fields:
ln (series line) : References a `line` object that connects the coordinates from the `start` and `end` chart points.
lb (series label) : References a `label` object that displays pivot data at the `end` chart point's coordinates.
isHigh (series bool) : Specifies whether the pivot at the `end` chart point's coordinates is a pivot high.
vol (series float) : The cumulative volume across the bars between the `start` and `end` chart points.
start (chart.point) : References a `chart.point` object containing the coordinates of the previous pivot point.
end (chart.point) : References a `chart.point` object containing the coordinates of the current pivot point.
ZigZag
A structure for objects that maintain Zig Zag drawing settings, pivots, and cumulative volume data.
Fields:
settings (Settings) : References a `Settings` object that specifies the Zig Zag drawing's calculation and display properties.
pivots (array) : References an array of `Pivot` objects that store pivot point, drawing, and volume information.
sumVol (series float) : The cumulative volume across bars covered by the latest `Pivot` object's line segment.
extend (Pivot) : References a `Pivot` object that projects a line from the last confirmed pivot point to the current bar's `close`.
News2024H1Library "News2024H1" - This contains news events from 2024 H1 News Events
f_loadNewsRows()
f_loadExcSevByTypeId()
f_loadExcTagByTypeId()
NewsTypesLibrary "NewsTypes" Provides the based library for the news system
f_hhmmToMs(_hhmm)
Parameters:
_hhmm (int)
f_addNews(_d, _hhmm, _tid, _dArr, _tArr, _idArr)
Parameters:
_d (string)
_hhmm (int)
_tid (int)
_dArr (array)
_tArr (array)
_idArr (array)
f_addNewsMs(_d, _ms, _tid, _dArr, _tArr, _idArr)
Parameters:
_d (string)
_ms (int)
_tid (int)
_dArr (array)
_tArr (array)
_idArr (array)
f_loadTypeSevByTypeId()
PineML_v6Library "PineML_v6"
ML Library for lightweight strategies. Implements k-NN with matrix storage.
method new_model(k, history, features)
Създава нов модел
Namespace types: series int, simple int, input int, const int
Parameters:
k (int) : Брой съседи (напр. 5)
history (int) : Дълбочина на паметта (напр. 1000 бара)
features (int) : Брой променливи, които ще следим
method train(model, feature_array, label)
Добавя нови данни към паметта на модела
Namespace types: KNN_Model
Parameters:
model (KNN_Model) : Инстанцията на модела
feature_array (array) : Масив с текущите стойности на индикаторите
label (float) : Резултатът (класът), свързан с тези данни
method predict(model, query_features)
Изчислява прогноза на база текущите данни
Namespace types: KNN_Model
Parameters:
model (KNN_Model)
query_features (array)
KNN_Model
Fields:
k_neighbors (series int)
max_history (series int)
features (matrix)
labels (array)
feature_count (series int)
LECAPS_BONCAP_LibraryLibrary "LECAPS_BONCAP_Library"
getInstrumentCount()
getTicker(index)
Parameters:
index (int)
getTickerShort(index)
Parameters:
index (int)
getMaturityPrice(index)
Parameters:
index (int)
getMaturityTimestamp(index)
Parameters:
index (int)
getMaturityYear(index)
Parameters:
index (int)
getMaturityMonth(index)
Parameters:
index (int)
getMaturityDay(index)
Parameters:
index (int)
isBoncap(index)
Parameters:
index (int)
isLecap(index)
Parameters:
index (int)
getInstrumentType(index)
Parameters:
index (int)
getDolarFuturesCount()
getDolarFuturesTicker(index)
Parameters:
index (int)
getDolarFuturesShort(index)
Parameters:
index (int)
getDolarFuturesExpiry(index)
Parameters:
index (int)
getDaysToMaturity(index)
Parameters:
index (int)
getDataSummary(index)
Parameters:
index (int)
arraysLibrary "arrays"
Supplementary array methods.
method delete(arr, index)
remove int object from array of integers at specific index
Namespace types: array
Parameters:
arr (array) : int array
index (int) : index at which int object need to be removed
Returns: void
method delete(arr, index)
remove float object from array of float at specific index
Namespace types: array
Parameters:
arr (array) : float array
index (int) : index at which float object need to be removed
Returns: float
method delete(arr, index)
remove bool object from array of bool at specific index
Namespace types: array
Parameters:
arr (array) : bool array
index (int) : index at which bool object need to be removed
Returns: bool
method delete(arr, index)
remove string object from array of string at specific index
Namespace types: array
Parameters:
arr (array) : string array
index (int) : index at which string object need to be removed
Returns: string
method delete(arr, index)
remove color object from array of color at specific index
Namespace types: array
Parameters:
arr (array) : color array
index (int) : index at which color object need to be removed
Returns: color
method delete(arr, index)
remove chart.point object from array of chart.point at specific index
Namespace types: array
Parameters:
arr (array) : chart.point array
index (int) : index at which chart.point object need to be removed
Returns: void
method delete(arr, index)
remove line object from array of lines at specific index and deletes the line
Namespace types: array
Parameters:
arr (array) : line array
index (int) : index at which line object need to be removed and deleted
Returns: void
method delete(arr, index)
remove label object from array of labels at specific index and deletes the label
Namespace types: array
Parameters:
arr (array) : label array
index (int) : index at which label object need to be removed and deleted
Returns: void
method delete(arr, index)
remove box object from array of boxes at specific index and deletes the box
Namespace types: array
Parameters:
arr (array) : box array
index (int) : index at which box object need to be removed and deleted
Returns: void
method delete(arr, index)
remove table object from array of tables at specific index and deletes the table
Namespace types: array
Parameters:
arr (array) : table array
index (int) : index at which table object need to be removed and deleted
Returns: void
method delete(arr, index)
remove linefill object from array of linefills at specific index and deletes the linefill
Namespace types: array
Parameters:
arr (array) : linefill array
index (int) : index at which linefill object need to be removed and deleted
Returns: void
method delete(arr, index)
remove polyline object from array of polylines at specific index and deletes the polyline
Namespace types: array
Parameters:
arr (array) : polyline array
index (int) : index at which polyline object need to be removed and deleted
Returns: void
method popr(arr)
remove last int object from array
Namespace types: array
Parameters:
arr (array) : int array
Returns: int
method popr(arr)
remove last float object from array
Namespace types: array
Parameters:
arr (array) : float array
Returns: float
method popr(arr)
remove last bool object from array
Namespace types: array
Parameters:
arr (array) : bool array
Returns: bool
method popr(arr)
remove last string object from array
Namespace types: array
Parameters:
arr (array) : string array
Returns: string
method popr(arr)
remove last color object from array
Namespace types: array
Parameters:
arr (array) : color array
Returns: color
method popr(arr)
remove last chart.point object from array
Namespace types: array
Parameters:
arr (array) : chart.point array
Returns: void
method popr(arr)
remove and delete last line object from array
Namespace types: array
Parameters:
arr (array) : line array
Returns: void
method popr(arr)
remove and delete last label object from array
Namespace types: array
Parameters:
arr (array) : label array
Returns: void
method popr(arr)
remove and delete last box object from array
Namespace types: array
Parameters:
arr (array) : box array
Returns: void
method popr(arr)
remove and delete last table object from array
Namespace types: array
Parameters:
arr (array) : table array
Returns: void
method popr(arr)
remove and delete last linefill object from array
Namespace types: array
Parameters:
arr (array) : linefill array
Returns: void
method popr(arr)
remove and delete last polyline object from array
Namespace types: array
Parameters:
arr (array) : polyline array
Returns: void
method shiftr(arr)
remove first int object from array
Namespace types: array
Parameters:
arr (array) : int array
Returns: int
method shiftr(arr)
remove first float object from array
Namespace types: array
Parameters:
arr (array) : float array
Returns: float
method shiftr(arr)
remove first bool object from array
Namespace types: array
Parameters:
arr (array) : bool array
Returns: bool
method shiftr(arr)
remove first string object from array
Namespace types: array
Parameters:
arr (array) : string array
Returns: string
method shiftr(arr)
remove first color object from array
Namespace types: array
Parameters:
arr (array) : color array
Returns: color
method shiftr(arr)
remove first chart.point object from array
Namespace types: array
Parameters:
arr (array) : chart.point array
Returns: void
method shiftr(arr)
remove and delete first line object from array
Namespace types: array
Parameters:
arr (array) : line array
Returns: void
method shiftr(arr)
remove and delete first label object from array
Namespace types: array
Parameters:
arr (array) : label array
Returns: void
method shiftr(arr)
remove and delete first box object from array
Namespace types: array
Parameters:
arr (array) : box array
Returns: void
method shiftr(arr)
remove and delete first table object from array
Namespace types: array
Parameters:
arr (array) : table array
Returns: void
method shiftr(arr)
remove and delete first linefill object from array
Namespace types: array
Parameters:
arr (array) : linefill array
Returns: void
method shiftr(arr)
remove and delete first polyline object from array
Namespace types: array
Parameters:
arr (array) : polyline array
Returns: void
method push(arr, val, maxItems)
add int to the end of an array with max items cap. Objects are removed from start to maintain max items cap
Namespace types: array
Parameters:
arr (array) : int array
val (int) : int object to be pushed
maxItems (int) : max number of items array can hold
Returns: int
method push(arr, val, maxItems)
add float to the end of an array with max items cap. Objects are removed from start to maintain max items cap
Namespace types: array
Parameters:
arr (array) : float array
val (float) : float object to be pushed
maxItems (int) : max number of items array can hold
Returns: float
method push(arr, val, maxItems)
add bool to the end of an array with max items cap. Objects are removed from start to maintain max items cap
Namespace types: array
Parameters:
arr (array) : bool array
val (bool) : bool object to be pushed
maxItems (int) : max number of items array can hold
Returns: bool
method push(arr, val, maxItems)
add string to the end of an array with max items cap. Objects are removed from start to maintain max items cap
Namespace types: array
Parameters:
arr (array) : string array
val (string) : string object to be pushed
maxItems (int) : max number of items array can hold
Returns: string
method push(arr, val, maxItems)
add color to the end of an array with max items cap. Objects are removed from start to maintain max items cap
Namespace types: array
Parameters:
arr (array) : color array
val (color) : color object to be pushed
maxItems (int) : max number of items array can hold
Returns: color
method push(arr, val, maxItems)
add chart.point to the end of an array with max items cap. Objects are removed and deleted from start to maintain max items cap
Namespace types: array
Parameters:
arr (array) : chart.point array
val (chart.point) : chart.point object to be pushed
maxItems (int) : max number of items array can hold
Returns: chart.point
method push(arr, val, maxItems)
add line to the end of an array with max items cap. Objects are removed and deleted from start to maintain max items cap
Namespace types: array
Parameters:
arr (array) : line array
val (line) : line object to be pushed
maxItems (int) : max number of items array can hold
Returns: line
method push(arr, val, maxItems)
add label to the end of an array with max items cap. Objects are removed and deleted from start to maintain max items cap
Namespace types: array
Parameters:
arr (array) : label array
val (label) : label object to be pushed
maxItems (int) : max number of items array can hold
Returns: label
method push(arr, val, maxItems)
add box to the end of an array with max items cap. Objects are removed and deleted from start to maintain max items cap
Namespace types: array
Parameters:
arr (array) : box array
val (box) : box object to be pushed
maxItems (int) : max number of items array can hold
Returns: box
method push(arr, val, maxItems)
add table to the end of an array with max items cap. Objects are removed and deleted from start to maintain max items cap
Namespace types: array
Parameters:
arr (array) : table array
val (table) : table object to be pushed
maxItems (int) : max number of items array can hold
Returns: table
method push(arr, val, maxItems)
add linefill to the end of an array with max items cap. Objects are removed and deleted from start to maintain max items cap
Namespace types: array
Parameters:
arr (array) : linefill array
val (linefill) : linefill object to be pushed
maxItems (int) : max number of items array can hold
Returns: linefill
method push(arr, val, maxItems)
add polyline to the end of an array with max items cap. Objects are removed and deleted from start to maintain max items cap
Namespace types: array
Parameters:
arr (array) : polyline array
val (polyline) : polyline object to be pushed
maxItems (int) : max number of items array can hold
Returns: polyline
method unshift(arr, val, maxItems)
add int to the beginning of an array with max items cap. Objects are removed from end to maintain max items cap
Namespace types: array
Parameters:
arr (array) : int array
val (int) : int object to be unshift
maxItems (int) : max number of items array can hold
Returns: int
method unshift(arr, val, maxItems)
add float to the beginning of an array with max items cap. Objects are removed from end to maintain max items cap
Namespace types: array
Parameters:
arr (array) : float array
val (float) : float object to be unshift
maxItems (int) : max number of items array can hold
Returns: float
method unshift(arr, val, maxItems)
add bool to the beginning of an array with max items cap. Objects are removed from end to maintain max items cap
Namespace types: array
Parameters:
arr (array) : bool array
val (bool) : bool object to be unshift
maxItems (int) : max number of items array can hold
Returns: bool
method unshift(arr, val, maxItems)
add string to the beginning of an array with max items cap. Objects are removed from end to maintain max items cap
Namespace types: array
Parameters:
arr (array) : string array
val (string) : string object to be unshift
maxItems (int) : max number of items array can hold
Returns: string
method unshift(arr, val, maxItems)
add color to the beginning of an array with max items cap. Objects are removed from end to maintain max items cap
Namespace types: array
Parameters:
arr (array) : color array
val (color) : color object to be unshift
maxItems (int) : max number of items array can hold
Returns: color
method unshift(arr, val, maxItems)
add chart.point to the beginning of an array with max items cap. Objects are removed and deleted from end to maintain max items cap
Namespace types: array
Parameters:
arr (array) : chart.point array
val (chart.point) : chart.point object to be unshift
maxItems (int) : max number of items array can hold
Returns: chart.point
method unshift(arr, val, maxItems)
add line to the beginning of an array with max items cap. Objects are removed and deleted from end to maintain max items cap
Namespace types: array
Parameters:
arr (array) : line array
val (line) : line object to be unshift
maxItems (int) : max number of items array can hold
Returns: line
method unshift(arr, val, maxItems)
add label to the beginning of an array with max items cap. Objects are removed and deleted from end to maintain max items cap
Namespace types: array
Parameters:
arr (array) : label array
val (label) : label object to be unshift
maxItems (int) : max number of items array can hold
Returns: label
method unshift(arr, val, maxItems)
add box to the beginning of an array with max items cap. Objects are removed and deleted from end to maintain max items cap
Namespace types: array
Parameters:
arr (array) : box array
val (box) : box object to be unshift
maxItems (int) : max number of items array can hold
Returns: box
method unshift(arr, val, maxItems)
add table to the beginning of an array with max items cap. Objects are removed and deleted from end to maintain max items cap
Namespace types: array
Parameters:
arr (array) : table array
val (table) : table object to be unshift
maxItems (int) : max number of items array can hold
Returns: table
method unshift(arr, val, maxItems)
add linefill to the beginning of an array with max items cap. Objects are removed and deleted from end to maintain max items cap
Namespace types: array
Parameters:
arr (array) : linefill array
val (linefill) : linefill object to be unshift
maxItems (int) : max number of items array can hold
Returns: linefill
method unshift(arr, val, maxItems)
add polyline to the beginning of an array with max items cap. Objects are removed and deleted from end to maintain max items cap
Namespace types: array
Parameters:
arr (array) : polyline array
val (polyline) : polyline object to be unshift
maxItems (int) : max number of items array can hold
Returns: polyline
method isEmpty(arr)
checks if an int array is either null or empty
Namespace types: array
Parameters:
arr (array) : int array
Returns: bool
method isEmpty(arr)
checks if a float array is either null or empty
Namespace types: array
Parameters:
arr (array) : float array
Returns: bool
method isEmpty(arr)
checks if a string array is either null or empty
Namespace types: array
Parameters:
arr (array) : string array
Returns: bool
method isEmpty(arr)
checks if a bool array is either null or empty
Namespace types: array
Parameters:
arr (array) : bool array
Returns: bool
method isEmpty(arr)
checks if a color array is either null or empty
Namespace types: array
Parameters:
arr (array) : color array
Returns: bool
method isEmpty(arr)
checks if a chart.point array is either null or empty
Namespace types: array
Parameters:
arr (array) : chart.point array
Returns: bool
method isEmpty(arr)
checks if a line array is either null or empty
Namespace types: array
Parameters:
arr (array) : line array
Returns: bool
method isEmpty(arr)
checks if a label array is either null or empty
Namespace types: array
Parameters:
arr (array) : label array
Returns: bool
method isEmpty(arr)
checks if a box array is either null or empty
Namespace types: array
Parameters:
arr (array) : box array
Returns: bool
method isEmpty(arr)
checks if a linefill array is either null or empty
Namespace types: array
Parameters:
arr (array) : linefill array
Returns: bool
method isEmpty(arr)
checks if a polyline array is either null or empty
Namespace types: array
Parameters:
arr (array) : polyline array
Returns: bool
method isEmpty(arr)
checks if a table array is either null or empty
Namespace types: array
Parameters:
arr (array) : table array
Returns: bool
method isNotEmpty(arr)
checks if an int array is not null and has at least one item
Namespace types: array
Parameters:
arr (array) : int array
Returns: bool
method isNotEmpty(arr)
checks if a float array is not null and has at least one item
Namespace types: array
Parameters:
arr (array) : float array
Returns: bool
method isNotEmpty(arr)
checks if a string array is not null and has at least one item
Namespace types: array
Parameters:
arr (array) : string array
Returns: bool
method isNotEmpty(arr)
checks if a bool array is not null and has at least one item
Namespace types: array
Parameters:
arr (array) : bool array
Returns: bool
method isNotEmpty(arr)
checks if a color array is not null and has at least one item
Namespace types: array
Parameters:
arr (array) : color array
Returns: bool
method isNotEmpty(arr)
checks if a chart.point array is not null and has at least one item
Namespace types: array
Parameters:
arr (array) : chart.point array
Returns: bool
method isNotEmpty(arr)
checks if a line array is not null and has at least one item
Namespace types: array
Parameters:
arr (array) : line array
Returns: bool
method isNotEmpty(arr)
checks if a label array is not null and has at least one item
Namespace types: array
Parameters:
arr (array) : label array
Returns: bool
method isNotEmpty(arr)
checks if a box array is not null and has at least one item
Namespace types: array
Parameters:
arr (array) : box array
Returns: bool
method isNotEmpty(arr)
checks if a linefill array is not null and has at least one item
Namespace types: array
Parameters:
arr (array) : linefill array
Returns: bool
method isNotEmpty(arr)
checks if a polyline array is not null and has at least one item
Namespace types: array
Parameters:
arr (array) : polyline array
Returns: bool
method isNotEmpty(arr)
checks if a table array is not null and has at least one item
Namespace types: array
Parameters:
arr (array) : table array
Returns: bool
method flush(arr)
remove all int objects in an array
Namespace types: array
Parameters:
arr (array) : int array
Returns: int
method flush(arr)
remove all float objects in an array
Namespace types: array
Parameters:
arr (array) : float array
Returns: float
method flush(arr)
remove all bool objects in an array
Namespace types: array
Parameters:
arr (array) : bool array
Returns: bool
method flush(arr)
remove all string objects in an array
Namespace types: array
Parameters:
arr (array) : string array
Returns: string
method flush(arr)
remove all color objects in an array
Namespace types: array
Parameters:
arr (array) : color array
Returns: color
method flush(arr)
remove all chart.point objects in an array
Namespace types: array
Parameters:
arr (array) : chart.point array
Returns: chart.point
method flush(arr)
remove and delete all line objects in an array
Namespace types: array
Parameters:
arr (array) : line array
Returns: line
method flush(arr)
remove and delete all label objects in an array
Namespace types: array
Parameters:
arr (array) : label array
Returns: label
method flush(arr)
remove and delete all box objects in an array
Namespace types: array
Parameters:
arr (array) : box array
Returns: box
method flush(arr)
remove and delete all table objects in an array
Namespace types: array
Parameters:
arr (array) : table array
Returns: table
method flush(arr)
remove and delete all linefill objects in an array
Namespace types: array
Parameters:
arr (array) : linefill array
Returns: linefill
method flush(arr)
remove and delete all polyline objects in an array
Namespace types: array
Parameters:
arr (array) : polyline array
Returns: polyline
BoxesLibLibrary "BoxesLib"
isOverlappingBox(_boxes, _top, _bottom)
Parameters:
_boxes (array)
_top (float)
_bottom (float)
isTooCloseBox(_boxes, _top, _bottom, zoneProximityPct)
Parameters:
_boxes (array)
_top (float)
_bottom (float)
zoneProximityPct (float)
createBox(_boxes, _top, _bottom, _leftBarIndex, _color, _txt, _is_breakout, numberLimit, zoneProximityPct, currentClose, isConfirmed)
Parameters:
_boxes (array)
_top (float)
_bottom (float)
_leftBarIndex (int)
_color (color)
_txt (string)
_is_breakout (bool)
numberLimit (int)
zoneProximityPct (float)
currentClose (float)
isConfirmed (bool)
manageBoxes(_boxes, _is_breakout, currentClose, isConfirmed)
Parameters:
_boxes (array)
_is_breakout (bool)
currentClose (float)
isConfirmed (bool)
MK_FractalLibrary "MK_Fractal"
ResCal(price, degree)
Parameters:
price (float)
degree (float)
SupCal(price, degree)
Parameters:
price (float)
degree (float)
CalcAllLevels(price)
Parameters:
price (float)
FindNearestLevel(current_price, resistances, supports)
Parameters:
current_price (float)
resistances (array)
supports (array)
IsTouchSupport(current_price, supports, tolerance_pct)
Parameters:
current_price (float)
supports (array)
tolerance_pct (float)
IsTouchResistance(current_price, resistances, tolerance_pct)
Parameters:
current_price (float)
resistances (array)
tolerance_pct (float)
GetLevelByDegree(price, degree)
Parameters:
price (float)
degree (float)
GetFormattedLevels(price)
Parameters:
price (float)
TRZigZagLibLibrary "TRZigZagLib"
method directionName(this)
Gets pivot direction as string
Namespace types: Pivot
Parameters:
this (Pivot) : Pivot instance
Returns: "HIGH" or "LOW"
method isHigh(this)
Checks if pivot is a high
Namespace types: Pivot
Parameters:
this (Pivot) : Pivot instance
Returns: true if pivot is a high
method isLow(this)
Checks if pivot is a low
Namespace types: Pivot
Parameters:
this (Pivot) : Pivot instance
Returns: true if pivot is a low
method newSettings(minLength, minBarSize, maxBarSize)
Creates default ZigZag settings
Namespace types: series int, simple int, input int, const int
Parameters:
minLength (int) : Minimum pivot length (default: 10)
minBarSize (int) : Minimum bars between pivots (default: 5)
maxBarSize (int) : Maximum bars to lookback (default: 300)
Returns: New ZigZagSettings instance
method setLineStyle(this, lineColor, lineWidth, lineStyle)
Sets line appearance
Namespace types: ZigZagSettings
Parameters:
this (ZigZagSettings) : Settings instance
lineColor (color)
lineWidth (int)
lineStyle (string)
Returns: Modified settings instance
method newZigZag(settings, depth)
Creates a new ZigZag instance
Namespace types: ZigZagSettings
Parameters:
settings (ZigZagSettings) : ZigZag settings
depth (int) : Depth value for this ZigZag
Returns: New ZigZag instance
method calculate(this)
Calculates ZigZag using LuxAlgo-style pivot detection
Namespace types: ZigZag
Parameters:
this (ZigZag) : ZigZag instance
method getLastPivots(this, count)
Gets the last N pivots
Namespace types: ZigZag
Parameters:
this (ZigZag) : ZigZag instance
count (int) : Number of pivots to get (default: 5)
Returns: Array of pivots
method getPivot(this, index)
Gets pivot at index
Namespace types: ZigZag
Parameters:
this (ZigZag) : ZigZag instance
index (int) : Index (0 = most recent)
Returns: Pivot or na
method truncate(this, maxBars)
Truncates old pivots beyond lookback window
Namespace types: ZigZag
Parameters:
this (ZigZag) : ZigZag instance
maxBars (int) : Maximum bars to keep
method newMultiZigZag(settings, minDepth, maxDepth, count)
Creates a new MultiZigZag manager
Namespace types: ZigZagSettings
Parameters:
settings (ZigZagSettings) : Base settings for all ZigZags
minDepth (int) : Minimum depth value
maxDepth (int) : Maximum depth value
count (int) : Number of ZigZag instances (max 11)
Returns: New MultiZigZag instance
method calculateAll(this)
Updates all ZigZag instances
Namespace types: MultiZigZag
Parameters:
this (MultiZigZag) : MultiZigZag instance
method getZigZag(this, index)
Gets ZigZag at index
Namespace types: MultiZigZag
Parameters:
this (MultiZigZag) : MultiZigZag instance
index (int) : Index (0 to count-1)
Returns: ZigZag instance or na
method getDepth(this, index)
Gets depth value at index
Namespace types: MultiZigZag
Parameters:
this (MultiZigZag) : MultiZigZag instance
index (int) : Index (0 to count-1)
Returns: Depth value
method size(this)
Gets total number of ZigZag instances
Namespace types: MultiZigZag
Parameters:
this (MultiZigZag) : MultiZigZag instance
Returns: Count of ZigZag instances
method truncateAll(this, maxBars)
Truncates all ZigZag instances
Namespace types: MultiZigZag
Parameters:
this (MultiZigZag) : MultiZigZag instance
maxBars (int) : Maximum bars to keep
method distance(p1, p2)
Calculates distance between two pivots
Namespace types: Pivot
Parameters:
p1 (Pivot) : First pivot
p2 (Pivot) : Second pivot
Returns: Price distance (absolute)
method barSpan(p1, p2)
Calculates bar span between two pivots
Namespace types: Pivot
Parameters:
p1 (Pivot) : First pivot
p2 (Pivot) : Second pivot
Returns: Bar span (absolute)
method isAlternating(pivots)
Checks if pivots are alternating (high-low-high or low-high-low)
Namespace types: array
Parameters:
pivots (array) : Array of pivots
Returns: true if alternating pattern exists
Pivot
Pivot point in the chart
Fields:
d (series int) : Direction: 1=high, -1=low
x (series int) : Bar index
y (series float) : Price
confirmed (series bool) : Pivot confirmation flag
ZigZagSettings
ZigZag configuration settings
Fields:
minLength (series int) : Minimum pivot detection length
minBarSize (series int) : Minimum bars between pivots
maxBarSize (series int) : Maximum bars to lookback
lineColor (series color) : ZigZag line color
lineWidth (series int) : ZigZag line width
lineStyle (series string) : ZigZag line style
ZigZag
Single ZigZag instance
Fields:
settings (ZigZagSettings) : Configuration settings
pivots (array) : Array of pivot points (max 25 for patterns)
lastUpdateBar (series int) : Last bar index when updated
depth (series int) : Current depth value
MultiZigZag
Multi-depth ZigZag manager
Fields:
zigzags (array) : Array of ZigZag instances (max 11)
depthValues (array) : Array of depth values being used
currentBar (series int) : Current bar index being processed
AssetCorrelationLibraryLibrary "AssetCorrelationLibrary™"
detectIndicesFutures(ticker)
Detects Index Futures (NQ/ES/YM/RTY + micro variants)
Parameters:
ticker (string) : The ticker string to check (typically syminfo.ticker)
Returns: AssetPairing with secondary and tertiary assets configured
detectMetalsFutures(ticker)
Detects Metal Futures (GC/SI/HG + micro variants)
Parameters:
ticker (string) : The ticker string to check
Returns: AssetPairing with secondary and tertiary assets configured
detectForexFutures(ticker)
Detects Forex Futures (6E/6B + micro variants)
Parameters:
ticker (string) : The ticker string to check
Returns: AssetPairing with secondary and tertiary assets configured
detectEnergyFutures(ticker)
Detects Energy Futures (CL/RB/HO + micro variants)
Parameters:
ticker (string) : The ticker string to check
Returns: AssetPairing with secondary and tertiary assets configured
detectTreasuryFutures(ticker)
Detects Treasury Futures (ZB/ZF/ZN)
Parameters:
ticker (string) : The ticker string to check
Returns: AssetPairing with secondary and tertiary assets configured
detectForexCFD(ticker, tickerId)
Detects Forex CFD pairs (EUR/GBP/DXY, USD/JPY/CHF triads)
Parameters:
ticker (string) : The ticker string to check
tickerId (string) : The full ticker ID (syminfo.tickerid) for primary asset
Returns: AssetPairing with secondary and tertiary assets configured
detectCrypto(ticker, tickerId)
Detects major Crypto assets (BTC, ETH, SOL, XRP, alts)
Parameters:
ticker (string) : The ticker string to check
tickerId (string) : The full ticker ID for primary asset
Returns: AssetPairing with secondary and tertiary assets configured
detectMetalsCFD(ticker, tickerId)
Detects Metals CFD (XAU/XAG/Copper)
Parameters:
ticker (string) : The ticker string to check
tickerId (string) : The full ticker ID for primary asset
Returns: AssetPairing with secondary and tertiary assets configured
detectIndicesCFD(ticker, tickerId)
Detects Indices CFD (NAS100/SP500/DJ30)
Parameters:
ticker (string) : The ticker string to check
tickerId (string) : The full ticker ID for primary asset
Returns: AssetPairing with secondary and tertiary assets configured
detectEUStocks(ticker, tickerId)
Detects EU Stock Indices (GER40/EU50) - Dyad only
Parameters:
ticker (string) : The ticker string to check
tickerId (string) : The full ticker ID for primary asset
Returns: AssetPairing with secondary asset configured (tertiary empty for dyad)
getDefaultFallback(tickerId)
Returns default fallback assets (chart ticker only, no correlation)
Parameters:
tickerId (string) : The full ticker ID for primary asset
Returns: AssetPairing with chart ticker as primary, empty secondary/tertiary (no correlation)
applySessionModifierWithBackadjust(tickerStr, sessionType)
Applies futures session modifier to ticker WITH back adjustment
Parameters:
tickerStr (string) : The ticker to modify
sessionType (string) : The session type (syminfo.session)
Returns: Modified ticker string with session and backadjustment.on applied
applySessionModifierNoBackadjust(tickerStr, sessionType)
Applies futures session modifier to ticker WITHOUT back adjustment
Parameters:
tickerStr (string) : The ticker to modify
sessionType (string) : The session type (syminfo.session)
Returns: Modified ticker string with session and backadjustment.off applied
isTriadMode(pairing)
Checks if a pairing represents a valid triad (3 assets)
Parameters:
pairing (AssetPairing) : The AssetPairing to check
Returns: True if tertiary is non-empty (triad mode), false for dyad
getAssetTicker(tickerId)
Extracts clean ticker string from full ticker ID
Parameters:
tickerId (string) : The full ticker ID (e.g., "BITGET:BTCUSDT.P")
Returns: Clean ticker string (e.g., "BTCUSDT.P")
resolveTriad(chartTickerId, pairing)
Resolves triad asset assignments with proper inversion flags
Parameters:
chartTickerId (string) : The current chart's ticker ID (syminfo.tickerid)
pairing (AssetPairing) : The detected AssetPairing
Returns: Tuple
resolveDyad(chartTickerId, pairing)
Resolves dyad asset assignment with proper inversion flag
Parameters:
chartTickerId (string) : The current chart's ticker ID
pairing (AssetPairing) : The detected AssetPairing (dyad: tertiary is empty)
Returns: Tuple
resolveAssets(ticker, tickerId, assetType, sessionType, useBackadjust)
Main auto-detection entry point. Detects asset category and returns fully resolved config.
Parameters:
ticker (string) : The ticker string to check (typically syminfo.ticker)
tickerId (string) : The full ticker ID (typically syminfo.tickerid)
assetType (string) : The asset type (typically syminfo.type)
sessionType (string) : The session type for futures (typically syminfo.session)
useBackadjust (bool) : Whether to apply back adjustment for futures session alignment
Returns: AssetConfig with fully resolved assets, inversion flags, and detection status
resolveCurrentChart()
Simplified auto-detection using current chart's syminfo values
Returns: AssetConfig with fully resolved assets, inversion flags, and detection status
AssetPairing
Core asset pairing structure for triad/dyad configurations
Fields:
primary (series string) : The primary (chart) asset ticker ID
secondary (series string) : The secondary correlated asset ticker ID
tertiary (series string) : The tertiary correlated asset ticker ID (empty for dyad)
invertSecondary (series bool) : Whether secondary asset should be inverted for divergence calc
invertTertiary (series bool) : Whether tertiary asset should be inverted for divergence calc
AssetConfig
Full asset resolution result with mode detection and computed values
Fields:
detected (series bool) : Whether auto-detection succeeded
isTriadMode (series bool) : True if triad (3 assets), false if dyad (2 assets)
primary (series string) : The resolved primary asset ticker ID
secondary (series string) : The resolved secondary asset ticker ID
tertiary (series string) : The resolved tertiary asset ticker ID (empty for dyad)
invertSecondary (series bool) : Computed inversion flag for secondary asset
invertTertiary (series bool) : Computed inversion flag for tertiary asset
assetCategory (series string) : String describing the detected asset category
Note to potential users.
I did not really intend to make this public but i have to in order to avoid any potential compliance issues with the TradingView Moderation Team and the House Rules.
However if you are to use this library, you cannot make your code closed source / invite only as it is intellectual property. The only exception to this is if I am credited in the header of your code and i explicitly give permission to do so.
As per the TradingView house rules, you are completely FREE to do with this as you like, provided the script stays private.
Use the @fstarcapital tag to give credits
❤️ from cephxs
RLSR logreg_support_libLibrary "logreg_support_lib"
sigmoid(z)
Parameters:
z (float)
prng01(seed1, seed2)
Parameters:
seed1 (float)
seed2 (float)
normalize(value, minval, maxval)
Parameters:
value (float)
minval (float)
maxval (float)
calcpercentilefast(arr, percentile)
Parameters:
arr (array)
percentile (float)
calcpercentile_series_sampled(s, length, percentile, stride)
Parameters:
s (float)
length (int)
percentile (float)
stride (int)
calcRangeWithLog(value, minval, maxval, uselog)
Parameters:
value (float)
minval (float)
maxval (float)
uselog (bool)
calcMomentumAdvanced(src, length, momType)
Parameters:
src (float)
length (simple int)
momType (string)
normalizeMomentumByType(rawMom, momType, momMin, momMax, momNorm)
Parameters:
rawMom (float)
momType (string)
momMin (float)
momMax (float)
momNorm (float)
normalizeMomentumByTypeExt(rawMom, momType, momMin, momMax, momNorm, bouncingdecay)
Parameters:
rawMom (float)
momType (string)
momMin (float)
momMax (float)
momNorm (float)
bouncingdecay (float)
calcrollingstddev(src, length)
Parameters:
src (float)
length (int)
addlog(buffer, level, msg)
Parameters:
buffer (string)
level (string)
msg (string)
calcfeaturecorrelation(x1, x2)
Parameters:
x1 (array)
x2 (array)
calcnoiseratio(src, lookback)
Parameters:
src (float)
lookback (int)
calccompatibilityscore(x1, x2)
Parameters:
x1 (array)
x2 (array)
getfuturereturn(offset, returnlookback)
Parameters:
offset (int)
returnlookback (int)
calculatema(source, length, matype)
Parameters:
source (float)
length (simple int)
matype (string)
adaptive_trigger_for_source(src, enabled, freeze, lookback, threshold, volahistory)
Parameters:
src (float)
enabled (bool)
freeze (bool)
lookback (int)
threshold (float)
volahistory (array)
checkadaptivetrigger5(s1, enabled1, freeze1, hist1, s2, enabled2, freeze2, hist2, s3, enabled3, freeze3, hist3, s4, enabled4, freeze4, hist4, s5, enabled5, freeze5, hist5, lookback, threshold)
Parameters:
s1 (float)
enabled1 (bool)
freeze1 (bool)
hist1 (array)
s2 (float)
enabled2 (bool)
freeze2 (bool)
hist2 (array)
s3 (float)
enabled3 (bool)
freeze3 (bool)
hist3 (array)
s4 (float)
enabled4 (bool)
freeze4 (bool)
hist4 (array)
s5 (float)
enabled5 (bool)
freeze5 (bool)
hist5 (array)
lookback (int)
threshold (float)
ring_start_index(rb_write_idx, rb_count, rb_cap)
Parameters:
rb_write_idx (int)
rb_count (int)
rb_cap (int)
AdjCloseLibLibrary "AdjCloseLib"
Library for producing gap-adjusted price series that removes intraday gaps at market open
get_adj_close(_gapThresholdPct)
Calculates gap-adjusted close price by detecting and removing gaps at market open (09:15)
Parameters:
_gapThresholdPct (float) : Minimum gap size (in percentage) required to trigger adjustment. Example: 0.5 for 0.5%
Returns: Adjusted close price for the current bar (always returns a numeric value, never na)
@details Detects gaps by comparing 09:15 open with previous day's close. If gap exceeds threshold,
subtracts the gap value from all bars between 09:15-15:29 inclusive. State resets after session close.
get_adj_ohlc(_gapThresholdPct)
Calculates gap-adjusted OHLC values by subtracting detected gap from all price components
Parameters:
_gapThresholdPct (float) : Minimum gap size (in percentage) required to trigger adjustment. Example: 0.5 for 0.5%
Returns: Tuple of
@details Useful for calculating indicators (ATR, Heikin-Ashi, etc.) on gap-adjusted data.
Applies the same gap adjustment logic to all OHLC components simultaneously.
Count█ OVERVIEW
A library of functions for counting the number of times (frequency) that elements occur in an array or matrix.
█ USAGE
Import the Count library.
import joebaus/count/1 as c
Create an array or matrix that is a `float`, `int`, `string`, or `bool` type to count elements from, then call the count function on the array or matrix.
id = array.from(1.00, 1.50, 1.25, 1.00, 0.75, 1.25, 1.75, 1.25)
countMap = id.count() // Alternatively: countMap = c.count(id)
The "count map" will return a map with keys for each unique element in the array or matrix, and with respective values representing the number of times the unique element was counted. The keys will be the same type as the array or matrix counted. The values will always be an `int` type.
array mapKeys = countMap.keys() // Returns unique keys
array mapValues = countMap.values() // Returns counts
If an array is in ascending or descending order, then the keys of the map will also generate in the same order.
intArray = array.from(2, 2, 2, 3, 4, 4, 4, 4, 4, 6, 6) // Ascending order
map countMap = intArray.count() // Creates a "count map" of all unique elements
array mapKeys = countMap.keys() // Returns // Ascending order
array mapValues = countMap.values() // Returns count
Include a value to get the count of only that value in an array or matrix.
floatMatrix = matrix.new(3, 3, 0.0)
floatMatrix.set(0, 0, 1.0), floatMatrix.set(1, 0, 1.0), floatMatrix.set(2, 0, 1.0)
floatMatrix.set(0, 1, 1.5), floatMatrix.set(1, 1, 2.0), floatMatrix.set(2, 1, 2.5)
floatMatrix.set(0, 2, 1.0), floatMatrix.set(1, 2, 2.5), floatMatrix.set(2, 2, 1.5)
int countFloatMatrix = floatMatrix.count(1.0) // Counts all 1.0 elements, returns 5
// Alternatively: int countFloatMatrix = c.count(floatMatrix, 1.0)
The string method of count() can use strings or regular expressions like "bull*" to count all matching occurrences in a string array.
stringArray = array.from('bullish', 'bull', 'bullish', 'bear', 'bull', 'bearish', 'bearish')
int countString = stringArray.count('bullish') // Returns 2
int countStringRegex = stringArray.count('bull*') // Returns 4
To count multiple values, use an array of values instead of a single value. Returning a count map only of elements in the array.
countArray = array.from(1.0, 2.5)
map countMap = floatMatrix.count(countArray)
array mapKeys = countMap.keys() // Returns keys
array mapValues = countMap.values() // Returns counts
Multiple regex patterns or strings can be counted as well.
stringMatrix = matrix.new(3, 3, '')
stringMatrix.set(0, 0, 'a'), stringMatrix.set(1, 0, 'a'), stringMatrix.set(2, 0, 'a')
stringMatrix.set(0, 1, 'b'), stringMatrix.set(1, 1, 'c'), stringMatrix.set(2, 1, 'd')
stringMatrix.set(0, 2, 'a'), stringMatrix.set(1, 2, 'd'), stringMatrix.set(2, 2, 'b')
// Count the number of times the regex patterns `'^(a|c)$'` and `'^(b|d)$'` occur
array regexes = array.from('^(a|c)$', '^(b|d)$')
map countMap = stringMatrix.count(regexes)
array mapKeys = countMap.keys() // Returns
array mapValues = countMap.values() // Returns
An optional comparison operator can be specified to count the number of times an equality was satisfied for `float`, `int`, and `bool` methods of `count()`.
intArray = array.from(2, 2, 2, 3, 4, 4, 4, 4, 4, 6, 6)
// Count the number of times an element is greater than 4
countInt = intArray.count(4, '>') // Returns 2
When passing an array of values to count and a comparison operator, the operator will apply to each value.
intArray = array.from(2, 2, 2, 3, 4, 4, 4, 4, 4, 6, 6)
values = array.from(3, 4)
// Count the number of times and element is greater than 3 and 4
map countMap = intArray.count(values, '>')
array mapKeys = countMap.keys() // Returns
array mapValues = countMap.values() // Returns
Multiple comparison operators can be applied when counting multiple values.
intMatrix = matrix.new(3, 3, 0)
intMatrix.set(0, 0, 2), intMatrix.set(1, 0, 3), intMatrix.set(2, 0, 5)
intMatrix.set(0, 1, 2), intMatrix.set(1, 1, 4), intMatrix.set(2, 1, 2)
intMatrix.set(0, 2, 5), intMatrix.set(1, 2, 2), intMatrix.set(2, 2, 3)
values = array.from(3, 4)
comparisons = array.from('<', '>')
// Count the number of times an element is less than 3 and greater than 4
map countMap = intMatrix.count(values, comparisons)
array mapKeys = countMap.keys() // Returns
array mapValues = countMap.values() // Returns
ChainAggLib - library for aggregation of main chain tickersLibrary "ChainAggLib"
ChainAggLib — token -> main protocol coin (chain) and top-5 exchange tickers for volume aggregation.
Library only (no plots). All helpers are pure functions and do not modify globals.
norm_sym(s)
Parameters:
s (string)
get_base_from_symbol(full_symbol)
Parameters:
full_symbol (string)
get_chain_for_token(token_symbol)
Parameters:
token_symbol (string)
get_top5_exchange_tickers_for_chain(chain_code)
Parameters:
chain_code (string)
get_top5_exchange_tickers_for_token(token_symbol)
Parameters:
token_symbol (string)
join_tickers(arr)
Parameters:
arr (array)
contains_symbol(arr, symbol)
Parameters:
arr (array)
symbol (string)
contains_current(arr)
Parameters:
arr (array)
get_arr_for_current_token()
get_chain_for_current()
BossExoticMAs
A next-generation moving average and smoothing library by TheStopLossBoss, featuring premium adaptive, exotic, and DSP-inspired filters — optimized for Pine Script® v6 and designed for Traders who demand precision and beauty.
> BossExoticMAs is a complete moving average and signal-processing toolkit built for Pine Script v6.
It combines the essential trend filters (SMA, EMA, WMA, etc.) with advanced, high-performance exotic types used by quants, algo designers, and adaptive systems.
Each function is precision-tuned for stability, speed, and visual clarity — perfect for building custom baselines, volatility filters, dynamic ribbons, or hybrid signal engines.
Includes built-in color gradient theming powered by the exclusive BossGradient —
//Key Features
✅ Full Moving Average Set
SMA, EMA, ZEMA, WMA, HMA, WWMA, SMMA
DEMA, TEMA, T3 (Tillson)
ALMA, KAMA, LSMA
VMA, VAMA, FRAMA
✅ Signal Filters
One-Euro Filter (Crispin/Casiez implementation)
ATR-bounded Range Filter
✅ Color Engine
lerpColor() safe blending using color.from_gradient
Thematic gradient palettes: STOPLOSS, VAPORWAVE, ROYAL FLAME, MATRIX FLOW
Exclusive: BOSS GRADIENT
✅ Helper Functions
Clamping, normalization, slope detection, tick delta
Slope-based dynamic color control via slopeThemeColor()
🧠 Usage Example
//@version=6
indicator("Boss Exotic MA Demo", overlay=true)
import TheStopLossBoss/BossExoticMAs/1 as boss
len = input.int(50, "Length")
atype = input.string("T3", "MA Type", options= )
t3factor = input.float(0.7, "T3 β", step=0.05)
smoothColor = boss.slopeThemeColor(close, "BOSS GRADIENT", 0.001)ma = boss.maSelect(close, len, atype, t3factor, 0.85, 14)
plot(ma, "Boss Exotic MA", color=smoothColor, linewidth=2)
---
🔑 Notes
Built exclusively for Pine Script® v6
Library designed for import use — all exports are prefixed cleanly (boss.functionName())
Some functions maintain internal state (var-based). Warnings are safe to ignore — adaptive design choice.
Each MA output is non-repainting and mathematically stable.
---
📜 Author
TheStopLossBoss
Designer of precision trading systems and custom adaptive algorithms.
Follow for exclusive releases, educational material, and full-stack trend solutions.
movingaverage, trend, adaptive, filter, volatility, smoothing, quant, technicalanalysis, bossgradient, t3, alma, frama, vma
ema 狀態機Library "ema_flow_lib"
ema_flow_state(e10, e20, e100, entanglePct, farPct, e10_prev, e20_prev)
Parameters:
e10 (float)
e20 (float)
e100 (float)
entanglePct (float)
farPct (float)
e10_prev (float)
e20_prev (float)
state_name(s)
Parameters:
s (int)
phx_fvgfvg generator 4h and current time frame
library to import fvg from 4h with midle line and proximity support and resistance






















