PINE LIBRARY

XGBoostMini

731
This advanced library implements a fully functional, optimized, and native XGBoost (Extreme Gradient Boosting) binary classification model, allowing you to train an ensemble of decision trees and perform real-time inference directly on price data and technical indicators.

🔷 XGBoost Extreme Gradient Boosting
XGBoost is one of the most famous, powerful, and widely used machine learning libraries in the world. Is an ensemble learning model. It works by sequentially combining many weak decision trees (weak learners), where each new tree is specifically trained to correct the errors (residuals) made by the preceding trees. It has become the gold standard for solving tabular data problems and is renowned for dominating competitions on the Kaggle platform for years thanks to its extraordinary combination of speed and precision.
snapshot

🔹 Key Features That Make It Unique
  • Speed and Efficiency (Parallelization): Unlike traditional Gradient Boosting, which builds trees in a strictly sequential manner, XGBoost leverages multithreading to parallelize tree construction, drastically reducing computation time.
  • Built-in Regularization (L1 and L2): It includes penalties for model complexity, which helps prevent overfitting (the phenomenon where the model memorizes training data but fails on unseen data).
  • Missing Values Handling: It features built-in internal logic to automatically determine how to handle missing or NaN data during the splitting phase, without requiring mandatory upfront preprocessing.
  • Approximate Split Algorithms: For massive datasets, it uses intelligent techniques to find optimal split points without having to evaluate every single value, further accelerating the process.


🔹 What is XGBoost and Where Did It Come From?
XGBoost (eXtreme Gradient Boosting) is one of the most powerful and widely used machine learning algorithms in the world, particularly for structured and tabular data.
It was created in 2014 by Tianqi Chen (then a researcher at the University of Washington) as an open-source research project, and it became a global phenomenon in 2016 following the publication of its landmark paper presented at the SIGKDD conference. Chen aimed to push the concept of Gradient Boosting (sequentially combining weak decision trees, where each new tree corrects the errors of previous ones) beyond the limitations of traditional software at the time. The goal was to build a system that combined extreme computational speed (leveraging parallel hardware) with extraordinary predictive accuracy, introducing advanced techniques such as mathematical regularization to prevent overfitting.

🔹 Why XGBoost is a Brilliant Choice for Financial Time Series Trading
In quantitative trading, financial market data (prices, volumes, and technical indicators like RSI, MACD, and moving averages) almost always comes in a tabular format. Here is why XGBoost frequently outperforms more complex models (such as Neural Networks or Transformers) when analyzing financial time series:
  • Tabular Data Dominance: Unlike images or text, historical time series structured as indicators and extracted features benefit immensely from decision trees. XGBoost excels at discovering complex threshold rules (e.g., "if the RSI is below 30 and volatility exceeds X, then...").
  • Noise Management and Regularization: Financial markets are notoriously noisy. XGBoost’s regularization parameters penalize tree complexity, preventing the model from memorizing past data and forcing it to uncover generalizable patterns.
  • Robustness to Outliers: Flash crashes, sudden volume spikes, or data anomalies do not throw decision trees off balance—unlike linear models or neural networks, which are often sensitive to extreme values.
  • Interpretability via Feature Importance: In trading, guessing direction is not enough; you must understand why. XGBoost natively computes the importance of each variable (via structural gain), allowing you to discover which technical indicators are genuinely driving strategy performance versus those that are just noise.
  • Real-Time Inference Speed: Because it relies on simple sequential logical comparisons (inference across shallow decision trees), it is ideal for real-time execution directly on platforms like TradingView without excessive latency.


🔷 1. User-Defined Types (UDTs)
The code leverages Pine Script v6 data structures to define the model architecture:
  • XGBTreeDepth3: Represents a single weak learner with a fixed depth of 3 levels. It stores feature indices, split thresholds, information gains for each node, and the terminal leaf weights (w0 through w7) for all 8 possible leaf regions.
  • XGBModel: Encapsulates the entire trained tree ensemble, the best recorded validation loss (best_val_loss), and the optimal number of trees to retain (best_tree_count).
  • SplitCandidate: An internal helper structure used to evaluate optimal split points during tree growth.


🔷 2. Inference & Analysis Methods
  • predict_tree: Traverses the depth-3 decision tree by sequentially evaluating feature values against stored thresholds until a terminal leaf node is reached.
  • predict_probability: Aggregates the raw scores (logits) across all trees in the ensemble, applies the learning rate, and maps the final output to a logistic probability ranging from 0.0 to 1.0 via the Sigmoid function (including numerical protection against overflow/underflow).
  • calculate_feature_importance: Computes relative feature importance (0.0 to 1.0) by aggregating the structural gain accumulated by each variable across the entire ensemble.


🔷 3. Static Quantile Pre-Binning
The find_split_subset_fast function and the initial training phase implement Static Quantile Pre-Binning: prior to boosting, historical feature values are sorted and binned into quantitative buckets. This dramatically accelerates the search for optimal split points during tree construction, significantly reducing computational overhead.

🔷 4. The Training Pipeline
This is the core of the library, executing an iterative boosting loop that includes:
1. Row and Column Subsampling: Supports random sampling of instances and features to mitigate overfitting.
2. Gradient Computation: Computes first-order gradients and second-order Hessians based on binary cross-entropy loss.
3. Depth-3 Tree Construction: Progressively identifies optimal splits level by level using XGBoost regularization criteria.
4. Early Stopping & Validation: Automatically carves out a validation subset and halts training if the validation loss fails to improve over a specified number of rounds, subsequently rolling back to the optimal tree count.

🔷 Constraints to Consider

🔹 Architectural & Complexity Limitations (Fixed Depth of 3)
The tree is hardcoded with a fixed depth of 3 (XGBTreeDepth3), meaning it can evaluate a maximum of 3 levels of decisions (up to 8 terminal leaves). This can result in an inability to capture complex interactions. In financial markets, complex patterns often require deeper trees to combine multiple simultaneous conditions. A depth of 3 severely limits the learning capacity for advanced non-linear relationships.

🔹 Computational & Execution Limitations
Training a Gradient Boosting model requires a high volume of computations (nested loops for scanning matrices, calculating quantiles, sorting arrays, and evaluating gradients). Increasing the number of trees, feature matrix size, or number of bins too much will cause the script to abort due to exceeding the maximum execution loop limit allowed per single script (typically a few tens of thousands of operations before timing out).

Validation splits data by simply taking a portion of the rows. In financial time series, this can cause Data Leakage if training and validation data mix without strictly respecting the chronological sequence (the model might "peek" into the future if a Walk-Forward or Time-Series Split approach is not used).

Without a rigorous Out-Of-Sample (OOS) test set, a model trained directly on past prices will easily tend to find spurious correlations (market "noise" rather than real signals), failing miserably when applied to future real-time data.

🔹 Technical Rationale for Design Choices
There are very specific technical reasons why advanced features like dynamic Walk-Forward or continuous Rolling Retraining have not been natively integrated into this library:
  • The Computational Bottleneck
    A true Walk-Forward or Rolling Retraining (retraining the model bar-by-bar or across rolling time blocks) requires repeating the entire training process—quantile calculation, matrix scanning, iterative tree construction—hundreds or thousands of times on massive historical datasets. Continuous retraining would immediately trigger an Execution Timeout error.
  • Memory & Historical Data Architecture
    Managing matrices and historical arrays carries strict performance constraints. Accessing past data from hundreds of bars while applying complex temporal slicing logic rapidly consumes the heap memory allocated for the script, slowing down or freezing the chart.


This is why a "static and lightweight" approach was chosen for this library. The script trains the model once (or on a fixed portion of data) and leverages the speed of pre-compiled trees to perform real-time inference without exceeding computational limits.

---------------------------------------------------------------

Library "XGBoostMini"
XGBoost Mini Library featuring Static Quantile Pre-Binning, Early Stopping, Subsampling, and Feature Importance.

method predict_tree(self, features)
  Evaluates the raw score (logit sum contribution) of a single depth-3 tree on a feature vector.
Traverses the binary decision tree hardcoded for 3 levels (up to 8 terminal leaves).
  Namespace types: XGBTreeDepth3
  Parameters:
    self (XGBTreeDepth3)
    features (array<float>)

predict_probability(model, features, learning_rate)
  Computes the final Sigmoid probability (0.0 to 1.0) by aggregating the boosted ensemble.
Applies learning rate scaling and numerical overflow/underflow clamping to the raw accumulated score.
  Parameters:
    model (XGBModel)
    features (array<float>)
    learning_rate (float)

calculate_feature_importance(model, n_features)
  Calculates relative Feature Importance (0.0 - 1.0) based on accumulated structural gain across the ensemble.
  Parameters:
    model (XGBModel)
    n_features (int)

train_model(X_matrix, y_target, num_trees, learning_rate, lambda_reg, quantile_bins, min_samples_split, subsample, colsample_bytree, val_ratio, patience)
  Main entry point to train the XGBoost ensemble.
Implements Static Quantile Pre-Binning, Row/Column Subsampling, Binary Cross-Entropy Loss, and Early Stopping.
  Parameters:
    X_matrix (matrix<float>)
    y_target (array<float>)
    num_trees (int)
    learning_rate (float)
    lambda_reg (float)
    quantile_bins (int)
    min_samples_split (int)
    subsample (float)
    colsample_bytree (float)
    val_ratio (float)
    patience (int)

XGBTreeDepth3
  XGBTreeDepth3
  Fields:
    f_r (series int)
    t_r (series float)
    g_r (series float)
    f_l (series int)
    t_l (series float)
    g_l (series float)
    f_right (series int)
    t_right (series float)
    g_right (series float)
    f_ll (series int)
    t_ll (series float)
    g_ll (series float)
    f_lr (series int)
    t_lr (series float)
    g_lr (series float)
    f_rl (series int)
    t_rl (series float)
    g_rl (series float)
    f_rr (series int)
    t_rr (series float)
    g_rr (series float)
    w0 (series float): to w7 Leaf node terminal weights (predictions) for all 8 possible regions of a depth-3 tree.
    w1 (series float)
    w2 (series float)
    w3 (series float)
    w4 (series float)
    w5 (series float)
    w6 (series float)
    w7 (series float)

XGBModel
  XGBModel
  Fields:
    ensemble (array<XGBTreeDepth3>): Array storing all trained XGBTreeDepth3 weak learners.
    best_val_loss (series float): Lowest validation loss achieved (used for tracking convergence).
    best_tree_count (series int): Optimal number of trees retained after early stopping.

Clause de non-responsabilité

Les informations et publications ne sont pas destinées à être, et ne constituent pas, des conseils ou recommandations financiers, d'investissement, de trading ou autres fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.