an innovative modification of Particle Swarm Optimization algorithm for discrete and continuous ... tested on a simple investment strategy, based on one of the well known indicators Rate of Change ... were performed on a data gathered from one of the most important of currency pairs â EURUSD. ..... 676.2 â 11 minutes.
Journal of Theoretical and Applied Computer Science ISSN 2299-2634 (printed), 2300-5653 (online)
Vol. 8, No. 4, 2014, pp. 45–55 http://www.jtacs.org
Improved Particle Swarm Optimization method for investment strategies parameters computing Aneta Bera, Dariusz Sychel, Bartłomiej Sacharski Faculty of Computer Science and Information Technology, West Pomeranian University of Technology, Szczecin, Poland {abera, dsychel, bsacharski}@wi.zut.edu.pl
Abstract:
The paper studies the problem of computing the parameters for investment strategies. Proposed is an innovative modification of Particle Swarm Optimization algorithm for discrete and continuous data. The article shows how discrete and continuous version of the algorithm can be combined in order to achieve the best results. Moreover, the presented algorithm is expanded by a multi-swarm mechanism which allows to achieve better results in a fixed time. The proposed algorithm was tested on a simple investment strategy, based on one of the well known indicators Rate of Change (further referred as ROC) that uses a mixture of discrete and continuous parameters. All the tests were performed on a data gathered from one of the most important of currency pairs — EURUSD.
Keywords:
machine learning, pattern recognition, time series, investment strategies, algorithmic trading, forecasting, prediction
1. Introduction Algorithmic trading [1], or algotrading, is a way to automate trading using various strategies that can decide whether to take any actions — to buy, or to sell stocks. These strategies often need to be reevaluated and re-trained because of ever-changing market characteristics. Furthermore it is expected of them to provide highly accurate decisions in short time. Investment strategies are a subject of intensive research and many publications. The example of such work is article written by D. Paraschiv et. al. [2]. Authors introduce algorithmic trading on artificial stock markets and present a deep analysis of existing approaches. In this article new framework of the artificial market is proposed. R. Barbosa et. al. in their work present an investment strategy based on five agents [3]. Authors consider results to be promising. One of commonly used algorithm for optimization problems is Particle Swarm Optimization (further referred to as PSO). It has been adapted in many various applications. In [4] PSO is used in analysis of outcomes of construction claims. I. Ibrahim et. al. proposed to use the mentioned algorithm for combinatorial optimization problems [5]. Existing articles show the possibility of using PSO in various classification problems. The example of such a research is [6]. A discrete version of this algorithm is used for rules induction in [7]. There are many ways of applying PSO to discrete problems [8], however combining discrete and continuous versions is not common. Considering that investment strategies became more complex there is a need for a method that would find parameters values that maximize strategies efficiency and would do that in a
46
Aneta Bera, Dariusz Sychel, Bartłomiej Sacharski
reasonable time. The problem of creating strategies and finding optimal parameters values has been the subject of many researches. The PSO algorithm is used for strategies optimization for example by F. Wang et. al [9]. They modified the classic version of the algorithm to reduce the time needed to achieve results by reducing inertia weight overtime. Wilinski et.al. use the classic PSO for searching parameters in broad spaces to find strategy parameters in considerably short time [10]. PSO in article written by J. Nenortaite et. al. [11] is used to train artificial neural networks (further referred to as ANN). Applying of ANN allows to predict a possible profit for the next day based on historical values. The aim of this article is to adapt a modified version of PSO algorithm for optimization of investment strategies parameters. The presented method is a combination of continuous and discrete versions of PSO and uses multiple swarms to increase its performance and accuracy.
2. Improved Particle Swarm Optimization Method PSO is a popular method for optimizing various problems. However, many applications require adapting PSO so it meets certain conditions related to the nature of the optimized problems. In case of investment strategies one can meet with continous and discrete problems at the same time, so a method that combines both types of attributes is required.
2.1. Basic Particle Swarm Optimization Algorithm The classic PSO algorithm [12] was presented as a method for optimizing continuous nonlinear functions. Initially, particles, which form a swarm, traverse given search-space looking for best positions (that are calculated using evaluation function) which represent possible best solutions. Each of them remembers its best known position. Information about global best position is also saved. In each iteration, particles move depending on their individuality ϕp and swarm’s influence ϕg , towards either particle’s own best known position or swarm’s best known position. This step is repeated until termination criterion is met (for instance: a certain number of iterations, solution has reached certain threshold, or swarm’s best known position has not changed for a certain amount of iterations). The classic version of algorithm can be represented as a list of the following steps: 1. Initialize swarm — for each of the particles: 1.1 use uniformly distributed random vector, that is limited by lower and upper boundaries of search space, as a starting position x, 1.2 use generated position as particle’s best known position pbest , 1.3 initialize particle’s velocity v with random values, 2. Find best position inside the swarm and use it as swarm’s best known position gbest , 3. While termination criterion is not met, repeat for each particle in every iteration i: 3.1 generate two uniformly distributed random values: Rp (0, 1) and Rg (0, 1), 3.2 for each dimension j: update each particle’s velocity: vi+1,j = ωvi,j + ϕp Rp (pbest,j − xi,j ) + ϕg Rg (gbest,j − xi,j ),
(1)
including velocity limits: vi+1,j
vmin,j , vi+1,j < vmin,j vmax,j , vi+1,j > vmax,j , = vi+1,j , otherwise
(2)
Improved Particle Swarm Optimization method for investment strategies parameters computing
47
3.3 update particle’s position: xi+1 = xi + vi+1 , including data bounds for dimension j: xmin,j , xi+1,j < xmin,j xmax,j , xi+1,j > xmax,j , xi+1,j = xi+1,j , otherwise
(3)
(4)
3.4 if f (xi+1 ) > f (pbest ) then update particle’s best known position pbest , 3.5 if f (xi+1 ) > f (gbest ) then update swarm’s best known position gbest , 4. Return the solution — swarm’s best known position.
2.2. Improved Algorithm The proposed modified algorithm can process both continuous and discrete data. It is further referred as to DC-PSO. When working with continuous parameter new positions are calculated using the classic approach, whereas when working with discrete parameters an alternative approach is used. In that case, a position represents an index in an array of ascendingly sorted possible values. It can be said, that when dealing with a continuous parameter the velocity represents a specific position change, while when dealing with discrete parameter velocity represents maximum index shift that can be applied. The equation for velocity update in discrete case can be defined as follows: xi+1,j = xi,j + S(v),
(5)
where S(v) is a discrete step function defined as: S(v) =
dve X
s(v).
(6)
i=1
In the discrete case the position is an index to a real value. Moreover the velocity determines the probability of actual change in position, hence it is used to designate real value of index change. This probability is proportional to distance between v value and 0. Velocity’s sign determines the direction of the movement. Decision whether to make a move is determined by comparing velocity’s value with uniformly distributed random values as seen in: 1 · sgn(v), v 6= 0 and ψ(|v|) > R(0, 1) s(v) = (7) 0, otherwise, where sgn(v) is 1 or −1 depending on the sign of v. Knowing that random values are ranged from [0, 1] it is necessary to translate velocity into a new domain before both can be compared. Transformation function used to change range of variable proposed in this work is sigmoid function: 1 . (8) ψ(v) = 1 + e−v One of the drawbacks of the classic algorithm is a premature convergence. In order to counteract this problem, a solution that was found by one swarm is saved and then another swarm is created. Process is repeated each time when the old swarm is trapped in local optimum and iteration pool has not been used up. The whole algorithm with presented improvements is shown in Figure 1.
48
Aneta Bera, Dariusz Sychel, Bartłomiej Sacharski
Start
Y
Init swarm
Y
Is termination criterion met?
N
N
Are all particles updated? End
Is swarm trapped?
Y
Calculate best position
N Update velocity
Are all dimensions updated?
Next dimension
Next particle
Y
N N
Is dimension discrete?
Update position
Y
Calculate step
Update position index
Figure 1. Proposed DC-PSO algorithm
3. Characteristics of investment strategies One of the examples of mixed-type problems are investment strategies — some of the attributes/parameters are continous, while others are discrete. This section is dedicated to one of the indicators that is often used as a basis for many strategies.
3.1. Rate of Change indicator properties Investment strategy, that is used for tests of authors algorithm, is based on popular indicator Rate Of Change [13], [14]. ROC is an indicator which measures the percentage change between the latest price and closing price of k periods before. The method of calculating the value of the ROC, for a given point in time, represents formula: ROC(now) =
Current close price − Close price k periods before Close price k periods before
This value can be calculated at each point. Set of these points forms the curve.
(9)
Improved Particle Swarm Optimization method for investment strategies parameters computing
49
There are several properties of the ROC curve and probably the most important is that when the oscillator value is below zero, then the current price is higher than one from k-candles before. Analogically, when the oscillator is above zero, it means that the current price is lower than the price from k-candles before. So it can be said that the ROC curve shows how the observed market price changes during the time. Moreover, increasing indicator line shows that the differences between the current price and ones from k-time before are increasing, decreasing says, that this differences decrease. Another important property of the described indicator, used while implementing the strategy, is that it indicates the moments in which transactions should be made. Simple rules say that when the ROC line crosses zero level from bottom then this is a good time to buy, when ROC crosses zero level from top then this is a good time to sell. These situations are presented in the Figure 2. 0.6 0.4
ROC value
0.2 0 0.2 0.4 0.6 0.8 475
480
485 time
490
495
500
Figure 2. Sample ROC curve with signals to buy (lighter arrows) and sell (darker arrows)
3.2. Strategy based on ROC indicator To test the proposed version of the PSO algorithm, authors have developed one variant of simple strategy based on the presented ROC indicator. This strategy is based only on opening and closing (after some time) short positions according to signals from ROC curve. To determine if PSO gives satisfactory results it is necessary to add some parameters which will be the subject of optimization. Considered strategy has 6 parameters: — p1 is the parameter which is directly related to the process of creating ROC curve in Equation 9, — p2 is the number of forward steps to close the position, — p3 is threshold for the average volume value, — p4 is the number of candles that determines average volume value, — p5 is the buffer/offset for ROC curve, — p6 is the stop loss condition which was described in [15], [16]. The action of opening/closing short position is taken when the following conditions are met: 1. ROC(i)·ROC(i−1) ≤ 0 — the intersection of the ROC curve with zero level (a condition resulting from the assumption about indicator action),
50
Aneta Bera, Dariusz Sychel, Bartłomiej Sacharski
2. ROC(i − 1) − p5 > ROC(i) — ROC value calculated for current candle needs to be smaller than previous value reduced by the buffer p5 , 3. mean(volume(i − p4 : i)) < p3 — mean value from p4 last candles should be smaller than some threshold (p3 ). To determine which set of parameters is better than the other it is necessary to introduce the ratio. The most commonly used ratios are: — resulting profit — criterion oriented on achieving highest profit regardless of how it is done (possible occurrences of local drops), — Calmar ratio which was proposed by Young in 1991 [17]. In further research first criterion is used to maximize the profit ignoring possible risk which is included in Calmar ratio.
4. Using PSO for investment strategy parameters search During tests the data was divided into smaller intersecting timeframes. Initially the beginning of first timeframe was set on the first candle in data. Each frame consists of two parts: — learn period in which PSO is used to find optimal parameters values, — test period in which found parameters values are tested. After that the start point for next timeframe is moved forward by the length of test period. Presented testing method is shown in Figure 3.
LEARN
TEST
LEARN
TEST
LEARN
TEST
time Figure 3. Method of testing
The way that PSO is used in searching parameters for implemented strategy can be written in following steps: 1. set investment strategy constant variables, 2. set search domain constraints (parameters bounds), 3. set DC-PSO parameters, 4. optimize investment strategy parameters using DC-PSO, 5. test obtained solution on training set.
5. Performed tests All tests were performed on data for EURUSD currency pair from 2012. Learn period consisted of 1000 candles while test period consisted of 100 candles (following learn to test ratio was proposed and validated in [10]). The tests consisted of 30 iterations in which timeframe was moved by 100 candles.
Improved Particle Swarm Optimization method for investment strategies parameters computing
51
The search space is limited by the following constraints: — p1 from 16 to 240, — p2 from 8 to 20, — p3 from −500 to 500, — p4 from 6 to 20, — p5 from −10 · spread to −spread, — p6 from 8 · spread to 40 · spread. Parameters for both versions of PSO algorithm were as follows: — number of iterations — 100, — number of particles in swarm — 150, — inertia — 0.85, — particle’s individuality — 0.45, — swarm’s influence — 0.65. Additionally, velocity limits for classic implementation were set from 0.1 to 0.9, whereas the modified version didn’t impose any limits. Due to the fact that original algorithm does not provide any sort of boundaries control, it is possible that a particle will move to a position outside of specified limits. Therefore it is necessary to provide limitations such as in Equation 4. A random approach was used when working with discrete parameters — instead of adding velocity to a position, a random integer is generated and assigned to a said parameter. Similarly to the classical version of PSO, the modified algorithm provides data and velocity limits. In order to make it impossible for a particle to leave the search space, the velocity is limited and truncated to space boundaries (Equation 2). If particle’s new position is not located within set boundaries then values are shifted to the nearest boundary. Moreover, if the global best position has not changed during a certain time (number of iteration — in this case assigned to 15), a new swarm is created, unrelated to the previous one. Tables below show tests results for two versions of algorithm. Table 1 presents results achieved during learn periods. Table 2 has the results for tests periods. In Table 1 columns modified PSO and classic PSO present obtained profits for each iteration of learn period. Next column, higher profit, indicates which of methods has yielded better result. As can be seen, modified version of PSO (modified) resulted in better performance (19 wins to 11 losses). Last column, difference, presents the difference between profit obtained by modified and classic version of PSO (difference = modified PSO − classic PSO) — positive value means that modified version has better result while negative value means that classic version has won. Table 1: Result from learning periods timeframe 1 2 3 4 5 6 7 8 9
modified PSO 0.1440 0.1327 0.0949 0.0897 0.0800 0.0826 0.1052 0.1215 0.1217
classic PSO 0.1328 0.1297 0.0936 0.0977 0.0892 0.0819 0.0792 0.1158 0.1177
higher profit modified modified modified classic classic modified modified modified modified
difference 0.0112 0.0030 0.0013 -0.0080 -0.0092 0.0007 0.0260 0.0057 0.0040
52
Aneta Bera, Dariusz Sychel, Bartłomiej Sacharski Table 1: Result from learning periods (continuation) timeframe 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
modified PSO 0.1403 0.1285 0.0829 0.0949 0.1048 0.1365 0.1153 0.1070 0.1104 0.0418 0.0956 0.0850 0.0733 0.0866 0.0845 0.0811 0.0809 0.0567 0.0543 0.0541 0.0545
classic PSO 0.1474 0.1380 0.1094 0.1031 0.1054 0.1313 0.1130 0.0888 0.0837 0.0666 0.0966 0.0751 0.0740 0.0762 0.0838 0.0730 0.0805 0.0485 0.0524 0.0530 0.0673
higher profit classic classic classic classic classic modified modified modified modified classic classic modified classic modified modified modified modified modified modified modified classic
difference -0.0071 -0.0095 -0.0265 -0.0082 -0.0006 0.0052 0.0023 0.0182 0.0267 -0.0248 -0.0010 0.0099 -0.0007 0.0104 0.0007 0.0081 0.0004 0.0082 0.0019 0.0011 -0.0128
The results shown in Table 2 confirm the conclusions from learning period. Modified version of PSO once again gives better results than classic (18 wins to 8 losses). These results are especially important in case of investment strategies because of the fact that every investor desires parameters that would be usable in the nearest future and which would result in highest possible profit. Table 2: Result from testing periods timeframe 1 2 3 4 5 6 7 8 9 10 11 12 13 14
modified PSO 0.0089 -0.0019 -0.0111 -0.0078 -0.0032 0 0.0206 0.0036 0.0155 0.0026 -0.0218 0.0093 0.0018 0.0252
classic PSO 0.0041 -0.0024 -0.0004 -0.0096 -0.0012 0 0 0.0036 0.0224 -0.002 -0.024 -0.0014 0.0013 0.0071
higher profit modified modified classic modified classic equal modified equal classic modified modified modified modified modified
difference 0.0048 0.0005 -0.0107 0.0018 -0.002 0 0.0206 0 -0.0069 0.0046 0.0022 0.0107 0.0005 0.0181
Improved Particle Swarm Optimization method for investment strategies parameters computing
53
Table 2: Result from testing periods (continuation) timeframe 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
modified PSO -0.0004 0.0189 0.0163 -0.0148 0 -0.0105 -0.0036 0.0028 -0.0079 0.0027 0 -0.007 0 0 0 0.0094
classic PSO -0.0034 0.0115 0.0121 -0.0108 0.0081 -0.014 -0.008 0.001 -0.0061 0.0027 0 -0.006 -0.0092 -0.0039 0.0142 0.0049
higher profit modified modified modified classic classic modified modified modified classic equal equal classic modified modified classic modified
difference 0.003 0.0074 0.0042 -0.004 -0.0081 0.0035 0.0044 0.0018 -0.0018 0 0 -0.001 0.0092 0.0039 -0.0142 0.0045
Figure 4 depicts cumulative profit1 curves for both algorithms. As can be seen there is an evident advantage of the modified version. There are also some similarities and tendencies in some periods which indicate that there are certain characteristics in the market. It is worth noting that in periods from 10 to 14, even though classic version has achieved better results in learn periods, it is has performed worse in test periods (higher losses than in the modified version). Proposed modification Classic version of algorithm
0.07 0.06
Cumulative profit
0.05 0.04 0.03 0.02 0.01 0 0.01 0.02 0
5
10
15 Time frame
20
25
30
Figure 4. Cumulative profit for modified and classic PSO algorithm
Besides checking the quality of results, execution times were also examined. All the tests were performed on the same machine with Intel i7 2600K processor and 8GB of RAM (1600 MHz). Results shown in Table 3 present the average time necessary to calculate a solution for one timeframe. To show benefits of using PSO algorithm both methods were compared to 1
Cumulative profit is the sum of all preceding and current profit
54
Aneta Bera, Dariusz Sychel, Bartłomiej Sacharski
iterative method of parameters searching, that checks all possible combinations of parameters using following data limits: — p1 from 16 to 240 with step = 1, — p2 from 8 to 20 with step = 1, — p3 from −500 to 500 with step = 1, — p4 from 6 to 20 with step = 1, , — p5 from −10 · spread to −spread with step = spread 2 — p6 from 8 · spread to 40 · spread with step = spread. Due to the fact that the given search space for iterative method is enormous (number of possible combinations is 2.7537e+10) it is necessary to approximate the execution time using the time needed to calculate the strategy (≈ 0.14s). As can be seen in Table 3 using iterative method is pointless because in the time that is needed to calculate results they would become outdated and obsolete. Both of the PSO methods give the results in reasonable time — considering that the difference in execution time of classic and modified version is rather small it would seem that it is better to use proposed algorithm as it yields higher cumulative profit. Table 3. Time tests method Exhaustive research Classic PSO Modified PSO
time in seconds 3889500000 676.2 739.3
time ≈ 123 years ≈ 11 minutes ≈ 12 minutes
6. Conclusion The paper presents an innovative method for optimization that is a modification of the classic PSO algorithm. The proposed method allows to solve mixed-type (both continuous and discrete) problems, for instance optimizing problems related to investment strategies. The method was compared with the classic version using simple strategy based on the ROC indicator. Furthermore, times of execution of both versions were compared with an iterative algorithm that traverse all possible combinations of parameters. Results achieved indicate better accuracy of the modified version compared to the classic algorithm while having only slight difference in execution times. Regarding the computation time, it is reasonable to conduct further research to reduce said time without loosing the quality of the results. It is worth noting that time complexity of both variants of algorithm depends directly on the complexity of the investment strategies i.e. number of parameters — what is another motivation for further research on reducing time necessary for calculations.
References [1] Nuti, G., Mirghaemi, M., Treleaven, P., Yingsaeree, C.: Algorithmic Trading. Computer, 44(11), pp. 61–69, 2011. ISSN 0018-9162. [2] Paraschiv, D., Raghavendra, S., Vasiliu, L.: Algorithmic Trading on an Artificial Stock Market. In: Badica, C., Mangioni, G., Carchiolo, V., Burdescu, D. (eds.), Intelligent Distributed Computing, Systems and Applications, volume 162 of Studies in Computational Intelligence, pp. 281–286. Springer Berlin Heidelberg, 2008. ISBN 978-3-540-85256-8. [3] Barbosa, R., Belo, O.: A Diversified Investment Strategy Using Autonomous Agents. In: Fink, A., Lausen, B., Seidel, W., Ultsch, A. (eds.), Advances in Data Analysis, Data Handling and
Improved Particle Swarm Optimization method for investment strategies parameters computing
[4] [5]
[6]
[7] [8] [9]
[10]
[11]
[12] [13] [14] [15] [16] [17]
55
Business Intelligence, Studies in Classification, Data Analysis, and Knowledge Organization, pp. 339–350. Springer Berlin Heidelberg, 2010. ISBN 978-3-642-01043-9. Chau, K.: Application of a PSO-based neural network in analysis of outcomes of construction claims. Automation in Construction, 16(5), pp. 642 – 646, 2007. ISSN 0926-5805. Ibrahim, I., Yusof, Z., Nawawi, S., Rahim, M., Khalil, K., Ahmad, H., Ibrahim, Z.: A Novel Multi-state Particle Swarm Optimization for Discrete Combinatorial Optimization Problems. In: Computational Intelligence, Modelling and Simulation (CIMSiM), 2012 Fourth International Conference on, pp. 18–23. 2012. ISSN 2166-8531. Misra, B. B., Satapathy, S. C., Dash, P.: Particle Swarm Optimized Polynomials for Data Classification. In: Intelligent Systems Design and Applications, 2006. ISDA ’06. Sixth International Conference on, volume 1, pp. 649–654. 2006. ISBN 0-7695-2528-8. Khan, N., Iqbal, M., Baig, A.: Data Mining by Discrete PSO Using Natural Encoding. In: Future Information Technology (FutureTech), 2010 5th International Conference on, pp. 1–6. 2010. Rezaee Jordehi, A., Jasni, J.: Particle swarm optimisation for discrete optimisation problems: a review. Artificial Intelligence Review, pp. 1–16, 2012. ISSN 0269-2821. Wang, F., Yu, P., Cheung, D.: Complex stock trading strategy based on Particle Swarm Optimization. In: Computational Intelligence for Financial Engineering Economics (CIFEr), 2012 IEEE Conference on, pp. 1–6. 2012. ISBN 978-1-4673-1802-0. Wilinski, A., Bera, A., Nowicki, W., Blaszynski, P.: Study on the Effectiveness of the Investment Strategy Based on a Classifier with Rules Adapted by Machine Learning. ISRN Artificial Intelligence, 2014, 2014. Nenortaite, J., Simutis, R.: Adapting particle swarm optimization to stock markets. In: Intelligent Systems Design and Applications, 2005. ISDA ’05. Proceedings. 5th International Conference on, pp. 520–525. 2005. ISBN 0-7695-2286-6. Kennedy, J., Eberhart, R.: Particle swarm optimization. In: Neural Networks, 1995. Proceedings., IEEE Int. Conf. on, volume 4, pp. 1942–1948 vol.4. 1995. ISBN 0-7803-2768-3. Rosenbloom, C.: The Complete Trading Course: Price Patterns, Strategies, Setups, and Execution Tactics. Wiley Trading. Wiley, 2010. ISBN 9780470947296. Murphy, J.: Technical Analysis of the Financial Markets: A Comprehensive Guide to Trading Methods and Applications. Penguin Group US, 1999. ISBN 9781101659199. Di Lorenzo, R.: Basic Technical Analysis of Financial Markets: A Modern Approach. Perspectives in Business Culture. Springer, 2013. ISBN 9788847054219. Stop-Loss Order. In: Lee, C.-F., Lee, A. (eds.), Encyclopedia of Finance, pp. 262–262. Springer US, 2006. ISBN 978-0-387-26284-0. Young, T. W.: Calmar Ratio: A Smoother Tool. Futures, 1991.