Designing Strategies for Autonomous Stock Trading Agents using a Random Forest Approach

Machine learning-based autonomous agents are valuable for back-testing stock trading strategies, including algorithmic trading. Several studies in the financial literature have proposed artificial intelligence-based algorithms that support decision making for financial investment, but few studies have provided systematic processes for designing intelligent trading agents. This paper overviews the steps involved in designing agents that forecast stock prices in a trading strategy. These steps include data preprocessing, time-series segmentation, dimensionality reduction, clustering, and others. Our main contributions are: (i) a systematic process that guides the design and development of trading agents, and (ii) a random forest forecasting model. Keywords—Decision trees; financial forecasting; machine learning; random forest; trading agents; trading strategy


I. INTRODUCTION
The success of a stock trading strategy depends on the quality of the information underlying its decision-making process. Most importantly, a successful trading strategy must quickly respond to data inputs for making informed decisions. Forecasting the level of a financial market index or individual stock is a classic problem. The efficient market hypothesis claims that forecasting stock prices is infeasible and that financial time series follow a random walk [1]. However, technical analysts consider that information regarding stocks and stock market indexes are reflected in historical prices and that prices can be forecasted. Financial market time-series forecasting has been widely studied in various disciplines, including finance, economics, engineering, computer science, and mathematics [2].
Financial market time series are commonly analyzed and forecasted by fundamental analysis [3,4] and technical analysis [5][6][7]. The fundamental approach, typically used for long-term decision making on financial investments, focuses on the macroeconomic and company-specific factors that can influence the movements of stock index prices. Fundamental analysis is less common in the financial forecasting literature, because forecasting models that identify the reason(s) for stock price changes are difficult to construct. In contrast, technical analysis recognizes the patterns in a historical financial time series and implicitly assumes that some patterns are exploitable. Analytical and forecasting models based on technical analysis indicators are widely used in the financial literature (Murphy 1986(Murphy , 1999. Several machine learning techniques have been applied to financial time series [5,[8][9][10][11][12][13]. Machine learning techniques identify the patterns in financial time series using a data driven approach. Common machine learning techniques for forecasting financial time series are decision trees [14], random forest (RF) [15][16][17], artificial neural networks (ANNs) [18][19][20][21][22] and evolutionary algorithms, such as genetic algorithms [23][24][25][26][27] and genetic programming (GP) [28][29][30][31][32][33]. Machine learning applications of financial time-series forecasting are reviewed in [34][35].
Automated trading in financial markets, including the foreign exchange (FX) and stock markets, is among the most logical applications of autonomous agents. The systematic design and evaluation of active trading strategies requires a detailed understanding of market mechanisms and operations and an awareness of the issues common to trading strategies. The present study provides a systematic process that guides the design and development of a stock market trading strategy for autonomous trading agents. The study also identifies the essential attributes representing the inputs of a machine learning algorithm for financial forecasting and discusses the potential and impact of these attributes. Designing and evaluating trading agents for financial markets is a non-trivial task giving the complexity and dynamic nature of financial data.
To accomplish our goals, we build a classification model that forecasts stock market prices using attribute selection and an RF algorithm. The characteristics and importance of the attributes are described in detail, and the most relevant attributes for forecasting and hence generating the trading rules are identified. The RF model is trained on the seasonality data's attribute space, directional changing price events, fundamental and technical indicators, and other financial data. Based on the attribute subset obtained from the attribute search space, the classifier computes the forecasting results, which are then used in designing the trading strategy. The impact of number of trees in the RF model and length of the training period are carefully evaluated. The agent strategy (and hence the performance of the generated trading rules) is evaluated in a stock market simulation of three datasets: the daily trade data of Standard and Poor (S&P100), Financial Times Stock Exchange (FTSE 100), and the Deutscher Aktienindex (DAX) indices of German stock. To identify the profitable trading opportunities over various time scales within the trading run, we connect the machine learning algorithm to the appropriate attribute space. Our RF learning algorithm can help traders to generate profitable trading strategies and bridge the gap between attribute selection and trading-strategy design.
The remainder of this paper is structured as follows. Section 2 briefly reviews the related literature. Section 3 *Corresponding Author www.ijacsa.thesai.org systematically describes the agent design process, including attribute selection and the RF algorithm. Section 4 demonstrates and examines the experimental results. Section 5 offers concluding remarks.

II. RELATED WORK
Agent-based modeling is an efficient and cost-effective method for analyzing complex dynamic phenomena, such as trading in financial markets and portfolio management [36][37][38][39]. By finding the optimal attribute sets and associations between the inputs and outputs of agent-based models, we can better understand the dynamics of financial markets and the modeled trading strategy. The financial literature includes several methods that explain and simplify the identification of attribute relationships through sampling and modeling [40][41][42][43][44].
The work by [51] proposed a hybrid system that forecasts the future levels of a stock market index. They introduced a two-stage fusion approach using support vector regression (SVR) in the first stage and an ANN, RF, and SVR in the second stage, yielding three fusion prediction models: SVR-ANN, SVR-RF, and SVR-SVR. The second stage reduces the overall forecasting error. Patel et al. in [51] input ten technical analysis indicators to each forecasting model and emphasized the importance of providing adequate information to the model. To evaluate the effectiveness of their results, they compared the forecasting performance with single-stage models based on ANN, RF, and SVR.
The work by [17] applied deep ANNs, gradient-boosted decision trees (GBDTs), RF, and a combination of these methods (referred to as an ensemble) to the S&P 500 index. They forecasted the daily price returns by binary classification after training the four models on the momentum feature space. The average profits predicted by the ensemble (0.45% per day) were higher than those predicted by deep learning (0.33% per day), GBDT (0.37% per day), and RF (0.43% per day). Their results suggest that machine learning algorithms can provide sustainable profit opportunities in the short term.
The authors in [16] proposed an algorithmic trading strategy for the intraweek FX market trading based on an RF model and Probit regression. The RF model forecasted the medium-term evolution, whereas the Probit model was applied to the technical analysis indicators for predicting the price value of the next day. The profitability of the proposed model was evaluated on USD/EUR, UDS/JPY, and USD/GBP from January 2014 to 2019. Their strategy improved the prediction accuracy and helped to identify the best times of entering and exiting the market.
The work in [14] built an experimental prediction framework based on a tree classifier, which analyzes the prices up to n days earlier, and forecasts whether prices will rise or fall. They trained two algorithms, RF and GBDT (using XGBoost), on technical analysis indicators, and varied the time interval of the analysis. The authors highlighted the importance of flexibility enabled by different features, each giving a specific interpretation.
The present study makes three contributions to the literature. First, it provides a simplified, systematic process for designing trading agents. We provide guidelines for determining the minimal set of attributes needed for an effective design, offering essential insights into agent design. Second, we evaluate the performance of the trading strategy using the existing standards in the financial forecasting literature. Third, we generate a diverse set of trading rules with a focus on attribute selection, allowing an extended training phase and trading rules that best exploit the RF algorithm.

III. METHODS
As mentioned above, trading agents developed by machine learning techniques can forecast financial time-series data. This section sets a framework to describe the main components in designing and developing intelligent trading agents to guide developers and practitioners in building such agents. The works by [52,53] present the operational steps of an intelligent trading system that focuses on forecasting price movements in financial time series.
Simplicity is an essential characteristic of trading agents, because intricate design and complex trading rules will confuse the trading decisions and the dynamics of the trading rules. Forecasting of a price time-series significantly depends on the set of attributes used in the model. Therefore, selecting the right set of attributes for the trading agent is essential. Understanding the role of the attributes and settings can benefit investment profitability.

A. Data Preprocessing
The first step pre-processes the data utilized in the learning process. The main tasks in this step are defining the input and output variables, detecting the outliers, and structuring the data within a specified time interval. Data preprocessing ensures a reliable and accurate forecasting performance.

1) Defining the input and output variables:
When designing a trading agent, we must first define the input variables used by the machine learning technique and the output signals to be forecasted. The sets of input and output variables define the trading rules and control risk, respectively. Technical analysis indicators are considered as the primary modeling inputs. Trading agents for short forecasting horizons are thought to require the technical analysis indicators, whereas those for longterm forecasting mainly use the fundamental input variables. Intraday seasonality statistics are the essential inputs of a forecasting model. The output variables are binary classifications of three class values signifying the buy, sell, and hold signals, which predict the direction and size of price movements throughout the trading period.
2) Outlier detection: After defining the input and output variables, the data are processed to remove noise and outliers, and the data elements are normalized and structured within a specified time interval. This process includes filtering the outliers and eliminating dimensionality from the data, data reduction, normalization, and detrending [54,55]. Some studies [54,55,[56][57][58] suggest removing conditional transactions, late transactions, transactions reported out of sequence, and transactions with special settlement conditions. If these outlier transactions remain, they create noise and possibly erroneous data signals.
3) Time interval: As prices in a financial time series arrive at unequal time intervals, designing the trading rules is a challenging task. In particular, the time interval affects the technical analysis of the time series. Most of the reported intelligent forecasting models compute the technical parameters using the prices (open, high, low, and close) during a trading day, without measuring the magnitude of the price change. Therefore, the input data are considered as continuous data values (an interval-based dataset). This approach has proven effective in several studies [59][60][61][62] that assess whether today's closing asset price will extend yesterday's price trend.
Alternatively, trend-deterministic data can be analyzed within a directional change (DC) event framework [59,61], which treats the input data as discrete entities to be sampled at irregular time intervals. The sampling interval is decided by the magnitude of a price change defined by the trader. The DC approach, which converts the discrete nature of a pricing series into a continuous DC event, has been critical in the FX market [43,[59][60][61]63] and stock markets [28,29,64].

B. Time-Series Segmentation
The time-series data of financial prices are naturally dynamic and unevenly spaced in time. These attributes complicate the analysis phase and forecasting investment opportunities, as discussed in subsection 3.1.3. To reduce the number of dimensions for later analysis, the time series is commonly segmented in a preprocessing step. The resulting time series contains less data, allowing better detection of its significant patterns. Well-known financial time-series segmentation methods are intraday seasonality statistics [15,60], scaling laws [61], and technical analysis indicators [6,7]. By identifying trends and patterns in the segmented data, a trading agent can lower the computational costs and improve the forecasting accuracy.
1) Trend and seasonality: Seasonality analysis identifies periodic patterns in the historical data and formulates predictions based on the behavior patterns of earlier periods. Periodic past behaviors are assumed as the most likely future scenarios [14,28,65]. Therefore, seasonality statistics can reveal the most active trading hours of a stock index, the volume of transactions throughout a given day, and the probable development of trading flows. The intraday seasonality in a time series is defined as the hourly activity changes over a specified period of the time series. For example, the intraday seasonality can be represented by the aggregate number of pricing data points observed in each hour of a single day (hour 0-23), divided by the total number of pricing data points in the same period.
2) Technical analysis indicator: Technical analysis forecasts the prices and returns of actively traded financial instruments, such as individual stocks, market indices, and FX. www.ijacsa.thesai.org This approach assumes that patterns appear in historical prices. In statistical terms, technical indicators measure the various features in the time series of a given stock or market index, such as price trends, volatility, and momentum. Various technical indicators have been used for forecasting future price movements and developing rules of trading decisions [6,66].
In this study, the trading agent was modeled using eight popular technical indicators calculated from historical price data. The calculation formulas of these indicators are given in Table I.
The first technical indicator is the moving average (MA). The MA indicator continuously updates the average price within a specified window of time. In this study, the forecasting model inputs a 10-day MA and provides a short-term prediction. If the current price is above or below the MA, the price trend is upward or downward, respectively.
To understand the trade break out (TBR) indicator, we must define two concepts: support and resistance. At the support and resistance points, the price trend is expected to stop declining and rising, respectively. The TBR assumes that price trends will reverse at the support and resistance points; however, when these points represent a break out, possibly because new information emerges on the market or (for an individual stock) the company, the price trend is expected to continue in the same direction. Traders watching these break-outs will buy when a price trend exceeds the point of resistance and sell when the price trend falls below its support level.
The filter (FLR) technical indicator signifies a buy or sell signal depending on whether the price trend has been reversed by a predefined threshold. For example, if the price rises by a defined percentage of its earlier low value (reversing a downward price trend), the FLR issues a -buy‖ order.
Price volatility (VOL) is the standard deviation calculated from the historical prices over a specified period. The VOL indicator suggests a buying opportunity when the volatility is declining, as the price may be trending upward. In contrast, a cycle of increasing volatility may signify a downward price trend, presenting a -sell‖ opportunity.
Momentum (Mom) is a simple trend-following indicator defining the rate or strength of the price movement. The Mom indicator relates the very recent closing price to the previous closing price over a time interval specified by the analyst. The Mom indicator also determines the momentum MA (MomMA).
Return on investment (ROI) in the trading context (distinct from the ROI of the data in a company's financial statements) relates the trading profits to the strategy's capital. Here, the ROI is the ratio of the price return to invested capital over a specified period. In our decision support system framework, the average price return is determined throughout the previous n days. The ROI is real-valued and can be positive (indicating a profit) or negative (indicating a loss).
The relative strength index (RSI) is a momentum indicator that contrasts the sizes of the recent upward and downward price trends over a defined period. This measure evaluates the rate and change of an asset's price movements. To define an upward price-trend change, we set Uptrend t = p t − p t−1 if pt > p t−1 and 0 otherwise; conversely, we define a downward price trend as Downtrend t = p t−1 -p t if p t < p t−1 and 0 otherwise. The relative strength RSI(n) is then defined as the average of the last n upward price-trend changes divided by the average of the last n downward price-trend changes. The RSI fluctuates between zero (average upward price change = 0) and 100 (downward price change = 0). In typical scenarios, an asset or overall market is overbought when the RSI exceeds 70 and is oversold when the RSI falls below 30.

3) Fundamental analysis:
A fundamental analysis of a time series of securities prices investigates the likely factors affecting the time-series dynamics. These factors are derived from various sources, including financial statements, liquidity measures, market capitalization, and earnings announcements. Several studies have confirmed the relationships between these financial data and future price returns [3,67].
The core mechanism of fundamental analysis is estimating an asset's fundamental value using the financial data from all available sources. A trader can compare a fundamental value with the asset's current price and make a trade decision based on that comparison. If an asset's price exceeds its fundamental value, it is regarded as overvalued, and a sell order is recommended. Conversely, if the price is below its fundamental value, the asset is considered undervalued and a buy is recommended.
The trading strategy can consider the fundamental price value [67] or more than one value [3]. In the present analysis, an agent considers one fundamental price value as in [67]. Throughout the model run, the asset's fundamental value is described by an exogenic random procedure: where f t denotes the fundamental value of the asset price at time t and η t is a normal noise process with mean µ and standard deviation σ. www.ijacsa.thesai.org

C. Dimensionality Reduction
Restricting the number of attributes in the trading-agent model is essential, as not all attributes are valuable for forecasting a trade signal, and some are irrelevant. Reducing the attribute number and feature dimensionality improves the learning time, shortening the computational time, and improving the scalability. This step is particularly important for adaptive learning algorithms, which depend on the learning speed to route the streaming data over a suitable timeframe. Reducing the dimensionality reduces the memory and datastorage requirements, and simplifies the behavior of the trading agents. It also captures the dependencies and correlations among the attribute features.

D. Algorithm Definition
During learning by a machine learning technique, the agents continuously interact with the environment and thus learn the optimal trading rules to achieve a specific goal. The setting involves current price movements and historical data of stock market trading provided by an extensive array of raw data and technical analysis indicators. When selecting a machine learning technique, these historical data and technical indicators are prerequisites for designing agents that observe the price time-series environment and learn the stock trading rules. Most of the total design time is expended in providing the agents with informative data and tools, which is performed in the first three phases (data preprocessing, time-series segmentation, and dimensionality reduction). Although each phase generates inputs for the subsequent phase, the progression is unsteady and moves back and forth between the phases to optimize the result. After processing and reducing the data, the trading rules are generated from the selected input data by a machine learning technique, and the output signals are forecasted.

1) RF methodology:
Forecasting by a machine learning technique is subjected to many factors: processing speed, pattern learning capability, natural interpretation of the trading strategy, knowledge simplification after learning, flexibility and adaptability to changes, and strategic optimizers for the retraining and forecasting horizons. Decision trees and RF are popular algorithms for forecasting price changes and developing automated trading agents. The RF algorithm has been successfully used in feature pruning (attributes in our case) owing to its broad capability and strength. In this study, the RF algorithm generates the trading rules for the trading agent.
RF is a supervised machine learning algorithm involving multiple decision trees on different subspaces of the attribute space. One decision tree connects a decision to a set of descriptive variables or attributes. Each decision tree is formed as a set of variables and conditions, hierarchically structured, and continuously utilized in a dataset. The decision trees are diversified by generating them from several training datasets that are randomly resampled from the original dataset. In financial forecasting, a variety of decision tress offers autonomous numerical estimates of the arbitrage opportunity (i.e., increase in price change) rather than class labels for predictions. The RF algorithm learns the relationships among the following inputs: (a) row data (opening, closing, high and low prices, and the trading volume per day), (b) fundamental analysis information, (c) eight technical indicators, (d) DC price events, and (e) intraday seasonality statistics. The algorithm then generates one of three trading signals (buy, sell, or hold). Fig. 2 depicts the design framework of the forecasting model based on the RF algorithm for stock market prediction. To create a single decision tree, we start from the root node and recursively split the data into two child nodes using a set of rules based on the market domain. The tree-building terminates when a definite stop condition is reached. The RF algorithm selects the attributes by a random process rather than employing all attributes. The resampling is performed by a random module and the combinations of attributes for the data-splitting are optimized to acquire more representative nodes. IF-THEN rules are derived from the trained decision trees by tracing the path from the root node toward each leaf node in each decision tree. The obtained rules are merged into a single decision-rule set. Along each path, the attributes and variables of the nodes are determined under the rule conditions, and the class of the leaf node represents the result of the rules (the trading signals). Fig. 3 shows a possible decision tree generated by the RF algorithm, and the following code derives a decision rule from the decision tree.
2) RF algorithm: The RF algorithm constructs an ensemble model of multiple unpruned decisions from random subsets of attributes that do not overfit the data. In this study, we build a RF of decision trees from bootstrapped training data. Therefore, instead of defining the best split among the entire set of input variables, we extract a subset of randomly selected attributes. In Algorithm 1 below, forests of decision trees are constructed using the training dataset D = {(x 1 , y 1 ), (x 2 , y 2 ), · · ·, (x T , y T )}. The number of attributes is A and the number of trading days is T. Algorithm 1 is the core mechanism of the RF algorithm. www.ijacsa.thesai.org The aim is to find a function f : X→ Y, where X is the attribute space and Y is the output trading signal space. The procedure is outlined below.
1. To form a bootstrap sample, randomly select n data from D drawn with or without replacement from the training dataset.
2. Among the A attributes, select a A attributes to create a decision tree. Using a subset of data ensures variety and avoids the overfitting problem.
3. At each node, randomly select the attributes and optimize the data split (based on the Gini impurity). Note that the smaller the value of Gini D , the higher is the purity of D. The Gini impurity measures a node's splitting quality. At each node N, it is defined as 4. Grow n decision trees to the most considerable extent by reiterating steps 1 to 3 until the maximum number of nodes is reached. The depth of each decision tree is controlled by the allowed maximum depth and the minimum number of required variables and constants at a leaf node. This so-called pruning criterion reduces the underlying complexity of the decision tree.

Define the mean prediction of all individual decision
trees in the forest. The aggregate prediction represents the output at time t.

3) Variable adjustment:
After choosing a machine learning technique, the input variables and output signals are adjusted to fit the desired algorithm. In this phase, the input dataset is decomposed for dividing the forecasting model into smaller sub-models. The machine learning technique (RF in this case) models the relationships among the input variables based on their initial definitions, and produces output signals. In the financial literature, machine learning techniques build the forecasting model using different numbers and types of input variables. Fundamental analysis offers numerous candidate attribute variables, such as firm size, cash flow, liquidity, and profitability. In contrast, technical indicators describe technical attributes such as moving averages, volatility, and momentum. In financial forecasting, the set of input variables for building a forecasting model (and thus designing a trading agent) is unlimited.
4) Architecture configuration: The architecture is configured by adjusting the set of input and output variables of the chosen machine learning technique. A practical design for a trading agent requires three main components. The first specifies the trading rules governing the entry and exit times of trades. Exploiting the forecasted output signals, these trading rules discover the optimal time to buy or sell an asset's quantity to maximize the investment profit. The second component evaluates the forecast and operates the risk control system. This component requires a set of rules that evaluate and protect the invested assets. Evaluation and risk control strategies include defining the profit objectives and risk appetites intended for each trade (e.g., maximum loss). The third component is the trading management mechanism, which manages the trading position size, resources, time window, and risk. This component emphasizes the impact of real-world constraints, such as transaction costs, news, transaction volume limits, and volatility [52].

E. Training
Once an agent has been developed from a training dataset and its variables have been adjusted on the validation set, the agent-trading strategy must be trained and evaluated on a new subset of the time-series data (namely, the test dataset). Here, a new subgroup is employed because the agent-trading strategy may be influenced by the training dataset. Therefore, it may overfit the data, producing an unrealistically high profitability. The training phase proceeds in three steps: testing the www.ijacsa.thesai.org performance of the methods, determining the forecasting horizon, and estimating the retraining period.

1) Training dataset size:
Most of the financial forecasting models divide a financial time series into training and testing datasets. This approach is static because the same data are used throughout the complete testing period, with no updates. Splitting the data into training and testing datasets reduces the quantity of data available for learning and the resulting design may exclude various significant price events. Moreover, the forecasting performance sometimes depends on the time-series distribution used in the training and test datasets. This dependence is problematic, as the performance can be driven artificially high. Alternatively, the forecasting model can be trained by dynamic sliding-window cross-validation. In this approach, several evaluations are performed on small amounts of sequentially ordered data. Because the training-time window size may strongly affect the forecasting accuracy, we trialed sliding windows of different sizes.
2) Retraining period: Periodic retraining assesses the trading rules generated by the agent, thereby improving the average forecasting accuracy and accumulated profits. However, increasing the size of the training dataset over time may affect the forecasting performance. The price trends must be forecasted using an up-to-date training dataset representing the current market conditions. When designing the agent, the trading rules should be updated whenever the agent's wealth drops below a defined threshold value. This approach has been practiced in several automated agent-trading strategies [73].

F. Forecasting Evaluation
Among the many performance measures of forecasting models for trading agents, the ROI is probably the most popular choice.

1) Evaluation measure:
The ROI represents the realized profits or losses of the trading strategy relative to the amount of money invested in the design. In our analysis, each trading decision was evaluated using the mean absolute percentage error (MAPE), mean absolute error (MAE), relative root mean squared error (rRMSE), and mean squared error, which are respectively calculated as follows: Here, R t and F t are the actual and forecasted values, respectively.
The investment performance was evaluated with three performance indicators: Rate of correctness (RC), rate of missing chances (RMC), and rate of failure (RoF). These performance measures are calculated by Eqs. 7, 8, and 9, respectively. The four criteria in these equations are defined in the contingency table (Table II). 2) Risk control: A successful trading agent requires a risk control mechanism that protects the invested assets by a set of trading rules such as a stop-loss limit order. Risk control strategies must define the profit objectives and risk appetites of each trade. Here we employed the Sharpe ratio (SR), which assesses the investment performance relative to risk. The SR is calculated by Eq. 10. Note that the higher the SR, the better is the risk-adjusted return, and (by inference) the better are the trading rules. According to Eq. 10, √ where µ is the mean of the portfolio returns, RF is the riskfree rate, σ is the portfolio's standard deviation, and n is the number of observations in the time series.

IV. RESULT
The previous section described the essential features and parameters of an autonomous trading agent. To handle the various possible mixtures of such features and parameters, a systematic design approach is required for an autonomous trading agent. This section details the verification experiments of the proposed approach. The results are expected to assist the design of autonomous trading agents and decision support systems for investment arbitrage in stock markets.

A. Data
The effectiveness of the proposed autonomous trading-agent design was tested on three stock market indices: Yahoo Finance S&P500 (US) [74], Yahoo Finance FTSE100 (UK) [75], and the Yahoo Finance DAX (GER) [76]. For the analysis, we extracted four years of historical data from September 1, 2015 to August 30, 2019. These three market indices were chosen for their popularity and strong liquidity in the global financial market. The study period covers various market conditions and time-series scenarios. The technical indicators were computed from the close, high, low, and opening levels of the three stock indices. All data were obtained from the Yahoo Finance websites.

B. Trading Strategies for Comparison
To verify the efficiency and effectiveness of the autonomous trading agent, we compared its trading performance with those of five well-known trading strategies: Buy and Hold (B&H), www.ijacsa.thesai.org technical analysis (TA), fundamental analysis (FA), zerointelligence with constraint agents (ZI), and GP. The B&H strategy is a reactive investment strategy that emphasizes buying several stock assets and holding them for an extended period, regardless of fluctuations in the market-price time series. The TA strategy forecasts an asset's price movement using TA indicators (see subsection 3.2.2), and the FA strategy analyzes the potential influencing factors of the asset-price time series (subsection 3.2.3). The ZI randomly places orders under given budget constraints. However, such benchmarking is based only on the architecture of the proposed RF agent. Whether our RF proposal outperforms the existing machine learning and artificial intelligence techniques is an open question. Does the proposed RF-based agent architecture effectively outperform the other architectures based on other forecasting techniques? To answer this question, we added a fifth trading strategy based on GP, an evolutionary approach that extends genetic algorithms. GP evolves the programs of artificial intelligence to generate applicable solutions in a decision tree structure. Decision trees are generated by combining various defined functions and terminals. Both the function and terminal sets are derived from the problem search domain. Here we employed the GP trading agent designed by [29], which searches over the space of TA indicators.

C. Trading Performance Results
In the experiments, we varied the time horizon and combination of input attributes (parameters). The reported values were averaged over 30 runs in each trading model. After determining the agent design's most significant attributes based on the forecasting performance, the dataset was divided into two subsets using sliding-window cross-validation. The subsets were used for training and validating the RF algorithm. After adjusting the model settings, we compared the performances of our RF-based agent design and the five competing trading agents: B&H, TA, FA, ZI, and GP. In the first evaluation, we measured the investment effectiveness of the RF-based agent using the ROI and SR as the investment performance metrics (a higher value indicates better performance). Table III compares the performance data of the six agents for each market index. The trading agent based on the RF model significantly outperformed the other trading agents over the four-year study period. The RF model achieved an average annualized profit of 13.22%. On the S&P500 data, the SR of the RF-based model was 1.72, followed by 0.97 for the B&H strategy, 1.21 for the TA agent, and 1.12 for the GP agent. The RF strategy also delivered the highest ROI and SR on the FTSE100 and DAX datasets. The results of the B&H and FA agents were comparable, as demonstrated in Table III. The RF, TA, and GP agents employing the TA indicators significantly outperformed the remaining three trading agents. Therefore, generating the trading rules using TA indicators promises high investment results. However, determining the possibility of future trading achievement based on the ideal combination of TA indicators and its time horizon is a challenging task.
The RF algorithm limits the number of attributes used without compromising the profitability of the trading strategy. In particular, the RF-based trading agent can outperform the B&H approach when the selected attributes (inputs to the machine learning algorithm) are appropriately combined.
In summary, after studying the literature on automated trading agents and testing our RF-based approach, we conclude that a developed level of intelligence and diversity of the chosen attributes is essential for boosting the investment returns of the trading agent. Learning and adaptation intelligence can improve the ability of automated agents to process the market information and dynamics of price time series and thereby adapt to financial market events and different circumstances.

D. Forecasting Accuracy
In the second part of the study, we analyzed the forecasting accuracy of the RF and GP agents. As shown in Table IV, the RF-and GP-based forecasting models achieved strong RC, RMC, and RoF. For the RF agents, the RC ranged from 72.08% to 81.92% (average 77.22%), the RMC varied from 17.11% to 24.46% (average 20.90%), and the RoF ranged from 5.84% to 18.45% (average 13.18%). Clearly, the RF agents outperformed the GP agents in forecasting accuracy. We must clarify that the results in Table III influenced the accuracy of the forecasting performance. Moreover, selecting the right set of attributes improved the accuracy of classifying the training dataset, but does not guarantee better trading-strategy performance. Our findings suggest a substantial relationship between the profits generated and the fundamental/technical indicators, which may be important for creating practical trading rules.
The results of the RF algorithm emphasize the importance of attribute selection (data preprocessing, time-series segmentation, dimensionality reduction, and variable adjustments) as a preprocessing stage. The proposed agent design can assist (to some extent) the forecasting of stock prices. Accordingly, we conclude that when designing a trading agent to predict stock prices, we must examine the attribute types used to build the trading strategy and their values.

E. Statistical Significance Test
The reliability and precision of the findings were tested in a paired t-test, which contrasts the means of two agents' datasets with variable ROIs. For this purpose, we divided the dataset into two equal subsets. An adequate paired t-test requires a roughly normally distributed variation of the data. Therefore, the normality was checked through a normal probability plot and a Jarque-Bera test.
The paired t-test was applied by specifying two values: the t-statistic value tsv and the t-critical value (two-tail variable) tc under the 95% confidence level. The first and second datasets were represented by A and B, respectively, where a ∈ A and b ∈ B. The means of A and B were denoted as m A and m B , respectively. Both datasets were of equal size n. The numeral i indexed the record in both datasets, where 1≤ i ≤ n. The null hypothesis stated that B is pairwise statistically equivalent to A, where m A − m B = 0. We define a new dataset D with n records, in which each form is specified by d i = a ib i , d i ∈ D. The mean and standard deviation of this set were denoted by m D and σ, respectively. The tsv was calculated as ( ) (11) where SE(µ D ) is the standard error of the mean difference, defined as The t-critical value was determined from the t-distribution table. If |tsv|< |tc|, the difference between datasets A and B is trivial, and we can accept the null hypothesis. Table V shows the t-statistic (tsv) and Pearson correlation (corr) values of the paired t-test at the 95% confidence level. The results of the six trading agents on the first part of the dataset approximated those on the second part of the dataset with 95% confidence. Furthermore, the correlation coefficients demonstrate a high level of dependency between the results of the two datasets.

V. DISCUSSION
This study designed a simple automated intelligence agent for trading in the financial markets. An algorithmic (automated) trading agent is vital for developing trading strategies and forecasting models. This study provides a systematic approach for designing a trading agent using the RF approach. The approach was experimentally tested on four years of historical data of three stock indices: S&P500, FTSE100, and DAX.
The contribution of our work was twofold. First, we presented the main steps of building an intelligent automated trading agent for stock market trading. We categorized the main stages into five essential components: (i) the primary goal of the trading strategy (e.g., short term, long term, and high frequency); (ii) the context of the financial market domain; (iii) the input variables used; (iv) the proposed machine learning technique, and (v) diversity of the data sources.
Our second contribution was the design and implementation of adaptive RF trading agents for financial markets using various parameters (TA indicators and fundamental factors). It was hypothesized that trading agents learn, adapt, and identify periodic patterns in price time series. The RF trading agent evolved the trading rules of financial market trading investment. After the learning process, the RF algorithm achieved higher returns and better forecasting accuracy than the Buy & Hold trading strategy and agents based on technical and FA. We believe that this outperformance was achieved by selecting the right set of attributes (i.e., the inputs to the machine learning algorithm).
In future research, we could adapt machine learning techniques to dynamically select the appropriate set of attributes based on the market conditions. For this purpose, data reduction and segmentation could be combined with the sequential steps of developing an intelligent selection approach, as proposed in this study.
As another research direction, we could systematically test diverse intelligence trading strategies in the automated intelligence-based approach, and hence assess the required level of intelligence for designing autonomous trading agents. Examining and understanding the impact of complexity on a trading strategy might improve the effectiveness of that strategy. Nonetheless, a straightforward trading-strategy design, such as that developed in this study, might be more suitable than a complex one. To conclude, we should explore different artificial intelligence techniques and their effects on the effectiveness and trading rules of the strategy.