Cryptocurrency Price Forecasting Using Xgboost Regressor and Technical Indicators
Cryptocurrency Price Forecasting Using Xgboost Regressor and Technical Indicators
abdelatif.hafid@yahoo.com
2 GAIA, Ericsson, Montreal, Canada
maad.ebrahim@ericsson.com
3 Computer and Information Science, Fordham University, NY 10023, USA.
mrahouti@fordham.edu; aalfatemi@fordham.edu
4 College of IST, Penn State University, Monaca, PA 15601, USA.
arXiv:2407.11786v1 [cs.LG] 16 Jul 2024
dko5179@psu.edu
Abstract—The rapid growth of the stock market has at- faces challenges such as high price volatility and relatively
tracted many investors due to its potential for significant profits. smaller capitalization. Success in cryptocurrency financial
However, predicting stock prices accurately is difficult because trading hinges on the careful analysis and selection of data,
financial markets are complex and constantly changing. This is
especially true for the cryptocurrency market, which is known making the development of machine learning models crucial
for its extreme volatility, making it challenging for traders and for extracting meaningful insights. Models such as Long
investors to make wise and profitable decisions. This study Short Term Memory (LSTM) and Random Forest (RF) are
introduces a machine learning approach to predict cryptocur- instrumental in predicting cryptocurrency prices by leveraging
rency prices. Specifically, we make use of important technical historical data and patterns, thereby aiding effective decision-
indicators such as Exponential Moving Average (EMA) and
Moving Average Convergence Divergence (MACD) to train and making in this volatile market. Despite the potential, there have
feed the XGBoost regressor model. We demonstrate our approach been limited studies attempting to create successful trading
through an analysis focusing on the closing prices of Bitcoin strategies in the cryptocurrency market.
cryptocurrency. We evaluate the model’s performance through With the advent of FinTech, machine learning models have
various simulations, showing promising results that suggest its been increasingly adopted to forecast stock price movements,
usefulness in aiding/guiding cryptocurrency traders and investors
in dynamic market conditions. transforming the landscape of financial analysis and trading.
These models leverage large datasets and complex algorithms
Index Terms—Artificial intelligence, Bitcoin, Machine learn- to identify patterns and predict future price trends, which has
ing, Market forecasting, Price prediction, Regression analysis,
XGBoost. led to notable success across various markets, including the
S&P 500 and NASDAQ [1]. In the cryptocurrency market,
which is characterized by its high volatility and rapid price
I. I NTRODUCTION fluctuations, machine learning techniques have proven particu-
Over the past few years, the rapid expansion of the stock larly valuable. Studies have demonstrated the efficacy of deep
market has made it an appealing option for investors seeking learning methods, such as Stacked Denoising Autoencoders
high returns and easy access. However, investing in stocks (SDAE) and LSTM networks, in predicting Bitcoin prices
carries inherent risks, underscoring the need for a well-defined with high accuracy [2], [3]. These models utilize a variety of
investment strategy. Traditionally, investors relied on empirical inputs, including historical price data, trading volume, public
methods such as technical analysis, guided by financial ex- sentiment, and macroeconomic indicators, to generate predic-
pertise. With the widespread adoption of financial technology tions that can guide investment decisions. The integration of
(FinTech), statistical models incorporating machine learning machine learning into FinTech has thus provided investors
techniques have emerged for forecasting stock price move- with powerful tools to navigate the complexities of financial
ments. This shift has demonstrated significant success across markets, enhancing their ability to make informed and strategic
various markets, including the S&P 500, NASDAQ [1], and the trading decisions.
cryptocurrency market [2], [3]. In this research, our emphasis Despite the advantages of the cryptocurrency market, such
is on the cryptocurrency market, a dynamic force in finance, as abundant market data and continuous trading, it faces signif-
with a particular focus on Bitcoin price prediction [4]. icant challenges like high price volatility and relatively smaller
Furthermore, Blockchain technology, the backbone of cryp- capitalization. Successful trading in this market depends on
tocurrencies, has gained substantial attention in the banking careful data analysis and selection, making the development
and financial industry due to its secure and transparent de- of machine learning models crucial for extracting meaningful
centralized database [5]. Despite the advantages of abundant insights. Models like LSTM and RF are instrumental in
market data and continuous trading, the cryptocurrency market predicting cryptocurrency prices by utilizing historical data
2
and patterns, thus aiding effective decision-making in this the machine learning model and its mathematical formulation.
volatile landscape. While there have been limited studies on In Section V, we evaluate/assess our proposed model. Section
developing successful trading strategies in the cryptocurrency V also provides a comparison between the proposed work and
market, our research aims to bridge this gap by introducing a existing studies in the literature. Finally, Section VI concludes
novel machine learning strategy using the XGBoost regressor the paper.
model, which incorporates essential technical indicators and
historical data to enhance financial trading strategies. II. R ELATED W ORK
This research introduces an efficient machine learning ap-
proach for forecasting cryptocurrency prices, specifically fo- The effort to forecast cryptocurrency prices has garnered
cusing on Bitcoin. The motivation behind this study stems significant interest in recent years, leading to the development
from the inherent volatility and complexity of the cryptocur- of various methods to address this complex problem [6]. This
rency market, which pose significant challenges for traders section reviews advanced studies that employ machine learning
and investors. Traditional methods of technical analysis and for predicting cryptocurrency prices, with a particular focus
empirical strategies are often insufficient in predicting price on Bitcoin due to its dominant position and the extensive
movements in such a dynamic environment. To address this, availability of data.
we propose using the XGBoost regressor model, a powerful Among these advancements, machine learning has signifi-
machine learning technique known for its robustness and cantly impacted cryptocurrency price forecasting by providing
accuracy. Our methodology integrates a comprehensive set models that adeptly navigate the complex and volatile digital
of technical indicators, including the Exponential Moving currency market [7]. These methods range from simple regres-
Average (EMA), Moving Average Convergence Divergence sion models to advanced deep learning networks, each capable
(MACD), Relative Strength Index (RSI), and other relevant of detecting patterns and predicting future prices based on
metrics derived from historical market data. The data is historical data [8].
sourced from Binance via its API, covering a detailed time Cryptocurrency value fluctuations are influenced by numer-
span with high-frequency intervals, which allows for capturing ous factors, which has prompted the adoption of machine
rapid market changes. learning for price prediction [9], [10]. For instance, studies
The proposed model undergoes extensive preprocessing and by Greaves and AU [11] have investigated using network
feature engineering to enhance its predictive capabilities. By attributes and machine learning to predict Bitcoin prices.
employing regularization techniques, we mitigate the risk of Similarly, Jang and Lee [12] combined blockchain-related
overfitting and fine-tune the model parameters through a grid features, time series analysis, and Bayesian neural networks
search for optimal performance. Our results demonstrate that (BNNs) for Bitcoin price analysis.
the XGBoost regressor model significantly improves predic- Building on this foundation, further research by [13],
tion accuracy, evidenced by low Mean Absolute Error (MAE) [14], and [15] has applied machine learning to Bitcoin price
and Root Mean Squared Error (RMSE) values, as well as a forecasting. Saad et al. [15] not only predicted prices but
near-perfect R-squared value. This study contributes to the also identified critical network attributes and user behaviors
state-of-the-art by providing a robust and scalable solution for influencing price variations in Bitcoin and Ethereum [16],
cryptocurrency price prediction, leveraging advanced machine alongside the supply and demand dynamics of cryptocurren-
learning techniques to navigate the complexities of financial cies. Additionally, Sin and Wang [17] utilized neural networks
markets and aiding in informed decision-making for traders for price predictions, leveraging blockchain data features.
and investors. Continuing this trend, Christoforou et al. [18] developed a
The key contributions of this paper are summarized as Bitcoin price prediction model using neural networks, focusing
follows: on factors affecting price volatility and utilizing blockchain
• Introduce an efficient machine learning strategy using data and network activity metrics for forecasting. Furthermore,
the XGBoost regressor model for cryptocurrency price Chen et al. [19] and Akyildirim et al. [20] demonstrated the ap-
prediction. plication of machine learning in forecasting Bitcoin prices and
• Integrate a comprehensive set of technical indicators, mid-price movement of Bitcoin futures, respectively. These
including EMA, MACD, RSI, and others, with historical studies highlight the ability of machine learning to harness vast
market data. datasets and identify complex patterns, enhancing predictive
• Employ regularization techniques to mitigate overfitting accuracy beyond traditional statistical approaches.
and fine-tuned model parameters through grid search. Moreover, some studies have demonstrated the effectiveness
• Demonstrate significant improvements in prediction accu- of combining machine learning techniques with blockchain
racy with low MAE, RMSE, and a near-perfect R-squared data for cryptocurrency price forecasting. For example, Martin
value. et al. [21] introduced a hybrid method that merges diverse data
• Provide a robust and scalable solution for navigating and analytical techniques, enhancing accuracy in this complex
the complexities of financial markets, aiding informed field. Liu et al. [22] focused on optimizing performance and
decision-making for traders and investors. interpretability in financial time series, showcasing the benefits
The rest of the paper is organized as follows. Section II of combining various machine learning approaches. He et al.
reviews the most relevant existing works. Section III explains [23] developed a deep learning ensemble model for financial
how we collected and prepared the data. Section IV proposes time series forecasting, applicable to cryptocurrencies, illus-
3
2) Technical Indicators: Technical analysis indicators rep- cryptocurrency price forecasting using the XGBoost regressor
resent a trading discipline utilized to assess investments and model combined with various technical indicators such as
pinpoint trading opportunities through the analysis of statis- EMA, MACD, RSI, and more. The process includes data col-
tical trends derived from trading activities, including price lection and preprocessing, feature engineering, model training
movements and volume [30]. In this study, we explore in- with hyperparameter tuning, and model evaluation. Details of
dicators to feed our machine learning model, such as EMA, this methodology are discussed in the following subsections.
MACD, relative strength index, momentum, price rate of
change, and stochastic oscillator.
Let (x(i) , y (i) ) denotes a single sample/observation, and the
We employ EMA with different periods, where EMA10 ,
set of samples is represented by:
EMA30 , and EMA200 represent the average price of Bitcoin
over the last 10, 30, and 200 periods, respectively. To measure
the magnitude of recent price changes and evaluate overbought
or oversold conditions, we use RSI. Specifically, RSI10 , RSI14 ,
RSI30 , and RSI200 assess price changes over 10, 14, 30, and
200 periods, respectively. In addition, we apply Momentum
(MOM) indicators to gauge the rate of change in Bitcoin
prices, with MOM10 and MOM30 reflecting changes over the n o
last 10 and 30 periods, respectively. S = (x(1) , y (1) ), (x(2) , y (2) ), . . . , (x(m) , y (m) )
Furthermore, we incorporate MACD, a trend-following
momentum indicator that illustrates the relationship between
two moving averages of Bitcoin prices. Additionally, we use
%K10, %K30, and %K200 as components of the stochastic
oscillator, which compare the current price of Bitcoin to its
price range over the last 10, 30, and 200 periods, respectively.
Finally, we include the Percentage Rate of Change with 9
(i)
periods (PROC9 ), measuring the percentage change in Bitcoin where x(i) ∈ Rn and y (i) = Cp .
prices over the last 9 periods.
Considering both technical indicators and historical data for
IV. M ETHODOLOGY price prediction necessitates the integration of diverse datasets.
This section details the proposed methodology for our To achieve this, we combine technical indicators and historical
machine learning approach to cryptocurrency price forecast- data as inputs to our model. The feature vector at a given time
ing. Algorithm 1 outlines our machine learning approach for t can be expressed as follows:
5
Algorithm 1 Cryptocurrency price forecasting using XGBoost regressor and technical indicators.
1: Input: Historical market data H = {h1 , h2 , ..., hT }, Technical indicators T = {t1 , t2 , ..., tN }, Target variable Y = {y1 , y2 , ..., yT }
2: Output: Trained XGBoost regressor model M
3: procedure DATA P REPARATION
4: // Collect and preprocess data
5: D ← Collect data from Binance API
6: Dtrain , Dtest ← Split data into training and testing
7: // Scale features
8: Dtrain ← StandardScaler.fit transform(Dtrain )
9: Dtest ← StandardScaler.transform(Dtest )
10: end procedure
11: procedure F EATURE E NGINEERING
12: // Extract historical data features
13: H ← Extract historical data features from D
14: // Calculate technical indicators: T E
15: T ← Calculate T E (EMA, MACD, RSI, etc.)
16: // Combine features
17: X ←H ∪T
18: end procedure
19: procedure M ODEL T RAINING
20: // Initialize XGBoost regressor
21: M ← XGBoost Regressor
22: // Define hyperparameter grid
23: G ← {η, Dmax , N, λ, α, γ, S, C}
24: // Perform grid search with cross-validation
25: θ ← GridSearchCV(M, G, scoring=RMSE)
26: // Train model with best hyperparameters
27: M ← M.f it(Xtrain , Ytrain )
28: end procedure
29: procedure M ODEL E VALUATION
30: // Predict on test set
31: Ŷ ← M.predict(Xtest )
32: // CalculateP evaluation metrics
33: MAE ← n1q n i=1 |yi − ŷi |
RMSE ← n1 n
P
i=1 (yi − ŷi )
34: 2
Pn
(y −ŷ )2
35: R2 ← 1 − Pi=1 i i
n (y −ȳ)2
i=1 i
36: // Display results
37: Display (MAE, RMSE, R2 )
38: end procedure
39: return Trained model M
TABLE II: Parameter grid for GridSearchCV. the learning process of algorithms. The table lists various
Parameter Values
hyperparameters commonly used in the XGBoost regressor
model, a popular gradient boosting framework [31]. Each
N 300, 400
η 0.01, 0.1, 0.2 hyperparameter is accompanied by its corresponding values
Dmax 3, 4 that are explored during the grid search process. For instance,
Wmin 1, 3 N represents the number of estimators (trees) in the XGBoost
S 0.8, 1.0
C 0.8, 1.0 model, with values of 300 and 400 being considered. Similarly,
γ 0, 0.1 η denotes the learning rate, with potential values of 0.01, 0.1,
α 0.5, 1 and 0.2.
λ 0.5, 1
Other hyperparameters include Dmax for maximum depth
of trees, Wmin for minimum child weight, S for subsampling
ratio, C for column subsampling ratio, γ for minimum loss
y (1)
reduction required to make further splits, α for L1 regular-
y (2) ization term on weights, and λ for L2 regularization term on
Y = .
weights.
.. This parameter grid serves as a roadmap for systematically
y (m) exploring various combinations of hyperparameters to identify
the optimal configuration for the XGBoost model, thereby
In this case study, the problem is to minimize the cost
enhancing its predictive performance. The best combination
function for XGBoost regressor, which is a regularized finite-
of hyperparameters for the XGBoost model was selected
sum minimization problem defined as:
based on the smallest RMSE, resulting in enhanced predictive
m
X train K
X performance. The chosen parameters are as follows:
min J(Θ) := L(yi , ŷi ) + R(fk ) (3)
Θ C γ η Dmax Wmin N α λ S
i=1 k=1 Θ=
0.8 0 0.2 4 3 300 1 0.5 1.0
Where:
Finally, the RMSE achieved with this parameter combina-
• Θ represents the set of parameters to be learned during
tion is the smallest observed during the hyperparameter tuning
training.
process.
• L(yi , ŷi ) is the loss function that measures the difference
between the true target value yi and the predicted target
V. R ESULTS AND A NALYSIS
balue ŷi for the i-th instance. In the context of this case
study, we employ the mean squared error (MSE) loss In this section, we provide simulations-based evaluations
function, which is expressed as follows: of the proposed machine learning model. In particular, we
compute the Mean Absolute Error (MAE), RMSE, and R-
n
X squared (R2 ).
L(yi , ŷi ) = (yi − ŷi )2 (4)
i=1 n
1X
MAE = |yi − ŷi | (6)
Here, yi is the true target value for sample i, and ŷi is n i=1
the predicted target value for sample i.
• R(fk ) represents the regularization term for each tree to MAE provides a simple and straightforward interpretation
control its complexity. It typically includes both L1 and of the average absolute deviation between the predicted and
L2 regularization. Assuming T is the number of leaves actual values. It is easy to understand and is less sensitive to
in tree fk and wj,k is the weight for leaf j in tree fk , the outliers compared to other metrics like RMSE.
regularization term for tree fk is: v
u n
u1 X
T
1 X 2
T RMSE = t (yi − ŷi )2 (7)
n i=1
X
R(fk ) = γT + λ wj,k + α |wj,k | (5)
2 j=1 j=1
RMSE provides a measure of the average magnitude of
The regularization terms (R(fk )) help control the com- prediction errors in the same units as the target variable. It
plexity of individual trees in the ensemble, preventing penalizes larger errors more heavily than MAE, making it
overfitting. particularly useful when large errors are undesirable.
During training, XGBoost regressor aims to find the set Pn
(yi − ŷi )2
of parameters (Θ) that minimizes the overall cost function. R2 = 1 − Pi=1n 2
(8)
The optimization is typically performed using techniques like i=1 (yi − ȳ)
gradient boosting, which involves iteratively adding weak where ȳ is the mean of the actual values of the target
learners to the ensemble to reduce the residual errors [31]. variable.
Table II presents a parameter grid used in GridSearchCV, R2 Score provides an indication of how well the model fits
a technique for hyperparameter tuning in machine learning the data relative to a simple baseline model (e.g., a model
models. Hyperparameters are predefined settings that control that always predicts the mean). It ranges from 0 to 1, where
7
Fig. 2: Scatter plot showing the residuals against the predicted Fig. 3: Scatter plot of actual vs. predicted values.
values.
higher values indicate a better fit. R2 score is widely used efficacy across the spectrum of actual values, showcasing its
for comparing different models and assessing overall model predictive performance.
performance.
Metric Value
RMSE 59.9504 A. State-of-the-Art Comparison
MAE 46.2229
R2 0.9999 Lastly, this subsection provides a comparison between the
TABLE III: Model evaluation metrics. work proposed in this paper and existing studies in the
literature.
Table III presents key evaluation metrics for our regression Table IV provides a comprehensive comparison of vari-
model. The RMSE is 59.9504, indicating the square root of ous machine learning approaches in financial forecasting and
the average squared difference between predicted and actual trading. Shynkevich et al. [32] leverage machine learning
values. The MAE is 46.2229, indicating the average absolute algorithms on daily stock price time series, achieving op-
difference between predicted and actual values. The model’s timal performance by analyzing different forecast horizons
R2 Score is 0.9999, reflecting an exceptionally strong fit to and input window lengths. Similarly, Liu et al. [2] employ
the data. Overall, the model demonstrates high accuracy and SDAE deep learning models utilizing historical data, public
predictive capability, with low errors and a near-perfect R2 attention, and macroeconomic factors, which result in superior
score. prediction accuracy. In addition, Jaquart et al. [33] implement
Another way to assess the performance of the XGBoost ensemble machine learning models on cryptocurrency market
Regressor model is to analyze the relationship between the data (streamed from CoinGecko [35]), producing statistically
predicted values and the residuals. Let ytest be the true target significant predictions and incorporating a long-short portfolio
values from the test dataset, ŷpred be the predicted target values strategy. Furthermore, Hafid et al. [3] use a Random Forest
from the model, and ε be the residuals calculated as ε = classifier with historical data and a few technical indicators to
ytest − ŷpred . achieve high accuracy in market trend prediction, effectively
Figure 2 shows a scatter plot of the residuals against the signaling buy and sell moments.
predicted values. The plot displays the relationship between Saad et al. [15] integrate economic theories with machine
the predicted values (scaled by 1000) and the residuals (scaled learning, analyzing user and network activity to attain high
by 10). A horizontal dashed line at y = 0 indicates perfect accuracy in price prediction and offer insights into network
prediction, where residuals are centered around zero. The plot dynamics. Moreover, Akyildirim et al. [34] apply SVM, LR,
illustrates the model’s ability to predict accurately across the ANN, and RF algorithms on historical price data and technical
range of predicted values. indicators, demonstrating consistent predictive accuracy and
Furthermore, Figure 3 presents a scatter plot depicting the trend predictability. In contrast, this paper introduces a novel
comparison between predicted values (in 1000s) and actual approach using an XGBoost regressor with technical indicators
values (in 1000s). The diagonal dashed red line represents and historical data, achieving low MAE, RMSE, and an R2
ideal prediction, where actual values align perfectly with value close to 1, thereby contributing a new machine learning
predicted values. This plot offers insight into the model’s strategy to the field.
8
TABLE IV: Comparison of machine learning approaches in financial forecasting and trading.
Paper Methodology Data Utilization Model Performance Contribution to Field
Shynkevich et al. Machine learning al- Daily price time series for stocks Optimal performance with Impact of forecast horizon and
[32] gorithms varied metrics input window length
Liu et al. [2] SDAE, deep learning Historical data, public attention, Superior performance in Improved prediction with SDAE,
macroeconomic environment predictions RMAE, and DA
Jaquart et al. [33] Ensemble machine Cryptocurrency market data Statistically significant pre- Long-short portfolio strategy
learning models dictions
Hafid et al. [3] RF classifier Historical data, few technical in- High accuracy in market Effective market trend prediction
dicators trend prediction (buy & sell)
Saad et al. [15] Integration of eco- User and network activity High accuracy in price pre- Understanding network dynamics
nomic theories with diction
machine learning
Akyildirim et al. [34] SVM, LR, ANN, RF Historical price data, technical Predictive accuracy in price Evidence of predictability in
indicators trends trends
This paper XGBoost regressor Technical indicators, historical Low MAE, RMSE, R2 ≈ 1 Novel machine learning strategy
data