Evolving and combining technical indicators to generate trading strategies

Technical analysis is a widely used approach for trading securities. Various indicators are used, such as moving average, stochastic oscillator and relative strength index. Applications of these indicators are typically based on experiences and rules of thumb which hardly are effective in general. This paper presents a technique for evolving indicator parameters using Non-Dominated Sorting Genetic Algorithm II and combining the indicators to generate a trading strategy. Experiments are conducted using actual stocks from the Stock Exchange of Thailand show that the proposed technique generates trading strategies that outperform other well-known techniques and is applicable to real world security trading.


Introduction
Traditional investment instruments include trading bonds, shares, futures, options, foreign exchanges, and precious metals. Among these securities, trading stocks has been the most popular alternative. Technical analysis focuses on price and volume movements of stocks [1]. Typically, traders use indicators, such as moving average, relative strength index, moving average convergence divergence and stochastic oscillator, to determine buy and sell signals. In addition, they may use chart patterns, such as price movement patterns and candlestick chart patterns, to predict future prices and trends.
It is challenging to achieve positive gains when the market involves a large number of investors trading against each other. The efficient market hypothesis confirms that advantages gained by an investor are vulnerable to be neutralized by others when they have access to the same kind of market information [1]. Investors then try to find extra information to help in trading and consider that historical data may provide indications of future price movements.
Trading rules derived from technical analysis have become the focus of many investors, especially high-frequency traders [2]. The rules have been mostly designed with parameters adopted from general traders' practices, such as the choices of durations in the moving average and relative strength index. Researches have begun to focus their attentions towards optimizing the trading rules. In [3][4][5][6], a genetic algorithm is employed to determine rules' coefficients, and in [7] a multi-objective optimization algorithm is employed to determine the coefficients. Moreover, it is investigated in [8] whether adaptive rules would make the investment more profitable.
This research presents a technique to generate strategies for trading stocks where Non-dominated sorting genetic algorithm II (NSGA-II) [9] is employed to determine the best parameters for technical indicators, and a decision tree is used to combine the indicators to create a strategy. The goal is to create a trading strategy that generates a high return on investment. The experiments are conducted on In the rest of the paper, six technical indicators are introduced; the proposed technique is presented; results of the experimental evaluations are shown and discussed; and finally conclusions are drawn.

Technical indicators used in the study
Six indicators are used in this research which consist of slope, exponential moving average (EMA), moving average convergent divergent (MACD), relative strength index (RSI), stochastic oscillators (STO) and average directional index (ADX).

Slope
Slope is a linear relationship between predictor variable and dependent variable. A slope can be positive or negative and calculated as: where m is the slope, pi is the price at time ti. A sample trading rule based on slope is: buy if the n-day slope is positive. Parameter of this rule is n days.

Exponential moving average
Exponential moving average (EMA) is an average price in a specified period of time. The price of the last price changes and responds faster than simple moving average (SMA). It can be calculated as: where EMAt is EMA at time t, N is length of EMA, P is price at time t. A sample trading rule based on EMA is: buy if EMA N days is greater than price. Parameter of this EMA trading rule is N day.

Moving average convergent divergent
Moving average convergent divergent (MACD) was invented by Gerald Appel in late 1970's. It is used to track the direction of the stock and the force of the stock price by using two moving averages: A sample trading rule based on MACD is: buy if MACD is greater than signal. Parameters of the MACD trading rules are fast length n1, slow length n2, and signal length n3.

Relative strength index
Relative strength index (RSI) is an indicator used to track the direction of a stock and measure the rate of change in stock prices over a period of time. The value is between 0 and 100 and can be calculated as: where RS is the average gain of up periods during a time frame divided by average loss of down periods during the time frame.

Stochastic oscillators
Stochastic oscillators (STO) are indicators used to analyze price movements over time. STO is usually used with short-term trading. The formula is calculated as: where Lowest Low is the lowest low price in n day look back period, and Highest High is the highest high price in n day look back period. A sample trading rule based on STO is: buy if STO is greater than signal. Parameters of the STO trading rule are %K n1, %D n2.

Average directional index
Average directional index (ADX) is used to determine the direction or trend of the price and can be calculated as: where +DM is the positive directional movement, -DM is the negative directional movement. A sample trading rule based on ADX is: buy if the positive directional index is greater than the negative directional index. Parameter of this trading rule is n days.

Proposed technique
In our proposed technique, for each indicator there are two trading rules associated with it. One is for buying, and the other is for selling. Parameters of each indicator are evolved by Non-dominated sorting genetic algorithm-II (NSGA-II) using 2 objective functions. Those rules are then combined by the Chi-square Automatic Interaction Detector (CHAID) algorithm to create a trading strategy. The total of 26 rules and their parameters are shown in Table 1.

Evolving trading rule parameters
In this research, the Non-Dominated Sorting Genetic Algorithm II (NSGA-II) algorithm is used to determine the optimal parameters for the rules. NSGA-II [6] is an optimization algorithm for finding where x is the vector of decision variables, ƒi(x) is a function of the objective i, NSGA-II returns a non-dominated set of answers, called a Pareto optimal set, where any answer x is better or not dominated by another answer y. NSGA-II is an improved version of NSGA which can increase performance in the spread of solution and convergence near true-pareto optimal front. Deb K., Pratap A., Agarwal S. and Meyarivan T. [6] simulated several test problems from previous study using NSGA-II and claimed that this technique outperformed the two elitist MOEAs, i.e., PAES and SPEA. NSGA-II procedure can be described as follow: • Create N initial, random populations.
• Calculate fitness values of each population. • Rank the population by a non-dominated sorting.
• Calculate a Crowding Distance.
• Use a binary tournament selection, binary simulation crossover (SBX) and polynomial mutation for generating an offspring population. • Combine parent population and offspring population.
• Rank the combined population and select N chromosomes by ranking and crowding distance for new generation. • Check the terminating condition. If the condition is met, the last generation is assigned to the best set of solutions; else the procedure will continue by assigning the last population to the initial population. The NSGA-II is used to determine the best parameters for trading rules. The example of chromosome encoding for MACD is shown in Table 2.
where TPBuy is the number of Buy signals from the rule while the actual tomorrow price is up, TPSell is number of Sell signals from the rule while the actual tomorrow price is down, TNBuy is number of Not Buy signals from the rule while the actual tomorrow price is down, TNSell is number of Not Sell signals  5 from the rule while the actual tomorrow price is Not Sell, FNBuy is number of Not Buy signals from the rule while the actual tomorrow price is Not Buy, FNSell is number of Not Sell signals from the rule while the actual tomorrow price is down, FPBuy is number of Buy signals from the rule while the actual tomorrow price is down, FPSell is number of Sell signals from the rule while the actual tomorrow price is up.

Creating trading strategy
The Chi-square Automatic Interaction Detector (CHAID) algorithm [10] is employed to create a trading strategy by combining rules generated from the previous step. CHAID is one of the decision tree algorithm for classification. CHAID is a multi-way tree which can split more than two nodes. The algorithm calculates independent chi-square tests to determine the p-value which is used to select the variable to separate the nodes. The statistics for the chi-square test of independence is where i is 1, 2, …, r and j is 1, 2, …, c, Oij is the observed(actual) frequency value, Eij is the expected frequency value. The CHAID algorithm consists of three steps, i.e., merging, splitting and stopping as follow: • Merging step will calculate a significant test on each categorical independent variable towards the dependent variables and merge most similar categories by selecting the smallest significant or biggest p-value of the category. • Splitting step will separate a node by considering the biggest significant or smallest p-value of independent variables obtained from the merging process. • Stopping step will checks the creation and separation of nodes, with the following requirements: o If the depth of tree reaches the maximum depth limit, the data classification process stops. o If the size of the node is less than the minimum node size, then the node will stop splitting.  The results of testing with data of 12 stocks for 2 most recent years (2016 to 2017) are shown in Table  3. Net Profit is the net profit achieved after completing all trades in the period, and Max System Drawdown is the largest peak to valley percentage decline experienced while trading in the period. We can see that strategies generated from the proposed technique yield the highest returns or are ranked among the top strategies for stocks that are in an upward trend. Also the generated strategies suffer the lowest losses or are among the top strategies for stocks that are in a downward trend. For some stocks, there may be a very few strategies that yield higher returns than our strategies; but there is no common strategy that generally outperforms our technique.

Experimental evaluations
Overall, strategies generated from the proposed technique outperform 8 other comparative techniques which mean that our technique is applicable to trading actual stocks.

Conclusion
This paper presents a technique to generate effective trading strategies using a combination of a multiobjective genetic algorithm and a decision tree. A strategy uses rules from 6 popularly used technical indicators which are slope, exponential moving average, moving average convergence divergence, relative strength index, stochastic oscillator and directional index. Appropriate parameters for each rule are determined by NSGA-II with sensitivity and specificity used as two objective functions. A trading strategy is then created by combining the evolved rules together via the CHAID algorithm. The technique is compared with eight popularly used strategies on 12 random stocks from Stock Exchange of Thailand. The results show that the proposed technique generates trading strategies that outperform other strategies, and the technique is applicable to real-world security trading.