An Automatic Stock Trading System Using Particle Swarm Optimization
An Automatic Stock Trading System Using Particle Swarm Optimization
net/publication/320886731
CITATIONS READS
4 506
4 authors, including:
Some of the authors of this publication are also working on these related projects:
All content following this page was uploaded by Sirapop Nuannimnoi on 11 July 2018.
505
The 14th International Conference ECTI-CON 2017, Phuket, Thailand, 27-30 June 2017
1. Simple Moving Average (SMA). Here, we use 100 days [12]. In that version of PSO, the cognitive coefficient ( ) and
moving average. When the closing price moves above the the social coefficient ( ) were set to vary linearly from the
100-day SMA, a buy signal is generated. A sell signal is beginning to the end. Parameter inertia weight factor also
given when price moves below the SMA line. decreases linearly with time.
2. Two moving averages are commonly used for crossover C. Trading Decision
buy or sell indications. Three pairs of (20, 50), (50, 100),
and (20, 100) are chosen to be our crossovers. The buy During the simulation process, the from equation
signal happens when the shorter line moves above the (1) is used to determine whether to buy, to sell or to hold the
longer line, and the sell signal occurs when vice versa. particular stock on that day with the conditions below.
3. Moving Average Convergence Divergence (MACD). The − If is higher than , then buy.
sell signal occurs when the MACD signal line moves − If is lower than − , then sell.
above the zero line, while the buy signal occurs when the − If is in the range of (− , ), hold the position.
line moves below the zero line.
Parameter is the decision threshold value for making buy/sell
4. Relative Strength Index (RSI) value ranges in 0 to 100. decision and ∈ (-0.1, 1.0). If approaches 1.0, the algorithm
Movements above 70 are considered overbought and suggests buying. In contrast, if approaches -1.0, the algorithm
provides sell signal, while an oversold condition giving a suggests selling.
buy signal would be a move below 30.
5. Stochastic Oscillator (STO) help identify trend velocity Later in the end of each simulation process, the return of
and movement. Sell signal occurs when the %D line moves investment calculated from return = (final portfolio value -
above value 80, while the buy signal occurs when the %D initial portfolio value) / initial portfolio value is used as PSOs
moves below value 20. objective function value. That is the goal is to find the optimal
set of weights for the particular stock that gives the highest return
6. Commodities Channel Index (CCI) identifies new trend or on investment during the considered period.
detect risk condition. CCI over +100 considers a sell signal
while CCI below -100 considers a buy signal. IV. EXPERIMENTATION
7. William Percent Range (%R) identifies the reversal point. To verify the performance of the proposed algorithm, ten
Readings over 80 or under 20 identify overbought (sell highly active stocks from different industries in NYSE, and ten
signal) and oversold (buy signal), respectively. stocks from The Stock Exchange of Thailand (SET) are used in
B. Optimization with PSO an experiment. Historical data of daily stock prices from 2015 to
2016 (about 505 days or records) are downloaded from
Particle Swarm Optimization (PSO) is a swarm intelligence [Link] for used in the simulation. These 20 stocks
technique developed for solving an optimization problem of real have different trends within the tested period; some are sideway,
numbers [10]. A swarm consists of a set of particles, each of while others are downward or upward.
which represents a possible solution. Each particle consists of a
position vector xi = (xi1, xi2,..., xiD) and a velocity vector vi = (vi1, A. Parameters
vi2,..., viD) where D is dimension or the number of decision In this experiment, parameters of PSO are set as widely used.
variables to be optimized. PSO algorithm searches for the and are set to be changed through time from 2.5 to 0.5, and
optimal solution in an iterative manner. In a common practice, from 0.5 to 2.5, respectively [12]. The inertia weight, , is
in each iteration the velocity of each particle is updated by using decreased linearly from 0.9 to 0.4 for more exploitation ability
the latest velocity, the cognitive information of the particle and at the beginning, and then more exploration ability at the end.
the social information from the whole swarm. Then the updated We set the number of particles at 20 and 25 to investigate the
velocity is used for changing the position of such particle. This difference, and the maximum number of allowable objective
iterative updates move the swarm to the optimal solution of the function calls at 150,000 per run as stopping criterion.
problem. Algorithmic details of PSO can be obtained from
literature [10][11][12]. B. Performance Comparison
For this problem, the vector of weights for trading signals is After the algorithm learns from the training dataset, the set
represented in the PSO as the position vector of each particle. In of optimized weights are used for trading with the testing dataset.
this work, we use PSO to search for optimal values of the Then the return on investment during the testing period is
weights from the decision equation shown below. calculated as described in subsection III.C. The obtained return
on investment is compared to those from the simple buy-and-
=( + + ⋯ + )/ ∑
. (1) hold strategy (B&H), and from the signal-follow strategy for
each individual indicators. The signal-follow strategy means
where trading signals , ,
, are generated from each that trading decision (buy, sell or hold) is determined by the only
indicator as described in subsection A. , ,
, are indicator in consideration. Here in this experiment, we compare
weights for each corresponding signal and n is the number of with the following most common trading signals. SMA100,
trading signals in use. These weights are to be optimized by a EMA20/50, EMA20/100, EMA50/100, MACD, RSI, STO, CCI
modified PSO and thus they are the decision variables of the and Williams %R. The buy and sell signals are determined as
optimization problem. Each determines the trading discusses in subsection III.A.
decision for day , and its meaning is described in the next
subsection. The PSO used in this paper was modified from a The experiment also studies the effects of some parameters
version with time-varying acceleration coefficients proposed in in the proposed PSOTrader algorithm. Swarm size of PSO
506
The 14th International Conference ECTI-CON 2017, Phuket, Thailand, 27-30 June 2017
generally has some effects on the optimization result and is V. CONCLUSION AND FUTURE WORKS
normally set between 20 and 30. So we tests the swarm size (n) Many technical trading signals are not very reliable and
of both 20 and 25. From subsection III.C, parameter (decision sometimes even conflicting. This paper proposes a trading
threshold) affects the frequency of trading. More frequent strategy that learns to follow an optimally-weighted set of
trading may suffer higher cost of trading commission fee. In this trading signals instead of following only one signal. The optimal
experiment, we study 3 different values of : 0.1, 0.3 and 0.5. weights are obtained from a learning using a modified PSO
The initial investment is set to 1,000,000 baht for simulating algorithm. Then, the decision to either buy, sell or hold the
each stock. The trading commission fee is set at 0.2% of every current volume of stocks is determined using those weighted
trading amount of both buying and selling orders. For each signals. The experimental results demonstrate that the proposed
buying order, a quarter amount of the whole money is used to algorithm clearly outperforms both buy-and-hold strategy and
buy the stock. For each selling order, a quarter volume of stocks all individual-signal following strategies in terms of return on
(if bought using the whole amount) is sold. This follows the investment.
basic concept of gradual investment, not buying the stock in just
one time using all the money. Possible directions for future works include adaptive setting
of the buying and selling proportion for each trading, further
For each stock, proportion of training data subset and testing
study of other technical signals for a more profitable trading
data subset is 90:10, meaning that the first 90% of dataset is used
decision, and improved optimization algorithms for optimizing
for modeling the PSO trading equation (1), which is then used
the weights. Risk on investment such as the Sharpe ratio or the
for testing the performance with the remaining 10% dataset. For
maximum drawdown, defined as the maximum loss from a peak
other trading strategies, the trading signals are calculated for the
to a trough of a portfolio before a new peak is attained, should
whole dataset, but the trading is simulated only with the 10%
also be considered in the experiment. Also different trading
testing subset, for a fair comparison.
period length (other than 2 years) should be tested.
C. Results and Discussions
REFERENCES
Table I and Table II report returns on investments for stocks
[1] J.M. Berutich, F. Lopez, F. Luna, and D. Quintana. Robust Technical
in NYSE and SET respectively, from trading by using each Trading Strategies Using GP for Algorithmic Portfolio Selection, Expert
strategy. The average returns (for all stocks) are summarized in Systems with Applications, vol. 46, 2016, pp. 307-315.
Table III for the sake of clarity. Excluding our proposed [2] S. Leigh. Essential Technical Analysis: Tools and Techniques to Spot
algorithm, SMA100 signal provides the lowest returns on Market Trends. New York, NY: Wiley, 2002.
average while Williams %R provides the highest returns on [3] K. Massimiliano, Investment using evolutionary learning methods and
average. Thus also shown in Table I and Table II are the numbers technical rules, European Journal of Operational Research, vol. 207,
of buying and selling executed by using SMA100 and Williams December 2010, pp. 1717-1727.
%R signals as well as the proposed PSOTrader algorithm, for [4] F. Allen, R. Karjalainen, Using genetic algorithms to find technical
investigation. trading rules, Journal of Financial Economics, vol. 51, pp. 245271.
[5] L. Núñez-Letamendia, Fitting the control parameters of a genetic
As seen in the tables, trading signals are not reliable to every algorithm: an application to technical trading systems design, European
stock. In some stocks, some trading signals work well, whereas Journal of Operational Research, vol. 179, pp. 847868.
in other stocks, those trading signals underperform. The [6] Y.K. Kwon, B.R. Moon, A hybrid neurogenetic approach for stock
performance of most trading signals for NYSE stocks and for forecasting, IEEE Transactions on Neural Networks, vol. 18, 2007, pp.
SET stocks are mixed. 851864.
From Table III, on average, B&H strategy performs badly (- [7] Y. Chen, et al., A genetic network programming with learning approach
0.95%), but SMA100 performs the worst (-1.01%) for this set of for enhanced stock trading model, Expert Syst. Appl., vol. 36, 2009,
pp.1253712546.
tested stocks. The best performer is our PSOTrader with swarm
[8] A. C. Briza, Prospero C. Naval Jr., Stock trading system based on the
size = 25 and = 0.1, yielding a profit of 3.81%. A simulation multi-objective particle swarm optimization of technical indicators on
with swarm size = 25 works better than 20 for every value. end-of-day market data, Applied Soft Computing, vol. 11, January 2011,
The lower value, the more frequently the algorithm trades pp. 1191-1201.
(buys and sells) as seen in Table I. This takes more chance of [9] H. Yong, K. Liu, X. Zhang, L. Su, E.w.t. Ngai, and M. Liu. "Application
trading that is enough to earn a higher profit, as seen in Table III. of Evolutionary Computation for Rule Discovery in Stock Algorithmic
Trading: A Literature Review." Applied Soft Computing, vol.36, 2015,
However a high frequency of trading does not guarantee a pp. 534-51.
high profitability. Look at the cases of SMA100 and Williams [10] J. Kennedy and R. Eberhart, Particle swarm optimization, In IEEE
%R. SMA100 makes more frequent trading than Williams %R International Conference on Proceedings Neural Networks, Nov-Dec.
does, but SMA100 gives less returns. The reason should be that 1995, vol. 4, pp. 19421948.
both employ only one technical signal which is not reliable. In [11] Y. del Valle, G.K. Venayagamoorthy, S., Mohagheghi, J.-C. Hernandez,
contrast, the trading decision in our PSOTrader depends upon a R.G. Harley, Particle Swarm Optimization: Basic Concepts, Variants and
Applications in Power Systems, IEEE Trans. on Evol. Comp, vol. 12, pp.
combined set of technical signals rather than only one signal. 171 195, April 2008.
The proposed strategy successfully learns the optimal weights [12] A. Ratnaweera, S.K. Halgamuge, and H.C. Watson. Self-Organizing
for each component signal. The obtained weighted signal thus Hierarchical Particle Swarm Optimizer With Time-Varying Acceleration
provides a more efficient trading strategy and results in the Coefficients, IEEE Transactions on Evolutionary Computation, vol. 8,
highest profit on average. 2004, pp. 240-255.
507
The 14th International Conference ECTI-CON 2017, Phuket, Thailand, 27-30 June 2017
508