You are on page 1of 6

GoldMiner: A Genetic Programming based

algorithm applied to Brazilian Stock Market

Alexandre Pimenta Eduardo G. Carrano


PPGEE Department of Electrical Engineering
Universidade Federal de Minas Gerais Universidade Federal de Minas Gerais
Belo Horizonte, MG, Brazil Belo Horizonte, MG, Brazil
Department of Computing carrano@cpdee.ufmg.br
Instituto Federal de Minas Gerais
Formiga, MG, Brazil Ciniro Aparecido Leite Nametala
alexandre.pimenta@ifmg.edu.br Department of Engineering and Computing
Instituto Federal de Minas Gerais
Frederico Gadelha Guimares Bambui, MG, Brazil
Department of Electrical Engineering ciniro@ifmg.edu.br
Universidade Federal de Minas Gerais
Belo Horizonte, MG, Brazil Ricardo H. C. Takahashi
fredericoguimaraes@ufmg.br Department of Mathematics
Universidade Federal de Minas Gerais
Belo Horizonte, MG, Brazil
taka@mat.ufmg.br

Abstract The possibility of obtaining financial gain by techniques such as fuzzy logic and neural networks are being
investing in the Stock Markets is a hard task since it is under widely used in an attempt to find satisfactory solutions to this
constant influence of economical, political and social factors. This problem [2].
paper aims to address the financial technical analysis of Stock
Markets, focusing on time series data instead of subjective
parameters. An algorithm based on genetic programming,
The automation of stock trade transactions is now a reality,
named GoldMiner, has been proposed to perform retrospective as it can be observed in several studies [2,4,6,7,9]. The
study in order to get predictions about the best time for trading algorithms developed for this purpose ignore the efficient
top stocks on the BOVESPA, the Brazilian stock exchange market hypothesis and, therefore, have faced difficulties in
market. nonlinear time series forecasting.

Keywords genetic programming, technical analysis, This work proposes a computer system for decision-
exchange market, decision making making in stock trading at the Brazilian exchange market,
BOVESPA. The developed algorithm, named as GoldMiner,
was built based on four main concepts: genetic programming,
I INTRODUCTION technical analysis of the stock market, scanning step, and
The stock market is seen as a possible source of income, trading systems with targets.
but its nature is complex and unstable. Predicting its future
behavior is not a simple task, since the stock market is The article was structured as follows: section II presents
constantly influenced by economical, political and social the work related to the topic. In section III, schemes of
factors. representation, operators of genetic programming and
indicators of technical analysis are. In section IV, an instance
Some economists claim that the efficient market of the problem and the testing framework is shown. Section V
hypothesis is valid [10]. This theory states that the price discusses the results in four BOVESPA top stocks: BOVA11,
variation of a stock behaves like a random walk and therefore VALE5, PETR4, and BBAS3. Finally, some conclusions are
any price forecasting technique consists on a useless effort. presented in Section VI.
Another issue to be considered is the non-linearity of price
variation of an asset on the stock exchange.
II RELATED WORK
Despite the difficulties in financial time series forecasting, In this work, the forecasting techniques employed to stock
many efforts have been destined to provide better markets will be classified in three categories: conventional
understanding about the stock markets. Unconventional

978-1-4799-4518-4/14/$31.00 2014 IEEE 397


methods, market methods and computational intelligence TABLE I USED INDICATORS
based methods.
Indicator Name
Conventional methods are those created mainly by SMA Simple Moving Average
statistics and econometrics. In Atsalakis and K. P. Valavanis EMA Exponential Moving Average
[1] a broad study about these techniques is presented. ARMA BB Bollinger Bands
(autoregressive moving average models), ARIMA CO Chaikin oscillator
(autoregressive integrated moving average), ARCH and OCV Oscillator Chaikin Volatility
AD Accumulation and Distribution
GARCH models are the techniques of this group with better
OBV On Balance Volume
results [1]. WR Williams %R
RSI Relative Strength Index
Market methods can be divided in two types: fundamental S Stochastic
analysis and technical analysis [5]. In fundamental analysis, VO Volume Oscillator
the investor has parameters that define the company financial PO Price Oscillator
position, such as net profit, debt level, payment of dividends,
among others. In technical analysis, the main focus is the
stock value and the trade movement in a given period. These indicators were chosen because they represent the
four main groups of technical analysis indicators: trend
With regard to computational intelligence based methods, followers, oscillators, systems of bands and operators that act
the most used techniques employ neural networks and/or against the trend [5]. Besides, all of them can be obtained
fuzzy logic. In Atsalakis and K. P. Valavanis [2] it is shown a based on the values of the time series, stock price at market
review of 150 publications that use these techniques. In this opening or closing, maximum or minimum of the day and the
study, the authors demonstrate that computational systems financial volume traded.
usually show better results than conventional methods.
Genetic programming (Genetic Programming - GP) [3] has
also been used for this purpose. Up to the authors knowledge, B. Genetic Programming
[4] was the first reference to present a solid employment of In GoldMiner, each individual is composed of two decision
Genetic Programming to forecast the stock market. trees, one for buying and another for selling. The logical
Myszkowski P. B. [6] [7] also uses genetic programming for operators AND, OR, XOR and the indicators of technical
such a kind of prediction. In these references the authors made analysis presented in Table I are used to compose such
experiments on the Forex1 and on the Warsaw stock markets, individuals. Figure 1 shows an example of a buying decision
showing gains above the market. tree and picture 2, a selling decision tree.

III - PROPOSED TOOL


The proposed tool was inspired by studies carried out in
[7], in which genetic programming and technical market
analysis are combined. Four main features distinguish
GoldMiner from the system presented by Myszkowski P.B.:
the trading system, the verification system, the goal system
and the construction and parameterization of the GP. Using
these characteristics, GoldMiner is a computational system
that uses genetic programming with technical analysis to make
decisions and trade stocks in the stock market in an automated
way.

The features of the system are presented below. Figure 1 - buying decision tree

A. Technical Analysis
In GoldMiner, 12 indicators of technical analysis [5] were
used as shown in Table I. All of these indicators are explained
in reference [5].

1
Forex is an equity market that is characterized by high liquidity, a fact that
allowed Myszkowski P.B. [6] [7] to work with daily trading, also called Figure 2 - selling decision tree
Daytrade Operations.

398
The initial population of the GP has been implemented 3. The selected individuals are tested on the
using the Grow method, the crossover by point and the shrink window tests I and II.
mutation [3]. The selection is based on binary tournament.
The configuration and construction of the GP and the training 4. The individual with higher fitness and more than
parameters are shown in Table II. The GP follows the steps of 20% return in window tests I and II is chosen to be
an evolutionary algorithm, as follows: evaluated in the forecasting window.
5. If no individual meets the requirements described
1. Generate the initial population using the Grow in step 4, run the procedure again from a new initial
method. population generated in step 1.
2. Calculate the Fitness of the solutions.
3. Perform selection using binary tournament. The implementation of the verification system was
necessary because it was observed that individuals with best
4. Apply the operators (mutation and crossover) fitness in the training phase can be very ineffective in the
according to the given probabilities. forecasting window. The success rate has risen considerably
5. If he stop criterion is not met, return to step 2. with the use of the verification system, reaching gains up to
30%.

TABLE II CONFIGURATION OF GP

Construction of the Tree Grow Method


Selection Method Tournament 2
Crossover 90%
Mutation 40% (Shrink)
Population 60
Fitness All (profit - losses)
Generation 10

The Fitness is calculated simply by counting the


profitability, i.e., all the profits subtracted from all losses.
Fitness = Sum of all profits - sum all losses

Each individual of the population can lead to three different


decisions: stay, sell or buy. These decisions are taken based on
the buying and selling decision trees, as shown in Table III. It
should be noticed that indicators of technical analysis do not
indicate the same action always, and situations just like the last
row of Table III may occur. Figure 3 window training, checking and forecast

TABLE III DECISION MAKING FOR BUYING AND SELLING D. Trading System and Goals
Buying Decision Tree Selling Decision Tree Action The core of a system that buys and sells stocks on the
False False Stay stock exchange can be divided in two key modules: (i)
True False Buy
False True Sell decision module and (ii) trading module. The decision module
True True Stay is responsible for making decisions that, in the case of
GoldMiner, are guided by two decision trees (buying and
selling trees). The second module addresses how the
C. Verification System negotiation will be executed. It is responsible for selling,
buying or performing shutdown operations.
As stated earlier, GoldMiner differs from studies carried
out by Myszkowski P.B. [7] in four specific points: the The trading module that was built has four characteristics:
targeting system, the system of trading on the stock exchange, It makes use of rented stocks.
the system of prior check solutions and parameterization of
the GP. In Figure 3, the windows of training, checking and It leverages the capital once the value of available assets.
forecasting are presented. It buys and sells on the closing price of the market.
It uses a target profit of 10% as stop condition.
The verification system was created in order to improve
the hit rate. Its operation consists on the following steps: The rented stocks can be traded without necessarily being
owned. This type of operation is a practice in the stock market
1. Evolve the population in the training window. called short selling [5].
2. After the training, ten individuals with the best
fitness are selected.

399
Another common practice in the stock market is to validation and prediction were performed in the range of T =
leverage [5] in purchase and sale transactions. In finance, 30 on 02-24-2010 until T = 1026 on 02-28-2014.
leverage is a general term for any technique applied to
multiply the profitability through debt. The leverage also Table V presents the intervals of training, checking and
increases the operation risks. GoldMiner works leveraging the prediction windows.
stocks by once. TABLE V TRAINING WINDOW, VERIFICATION AND FORECASTING

Window Work Time


Another feature of the trading system is the moment of Window check I T=30 to T=280
buying and selling the stocks. The system works with day Window check II T=281 to T=529
candles [5], and operations are done at market close. This Window training T=530 to T=777
decision was taken due to the high liquidity [5] of the stock Forecasting window T=778 +N to T=1026 N=0,2,4,6... to 60
exchange at the closing of the day.

It has been set that when GoldMiner reaches a profit of The starting date of the forecast window was added by two
10%, then the operations are closed. This profit is compatible days each test, and it was repeated 30 times. This variation in
with the Brazilian economy. The profitability of the Brazilian the forecast window aimed to test the consistency and
government bonds ranged between 8% and 10% annually, robustness of the results achieved by GoldMiner.
considering the forecast from 2013 to early 2014 [11]. The
system aims to return at least 10% of profit on a maximum of V-RESULTS
one hundred and ninety days. In Figure 4 and Table VI it can be observed the operation
of the trade system. Decisions to buy, sell and close for
BOVA112 stock are presented.
IV. INSTANCES AND TESTS
Myszkowski P. B. [7] proposed a software that operates
primarily with daytrade operations [5] in a strong liquidity
market. This fact enables that predictions and negotiations are
carried out at intervals of 5, 10 and 15 minutes. However, on
the BOVESPA, even for top stocks (so-called Blue Chips), any
forecasting system to daytrade operations should consider book
offerings. Indications to buy or to sell a stock at a given
moment can be executed only if there are interested buyers and
sellers available. Therefore, although BOVESPA liquidity is
not critical as in other markets (i.e. Forex), there are two
moments on the day in which buying and selling indications of
blue chips can be executed with high probability: one hour
after the market opening and at the final 30 minutes [8]. This
aspect justified the choice for day candles in GoldMiner, in
which the transactions are performed at the close of market.
Up to now, the GP was tested on BOVESPA with four
stocks, namely BOVA11 VALE5, PETR4 and BBSA3. These
stocks have been selected because they have representation in
BOVESPA index and they present liquidity for being bought
and sold in day candles. In Table IV, a brief description of each
of them is shown. Figure 4 BOVA11, example of the trading system

TABLE IV CODES OF TRADED STOCKS.


TABLE VI EXAMPLE OF THE TRADE SYSTEM WITH BOVA11
Code of the Description
stock
BOVA11 follows BOVESPA index Step Market Initial Final Evolution of Cash on
VALE5 Stocks of Vale SA - site http://www.vale.com Price Position Position Assets Hand
PETR4 Stocks of the company Petrleo Brasileiro SA-
http://www.petrobras.com.br
BBSA3 Stocks of the company - Bank of Brazil SA - 1 R$ 57,15 Stop Sold R$ 1.000,00 R$ 2.000,00
http://www.bb.com.br

Technical analysis uses the time series of a particular stock


as input. Each element is composed of Date, Open Value, 2
BOVESPA Index is a market index that measures the return on an
Maximum Day, Minimum Day, Closing Value and Negotiated investment in a theoretical portfolio calculated by BM & FBOVESPA,
Volume. To perform the experiment, GoldMiner used the same composed of stocks issued by companies that account for over 80% of the
time interval for the four stocks mentioned. The training, number of trades and of the financial volume verified in the cash market
(standard batch) of BM & FBOVESPA.

400
BOVA11 30 27 90% 53,83 days
2 R$ 54,86 Sold Bought R$ 1.040,50 R$ 0,00 PETR4 30 28 93,33% 55,76 days
VALE5 30 27 90% 33,56 days
BBAS3 30 29 96,66% 80,93 days
3 R$ 54,15 Bought Sold R$ 1.026,60 R$ 2.053,00
Boxplot graphs with details about these results can be seen
in Figures 5 and 6. They present the financial returns of the
4 R$ 51,95 Sold Bought R$ 1.067,57 R$ 0,00
stocks and the time required to obtain these returns.
In Figure 5, the variation of the intervals is minimal since
5 R$ 53,69 Bought Stop R$ 1.103,32 R$ 1.130,32
when the profitability reaches 10%, the operations are
terminated. Since the buying and selling operations are
It is important to emphasize that operating costs, rental conducted at market closing, most part of the profits are
costs and fractioning batch were not considered in this work. slightly above 10%. The outliers in Figure 5 also indicate that
It is expected to include such features in future versions of GoldMiner did not reach the set target. In the case of VALE5,
GoldMiner. In general, the rental price of a stock can vary it has been observed loss in some runs.
from 1% to 5% of its value. In the specific case of BOVA11,
the rent value for the period of the operations was 2.5% per
year [8].

Buying and Selling Rules:

An example of the application of optimal buying and


selling rules on the market can be seen in Table VI. From this
example.

Step 1 - On 03-08-2013, the initial capital was R$


1,000.00. The system indicates sale at stock price of R$ 57.15.
After rent and sale of the stock, there is R$ 2,000.00 in cash
and 17.49 stocks.

Step 2 - On 03-20-2013, the system indicates a purchase


with the stock price of R$ 54.86. A total of 36.54 stocks are
purchased, registering a surplus of 18.96 stocks with $0.00 in
cash. The assets increases to R$ 1,040.50. Figure 5 Financial return with GoldMiner to four top stocks.

Step 3 - On 04-11-2013, the system indicates a sale with


the stock price of R$ 54.15. The sale of 37.92 stocks is done,
registering a deficit of -18.96 stocks and R$ 2,053.00 in cash.
The assets decreases to R$ 1,026.60.

Step 4 - On 04-14-2013, the system indicates a purchase


at a stock price of R$ 51.95. The purchase of 39.51 stocks is
done and it a surplus of 20.55 stocks is registered, with $ 0.00
in cash. The assets increases to R$ 1,067.57.

Step 5 - Finally, on 04-23-2013, the system indicates a


sale and the GoldMiner reaches the goal profit of 10%. In this
case, the initial capital is increased from R$ 1,000.00 to R$
1,103.32 within 32 days.

The results obtained with GoldMiner are shown in Table


Figure 6 Time to obtain financial return.
VII. It can be seen that the target was hit in at least 90% of the
tests. As explained earlier, reaching the goal means getting a
In general, the outliers on Figure 6 indicate that the goal
return above 10% in 190 days or less.
was not reached within 190 days. However, some outliers
lower than 190 days have been obtained for PETR4 stock.
TABLE VII CONSOLIDATED RESULTS
The efficiency of the proposed system has been compared
Stock Runs Suc. Ratio Average with the Buy and Hold Strategy [5]. The Buy and Hold
Runs return
time
Strategy states that the best way to form a portfolio of stock

401
investments is to buy assets with positive history and keep VALE5 -1,88 9,21
these stocks for an indefinite period of time. BBAS3 5,12 10,77
Table VIII presents some of the results obtained with
BOVA11 stocks in the Buy and Hold strategy compared to the
results obtained with GoldMiner. The observed results indicate that the proposed algorithm is
able to reach good profit in several situations, being this
strategy considerably better than the Buy and Hold strategy. In
TABLE VIII - BUY AND HOLD VERSUS GOLDMINER TO BOVA11 addition, it was possible to obtain such profit in very short time
on the considered stocks.
Days Buy and GoldMiner Window Forecasting
Hold training window
VI - CONCLUSION
31 -6.45 10.00 530-777 778-1020
In this work, GoldMiner, a decision-making and trading
29 -6.98 10.00 530-777 780-1020 algorithms for the stock market is presented. This system uses
62 -11.93 11.53 530-777 782-1020 genetic programming combined with indicators of financial
technical analysis. The application identifies the time of
62 -17.18 11.33 530-777 784-1020
purchasing and selling stocks, considering the liquidity of the
60 -14.02 11.33 530-777 786-1020 market when it is ner to close.
58 -11.79 11.33 530-777 788-1020 This software was tested on BOVESPA with four top
stocks in different time windows. Initial results are promising;
the target time and profity were reached in more than 90% of
56 -11.98 11.33 530-777 790-1020
the runs.
The combination of genetic programming with technical
54 -11.42 11.33 530-777 792-1020 analysis has previously been treated in the literature, but the
inclusion of the two windows of verification and the trading
52 -9.43 11.33 530-777 794-1020 system features a key distinguishing point for reaching the
results of this work.

50 -11.33 11.62 530-777 796-1020 ACKNOWLEDGEMENTS

48 -11.33 11.47 530-777 798-1020 The authors would like to thank the Brazilian agencies
CAPES, CNPq and FAPEMIG by the financial support.
46 -10.58 11.47 530-777 800-1020
REFERENCES
44 -9.80 11.47 530-777 802-1020 [1] G. S. Atsalakis and K. P. Valavanis Surveying stock market forecasting
techniques - Part I: Conventional methods. In Zopounidis C.,
Computation Optimization in Economics and Finance Research
42 -11.53 11.47 530-777 804-1020 Compendium, p.p. 49-104, Nova Science Publishers, Inc, New York
[2] G. S. Atsalakis and K. P. Valavanis Surveying stock market forecasting
In which: techniques - Part II: Soft computing methods Expert Systems with
- Days the number of days that show the time required for the Applications Vol. 36 35932-5941 April 2009
return achieved by GoldMiner. [3] Koza J.R., "Genetic Programming: On the programming of computer by
means of natural selection", MIT Press,1992
- Buy and Hold - Return obtained after using the Buy and Hold
Strategy [4] Neely C., Weller P., Dittmar R., "Is Technical Analysis in the foreign
exchange market profitable? A Genetic Programming Approach, Jour. of
- GoldMiner - Represents the appreciation/depreciation of the stock finan. and Quantitative Analysis 32(4), pp. 405-426,1997
at the time GoldMiner was used.
[5] Murphy J. "Technical analysis of the financial markets", 1999.
- Window Training - Training Period of GoldMiner. [6] Myszkowski P.B. , Rachwalski L.., Trading rule discovery in warsaw
- Forecasting window - Period in which GoldMiner acts, buying and stock exchange using coevolutionary algorith., ms, 4th inter. symp.
selling stocks. advances in AI and App., Mragwo(poland), pp81-88,2009.
[7] Myszkowski P. B. and Bicz A. Evolutionary algorithm in Forex trade
Table IX shows the average profitability of all Goldminer strategy, Proceedings of the 2010 International Multiconference on 81-
executions compared to the average profitability of the Buy 88 18-20 Oct. 2010
and Hold strategy. [8] http://www.bmfbovespa.com.br
[9] D. Mallick, V. C. Lee and O. Yew Soon. An empirical study of Genetic
Programming generated trading rules in computerized stock trading
TABLE IX BUY AND HOLD X GOLDMINER AVERAGE PROFITABILITY service system, 2008 International Conference on 30 2008-July 2 2008
Stock Buy and Hold GoldMiner [10] Fama, Eugene F. Ecient capital markets: a review of theory and
empirical work. Journal of Finance, 25:383-417, 1970.
BOVA11 -9,69 10,15 [11] http://www.bcb.gov.br
PETR4 -9,37 10,94

402

You might also like