Use of Genetic Algorithm in Algorithmic Trading to Optimize Technical Analysis in the International Stock Market (Forex)

Document Type : Original article

Author

Master of Software Engineering, Azad University, Tehran, Iran

Abstract

Recent studies on financial markets have demonstrated that technical analysis can help us effectively predict the stock market index trend. Business systems are widely used for stock market analysis. This paper uses a genetic algorithm (GA) to develop a stock market trading optimization system. Our proposed system can generate a decision-making strategy for buying, holding, and selling stocks for each day and generate high returns for each stock. The system consists of two stages: removing restricted stocks and producing a stock trading strategy. Accordingly, evolutionary computation, like GA, is highly promising because of its intelligence, flexibility, and search strength (fast and efficient). The multiple-objective nature of the utilized algorithm can be regarded as the center of gravity of the research question. The proper functioning or malfunctioning of the resulting portfolio management can be employed as a benchmark for selecting or discarding the algorithm. On the other hand, the research question is focused on the application of technical analysis indicators. Therefore, both aspects of the research question, namely the multiple-objective nature of the algorithm in terms of the analysis method and technical indicators in terms of features selected for analysis, must be taken into account.

Keywords


Introduction

Technical tools, including moving averages, are used to forecast stock prices. Using two moving averages is the most common way to find buying and selling points on time, requiring two periods. Optimum lengths for both short-term and long-term periods per share vary according to their price trend. Finding these optimum lengths using traditional methods is costly and usually does not lead to a globally optimal solution. The best way is to use GA intelligent tools. This study seeks to use these indicators in mining stock trading rules. A model for optimal portfolio management is obtained using technical and multiple-objective GA (MOGA) indicators to maximize returns and minimize risk.

This paper utilizes fundamental indicators for stock ranking and selection. Recent research, including Silva et al. (2015), has employed technical and fundamental analyses for portfolio management. This research initially extracts fundamental and technical variables by simulating coefficients to form an optimal portfolio. Then, using real-world studies, trading rules are extracted to manage the portfolio.

Background Research

Absolute stock valuation is practically impossible. Therefore, researchers exploit innovative methods such as local search (LS), taboo search (TS), simulated annealing (SA), ant colony optimization (ACO), particle swarm optimization (PSO), and evolutionary algorithms (EA) to solve the problem (Fonseca & Fleming, 1993). Evolutionary algorithms are based on biological evolution, where solutions are encrypted in the form of chromosomes, and the best candidates are selected to create new populations by the fitness function. By repeating the evolutionary process, the best solution to the problem is obtained. This method considers different limitations of the problem in making chromosomes (Streichert et al., 2004).

In these studies, researchers used evolutionary algorithms to optimize portfolio risk and return using objective-based management planning in portfolio management.

In portfolio management, three types of approaches can be used (Magin et al., 2007):

  1. Passive management: In this approach, the investor focuses on following the market index. Investors who use this method believe that overcoming the market and achieving higher returns is impossible. They are trying to diversify their portfolio regardless of finding stocks traded below their actual (intrinsic) value. Here, the investor tries to coordinate its performance with the reference index.
  2. Active management: The investor mainly aims to get higher returns than a reference index in this approach. He tries to increase his portfolio return by buying stocks with high expected returns and selling stocks with low expected returns. In active management, two different methods are used:
  3. A) Value Investing: In this method, investors seek to buy stocks trading below their intrinsic value. He decides whether to buy or sell fundamental stock data, such as book value, dividends, and dividends per share (dps), and compares it to the market price.
  4. B) Growth Investing: In this method, the investor is looking for higher growth stocks. Using stock past-to-future generalization and assuming that the stock price does not change abruptly, the investor increases its return by investing in growing stocks.

In the active portfolio management approach, efforts are made to achieve better performance than the market by doing more transactions. Some researchers have employed technical analysis to mine trading rules, including Hirabayashi et al. (2009).

Research Hypothesis

Portfolio management is done using GA and technical indicators such as relative strength index (RSI) and moving averages (MA). In their research, Ruiz et al. (2014) indicated that a higher return on the market could be achieved by using the moving average of the stock price variable.

In GA, by optimizing the variables on the chromosome, we can minimize risk and maximize return in portfolio management.

Research Methods

This research seeks to design a portfolio management model. The goal is to design a model applicable to achieving an appropriate level of risk and return in the portfolio. For this purpose, technical variables are utilized to mine trading rules. The following technical variables will be employed in this study:

Exponential moving average (EMA)

Exponential moving average (EMA) is a trend-following indicator. It helps determine whether the process has started or is ending its cycle. Moving averages, including EMA, can be calculated in several ways. This method has assigned more weight to more up-to-date information (Murphy, 1999). This variable is calculated using Eq. (1). To use it, we use Table 1.

 

 

Eq. (1)

  • n is the length of the moving average period.
  • X is the stock price.
  • t is the period in question (day, week, or month)

 

Table 1. Assign points to the variable EMA

Event

Decision

Score

In case the price chart is bearish and crosses the EMA

A strong sell signal

-1

In case the EMA chart is bearish

Sell signal

-0.5

In case the EMA chart is bullish

Buy signal

0.5

In case the price chart is bullish and crosses the EMA

Strong buy signal

1

 

 

 

 

 

 

Hall Moving Average (HMA)

This variable is also a trend-following indicator. Hull Moving Average (HMA) has a smoother chart than EMA. The price charts of these variables are closer to each other. It is commonly used in mid-/long-term trades. This variable is calculated using Eq. (2). To use it, we use Table 2.

 

Eq. (2)

  • n is the length of the moving average period.
  • WMA is moving average calculation function.
  • t is the period in question (day, week, or month)

 

Table 2. Assign points to the variable HMA

Event

Decision

Score

The slope of HMA changes from positive to negative

A strong sell signal

-1

The HMA chart is bearish

Sell signal

-0.5

The HMA chart is bullish

Buy signal

0.5

The slope of HMA changes from negative to positive

Strong buy signal

1

 

Rate of price change (ROC)

The price rate of change is one of the indicators measuring the movement, expressing the percentage difference between the current closing price of a stock and the price of the previous n periods. This indicator indicates the rate of change in the price of a share. A rapid rise or fall in a given stock's price can indicate that it has been overbought or oversold. This variable is calculated using Eq. (3). To use it, we use Table 3.

 

Eq. (3)

  • n is number the period in question.
  • Xt is the closing price of a share in period t.

 

Table 3. Assign points to the variable ROC

Event

Decision

Score

In case the ROC chart is bearish and tends toward less than zero.

A strong sell signal

-1

In case of a bullish divergence, bullish ROC, and bearish price

Sell signal

-0.5

In case of a bearish divergence, bearish ROC, and bullish price

Buy signal

0.5

In case the ROC chart is bullish and tends toward more than zero.

Strong buy signal

1

 

Relative Strength Index (RSI)

RSI is one of the indicators measuring movement. It compares the magnitude of recent stock gains with recent stock losses to find overbought and oversold conditions. It also produces early signals, though they must be used with other indicators (Murphy, 1999). This variable is calculated using Eq. (4). To use it, we use Table 4.

 

Eq. (4).

  • RS is the average profit divided by the average loss.
  • t is the period in question.

                                                            

Table 4. Assign points to the variable RSI

Event

Decision

Score

In case the bearish RSI chart tends to be less than 70

A strong sell signal

-1

In case the RSI chart becomes bearish in upper and lower domains (bounds)

Sell signal

-0.5

In case the RSI chart becomes bullish in upper and lower domains (bounds)

Buy signal

0.5

In case the bullish RSI chart tends to be less than 30

Strong buy signal

1

 

Moving average convergence divergence (MACD)

Moving average convergence divergence (MACD) is also a trend-following indicator. As one of the reliable indicators of the market, it is equal to the difference between long-/short-term averages. This variable is calculated using Eq. (5). To use it, we use Table 5.

 

Eq. (5)

  • S is the number of shorter moving average periods.
  • l is the number of longer moving average periods.

 

Table 5. Assign points to the variable MACD

Event

Decision

Score

In case the bearish chart tends to be less than zero

A strong sell signal

-1

In case the negative part of the chart is bearish

Sell signal

-0.5

In case the positive part of the chart is bullish

Buy signal

0.5

In case the bullish chart tends to be more than zero

Strong buy signal

1

 

True strength index (TSI)

True strength index is also an indicator measuring the movement that simultaneously determines the trend and overbought and oversold conditions. This variable is calculated using Eq. (6). To use it, we use Table 6.

 

 

 

 

Eq. (6)

  • Xt is the price of a share in period t.
  • r is the number of periods associated with the first EMA
  • s is the number of periods associated with the second EMA
  • n is the number of periods associated with the trigger line

                                             

Table 6. Assign points to the variable TSI

Event

Decision

Score

In case the TSI chart crosses the trigger line in the overbought region and goes lower

A strong sell signal

-1

In case the TSI chart is bearish between the upper and lower bounds

Sell signal

-0.5

In case the TSI chart is bullish between the upper and lower bounds

Buy signal

0.5

In case the TSI chart crosses the trigger line in the oversold region and goes higher

Strong buy signal

1

 

Chromosome construction

N

S

B

Wn

W3

W2

W1

A chromosome was designed as follows:

 

 

important factors of technical variables

 

 

wherein:

  • B is the restriction on the sale of shares.
  • S is restrictions on shareholding.
  • N is the optimal number of shares in the portfolio.

Initially, the optimization problem started with an initial solution. For this purpose, hypothetical values were assigned to the important factors of technical variables, restrictions on the sale of shares, restrictions on shareholding, and the optimal number of shares in the portfolio. The required variables for each of the stocks in the portfolio were calculated using Eq. (7).

 

 

Eq. (7)

Then, the decision to buy or sell the share was made using the following rules:

If A>B for a given share, in case there is a surplus of money to invest, the amount   is allocated to the purchase of that share. Here, N is the optimal default value for the number of shares in the portfolio, and n is the number of shares.

If A

We use GA to optimize the variables on the chromosome. To this aim, we utilized multi-objective GA to minimize risk and maximize returns. Consequently, the necessary rules for optimal portfolio management at different levels of risk and return are obtained. After extracting the optimum coefficients of chromosomes, we evaluate portfolio performance in the test period.

 

Conclusion

Herein, in response to the main question, "how will multi-objective intelligent algorithms help portfolio formation?" portfolio management guidelines were initially extracted to optimize risk and return using technical and GA analysis in the training period. Afterward, the performance of the results obtained from the model training period was tested. The multiple-objective nature of the utilized algorithm can be regarded as the center of gravity of the research question. The proper functioning or malfunctioning of the resulting portfolio management can be used as a benchmark for selecting or discarding the algorithm. On the other hand, the research question is focused on the application of technical analysis indicators. Therefore, in conclusion, both aspects of the research question, namely the multiple-objective nature of the algorithm in terms of analysis method and technical indicators in terms of features selected for analysis, must be taken into account. Investors looking for less risk should include more stocks in their portfolios. They should wait for the technical indicators to give strong buy signals at the time of purchase, act as soon as possible and not wait for strong sell signals when selling the stock.

On the other hand, investors looking for higher returns should include fewer stocks in their portfolios and buy less strong signals. They also should wait longer when selling and wait for strong sell signals. As shown, investors with varying degrees of risk-taking can use technical analysis in portfolio management to achieve greater returns than market returns.

 

 

 

Ethical considerations

The author has completely considered ethical issues, including informed consent, plagiarism, data fabrication, misconduct, and/or falsification, double publication and/or redundancy, submission, etc.

Data availability

The dataset generated and analyzed during the current study is available from the corresponding author on reasonable request.

Fonseca, C.M. & Fleming, P.J. (1993). “Genetic Algorithms for Multiobjective Optimization: Formulation Discussion and Generalization”. Paper presented at the ICGA.
Hirabayashi, A.; Aranha, C. & Iba, H. (2009). “Optimization of the trading rule in foreign exchange using genetic algorithm”. Paper presented at the Proceedings of the 11th Annual conference on Genetic and evolutionary computation.
Maginn, J.L.; Tuttle, D.L.; McLeavey, D.W. & Pinto, J.E. (2007). Managing investment portfolios: a dynamic process. Vol. 3, John Wiley & Sons.
Murphy, J.J. (1999). Technical Analysis of the Futures Markets: A Comprehensive Guide to Trad-ing Methods and Applications, New York Institute of Finance: Prentice-Hall.
Ruiz, V.; Pérez, M.A. & Olasolo, A. (2014). “Dynamic portfolio management strategies based on the use of moving averages”. Procedia-Social and Behavioral Sciences, 109: 1277- 1281.
Silva, A.; Neves, R. & Horta, N. (2015). “A hybrid approach to portfolio composition based on fundamental and technical indicators”. Expert Systems with Applications, 42(4): 2036- 2048.
Streichert, F.; Ulmer, H. & Zell, A. (2004). “Evaluating a hybrid encoding and three crossover operators on the constrained portfolio selection problem”. Paper presented at the Evolutionary Computation, CEC2004, Congress on.