## 1. Introduction

The global financial crisis of 2007–2008 (GFC) was caused by many factors but one of the main causes was the excessive expansion of financial assets including derivatives [

1,

2,

3]. The world’s leading financial markets include major equity index futures such as the S&P 500, NASDAQ 100, DJIA, FTSE Russel 100, Nikkei 225 and KOSPI 200. Among them, the KOSPI 200 futures and options markets have been the largest trading market since prior to the GFC until the mid-2010s [

4]. As a single time series data, the index futures, which generate a large amount of data as a result of large-scale transactions, have been widely used for statistical analysis [

5,

6]. In recent years, data mining and machine learning techniques are utilized to investigate the futures market.

Time series data is a collection of observational data that is generated chronologically from most scientific and business domains [

7]. Many researchers in various fields have used time series data for their research [

8,

9]. Time series data in financial markets have unique characteristics compared to that in other fields such as electrocardiograms [

10]. In stock price time series data, investors in equity markets show various patterns of investment. They can be categorized as investors who adopt fundamental analysis and technical analysis [

11]. Fundamental analysts make investment decisions using global economic, industry and business indicators. On the other hand, assuming that the past behavior of a stock price affects the future price, technical analysts make investment decisions based on historical prices or patterns of price movement using complex indicators. Accordingly, technical analysts use pattern analysis methods to analyze stock price charts for trading decisions [

12]. Many studies on technical analysis for pattern matching have been carried out [

13,

14,

15,

16,

17]. This pattern analysis is a method of predicting the stock price by examining specific patterns observed in the past stock price chart and confirming the existence of similar patterns in the current stock price [

18].

An algorithm for efficient pattern recognition of the time series data is needed to build a trading system based on pattern recognition. The Euclidean distance method or artificial intelligence method has been used to find a similar pattern for stock prices [

19,

20,

21]. Hu et al. [

22] proposed a model which is an investment strategy using a short- and long-term evolutionary trend algorithm. De Oliveira, Nobre and Zarate [

23] also proposed a model for predicting stock prices in the Brazilian market, which combines fundamental and technical analysis using artificial neural networks. The system development includes forecasting the FX market financial time series, which combines an adaptive network-based fuzzy inference system and quantum behavioral particle gain optimization and forecasting market trends using chart patterns [

11]. Patel et al. [

24] also proposed a model to predict trends in financial markets by comparing four predictive models such as artificial neural networks, support vector machines, random forests and naïve-Bayes. There are also studies showing the efficiency of dynamic time warping algorithms for the problem of retrieving multi-attribute time sequences similar to financial time series data [

25]. The proposed method based on the dynamic time warping algorithm predefines the pattern used as a template for pattern matching [

26]. These studies have focused on optimization and efficiency in pattern recognition. However, there is a limit to a study on system trading at the optimal trading time point by checking the similarity of existing patterns in the futures market. This trading strategy requires efficient pattern recognition algorithms such as dynamic time warping [

27]. Among them, only a few studies use the dynamic time warping algorithm for futures trading [

28,

29,

30].

The purpose of this research is to construct a pattern matching trading system (PMTS) that extracts efficiently the optimized pattern of the proposed representative pattern in time series data and conducts trading to find the optimal trading exit point. For this goal, we propose an algorithm trading system that matches the time series pattern of the index futures data with the representative pattern using the naïve dynamic time warping (DTW) algorithm. As the experiment progresses, we consider various situations in futures contracts such as when margin calls are made, the liquidity and volatility increases, the trend changes for trades that enter into the calculation of the intraday trade and trades exit right before the closing of the market, to find the optimal trading exit point. Our experimental results show stable and effective trading entry and exit strategies with relatively low trading frequencies.

A number of financial instruments that are traded in financial markets exist and an enormous number of models or techniques have been developed for efficient investment strategies. Therefore, financial instruments and investment techniques as well as investors make an important contribution to the efficiency of the financial markets. It is well known that the efficiency of the financial markets have played an important role in sustaining economic growth. Financial market investors are able to make more efficient investments strategies by using the PMTS. In this sense, the system developed in this paper appears to contribute to the efficiency of the financial markets and hence play a role in sustaining economic growth.

The rest of this paper is organized as follows.

Section 2 introduces the concept of futures markets, the concept of dynamic time warping algorithms and the sliding window method. In

Section 3, the topics include the standardization of extracted raw daily index futures data, the dynamic trading pattern together with the dynamic time warping analysis for real-time pattern recognition and the proposed trading entry and exit simulation.

Section 3.4 describes the procedure of the experiments performed and discusses the experimental results.

Section 4 interprets the results and suggests the direction of future research.

## 2. Materials and Methods

#### 2.1. Futures Market

The futures market is a market for futures trading, which is one of many derivatives. The value of derivatives relies on other assets called underlying assets such as commodities, stocks, bonds, indices and interest rates. In other words, it changes when the value of the underlying assets changes. Prior to the establishment of futures markets, forward contracts have been traded to avoid the risk related to the value of the underlying asset. When one does not need to have the underlying asset at the present time but needs it in the future, he or she can make a forward contract with a counter party that presents the underlying asset’s delivery price and date. Due to the credit risk inherent in the forward contract, futures markets have been established by standardizing transactions and eliminating the credit risk.

The futures market was originally designed to help market participants avoid exposure to the risk of price fluctuations. In recent years, the role of risk hedging by futures contracts has become more prominent. For instance, although KOSPI 200 index futures are recognized as a high-return investment, the primary purpose of investing in the stock index futures is to avoid the risk related to stock prices. The stock index futures’ underlying asset is a stock price index which is an intangible product and hence it cannot be acquired or delivered to the counter party of the contract. Investors in index futures have a long position when the bull market is predicted and have a short position when the bear market is predicted in the future. Accordingly, investors in index futures can realize profits in both bull and bear markets if they make a correct prediction. In other words, they should predict the direction of stock price fluctuations accurately. They can hardly make profits by responding promptly with intuitive and qualitative investment decisions based on past trading experience. Indeed, quantitative and systematized trading strategies which use existing futures investment strategies and past time series data are required for making profits. It is essential to develop a quantitative method to determine the most useful trading positions and timing of index futures to realize high returns.

An investor in a futures market is classified as a hedger who avoids risk and a speculator who seeks profit [

31,

32,

33,

34,

35]. The hedger takes the position to hedge the stock price risk and rollover the position until the settlement date, whereas a speculator tends to clear his or her position whenever he or she can make profits. The futures market operates a margin system to avoid the credit risk due to the leverage effect on underlying assets. It includes the initial margin, maintenance margin and additional margin. The initial margin is at least 15% of the contract value and must be paid to enter a new futures contract. The maintenance margin is at least 10% of the contract value and must be maintained for holding a futures contract. Additional margin should be paid if the margin level is lower than the maintenance margin as the futures price fluctuates. The additional margin payment is notified by brokerage firms, which is called a margin call. If the margin call is triggered and the additional margin is not paid, the exchange arbitrarily clears the outstanding position by making a reverse trading.

#### 2.2. Dynamic Time Warping

The dynamic time warping (DTW) algorithm is known as an efficient method to measure the similarity between two sequences of time series data (

Figure 1). Intuitively, the sequences are warped in a nonlinear fashion to match each other. The DTW minimizes distortion effects due to time-dependent movement by using an elastic transformation of time series data to recognize the similar phases between different patterns along time. Even if there is a deformation relationship between two different sequences of time series data, the DTW determines the most similarities between them [

7]. Since the DTW was introduced in the 1960s [

36], the algorithm has been applied to spoken word recognition [

37,

38], gesture recognition [

39], behavioral perception [

40], data mining and time series clustering [

25,

41,

42,

43].

The objective of DTW is to compare two time series

$X=({\U0001d4cd}_{1},\text{}{\U0001d4cd}_{2},\cdots ,{\U0001d4cd}_{N}),\text{}N\in \mathbb{N}$ and

$Y=({\U0001d4ce}_{1},\text{}{\U0001d4ce}_{2},\cdots ,{\U0001d4ce}_{M}),\text{}M\in \mathbb{N}$ and calculate the minimum cumulative distance between them [

44]. Various modifications of the algorithm have been proposed to speed up DTW computations such as multiscaling [

45,

46]. Local distance measurement is required to compare two time series that differ in length. The concept of the cost function or the distance minimization, which is the core of DTW, is applied to a dynamic programming algorithm to produce a small value when two sequences are similar and a large value when two sequences are not similar. The algorithm provides a way to optimize the alignment and to minimize cost functions or the distance.

The DTW algorithm creates a distance matrix

${C}_{l}\in {\mathbb{R}}^{N\times M}:{c}_{i,j}=||{\U0001d4cd}_{i}-{\U0001d4ce}_{j}||,\text{}i\in \left[1:N\right],j\in \left[1:M\right]$ that represents all pairwise distances. It is called the local cost matrix for the alignment of two sequences X and Y. After generating this matrix, the algorithm uses a warping function that defines the similarity between

${\U0001d4cd}_{i}\in X$ and

${\U0001d4ce}_{j}\in Y$, which follows the boundary condition of assigning the first and last elements of X and Y and finds the optimal alignment path to pass through. This optimal alignment path is a sequence of points of

$P=({\U0001d4c5}_{1},\text{}{\U0001d4c5}_{2},\cdots ,{\U0001d4c5}_{K})$ with

${\U0001d4c5}_{l}=\left({\U0001d4c5}_{i},{\U0001d4c5}_{j}\right)\in \left[1:N\right]\times \left[1:M\right]$ for

$l\in \left[1:K\right]$ that satisfies all three criteria of the boundary condition, the monotonicity condition and the step size condition. The boundary condition is the first and last values of sequences in the optimal alignment path. The monotonicity condition is sequence of points on the path placed in chronological order. The step size condition limits the long jumping warping path in time. It is generally recommended to use the formulated basic step size condition as

${\U0001d4c5}_{l+1}-{\U0001d4c5}_{l}\in \left\{\left(1,1\right),\left(1,0\right),\left(0,1\right)\right\}$. The cost function used to calculate the local cost matrix of all the bidirectional distances is:

The aligned warping path with the least cost is called the

${P}^{*}$ optimal warping path. By definition, the optimal path increases exponentially as the length of X and Y increases linearly, so all possible warping paths between X and Y, which consume a large amount of computation, must be tested. This problem can be solved by O(MN) that is the time complexity of DTW algorithm [

7]. The DTW distance between X and Y, DTW(X, Y), is then defined as the total cost of

${P}^{*}$ as follows:

where

${P}^{N\times M}$ is the set of all possible warping paths.

#### 2.3. Pattern Matching Trading System

This section describes the structure and characteristics of the pattern matching trading system (PMTS) used in experiments for index futures trading. The experiments determine the entry and exit of trading by matching the daily index futures time series data with fixed patterns using the DTW algorithm.

Figure 2 shows an experimental procedure diagram of the pattern matching trading system. The first phase of the procedure is to collect the daily index futures data and to preprocess them for outlier processing, missing value processing and standardization of the data from KOSCOM’s Check Expert system. In the second phase, the fixed time series patterns and the collected index futures time series patterns are recognized to find similar patterns and then classified by the dynamic time warping algorithm. The third phase is to improve the performance with training data for trading entry and exit simulations with various parameters and perform the verification with testing data.

Phase 1: Data preparation for the pattern matching trading system

To conduct this experiment, 137,242 KOSPI 200 index futures data were collected every at 10 min intervals from 01/02/2001 to 12/30/2015. The collected index futures time series data are preprocessed by outlier processing and missing value processing. All extracted daily index futures data are standardized by setting the index futures data to 0 at 12:00 pm and scaling with the min-max method. The scaled data is obtained by the following equation:

where

$f\left(d\right),\forall d\in Dailyfuturesdataset$ (dfid) is the daily index futures data.

The processed data is divided into two groups: the pattern recognition group that consists of data from 9:00 am to 12:00 pm and the trading group that consists of data after 12:00 pm. If there is no data at 9:00 am due to a delayed market opening caused by a market action or regulation, the missing data is filled with the closing price of the previous date.

Phase 2: Pattern recognition and determination of the trading position

We construct two sets of fixed patterns using two different time divisions. The time from 9:00 am to 12:00 pm is divided into three time zones (from 9 am to 10 am, from 10 am to 11 am and from 11 am to 12 pm) and a total of 27 fixed time series patterns is set up consisting of all possible combinations of three steps (upward, stable and downward) in each time zone. The 27 fixed patterns can be described by 9 representative roughness patterns as a result of eliminating the similarity in terms of macroscopic viewpoints and endpoints. In addition, the time from 9:00 am to 12:00 pm is divided into two time zones (or the first half from 9 am to 10:30 am and the second half from 10:30 am to 12:00 pm) to set up 9 representative patterns consisting of three steps and then 4 industry recommendation patterns are added to have 13 representative patterns.

Figure 3 and

Figure 4 below show the structure of 27 fixed patterns and 13 representative patterns, respectively.

The daily market data between 9:00 am and 12:00 pm from 01/02/2001 to 12/30/2015 are assigned to one of the fixed patterns that is the most similar to the market data by using the dynamic time warping method and then the frequency of each selected pattern is counted. At this step, the fixed patterns with a higher frequency than the filtering criteria are selected. For each selected pattern of the daily market data, the price at 12:00 pm and 3:00 pm on a day included in training period is compared. Then, “up” is assigned to the pattern if the price at 3:00 pm is higher than that at 12:00 pm and “down” is assigned to the pattern if the price at 3:00 pm is lower than that at 12:00 pm. The ratio of “up” to “down” for each pattern is calculated and used to determine the trading position in the testing period. Once a pattern from 9:00 am to 12:00 pm is selected for market data on one day that is included in a testing period, the investment strategy at 12:00 pm on that day is determined as follows:

- -
Enter a long position at 12:00 pm and clear the position by taking a short position at 3:00 pm if the ratio of “up” to “down” for the selected pattern is higher than 1.

- -
Enter a short position at 12:00 pm and clear the position by taking a long position at 3:00 pm if the ratio of “up” to “down” for the selected pattern is lower than 1.

The margin of the futures trading is settled at 12:00 pm when the volatility and liquidity increase. Therefore, it is a critical time to enter a position. For intraday trades, the clearing time can be used at various points in time and is not limited at 3:00 pm.

Phase 3: PMTS simulation

In the last phase, we performed PMTS simulation by applying trading rule created in Phase 2.

Figure 5 shows the workflow of PMTS simulation.

As shown in this figure, we first set the sample period using a sliding window method and divide each window into training and testing periods. We use the daily index futures data at every 10 min from 9:00 am to 12:00 pm for pattern matching to the representative patterns constructed by data at every minute from 9:00 am to 12:00 pm. Then, using the DTW algorithm with various ranges of parameters, we conduct pattern matching to daily index futures data and determine the entry and exit position for the testing period. This process is repeated for all windows for the selected parameters. As a last step, we analyze the trading profit and determine the optimal parameters for PMTS.

Figure 6 shows the structure of the sliding windows.

The sliding window method has been used for simulation of time series data [

47,

48,

49,

50,

51].

Table 1 shows a set of 54 windows with an 18 months training period and a 3 months testing period. For example, Window1 is composed of the 18 months training period of 01/2001–06/2002 and the 3 months testing period of 07/2002–09/2002. Sliding 3 months from Window1, Window2 is set with a training period of 04/2001–09/2002 and a testing period of 10/2002–12/2002. The sliding is continued until the entire sample period is included and produces a total of 54 windows.

As a result of the PMTS execution for each window, a revenue profile for each pattern from 2:00 pm to 3:00 pm is generated. Our experiment uses a total of 7 clearing times at 10-min intervals from 14:00 to 15:00 to find the optimal clearing time.

## 4. Discussion

The purpose of this study is to develop a pattern matching trading system using the DTW algorithm with optimal parameters. Using KOSPI 200 index futures market data from 2001 to 2015, we conduct experiments with various ranges of parameters and find optimal parameters. Our experimental results show that the PMTS based on the DTW algorithm provides stable and effective trading strategies with relatively low trading frequencies. When financial market investors make more efficient investment strategies with the PMTS, the financial markets are more likely to be efficient. In this sense, the system developed in this paper contributes the efficiency of the financial markets and helps to achieve sustained economic growth.

A future study can be enriched by the studies presented in this paper. An interesting extension to the current study would include empirical studies using a more sophisticated DWP algorithm, such as the deepening dynamic time warping (DDTW) algorithm or the segmented dynamic time warping (SDTW) algorithm or the cluster generative statistical dynamic time warping (CSDTW) algorithm, from which better results are expected. This study could also be extended by experiments with various financial instruments such as interest rate futures contracts, options and other derivatives to find the optimal strategy.