Market Stock Decisions Based on Morphological Filtering Pere Marti-Puig, R. Reig-Bolaño, J. Bajo, and S. Rodriguez *

Abstract. In this paper we use a nonlinear processing technique based on mathematical morphology to develop a simple day trading system that automatically decides the timing to commute the marked strategy in terms of sort/long positions. In this short paper we show preliminary results. Keywords: Mathematical Morphology, Nonlinear Processing, Algorithmic trading.

1 Introduction The financial markets are supported by electronic platforms that provide real time efficient services. It is known as algorithmic trading the use of computer software to generate trading orders. By means of algorithms it is obtained support to decisions in aspects such as the timing, the price or the volume of the operation, managing risk and the market impact. Furthermore, in most cases the computer algorithms introduce orders in the electronic market without human intervention. Hedge founds, pension funds, mutual funds or institutional traders are some of the big users of these techniques. According to Boston-based financial services industry research and the consulting firm Aite Group, in 2006 a third of all EU and US stock trades were driven automatically. In 2009 the trading firms account for 73% of all US equity trading volume [1][2]. In this work we explore the mathematical morphology (MM) to develop a simple day trading system that automatically decides the timing to commute the marked strategy in terms of sort/long positions.

2 Mathematical Morphology: An Introduction Mathematical morphology was first proposed by J.Serra and G. Matheron in 1966, was theorized in the mid-seventies and matured from the beginning of 80’s. It can Pere Marti-Puig and R. Reig-Bolaño Grup de Tecnologies Digitals, University of Vic, C/ de la Laura, 13, 08500, Vic, España J. Bajo and S. Rodriguez BISITE Plaza de la Merced s/n, 37008, Salamanca, España e-mail: [email protected], [email protected] A.P. de Leon F. de Carvalho et al. (Eds.): Distrib. Computing & Artif. Intell., AISC 79, pp. 435–439. © Springer-Verlag Berlin Heidelberg 2010 springerlink.com

436

P. Marti-Puig et al.

process binary signals, originally, and it was fast extended to gray level signals. This technique is proved to be very useful in digital image processing. Mathematical morphology is based on two fundamental operators: dilation and erosion. These two basic operations are done by means of a structuring element. The structuring element is a set in the Euclidean space and it can takes different shapes as circles, squares, or lines. Using different structuring elements it will achieve different results; therefore, the selection of a suitable structuring element is fundamental. A binary signal can be considered a set and dilation and erosion are Minkowski addition and subtraction with the structuring element [3]. In the context of stock prices we can work with series of data that can be modeled as gray level signals. In this context, the addition and subtraction operations that are applied in binary morphology are replaced by suprermum and infimum operations. Moreover, on the digital signal processing framework, supremum and infimum can be changed by maximum and minimum operations. In this context, the erosion can be seen as the minimum value of the part of the function inside a mobile window that is defined by the structuring element. Then, given a one-dimensional signal, the function f containing the stock prices and a flat structuring element Y, the erosion is defined as:

ε Y ( f )( x ) = min f ( x + s ) s∈Y

(1)

As the erosion computes the minimum gray level inside the mobile window function it decreases the peaks and accentuates the valleys of the original function f. On the other hand the dilation (for gray level signals) is defined as:

δ Y ( f )( x ) = max f ( x − s ) s∈Y

(2)

The dilation gives the maximum gray level value of the part of the function included inside the mobile template defined by the structuring element, accentuating peaks and minimizing valleys. By combining dilation and erosion we can form other morphological operations. The opening and the closing are basic morphological filters. The morphological opening of a signal f by the structuring element Y is denoted by γY(f) and is defined as the erosion of f by Y followed of a dilation by the same structuring element Y. This is:

γ Y ( f ) = δ Y (ε Y ( f ))

(3)

The morphological closing of a signal f by the structuring element Y is denoted by ϕY(f) and it is defined as the dilation of f by Y followed of the erosion by the same structuring element:

ϕY ( f ) = ε Y (δ Y ( f ))

(4)

Opening and closing are dual operators. Closing is an extensive transform and opening is an anti-extensive transform. Both operations keep the ordering relation between two images (or functions, in our case) and are idempotent transforms [3]. In the 1-D context these operations create a more simple function than the

Market Stock Decisions Based on Morphological Filtering

437

original. By combining an opening and a closing, both of them with the same structuring element, we can only create four different morphological filters. Considering the operations γY and ϕY, the four filters we could obtain are γYϕY, ϕYγY, γYϕYγY and ϕYγYϕY. No other different filter can be produced as a consequence of idempotency property. To derive different families of morphological filters we need to combine openings and closings with different structuring elements. New filters, alternating sequential filters, can be obtained by alternating appropriately theses operators [3].

3 The Proposed Trading System The system is very simple and has been developed using MATLAB. Initially was thought to operate in the day tracking context but it could be modified to intraday operations. Once the markets of interest are closed, the system updates the stock information from any public Internet source, then it processes the data and, it is the case, it generates the orders to next day. The only stock information required is the open, close, high and low prices that the stocks, or any financial asset, reached along every day session. As we initially look for a simple system, we establish that the system will take the decisions from a reduced set of signals. Basically it computes the opening and the closing of the prices and uses a linear mean that is computed from the close day prices. Based on the cross of these three signals the system generates the decisions. To generate the market orders we have developed a finite state machine that governs the switching between the short and long scenarios. In fig.1 we can see de opening and closing signals (in blue) computed using a flat structuring element of length 8. The red signal is the mobile mean taken from 34 elements. Next, in fig. 2 we have used the Santander stock prices along a period of 10 years. We maintain the same parameters of the system. In fig. 2 (a) we show the opening and closing signals (in blue) with L=8 and the mobile mean of 34 samples (in red). In fig. 2 (b) there are represented the long (blue) and short (red) operations. The system parameters can be optimized for different kind of data. Using the same data of fig.2, the Santander stock prices, we have searched the structuring element and the median filter that maximize the profits. The system performance is summarized in fig. 3.

Fig. 1 Telefonica stock prices. Opening and closing signals (blue) computed with a flat structuring element of length 8 and the mobile mean of 34 elements (red).

438

P. Marti-Puig et al.

Fig. 2 Santander stock prices representation (1917 days). (a) Opening and closing signals with L=8 (in blue) and the mobile mean with T=34 (in red). (b) System performance.

Fig. 3 Santander stock prices representation (1917 days). (a) Opening and closing signals with L=34 (in blue) and the mobile mean with T=55 (in red). (b) System performance.

4 Conclusions In this paper we have explored the nonlinear mathematical morphological filters in order to trade markets automatically. We have evaluated the system using only a reduced set of financial assets and it works quite well as we can see graphically. We have obtained some preliminary results. More quantitative analysis is required on long historic datasets in order to know some statistical parameters such as the maximum potential losses that it can generate or the maximum number of days that the system can keep in losses. It is desirable that the system can follow the great market movements as well as generates small number of false signals and, if

Market Stock Decisions Based on Morphological Filtering

439

it is the case, that those signals do not incur in large losses. Some optimizations have to be done. New structuring elements and more sophisticated morphological filters must be evaluated. In the context of financial data processing, the mathematical morphology can be explored in a lot of different ways showing that could be a good tool to include among other well-known techniques. Acknowledgments. This work has been supported by UVIC.

References 1. Timmons, H.: A London Hedge Fund That Opts for Engineers, Not M.B.A.’s (August 18, 2006) 2. Aldridge, I.: High-Frequency Trading: A Practical Guide to Algorithmic Strategies and Trading Systems. Wiley & Sons, Chichester (2009) 3. Serra, J.: Image Analysis and Mathematical Morphology. Academic, New York (1982)

Abstract. In this paper we use a nonlinear processing technique based on mathematical morphology to develop a simple day trading system that automatically decides the timing to commute the marked strategy in terms of sort/long positions. In this short paper we show preliminary results. Keywords: Mathematical Morphology, Nonlinear Processing, Algorithmic trading.

1 Introduction The financial markets are supported by electronic platforms that provide real time efficient services. It is known as algorithmic trading the use of computer software to generate trading orders. By means of algorithms it is obtained support to decisions in aspects such as the timing, the price or the volume of the operation, managing risk and the market impact. Furthermore, in most cases the computer algorithms introduce orders in the electronic market without human intervention. Hedge founds, pension funds, mutual funds or institutional traders are some of the big users of these techniques. According to Boston-based financial services industry research and the consulting firm Aite Group, in 2006 a third of all EU and US stock trades were driven automatically. In 2009 the trading firms account for 73% of all US equity trading volume [1][2]. In this work we explore the mathematical morphology (MM) to develop a simple day trading system that automatically decides the timing to commute the marked strategy in terms of sort/long positions.

2 Mathematical Morphology: An Introduction Mathematical morphology was first proposed by J.Serra and G. Matheron in 1966, was theorized in the mid-seventies and matured from the beginning of 80’s. It can Pere Marti-Puig and R. Reig-Bolaño Grup de Tecnologies Digitals, University of Vic, C/ de la Laura, 13, 08500, Vic, España J. Bajo and S. Rodriguez BISITE Plaza de la Merced s/n, 37008, Salamanca, España e-mail: [email protected], [email protected] A.P. de Leon F. de Carvalho et al. (Eds.): Distrib. Computing & Artif. Intell., AISC 79, pp. 435–439. © Springer-Verlag Berlin Heidelberg 2010 springerlink.com

436

P. Marti-Puig et al.

process binary signals, originally, and it was fast extended to gray level signals. This technique is proved to be very useful in digital image processing. Mathematical morphology is based on two fundamental operators: dilation and erosion. These two basic operations are done by means of a structuring element. The structuring element is a set in the Euclidean space and it can takes different shapes as circles, squares, or lines. Using different structuring elements it will achieve different results; therefore, the selection of a suitable structuring element is fundamental. A binary signal can be considered a set and dilation and erosion are Minkowski addition and subtraction with the structuring element [3]. In the context of stock prices we can work with series of data that can be modeled as gray level signals. In this context, the addition and subtraction operations that are applied in binary morphology are replaced by suprermum and infimum operations. Moreover, on the digital signal processing framework, supremum and infimum can be changed by maximum and minimum operations. In this context, the erosion can be seen as the minimum value of the part of the function inside a mobile window that is defined by the structuring element. Then, given a one-dimensional signal, the function f containing the stock prices and a flat structuring element Y, the erosion is defined as:

ε Y ( f )( x ) = min f ( x + s ) s∈Y

(1)

As the erosion computes the minimum gray level inside the mobile window function it decreases the peaks and accentuates the valleys of the original function f. On the other hand the dilation (for gray level signals) is defined as:

δ Y ( f )( x ) = max f ( x − s ) s∈Y

(2)

The dilation gives the maximum gray level value of the part of the function included inside the mobile template defined by the structuring element, accentuating peaks and minimizing valleys. By combining dilation and erosion we can form other morphological operations. The opening and the closing are basic morphological filters. The morphological opening of a signal f by the structuring element Y is denoted by γY(f) and is defined as the erosion of f by Y followed of a dilation by the same structuring element Y. This is:

γ Y ( f ) = δ Y (ε Y ( f ))

(3)

The morphological closing of a signal f by the structuring element Y is denoted by ϕY(f) and it is defined as the dilation of f by Y followed of the erosion by the same structuring element:

ϕY ( f ) = ε Y (δ Y ( f ))

(4)

Opening and closing are dual operators. Closing is an extensive transform and opening is an anti-extensive transform. Both operations keep the ordering relation between two images (or functions, in our case) and are idempotent transforms [3]. In the 1-D context these operations create a more simple function than the

Market Stock Decisions Based on Morphological Filtering

437

original. By combining an opening and a closing, both of them with the same structuring element, we can only create four different morphological filters. Considering the operations γY and ϕY, the four filters we could obtain are γYϕY, ϕYγY, γYϕYγY and ϕYγYϕY. No other different filter can be produced as a consequence of idempotency property. To derive different families of morphological filters we need to combine openings and closings with different structuring elements. New filters, alternating sequential filters, can be obtained by alternating appropriately theses operators [3].

3 The Proposed Trading System The system is very simple and has been developed using MATLAB. Initially was thought to operate in the day tracking context but it could be modified to intraday operations. Once the markets of interest are closed, the system updates the stock information from any public Internet source, then it processes the data and, it is the case, it generates the orders to next day. The only stock information required is the open, close, high and low prices that the stocks, or any financial asset, reached along every day session. As we initially look for a simple system, we establish that the system will take the decisions from a reduced set of signals. Basically it computes the opening and the closing of the prices and uses a linear mean that is computed from the close day prices. Based on the cross of these three signals the system generates the decisions. To generate the market orders we have developed a finite state machine that governs the switching between the short and long scenarios. In fig.1 we can see de opening and closing signals (in blue) computed using a flat structuring element of length 8. The red signal is the mobile mean taken from 34 elements. Next, in fig. 2 we have used the Santander stock prices along a period of 10 years. We maintain the same parameters of the system. In fig. 2 (a) we show the opening and closing signals (in blue) with L=8 and the mobile mean of 34 samples (in red). In fig. 2 (b) there are represented the long (blue) and short (red) operations. The system parameters can be optimized for different kind of data. Using the same data of fig.2, the Santander stock prices, we have searched the structuring element and the median filter that maximize the profits. The system performance is summarized in fig. 3.

Fig. 1 Telefonica stock prices. Opening and closing signals (blue) computed with a flat structuring element of length 8 and the mobile mean of 34 elements (red).

438

P. Marti-Puig et al.

Fig. 2 Santander stock prices representation (1917 days). (a) Opening and closing signals with L=8 (in blue) and the mobile mean with T=34 (in red). (b) System performance.

Fig. 3 Santander stock prices representation (1917 days). (a) Opening and closing signals with L=34 (in blue) and the mobile mean with T=55 (in red). (b) System performance.

4 Conclusions In this paper we have explored the nonlinear mathematical morphological filters in order to trade markets automatically. We have evaluated the system using only a reduced set of financial assets and it works quite well as we can see graphically. We have obtained some preliminary results. More quantitative analysis is required on long historic datasets in order to know some statistical parameters such as the maximum potential losses that it can generate or the maximum number of days that the system can keep in losses. It is desirable that the system can follow the great market movements as well as generates small number of false signals and, if

Market Stock Decisions Based on Morphological Filtering

439

it is the case, that those signals do not incur in large losses. Some optimizations have to be done. New structuring elements and more sophisticated morphological filters must be evaluated. In the context of financial data processing, the mathematical morphology can be explored in a lot of different ways showing that could be a good tool to include among other well-known techniques. Acknowledgments. This work has been supported by UVIC.

References 1. Timmons, H.: A London Hedge Fund That Opts for Engineers, Not M.B.A.’s (August 18, 2006) 2. Aldridge, I.: High-Frequency Trading: A Practical Guide to Algorithmic Strategies and Trading Systems. Wiley & Sons, Chichester (2009) 3. Serra, J.: Image Analysis and Mathematical Morphology. Academic, New York (1982)