Physica A 294 (2001) 213–225
www.elsevier.com/locate/physa
Learning the optimal trading strategy Fabio Franci, Robert Marschinski, Lorenzo Matassini ∗ Max-Planck-Institut fur Physik komplexer Systeme, Nothnitzer Strasse 38, D 01187 Dresden, Germany Received 2 January 2001
Abstract Within a realistic model of the stockmarket, we derive the most successful trading strategy. We -rst identify the agent who has realized the largest percentual gain and then analyze all the operations this trader has performed during the simulation run. We report them in a proper trading space and we extend the model, introducing an additional operator acting with the help of a look up table derived from a clusterization of space. We discuss the robustness of this c 2001 Elsevier Science optimal strategy, its performance and the applicability to real markets. B.V. All rights reserved. PACS: 89.90.+n; 05.45.Tp; 64.60.−i Keywords: Trading strategy; Econophysics; Arti-cial -nancial market
1. Introduction Statistical physicists and condensed matter theorists have developed a signi-cant arsenal of tools for analyzing many-particle systems with strong, localized interactions. Methods such as mean--eld theory, the renormalization group, and -nite-scaling analysis allow physicists to explore complex, irreducible systems such as spin glasses, where the important details are in the interactions between the particles, rather than the individual particles themselves. Recently, physicists have realized that the methods developed above may also be useful for ecological and social systems. The leap of faith required is the assumption that it is not necessary to fully understand the individuals in the systems ∗
Corresponding author. Tel.: +49-351-871-1803; fax: +49-351-871-1999. E-mail address:
[email protected] (L. Matassini).
c 2001 Elsevier Science B.V. All rights reserved. 0378-4371/01/$ - see front matter PII: S 0 3 7 8 - 4 3 7 1 ( 0 1 ) 0 0 1 3 2 - 7
214
F. Franci et al. / Physica A 294 (2001) 213–225
themselves, but only to the point that one can construct reasonable rules for the interactions between individuals. Whether this leap of faith is justi-ed remains an open question but interest is rising within the physics community in complex, socio-economic systems like the stock market. To a physicist, the question of whether a -nancial market operates at a critical point is especially interesting. The traditional theory of critical phenomena states that a system will approach a critical point via deliberate tuning of the control parameter. This description does not seem to apply to markets, however. The rules governing market dynamics were not chosen in order to put the market in a critical state, but it appears to have arrived there spontaneously, without any external tuning. This phenomenon, originally proposed as a possible explanation for scaling in many natural phenomena, is known as self-organized criticality. Markets are diAcult to study scienti-cally for many reasons: (i) Stocks are strongly coupled to each other and to other systems, both natural and man-made; (ii) Investors’ responses to incoming news can be strongly nonlinear; (iii) Traders may receive imperfect information, since only some of it is broadcasted to all. The rest is transmitted through a complex network of acquaintances. These are essentially the reasons why stock price Cuctuations are usually considered a random process and there are many alternatives about the proper model of the distribution of return. In any case, some universal features have been found: They resemble the scaling laws characterizing physical systems dominated by the interaction of a large number of units [1– 4]. Since the details of the circumstances governing the expectations and decisions of all the individuals are unknown to the modeler, the behavior of a large number of heterogeneous agents may best be formalized using a probabilistic setting. Such a statistical modeling concept has a certain tradition in the so-called synergetics literature [2] which has adopted techniques from elementary particle physics to study various problems of social interactions among humans [5]. In a statistical approach the properties of macro variables are not necessarily identical to those of the corresponding micro variables nor does the mere aggregation of micro components always yield sensible macroeconomic relationships. This stands in contrast with the traditional economic theory, which interprets stock markets as equilibrium systems driven by exogenous events. In a previous work [6] we have proposed a very realistic model of the stockmarket based on the simulation of the book, the list collecting all the buying and selling orders. We have introduced only one type of investor, with a very simple 1 goal: Maximize the pro-t and minimize the risk. Every trader is characterized through the following quantities: Initial amount of money, inclination towards investment, number of owned shares, expected gain, maximum loss, target price, stop loss price, threshold in time. Fig. 1 explains the meaning of some of these parameters. Let us consider a trader who has got shares at the price and the time reported by the -lled circle. The price is constantly extended in time through the dotted line just for eye guide. The agent 1
From a modelling point of view!
F. Franci et al. / Physica A 294 (2001) 213–225
215
Fig. 1. The trading rectangle. Reported is the market price versus trading time. The -lled circle indicates the moment in which the trader has bought shares. The dotted line, constant at the buying price, is plotted only for eye guide. The upper line is the target price (TP), the lower line refers to the stop loss price (SL) and the threshold in time (TH) de-nes the right end of the trading rectangle.
has to identify a trading rectangle, de-ned in the following way: The upper horizontal line is the target price (the price that would make him=her sell the shares in order to cash the win), the lower horizontal line is the stop loss price (the price that would make him=her sell the shares in order to limit the loss), the leftmost vertical line is the buying time, the rightmost vertical line is the sum of the buying time plus a certain amount of time de-ned as threshold (amount of time after which the trader may start to change idea about the investment). As long as the market price is con-ned within the trading rectangle, the agent does not feel any pressure to perform any operation, but once this condition is violated the shares are likely to be sold for diHerent reasons, according to the escape position of the market price from the trading rectangle: From above to cash the win, from below to limit the loss, from the right to take time and think about the investment. The typical iteration of the simulation involves the following steps: (i) Random selection of a trader, (ii) control of the presence of some pending order, (iii) request to perform some action, like put an order, remove an order, change some parameter of an existing order, (iv) identi-cation of the fair price for the trader and therefore of the target and stop loss prices and (v) book check in order to -nd some matching between
216
F. Franci et al. / Physica A 294 (2001) 213–225
a buyer and a seller and therefore to de-ne the market price. For more details see Ref. [6]. The problem we want to address here consists on the establishment of a feedback between the market price and the behaviour of the traders. Usually one tries to do the following: given the typical features of a stockmarket, like fat tails of return distribution and correlated volatility, give an explanation of their emergence as a result of interactions between agents. Here we proceed along the opposite line: given the interactions among traders, we want to derive the optimal strategy for the market we are simulating. In other words, we would like to learn something from the best performing investor, maybe in order to use this know-how to make money in a real environment.
2. Evolution of the model As already mentioned, universal characteristics exhibited by -nancial prices comprise a distribution with fat tails (events with a distance bigger than 3 from the average return are not so uncommon as a Gaussian distribution would forecast) and a correlation in the volatility (alternation between tranquil and turbulent periods). Both these features are present in our model, whose typical evolution is reported in Fig. 2: the upper
Fig. 2. Typical snapshot from a simulation run. Upper panel: Development with time of the market price. Lower panel: Development with time of the corresponding volumes of exchange.
F. Franci et al. / Physica A 294 (2001) 213–225
217
panel is related to the market price, whereas the lower one reports the corresponding amount of exchanged shares. We can clearly see the alternation between normal and frenetic trading, the -rst being characterized by a slowly oscillating price and very low volumes: owners do not want to sell because they hope to get more money if they wait a little bit, agents without shares want to buy at a lower price. This dynamical equilibrium is unstable: After a signi-cant increase in the volumes a small burst occurs and the price increases to a more suitable value for owners. A rally is usually followed by a crash (a kind of settlement), as reported in [7] under the name of on–o6 intermittency, an aperiodic switching between static, or laminar, behavior and chaotic bursts of oscillations. In our model the imbalance between demand and supply is mainly responsible for the alternation between the two diHerent trading regimes and more obviously between raising and falling courses. In economics it has been for a long time a common sense that demand and supply should balance automatically. In reality, anyway, it became evident that it is hard to have such a balance for most of the popular commodities in our daily life [6,8]. Demand is in fact essentially a stochastic variable because human action can never be predicted perfectly; therefore the balance of demand and supply should also be viewed in a probabilistic way, characterized by a phase transition between the excess-demand and the excess-supply phases. It is a general property of a phase transition system that Cuctuations are largest at the phase transition point, and this property also holds in this demand–supply system. In the case of markets of ordinary commodities, consumers and providers are independent and the averaged supply and demand are generally not equal. The resulting price Cuctuations are generally slow and small in such market because the system is far away from the critical point. On the contrary in an open market of stocks or foreign exchanges, the market is governed by speculative dealers who frequently change their positions between buyers and sellers. Such a speculative action make demand and supply balance automatically on average by changing the market price, resembling a kind of self organized criticality. Complex systems often reveal more of their structure and organization in highly stressed situations than in equilibrium. In Fig. 3 we show the performance of each trader. The gain is given in percent. For a more detailed treatment of all the problems related to the evaluation of the performances we refer the interested reader to Ref. [6]. Here we do not want to discuss the estimation, but we only need a way to identify the best performer because our goal is to learn something from his=her way of trading. In the following we assume that the richness of a trader is simply given by the sum of cash and invested money. Of course these two numbers have a diHerent nature, since only the cash has an immediate buying power. Considering the invested money as real money we suppose that the trader can sell the shares exactly at the price they have been bought. But of course nobody can guarantee for that. Some authors use the term virtual for this kind of money, since it becomes real only after having sold the shares.
218
F. Franci et al. / Physica A 294 (2001) 213–225
Fig. 3. Distribution of the gain obtained by each trader.
3. Deriving the optimal strategy To further simplify the situation, we evaluate the performance of every agent discarding as many of the last operations as necessary in order to come back to a point characterized by cash money only. We then consider the diHerence between the money realized after having sold the shares and the originally invested capital. This quantity, related to the initial amount of money, gives an accurate estimation of the quality of the trading. In more details the absolute performance (AP)i is de-ned as j = mis
(AP)i =
j = mib
pisj sisj −
j=1
pibj sibj ;
(1)
j=1
where mis is the total number of selling orders, mib the total number of buying orders, pisj and sisj the selling price and the number of shares involved in the selling orders, pibj and sibj the same quantities related to buying orders. The relative performance (RP)i is obtained as (AP)i (2) Ri with Ri indicating the initial resources of trader i. Fig. 3 shows clearly that not all the traders have been equally lucky during the simulation. The straight line refers to the zero gain and it represents of course the (RP)i =
F. Franci et al. / Physica A 294 (2001) 213–225
219
average amount of money won from the traders, since the market we are simulating is a closed one, we have introduced only one stock, the trading activity has no extra cost and no dividend is paid. To de-ne a transaction one needs a buyer and a seller, for every investor gaining money there must be one other who is losing it. But the distribution depicted in Fig. 3 indicates the possibility to derive an optimal strategy, since only few people are really getting money, whereas the majority of the traders is in red. This fact suggests to take a closer look at the best performer in order to learn something. In particular it is interesting to analyze the book and derive the strategy 2 adopted by the best investor. In our model there is only one kind of trader, since we do not make any distinction between chartists and fundamentalists, optimists and pessimists, and so on (see [9] for a good insight on the eHect of microscopic diversity). All the investors want to get rich with the smallest possible risk and as fast as possible. They have access to the price history, they can communicate with a limited number of friends, they receive news and advertisements from the market. Then they decide whether to join the market or not and de-ne a price, since the only way to buy and sell some shares is through the book: one has to communicate the quantity and the price of the involved commodity. Of course in a stockmarket one cannot decide both the price and the time at which to trade. If you specify the moment to buy or sell (performing a so-called market order) you do not know exactly at which price you will trade the shares. On the other hand, if you have a given price in mind and you insert a so-called limit order you have to wait an amount of time that is not trivial to evaluate. Usually in the -rst case the price is not so diHerent from the last transaction and in the second situation the time one has to wait grows with the diHerence between the limit and the market price, namely the requested price and the price involved in the last exchange of shares. Let us consider the example reported in Fig. 4. Here we can see the evolution of the price and the corresponding operations performed by the winner. Upper triangles indicate a buy, lower triangles a sell. A dotted line connects two consecutive operations and has no meaning but visual guide. Since in this example a buy is always followed by a sell, the slope of the above mentioned line indicates the variation of money: If the slope is positive the trader has got money and viceversa. The lower panel reports on the involved volumes: It seems that this agent had a preferred number of shares to be exchanged, namely all the orders involved 6 to 8 pieces. The trader has performed 24 operations, 12 to buy shares and 12 to sell them. 10 times he has won money, twice he has lost something. It is interesting to comment the two losing cases, since they indicate two diHerent aspects of the adopted strategy. The situation indicated with the letter A is a typical stop loss: the agent has bought 2 As stressed in [6] all the traders in this model perform according to the same deterministic rules. What makes the diHerence is the weight given to the opinion of media and acquaintances, the interpretation of the trend in the market price, personal targets and so on. In other words, every investor has a diHerent set of parameters. Deriving the optimal strategy therefore means tuning in a proper way all the degrees of freedom that a trader has still free in the model.
220
F. Franci et al. / Physica A 294 (2001) 213–225
Fig. 4. Upper panel: Evolution of the market price and the corresponding operations performed by the best trader. Upper triangle indicate buying shares, lower triangle selling shares. Lines connect a buy with the relative sell. The two cases depicted by the letters A and B are the situations when the agent has lost money. The former is due to a stop loss order, the latter is due to the threshold in time. Lower panel: number of shares involved in the transactions.
at a relatively high price in a moment characterized by a high volatility. After a short period of time he has decided to cautiously sell the shares because the market price went down and he wanted to limit the losses. In fact this was a good idea because after that the trader could buy the shares again at a cheaper price. On the other hand, the situation under the letter B indicates the intervention of the threshold. Again the trader loses money (although this time a really small amount), but it was a good choice once more. After a while, in fact, he could buy the shares with a strong discount and sell immediately after, getting much more money than he had previously lost due to the threshold. In order to derive the optimal strategy we proceed in the following way. We extract from the book all the transactions where the winner is involved and we de-ne a trading space. Coordinates of this space are price returns, age of the shares, broadcasted information, remaining cash and so on. We need tens of iterations in order to clearly identify the best performer and hundreds of runs in order to properly populate the trading space with winners from several simulations. After this training period we proceed with a clusterization, as in [10], of the collected data in order to get a lookup table to be used in the next step, when the new trader starts to play, too.
F. Franci et al. / Physica A 294 (2001) 213–225
221
Fig. 5. Behavior of the best traders. Projection of the trading space over the (time, return) plane. The trivial recipe “buy when low, sell when high” is not enough in order to get good results. One has also to identify slope and duration of trends. Small circles suggest to buy shares, rectangles to sell them. When no clear indication is possible a small dot is reported. There are regions where it is obvious what to do: They are labeled with BUY, SELL and WAIT.
A two-dimensional projection of the trading space is reported in Fig. 5. Here only the price increment along the x-axis and the age of the share along the y-axis are shown. The -rst quantity is de-ned as the logarithmic diHerence of the price at time t1 and the price at time t2 , where t1 and t2 refer to two consecutive transactions involving the trader under observation. The quantity along the y-axis is just time, here expressed in months due to the considerations reported in Ref. [6] about correlation analysis. In Fig. 5, a circle is a suggestion to buy shares, a rectangle to sell them. Apart from the trivial idea to sell after a price increase and to buy in the complementary case, we can observe some interesting situations. According to the upper big circle (1), one should buy shares after a moderate price increment over a very long time. The second upper ellipse (2) gives similar indications but for the opposite case: If the market price is slowly falling down then it is better to sell before it is too late (anyway between regions 2 and 4 one should buy by default). The circle labelled by 3 comes from the identi-cation of an upper trend, since the price is rising within a rather short amount of time, a good moment to catch the train and buy shares. The fourth region is a consequence of the introduction of the stop loss price and it suggests to sell before it
222
F. Franci et al. / Physica A 294 (2001) 213–225
is too late. The four ellipses are not symmetric with respect to the zero price return and also the dimension and the shape are quite diHerent. This reCects typical asymmetries found in empirical data between positive and negative returns [11,12]. 4. Results We want to stress again the point that the lookup table one derives from Fig. 5 contains very valuable information beyond the trivial behavior to buy when cheap, sell when expensive or just follow trends. It is of fundamental importance during the tuning of the parameters characterizing every trader. To show that, let us see the performance of the new agent, whose strategy in nothing but consulting Fig. 5 when trading. The results are shown in Fig. 6, where 4 distributions are reported. They have been derived running 200 times the program and individuating, for each simulation, the RP of the worst and the best trader, together with the index performance and the RP of the new agent trading with the help of the lookup table. It is necessary to introduce the index because usually the -nal market price is bigger than the initial value and one could ask whether it would be more pro-table to buy at the beginning a certain number of shares and just keep them till the end. It is clear from Fig. 6 that the strategy we
Fig. 6. Distribution of losers and winners, together with the index (diHerence between the -nal and the initial market price) and the performance of the trader adopting the newly developed strategy.
F. Franci et al. / Physica A 294 (2001) 213–225
223
Fig. 7. Evolution with time of the market price of the shares of a company belonging to the S&P500 index.
have introduced and developed is really useful, since the new trader is systematically able to get more money than the winner, namely than all the other agents. As a further proof of the eAciency of such a strategy, we consider now a real time series. We have used one company belonging to the S&P500, whose temporal evolution of the quoted value is reported in Fig. 7. Before being able to make use of the lookup table, one has to tune it on the new time series. More precisely, one has to correctly identify the time scales, namely to be sure that the time reported along the y-axis of Fig. 5 corresponds to the time involved on the x-axis of Fig. 7. This point is very crucial and unfortunately we do not have any guarantee that we manage to do it because of the nonstationarity of such a time series. In any case, in order to apply our strategy to Fig. 7, we split the time series into two parts: The -rst is used to tune the time with the lookup table, the second is given as input to the lookup table itself. In other words, after the calibration we buy a certain amount of shares (related to the initial resources in the model) and we scroll over the time series to get indications on what to do. If the price increment dp after a time t is such that (dp,t) on Fig. 5 belongs to a sell zone then we sell all the shares, if (dp,t) is on a buy region we buy shares with respect to the resources, otherwise we consider the next point of the time series. The portion of the time series reported in Fig. 7 is the one really used for the simulation, having performed the calibration of the lookup table on a previous part of
224
F. Franci et al. / Physica A 294 (2001) 213–225
it. We have chosen this portion with the constraint that the -nal value is identical to the initial because we want to avoid spurious eHects due to the presence of a trend. Our strategy, once applied on Fig. 7, pays with an RP of +7%. It is not so much if we consider that the involved period of time is longer than three years, but it is without any doubt better than the zero-performance of the index.
5. Conclusions We have proposed a way to derive an optimal strategy for trading a stock in a common market. Using a previously developed model, we identify the best performing trader along several runs of the simulation process and we collect all the operations this agent has performed to realize the win. These transactions populate a trading space; via a proper clusterization of this space we derive a lookup table and we introduce a trader whose behavior is governed by it. The new operator has to compute the price returns, use them to select the right entry of the lookup table and perform what is indicated there. We have shown that such a strategy is really optimal within the model, since the lookup table trader gets systematically better performances than all the others. There are of course several limitations if one wants to apply such a strategy to empirical situations. Some of them are due to the assumptions of the model (no transaction costs, no arbitrage possibilities, zero execution time) and could be overcome. But at least two others are quite crucial, namely the correct time tuning between the empirical series and the modelled time and the perturbing eHect due to a real trade on a real market. The -rst task has to be performed very accurately because the use of the lookup table strictly depends on it: a mistake during the real time—simulated tick translation reduces the potentially optimal strategy to a nonsense. Unfortunately this point is quite far from being considered solved. The second problem is inherent and it is a common limitation for all the attempts to get a model of the stockmarket.
References [1] H.J. Blok, On the nature of the stock market: simulations and experiments, Cond-mat=0010211, 2000. [2] H. Haken, Synergetics: An Introduction, Springer, Berlin, 1983. [3] R.N. Mantegna, H.E. Stanley, Scaling behavior in the dynamics of an economic index, Nature 376 (1995) 46–49. [4] R.N. Mantegna, H.E. Stanley, An Introduction to Econophysics. Correlations and Complexity in Finance, Cambridge University Press, Cambridge, 2000. [5] J.B. Ramsey, On the existence of macro variables and macro relationships, J. Econ. Behav. Organ. 30 (1996) 275–299. [6] L. Matassini, F. Franci, On -nancial markets trading, Physica A 289 (2000) 526–542. [7] N. Platt, E.A. Spiegel, C. Tresser, On-oH intermittency: A mechanism for bursting, Phys. Rev. Lett. 70 (1993) 279–282. [8] H. Takayasu, M. Takayasu, Critical Cuctuations of demand and supply, Physica A 269 (1999) 24–29. [9] M. Levy, H. Levy, S. Solomon, Microscopic simulation of the stock market: The eHect of microscopic diversity, J. Phys. I France 5 (1995) 1087–1107.
F. Franci et al. / Physica A 294 (2001) 213–225
225
[10] L. Matassini, C. Manfredi, R. Hegger, H. Kantz, Analysis of vocal disorders in a feature space, Med. Eng. Phys. 22 (2000) 413–418. [11] J.P. Bouchaud, M. Potters, Theory of -nancial risks, From statistical physics to risk management, Cambridge University Press, Cambridge, 2000. [12] J.P. Bouchaud, A. Matacz, M. Potters, The leverage eHect in -nancial markets: Retarded volatility and market panic, Cond-mat=0101120, 2001.