You are on page 1of 72

THESIS

STOCK TRADING APPROACHES USING LSTM AND GANS

PATTAREEYA PIRAVECHSAKUL

GRADUATE SCHOOL, KASETSART UNIVERSITY


Academic Year 2021
THESIS APPROVAL
GRADUATE SCHOOL, KASETSART UNIVERSITY

Master of Engineering (Information and Communication


DEGREE: Technology for Embedded Systems)
Information and Communication Technology for Embedded
MAJOR FIELD: Systems
DEPARTMENT: Electrical Engineering
TITLE: Stock Trading Approaches using LSTM and GANs

NAME: Miss Pattareeya Piravechsakul

THIS THESIS HAS BEEN ACCEPTED BY

THESIS ADVISOR
(Assistant Professor Teerasit Kasetkasem, Ph.D.)
THESIS CO-ADVISOR
(Mr. Sanparith Marukatat, Ph.D.)
THESIS CO-ADVISOR
(Professor Itsuo Kumazawa, Ph.D.)
DEPARTMENT HEAD
(Assistant Professor Nithiphat Teerakawanich, Ph.D.)

DEAN
(Associate Professor Srijidtra Charoenlarpnopparut, Ph.D.)
THESIS

STOCK TRADING APPROACHES USING LSTM AND GANS

PATTAREEYA PIRAVECHSAKUL

A Thesis Submitted in Partial Fulfillment of


the Requirements for the Degree of
Master of Engineering
(Information and Communication Technology for Embedded Systems)
Graduate School, Kasetsart University
Academic Year 2021
C

ABST RACT
Pattareeya Piravechsakul : Stock Trading Approaches using LSTM and GANs.
Master of Engineering (Information and Communication Technology for Embedded
Systems), Major Field: Information and Communication Technology for Embedded
Systems, Department of Electrical Engineering.
Thesis Advisor: Assistant Professor Teerasit Kasetkasem, Ph.D.
Academic Year 2021

Growth in Artificial Intelligence (AI)-related financial research has


been growing exponentially. Examples of using AI in the financial market, portfolio
management, stock market prediction, trading strategy, and capture real-time
patterns. A stock market prediction is one of the most challenging tasks. There are a
lot of affected factors to make the financial market price go up or down such as
economy, politics, dynamic environment. Although, a lot of free technical indicator
tools available for traders and investors are provided by brokers, some technical
indicator tools might not be able to capture market behaviors and understand the
complexity of stock data. To address the challenging task, we proposed the deep
learning models, namely, Generative Adversarial Networks (GANs), and Long
Short-Term Memory (LSTM) that applied as stock price predictor to predict future
prices based on learning historical price data from Stock Exchange of Thailand
SET50 index. The condition GANs (CGANs) and the Wasserstein GANs
(WGANs) are a type of GANs were combined and used in this work. The SET50
index data between August 2011 to July 2019 was used for the training models.
After that, the models were tested through our trading operations to find a suitable
trading strategy in back testing and forward testing. The trading operations were
separated into 2 cases, long-only case, and long-short case. We also compared the
profitable model performance based on the Conditional Wasserstein GANs
(CWGANs) and the LSTM with buy & hold strategy and four technical indicators
strategy, namely, Moving Average (MOV), Moving Average Convergence
Divergence (MACD), Bollinger Bands (BB), and Relative Strength Index (RSI).
Moreover, we studied ability of the GANs model based on con the different
architectural designs of the GANs based on LSTM and CNN model. We found that
the CNN-CNN architecture of the CWGANs algorithm that received the best
performance in our data. The results show that the CWGANs and the LSTM trading
strategy are promising. Mostly, both models in long-only case and long-short case
can achieve a higher return than buy & hold, MOV, MACD, BB, and RSI strategy
in back testing and forward testing.

_________________ _______________________________ ____ / ____ / ____


Student's signature Thesis Advisor's signature
D

ACKNOWLEDGEMENT S

ACKNOWLEDGEMENTS

I would like to take this opportunity to thank everyone who has been involved
in this project. Without them, my research work to not complete the research
successfully.
Firstly, I would like to express my deep and sincere gratitude to my advisor,
Asst. Prof. Dr. Teerasit Kasetkasem who gave me a lot of knowledge such as financial
literacy, machine learning and etc. He always advises and supports me in everything.
Moreover, his vision, dynamism and motivation have deeply inspired me to do this
project about using AI in finance.
Secondary, Dr. Sanparith Marukatat, co-advisor from NECTEC researcher
(NSTDA) who explained to me to understand deep learning models involved in this
project. Furthermore, he suggested to me a lot of valuable ideas to do in this work. I am
really thankful and appreciate having a great chance to cooperate with him. Apart from
my thesis advisor and NECTEC researcher, I would like to thank another co-advisor,
Prof. Itsuo Kumazawa from Tokyo Institute of Technology who taught me the
fundamentals of artificial intelligence and machine learning.
Finally, I would like to thank to Thailand Advanced Institute of Science and
Technology and Tokyo Institute of Technology (TAIST-Tokyo Tech), Kasetsart
University, and the National Science and Technology Development Agency for
supporting my master’s degree and giving me a full scholarship that covers tuition fee
and living allowance.

Pattareeya Piravechsakul
TABLE OF CONTENTS

Page

....................................................................................................................................... C
ABSTRACT.................................................................................................................. C
ACKNOWLEDGEMENTS .......................................................................................... D
TABLE OF CONTENTS .............................................................................................. E
LIST OF TABLES ........................................................................................................ G
LIST OF FIGURES ...................................................................................................... H
INTRODUCTION ......................................................................................................... 1
OBJECTIVES ................................................................................................................ 3
REVIEW OF LITERATURES ...................................................................................... 4
BACKGROUNDS ......................................................................................................... 6
1. Moving Average (MOV) strategy ....................................................................... 6
2. Moving Average Convergence Divergence (MACD) strategy ........................... 7
3. Bollinger Bands (BB) Strategy ............................................................................ 9
4. Relative Strength Index (RSI) strategy.............................................................. 10
5. Long Short-Term Memory (LSTM) .................................................................. 11
6. Convolution Neural Network (CNN) ................................................................ 13
7. Generative Adversarial Networks (GANs) ....................................................... 16
7.1 Conditional Generative Adversarial Networks (CGANs) ........................ 18

7.2 Wasserstein Generative Adversarial Networks (WGANs) ...................... 20

METHODOLOGY ...................................................................................................... 21
1. Workflow Diagram ............................................................................................ 21
2. Financial Dataset ............................................................................................... 21
3. Data Preparation ................................................................................................ 22
3.1 Input Data Structures ................................................................................ 22
3.2 Data Normalization ..................................................................................... 22
F

3.3 Split Data ..................................................................................................... 23


4. Stock Price Predictor and its parameters ........................................................... 24
4.1 Long-Short Term Memory (LSTM) Stock Price Predictor ......................... 24
4.2 Generative Adversarial Networks (GANs) Stock Price Predictor .............. 25
5. Trading Operations ............................................................................................ 28
EXPERIMENTAL RESULTS AND DISCUSSION .................................................. 30
1. Selecting Model ................................................................................................. 30
2. Technical Indicators Setup ................................................................................ 31
3. Stock Price Prediction Setup ............................................................................. 31
4. Result Comparisons ........................................................................................... 32
4.1 First Part Period (Forward Testing).......................................................... 44
4.2 Second Part Period ...................................................................................... 47
4.2 Third Part Period ...................................................................................... 49
4.4 Fourth Part Period ....................................................................................... 50
DISCUSSION .............................................................................................................. 55
CONCLUSION ............................................................................................................ 57
LITERATURE CITED ................................................................................................ 59
CURRICULUM VITAE .............................................................................................. 62
LIST OF TABLES

Page

Table 1 RMSE value summary of each model ........................................................... 30


Table 2 Coefficient of Determination Performance .................................................... 31
Table 3 Performance of Investment (1 July 2015 – 3 July 2019) based on traditional
technical indicators ...................................................................................................... 35
Table 4 Performance of Investment (1 July 2015 – 3 July 2019) based on LSTM
trading in long-only case .............................................................................................. 36
Table 5 Performance of Investment (1 July 2015 – 3 July 2019) based on LSTM
trading in long-short case ............................................................................................. 37
Table 6 Performance of Investment (1 July 2015 – 3 July 2019) based on CWGANs
trading in long-only case .............................................................................................. 38
Table 7 Performance of Investment (1 July 2015 – 3 July 2019) based on CWGANs
trading in long-short case ............................................................................................. 39
Table 8 Performance of investment in SET50 index data (1 January 2020 – 31
December 2020) based on four technical indicators and buy & hold strategy ............ 46
Table 9 Performance of investment SET50 index data (1 January 2020 – 31 December
2020) based on LSTM and CWGANs stock price predictor. ...................................... 46
Table 10 Performance of investment in top 100 Thai stocks (1 January 2020 – 31
December 2020) ........................................................................................................... 48
Table 11 Performance of investment in top 100 Thai stocks (1 January 2021 – 15
October 2021) .............................................................................................................. 49
Table 12 Investment performance of the 9 picked stocks based on the traditional
technical indicators (1 January 2021 – 15 October 2021) ........................................... 51
Table 13 Investment performance of the picked 9 stocks based on our stock price
predictors (1 January 2021 – 15 October 2021)........................................................... 52
Table 14 %Average return of each trading strategy in each forward testing.............. 53
LIST OF FIGURES

Page

Figure 1. Moving Average Strategy.............................................................................. 6


Figure 2. Moving Average Convergence Divergence Strategy .................................... 8
Figure 3. Bollinger Bands Strategy ............................................................................... 9
Figure 4. RSI Strategy................................................................................................. 11
Figure 5. LSTM cell .................................................................................................... 12
Figure 6. Example of calculation of Convolution operation with features ................. 14
Figure 7. Valid and Same padding .............................................................................. 14
Figure 8. 1-D Convolution Neural Network ............................................................... 15
Figure 9. Generative Adversarial Network analogy ................................................... 17
Figure 10. Conditional Generative Adversarial Networks (CGANs) Architecture .... 19
Figure 11. Example of comparison generated output between Simple GANs model
(left picture) and CGANs model (right picture) .......................................................... 20
Figure 12. Workflow diagram..................................................................................... 21
Figure 13. Input Dataset for each model ..................................................................... 23
Figure 14. LSTM Stock Price Predictor ..................................................................... 25
Figure 15. CWGANs Stock Price Predictor ............................................................... 26
Figure 16. CWGANs Architecture ............................................................................. 27
Figure 17. Maximum drawdown................................................................................. 33
Figure 18. The trading strategy of traditional technical indicators, (a) MOV strategy,
(b) MACD strategy, (c) BB strategy and (d) RSI strategy on SET50 index data
between 3 July 2015 to 3 July 2019. ............................................................................ 41
Figure 19. Examples of LSTM trading strategy (a) Long-only case (past 8-day), (b)
Long-short case (past 8-day) on SET50 index data between 3 July 2015 to 3 July
2019.............................................................................................................................. 42
Figure 20. Examples of the long-only trading strategy of CWGANs stock price
predictor (a) past 6-day (P6), (b) past 8-day (P8), (c) past 16-day (P16) on SET50
index data between 3 July 2015 to 3 July 2019. .......................................................... 43
I

Figure 21. Examples of the long-short trading strategy of CWGANs stock price
predictor (a) past 6-day (P6), (b) past 8-day (P8), (c) past 16-day (P16) on SET50
index data between 3 July 2015 to 3 July 2019. .......................................................... 44
Figure 22. Average return comparison between our models and traditional indicators
(SET50 index data in 2020) ......................................................................................... 45
Figure 23. Average return comparison between our models and traditional indicators
(Top 100 Thai stocks in 2020) ..................................................................................... 47
Figure 24 Average return comparison between our models and traditional indicators
(Top 100 Thai stocks between 1 Jan 2021 to 15 Oct 2021 .......................................... 50
Figure 25 Average return comparison between our models and traditional indicators
(9 picked stocks between 1 Jan 2021 to 15 Oct 2021) ................................................. 52
INTRODUCTION

The financial market is one of the most attractive investments among many
people. There are many studies on stock market price movement prediction for
decades (Nelson, Pereira, & Oliveira, 2017), but a future price prediction is still very
difficult and complex. There are many technical indicators available in the market that
allow traders and investors can use. The technical indicators are created from
observation and measurable characteristics of quantitative historical data without
considering a financial statement such as a balance sheet, income statement, and cash
flow statement. The technical indicators are classified into two main types, a lagging
indicator, and a leading indicator. Examples of the lagging indicator based on a
historical data report are Moving Average Convergence Divergence (MACD), and
Bollinger Bands (BB). The lagging indicators provide traders and investors with
delayed buy or sell signals at later than the actual market trend. As a result, the
indicators have made investors/traders miss the best point to buy or sell. The leading
indicators attempt to forecast future price movements in the market such as Relative
Strength Index (RSI), Stochastic Oscillator, and On-Balance Volume (OBV). Most
traders use the leading indicator to buy a stock when a price is oversold and sell a
stock when a price is overbought.

In recent years, machine learning and deep learning algorithms have become a
part of every task, including in the financial market. The financial market approaches
to use the deep learning algorithms to predict future prices based on learning
historical price data. In this work, the deep learning algorithms used in this work are
Long-Short Term Memory Network (LSTM) and Generative Adversarial Networks
(GANs). Both models were used to predict the future price based on learning
historical price data from the Stock Exchange of Thailand SET50 index data. The
LSTM is very popular algorithm use in time-series data. Many applications show high
performance by using the LSTM model. The GANs model is very successful in image
generation. Many tasks in generating realistic photographs have shown the high
performance by using Generative Adversarial Networks. Thus, we expect both models
2

to make a profit and overcome the four technical indicators and Buy & Hold
strategies. Also, we studied the different architectures designs in generator model and
discriminator model of the GANs based on LSTM and CNN. Thus, there were 4
different architectural designs, LSTM-LSTM, LSTM-CNN, CNN-CNN, and CNN-
LSTM. Then, the best performance of LSTM and GANs based on the root mean
square error (RMSE) value and the R-squared value would trade in back testing and
forward testing cases to test the profitable model. Additionally, both models would
compare the profitability with five strategies, namely, buy and hold, MOV, MACD,
BB, and RSI strategies.
3

OBJECTIVES

The purposes of this thesis are as follows:

1. To study the ability of LSTM and GANs to learn stock price prediction.
2. To study the different architectural designs based on CWGANs model.
3. To compare the profitability of LSTM, CWGANs, Buy & Hold and the four
technical indicators, namely MOV, MACD, BB and RSI based on the Stock
Exchange of Thailand (SET) market.
4. To find a suitable trading strategy of deep learning models and overcome the
four technical indicators and buy & hold strategy.
4

REVIEW OF LITERATURES

Stock market prediction is the biggest challenges owing to non-stationary data,


chaotic data, and innate complexity. Typically, an economy, a company's strength,
and a personal financial statement can affect to the stock price. Moreover, increasing
company’s capital stock give a bad sign for stock price, it is so hard to know inside
information. However, many studies in financial area show that the stock market
prediction is predictable.

Examples of the literature reviews, Autoregression Integrated Moving


Average (ARIMA) model (Adebiyi, Adewumi, & Ayo, 2014) are from statistical
model perspectives. The ARIMA can be efficient in financial time series forecasting.
The ARIMA works well in linear and stationary data. However, a well-known fact is
the stock prices that movement in non-linear and non-stationary data. Therefore, the
ARIMA is not able to predict stock prices on long-term prediction. Long Short-Term
Memory (LSTM) (Nelson et al., 2017) is a type of deep learning that can solve the
vanishing gradient problem during back propagation instead of recurrent neural
network (RNN). The LSTM (Wei, 2019) can keep the important past information and
learn the order dependence between items in a sequence. There are three main gates
for the LSTM cell, a forget gate, an input gate, and an output gate. This is a very
important things to predict stock prices. It should be able to make higher performance
than the technical indicators.

Many tasks in generate realistic photographs have shown the high


performance by using Generative Adversarial Networks (GANs). There are a few
research works using the GANs in financial market such as stock price prediction,
stock price manipulation detection, and portfolio management. Although, the GANs
have a high ability to produce synthetic samples, training a GANs is difficult (Wang,
She, & Ward, 2019). The generator and discriminator might never converge. The
major problems of training GANs model are non-convergence, mode collapse
diminished gradient and hyperparameter selections. (Leangarun, Tangamchit, &
Thajchayapong, 2018) applied GANs for stock price manipulation. They use random
noise (Z) dimension that is equal the number of time steps multiplying with the
5

number of input features. Their result shows that the model performed well, GANs
can distinguish between unseen normal cases and manipulative stock pattern with
accuracy of 68.10%. (Zhang, Zhong, Dong, Wang, & Wang, 2019) used four
methods, namely, SVR, ANN, LSTM, and GANs for stock price prediction. Their
results show that GANs model has the highest accuracy based on RMSE value.
However, there were hardly research works that studied in finding suitable trading
strategy of the deep learning model based on the Stock Exchange of Thailand (SET)
market. This work, we employed GANs and LSTM model to find suitable trading
strategy and make profitable model. Also, both models compare the profitability with
buy & hold strategy and four technical indicators, namely MOV, MACD, BB, RSI
strategy.
6

BACKGROUNDS

1. Moving Average (MOV) strategy

Moving Average (MOV) is a basic technical analysis tool in a finance market.


The MOV is calculated from historical prices to keep track of prices trends for
specific securities. There are different types of moving average such as Simple
Moving Average, Exponential Moving Average, Weighted Moving Average, and
Double Exponential Moving Average. The differences between these types are from
the weights given to the current price. (Gurrib, 2014) used the moving average
crossover strategy in the S&P500 market index. Their results showed that the moving
average can yield a return of 24% over the 1593 investment days or around 4 years,
but it could not overcome the buy & hold strategy for wave5 in the S&P500 market
index based on their experiments.

In this experiment, we choose the simple moving average to trade in this


strategy. The MOV strategy uses two moving average line, a short-term moving
average line, and a long-term moving average line.

https://forextraininggroup.com/anatomy-of-popular-moving-averages-in-forex/

Figure 1. Moving Average Strategy


7

The Moving Average formula is as follow:

1
𝑆𝑀𝐴𝑘 = ∑𝑛𝑖=𝑛−𝑘+1 𝑃𝑖 (1)
𝑘

where 𝑆𝑀𝐴𝑘 , 𝑃𝑖 and 𝑛 are the simple moving average at 𝑘 window size, price value
and the number of observed values.

The MOV strategy is as follow:

A buy signal happens when the short-term moving average line is above the
long-term moving average on the first day. On the contrary, a sell signal happens
when the short-term moving average line is below the long-term moving average line
on the first day.

Figure 1 demonstrates the MOV strategy, a blue line represents a long-term


moving average line, a red line represents a short-term moving average line. When the
red line is above the blue line on the first day that occur the buy signal. The sell
signal, the red line is below the blue line on the first day.

2. Moving Average Convergence Divergence (MACD) strategy

Moving Average Convergence Divergence (MACD) (de Souza, Ramos, Pena,


Sobreiro, & Kimura, 2018) is a technical analysis indicator created by Gerald Appel
in the late 1970s. Although the MACD is created for many years, traders and
investors still use in the present. The MACD is one of the most popular technical
indicators that most traders use to consider a decision in the financial market. The
MACD indicator can inform both trend and momentum indicator. There are two lines
that are used in this strategy, a MACD line, and a signal line. The MACD line is
computed by subtracting the longer-period Exponential Moving Average (EMA) from
the shorter-period EMA. Then, the number of time periods EMA of the MACD line is
used with the MACD line as a signal line. The MACD trading (Vaidya, 2020), most
traders and investors like to use 12-day EMA for short term period and 26-day EMA
for long term period. For Signal Line, the number of days is 9-day EMA of MACD
signal. Therefore, we use these values as the default values in this work.
8

https://www.dailyfx.com/education/technical-analysis-tools/macd-indicator

Figure 2. Moving Average Convergence Divergence Strategy

The Moving Average Convergence Divergence formula is as follow:

𝐸𝑀𝐴𝑡 = (𝐶𝑢𝑟𝑟𝑒𝑛𝑡 𝑃𝑟𝑖𝑐𝑒𝑡 ∗ 𝐾) + (𝐸𝑀𝐴𝑡−1 ∗ (1 − 𝐾))


(2)

where 𝐾 is smoothing factor for the EMA (𝐾 = 𝑆𝑚𝑜𝑜𝑡ℎ𝑖𝑛𝑔 𝑉𝑎𝑙𝑢𝑒/(1 + 𝑡)), and t is
the number of days. The smoothing value can be altered upon preference, typically
chosen as 2.

𝑀𝐴𝐶𝐷 = 𝐸𝑀𝐴𝑠ℎ𝑜𝑟𝑡 𝑡𝑒𝑟𝑚 − 𝐸𝑀𝐴𝑙𝑜𝑛𝑔 𝑡𝑒𝑟𝑚


(3)

𝑆𝑖𝑔𝑛𝑎𝑙 𝑙𝑖𝑛𝑒 = 𝐸𝑀𝐴𝑛 (𝑀𝐴𝐶𝐷) (4)

The MACD strategy is as follow:

A buy signal occurs when the MACD line crosses above the signal line on the
first day as starting bullish crosses. A bearish crossover or a sell signal occurs when
the MACD line crosses below the signal line on the first day.

For example, (Figure 2) when the blue line that represents the MACD line is
above the red line that represents the Signal line on the first day, the buy signal is
9

generated. On the other hand, the sell signal is generated, the MACD line is below the
Signal line on the first day.

3. Bollinger Bands (BB) Strategy

Bollinger Bands (BB) (Shah & Manubhai, 2015) remain another widely
popular technical indicator. The BB were developed by John Bollinger in the 1980’s.
The Bollinger Bands use the N period Simple Moving Average (SMA) and standard
deviation of previous price N days as follow the equation (6). The BB are a type of
volatility indicator, they consist of a band of three lines, upper band, middle band, and
lower band of likely a channel of prices. The basic explanation of BB, the upper band
and the lower band are created as a channel that represent measuring overbought or
oversold conditions. When the stock price is above the upper band that means there is
a statistically high. In other words, the stock price is beginning to look expensive for
most traders. Generally, most traders believe that the stock price is likely to be cheap
or statistically low when the price is below the lower band. The distance of the bands
is created based on standard deviation. The BB can inform a trader how much the
volatility swings. When the volatility of price increase, that means the distance
between the upper band and the lower band will increase as well.

https://www.tradingview.com/chart/GOLDSILVER/AQAQPvfD-Gold-Silver-Ratio-RSI-Bollinger-Bands-Buy-or-Sell/

Figure 3. Bollinger Bands Strategy


10

The Bollinger Bands formula are as follow:

𝑵
∑ (𝑿 −𝑿) ̅̅̅̅𝟐
𝝈 = √ 𝒊=𝟏 𝑵𝒊 (5)

∑𝑵
𝒊=𝟏 𝑿𝒊
̅=
𝑿 (6)
𝑵

𝑩𝑩𝒊 = 𝑿𝒊 ± 𝝈𝒊 ∗ 𝒅 (7)

Where 𝜎, 𝑋̅, 𝑁, and 𝑑 are the standard deviation, the average of the total of
observations, the number of observation days, and the number of standard deviations
value away from the average value. Typically, the default of 𝑑 and N are set of 2 and
20, respectively.

The BB strategy is as follow:

Figure 3 shows the BB trading strategy. A buy signal is generated when the
stock price has fallen below the lower band on the first day. In contrast, the stock
price is outside the upper band on the first day, a sell signal is created.

4. Relative Strength Index (RSI) strategy

Relative Strength Index (RSI), developed by J. Welles Wilder and published


in 1978, is a part of momentum indicators. The RSI (Sahin & Ozbayoglu, 2014) is
still one of the most popular oscillator indices for most traders and investors. The RSI
gives the important signals to traders/investors to illustrate buy or sell signal. As a
result, this indicator measures speed and change of price movement to evaluate
overbought or oversold which is identical to Bollinger Bands, but there are different
calculations. The RSI indicator is calculated by using average price gain and average
price loss over a given period of times.

The Relative Strength Index formula is as follow:

100
𝑅𝑆𝐼 = 100 − 𝑥 (8)
𝑦
11

where x and y are average of N days’ up closing price and average of N days’ down
closing price. Typically, the default value of N-day is 14-day.

https://www.chartmill.com/documentation/technical-analysis-indicators/128-The-RSI-Indicator-and-Trading-Strategies

Figure 4. RSI Strategy

The RSI strategy to trade is as follow:

The RSI oscillates between 0 % and 100 %. A sell signal occurs when the
value is above 70 % represents overbought. To the contrary, oversold or buy signal is
generated when the value is below 30 %.

5. Long Short-Term Memory (LSTM)

Long Short-Term Memory neural network or LSTM for short (Fig. 5) is an


artificial recurrent neural network (RNN) used in field of deep learning. Here, the
LSTM is used in this work to forecast future stock prices.

The LSTM (Chouhan et al., 2018) (Liu, Liao, & Ding, 2018) is designed to
solve vanish gradient problem during back propagation of recurrent neural network
(RNN) so LSTM can reduce effect of short-term memory. In other words, the LSTM
can keep past information. This is important thing in the work to predict stock prices.
12

There are three different gates in LSTM cell (Wei, 2019), a forget gate, an input gate,
and an output gate.

Figure 5. LSTM cell

Forget gate decides which information will be forgotten or kept. The current
input and the previous hidden state are passed into the Sigmoid function. The output
values from the Sigmoid function will tell the network should forget or keep
information. If the values close to 0, this gate will forget the information. On the other
hand, this gate will keep the information when the values close to 1.

Input gate decides which information should be updated from the current state.
The hidden state and the current input are passed into the Sigmoid function, the output
values are between 0 to 1. 0 means not important information, 1 means important
information. At the same time, the hidden state and the current input are passed into
the hyperbolic tangent (tanh) function which generates the output values between -1
to 1 to assist the network. The new input is multiplied between the output of input
gate and the candidate input.

Output gate determines the next hidden state. This gate is multiplied between
the output values of the hidden state and the current input are passed into the Sigmoid
13

function and the cell state information under the tanh function. The new cell state and
the new hidden state will be carried to the next LSTM cell. Thus, the equation of new
cell state and hidden state output are defined as:

𝐶𝑡 = [(𝐶𝑡−1 ) ∗ (𝑓𝑡 )) + (𝐶𝑡̅ ) ∗ (𝑖𝑡 )] (9)

ℎ𝑡 = 𝑂𝑡 ∗ tanh (𝐶𝑡 ) (10)

where 𝐶𝑡 is the new cell state, ℎ𝑡 is the hidden state output, 𝐶𝑡−1 is the previous cell
state, 𝑓𝑡 is the output of the forget gate, 𝐶𝑡̅ is the candidate input (processed under the
tanh activation function), it is the output of the input gate, and 𝑂𝑡 is the output of the
output gate.

6. Convolution Neural Network (CNN)

Convolutional Neural Network (CNN) (O'Shea & Nash, 2015) is widely used
in image classification, image recognition and objects detections. CNN is a specific
type of artificial neural network model which can extract feature importance from
input data to learn which features are the important features from input data. CNN
takes input data with labels for training, each input data will pass a series of
convolution layers with filter or kernel. It can learn important features by using small
squares of input data.

Convolution

As seen in (Fig. 6), the convolution of 7x7 matrix (I) as input data multiplies
with 3x3 matrix (K) as a filter (or kernel), the operation will skip from left to right and
top to bottom. Here, we will get the dimension of output is 5x5 matrix. The output of
the operation will be called “Feature Maps”.
14

Figure 6. Example of calculation of Convolution operation with features

The feature maps (I*K) will show us understanding of what features our CNN
detect. The result in feature maps can change depending on the filter (or kernel)
values that we use.

Padding

Figure 7. Valid and Same padding

For example (Fig. 6), the dimension of output does not equal as the input, it
might affect to loss importance information. Therefore, we can solve the problem by
using same padding. There are two options for padding as shown in Fig. 7. Valid
15

padding means no padding. This case reduces output size. Same padding or usually
called “Zero padding” gives the output has the same dimension as the input by adding
zero values to the left/right and up/down of input data.

Strides

Stride is the number of data skipping that we want. The more the number of
data skipping, the less training time, meanwhile, some important information might be
lost.

1-D Convolution for Time series

There are several tasks about time series data. Examples of time series data,
climate changing of each day, house price, stock price, patient health monitoring etc.
We analyze time series data in order to extract significant characteristic of the data.
Nowadays, a lot of suitable machine learning algorithms are able to use with time
series analysis. One of them is Convolution Neural Network (CNN). As previous
mentioned details, CNN is famous in image processing area. Nevertheless, research
has shown that using CNN based on regression model for time series analysis has
high level of accuracy and robustness in prediction area.

Here, we use 1D Convolution in our project. We will describe how the 1D


convolution works on time series data that are illustrated in Fig. 8.

Figure 8. 1-D Convolution Neural Network

Time series data consist of two elements, length, and width. The length is the
number of timesteps that we want the network to see each epoch in training. The
16

width is the number of features. Examples of length and width for stock price data,
the length is the number of only trading day, the width can be open price, high price,
low price, close price, or technical indicator data. The input data is fed to each
Convolution layer. The convolution kernels within the layer can specific the length of
the 1D convolution window. The kernels will move from the starting to the ending of
the time series. Then the output data from 1-D CNN will be reduced dimension by
using Max-pooling layer or Flatten layer (option). After that, Fully Connected layer,
or Dense layer, will be used for the last layer with activation function to find the final
output.

7. Generative Adversarial Networks (GANs)

Generative Adversarial Networks, or GANs for short, (Eckerli & Osterrieder,


2021) are a novel deep neural network framework which consists of two neural
networks. One network is called a “Generator” or generative network, another
network is called a “Discriminator” or discriminative network. The generator
generates a fake data and fool the discriminator while the discriminator distinguishes
input data that is real data or fake data. To more understand how the GANs work
before discussing mathematically details, we example the easiest way through simple
Generative Adversarial Networks analogy as is shown Fig. 9.

The GANs analogy (Fig. 9) shows that there is a pre-owned designer bag shop
which buy a designer bag from customers. However, there are some customers who
are counterfeiter that take fake designer bag to sell at the pre-owned designer bag
shop. The counterfeiter tries to fool the shop owner that his bag is real while the shop
owner will authenticate whether the designer bag is real or fake. Sometimes, it’s very
easy to identify which is a real designer bag for the owner shop. Therefore, the
counterfeiter must learn many techniques to make fake designer bag to look like
authenticate designer bag. On the other hand, the owner shop would learn more about
how to authenticate designer bag techniques in order that the counterfeiter is not able
to fool too easy.
17

Figure 9. Generative Adversarial Network analogy

The gold of the counterfeiter as Generator can fool the owner shop. The gold
of the owner shop as Discriminator is able to distinguish whether the designer bag is
real or fake designer bag. The counterfeiter (Generator) and the owner shop
(Discriminator) will compete each other as zero-sum game. The two networks try to
get more accuracy of their goal. Eventually, the final goal is to consider the
equilibrium of their competition as a result the generator has ability to create fake data
that the discriminator is not able to indistinguishable that is the fake data.

A common loss function of original GANs is used in binary classification


problems. While training the discriminator model, we defined the label of real data is
1. On the other hand, the data is generated from the generator that the label is 0.
Therefore, the model should produce high probability when we feed the real data and
low probability when we feed the fake data. For generator model, we take random
noise (𝑧) to generate fake data. The random noise (𝑧) can be normal distribution or
uniform distribution. We expected generator model to generate the fake data that like
the real data as much as possible. Thus, the fake data was trained with label 1 for
training the generator. Here, the objective functions of the generator and discriminator
are defined as

The discriminator (D):

𝐿(𝐷(𝑥), 1) = log (𝐷(𝑥)) (11)


18

𝐿(𝐷(𝐺(𝑧)), 0) = log (1 − 𝐷(𝐺(𝑧))) (12)

The generator (G):

𝐿(𝐷(𝐺(𝑧)), 1) = log (1 − 𝐷(𝐺(𝑧))) (13)

where 𝑥, 𝑧, 𝐷, and 𝐺 are the real data, the fake data, the discriminator, and the
generator. As seen in equation (10) and (11), the objective functions of the
discriminator consist of 2 functions. The 2 functions come from the real data (label 1)
and the fake data (label 0). Here, the objective function of the discriminator should be
maximized while the objective function of the generator from equation (12) should be
minimized.

𝐿𝐷 (𝐷, 𝐺) = max{log(𝐷(𝑥)) + log (1 − 𝐷(𝐺(𝑧)))} (14)

𝐿𝐺 (𝐺) = min {log (1 − 𝐷(𝐺(𝑧)))} (15)

The combining of the equation (14) and (15) is shown in the equation (16). Here, we
get the minimax game that the generator (G) and the discriminator (D) compete each
other that are defined as.

𝐿(𝐷,𝐺) = min max(𝐸𝑥~𝑃𝑑𝑎𝑡𝑎(𝑥) [𝑙𝑜𝑔𝐷(𝑥)] + 𝐸𝑥~𝑃𝑧(𝑧) [log (1 − 𝐷(𝐺(𝑧)))] (16)


𝐺 𝐷

All mentioned above, it is a simple GANs concept. There are many more types
of GANs (Alqahtani, Kavakli-Thorne, & Kumar Ahuja, 2019), such as Conditional
GANs (CGANs), Wasserstein GANs (WGANs), Deep Convolution GANs
(DCGANs), Cycle GANs etc. However, this work focused on only CGANs and
WGANs model.

7.1 Conditional Generative Adversarial Networks (CGANs)

Conditional GANs or CGANs are used to solve a problem of simple GANs that
cannot control classes of generated output that we need. The generated output of the
simple GANs is examples from random classes, but the CGANs use example from the
19

classes we want to generate output. The CGANs architecture is demonstrated in Fig.


10, there is add label y both Generator model and Discriminator model. The label y is
specific class that you want the generator to generate the output.

Figure 10. Conditional Generative Adversarial Networks (CGANs) Architecture

The CGANs are very necessary for time series sequence data such as a stock
price prediction, video prediction. Because a future stock price should correlate with
the nearest past stock price. If we use the simple GANs model for stock price
prediction task, the model might generate random plausible output for a given dataset.
In other words, tomorrow’s stock price prediction from the model does not correlate
to stock price today.

To understand easily difference generated output between the simple GANs


model and the CGANs model. As demonstrated in Fig 11, in the left picture (Simple
GANs), we do not specific category or class that we want. Therefore, each category is
only randomly generated outputs. However, in the right picture (CGANs) is enforced
by specific category that we want, the model gives the plausible output match our
expected category. In this case, the expected category from left to right are t-shirt,
trouser, pullover, dress, coat, sandal, shirt, sneaker, bag, and ankle boot.
20

Figure 11. Example of comparison generated output between Simple GANs model
(left picture) and CGANs model (right picture)

7.2 Wasserstein Generative Adversarial Networks (WGANs)

Training GANs are quite difficult. Sometimes, the two models might never
converge or happen mode collapse. In the recent year, there was a novel framework of
GANs, namely Wasserstein Generative Adversarial Networks or WGANs, was
introduced in 2017 by Martin Arjovsky (Arjovsky, Chintala, & Bottou, 2017). Instead
of using a discriminator model to classify an input data that probability distribution of
the input data is closer to real or fake label. The WGANs proposes changing the
discriminator model from a classification to a critic. The critic performs to score
realness or fakeness of given data. This thing helps the training WGANs to increase
stable and decrease sensitivity. The WGANs are more stable than the basic GANs.
Also, the wight clipping of WGANs helps to avoid mode collapse. Therefore, this
experiment combined the WGANs and the CGANs.
21

METHODOLOGY

1. Workflow Diagram

Figure 12. Workflow diagram

Fig. 12 displays our workflow diagram. We started from data preparation


process. This process gets the price data from SIAMCHART website. Then, clean and
transform the data before feeding it to the model for training. After training and
selected the models, the models are tested through the back testing to find a suitable
trading strategy. Next, a suitable trading strategy that we choose will be tested in other
periods. This is called “Forward testing” process.

2. Financial Dataset

There are many platforms that provides free financial data. In this work, the
stock price data are downloaded from two platforms. Stock Exchange of Thailand
SET50 index are from Siamchart website. The SET50 index data is used from August
2011 to June 2021 or around ten years. There are a total of 2406 observations in the
SET50 index data. Another platform, Yahoo Finance provides individual historical
stock data. This platform also allows us to get a free API. We selected the individual
stock data from the stock prices of the top 100 listed companies based on SET market
22

by large ranked market capitalization. It can check the component stocks in SET50
index and SET100 index from www.set.or.th.

3. Data Preparation

3.1 Input Data Structures

All models were designed to work on a rolling window. First, the raw dataset
was downloaded from Yahoo Finance (or CSV file). It was reshaped dimension from
2 dimensions to 3 dimensions as input data to feed each model. The elements of 3
dimensions consist of the number of samples, the number of timesteps and the number
of features, respectively, as show in Fig. 11. There are 6 features, consist of Close
price, Open price, High price, Low price, and two technical indicators that are the 7-
day moving average (Mov7), the 8-day exponential moving average (EMA8). Here,
the 2406 samples, the 20 timestep (20 days historical stock price), and the 1 output
step (predict 1 day stock price) were applied in this work.

3.2 Data Normalization

Data Normalization is an important thing for machine learning model.


Especially, features of the input data have different ranges. We should normalize
input data before feeding the machine learning model. The normalization will change
the data values of each column to a same range. Typically, the normalization helps the
model to increase speeding learning and faster convergence.

There are many methods to normalize data. Here, Min-max scale method is
used to normalize the input data. The all data is normalized in range -1 to 1 to feed in
our models.
23

Figure 13. Input Dataset for each model

3.3 Split Data

In this experiment, we used the historical daily closing data from the SET50
index between 24 August 2011 and 30 June 2021. The total data was 2406 samples.
There are 1924 samples or 80% of the total samples as training data (24 August 2011
– 3 July 2017) and 482 data and others are testing samples (4 July 2017 – 30 June
2021) or 20% of the total samples. For the reason, why we used the SET50 index
data, because the SET50 index is proportional to the weighted gains of the top 50 Thai
stocks based on the capital markets. Moreover, the SET50 index contains many
stocks, and it should be able to capture the over market behaviors than an individual
24

stock. Since the network learns with the overall market data, it should be more
generalized and less prone to overfit with the training samples.

4. Stock Price Predictor and its parameters

We expected to build a deep learning model that is more robust and efficient than
the popular traditional technical indicators. In this work, we chose two deep learning
models to predict the future stock price. First model, Long-Short Term Memory, or
LSTM, that is one of the most widely deep learning models applied to time series
prediction. Another model, Generative Adversarial Networks, or GANs for short, are
very successful in generating realistic images. The output of the realistic images is so
fascinating and impressive. As a result, we applied the GANs in time series data to
generate a future stock price.

4.1 Long-Short Term Memory (LSTM) Stock Price Predictor

A network model (See Fig. 14) was designed based on the LSTM neural
network to work as a stock price predictor. The historical data from the Stock
Exchange of Thailand SET50 index were used. The historical data were normalized
and fed to the LSTM network as the training set of the network layers. Here, there are
4 layers of LSTM network. The dimension of the inner cells in the LSTM was 50
units each layer. The Dropout was set 0.2 and added each the LSTM layer in order to
prevent overfitting of the model. The optimizer for our LSTM model is Adam
algorithm with learning rate 0.01. The batch size was 128 and 500 epochs for training
the model. The linear activation function and the Tanh activation function were used
in the last Dense layer as the output.

We took 20 sequence length day of historical data and try to predict the future
price on the 21st day. The rolling window equals 1. Then, we move the 20 days
window and again predict on the 22nd day and so on. We iterate like this over the
whole samples. To evaluate the performance model by following statistical indicator,
namely Root mean square error (RMSE). We tried training the model several times
25

and selected the best results of the lowest RMSE value. Finally, we found that the
result used linear activation function as output layer that better than the Tanh
activation function.

Figure 14. LSTM Stock Price Predictor

4.2 Generative Adversarial Networks (GANs) Stock Price Predictor

The modification of GANs helps to improve the result of generated samples


better. Here, the conditional GANs (CGANs) model and the Wasserstein GANs
(WGANs) model were combined and used in this work. The 2 modifications of GANs
were called the conditional Wasserstein GANs or CWGANs for short.

The training process concept of the CWGANs model is shown in Fig. 15. Here,
latent space (Z) was sampled from normal distribution. The latent space was
concatenated with stock price as an input data to fed in the generator. This combined
step increased more accuracy for the generator to predict a future stock price than
feeding only the latent space. There were 20 timesteps and 6 input features of the
input data in the generator. The generator’s output timestep was 1. In other word, each
20 days historical stock price timestep (20 input timesteps) would predict 1 day as a
future stock price (1 output timestep). Next, the generator’s output was fed to the
critic (or the discriminator) as the input data of the critic. The critic performs to
predict scores the realness or fakeness of a given data.
26

Figure 15. CWGANs Stock Price Predictor

This experiments, we designed CWGANs architecture based on LSTM and


CNN model. Thus, there were 4 cases as the generator model – the critic model,
LSTM-LSTM, LSTM-CNN, CNN-LSTM and CNN-CNN. To find the best
architecture models based on our data, the RMSE value and the R-squared value were
applied to evaluate performance of each model. The lower RMSE value and the
higher R-squared value were the higher model performance. The highest performance
would be tested in back testing. We found that CNN-CNN architecture gave the least
RMSE value, and the highest R-squared value based on our experiments. However,
the LSTM-LSTM and the CNN-LSTM architecture, they fail to converge. In Fig. 16
showed the CNN-CNN architecture detail that use in this work. Here, the total
parameters of CNN-CNN architecture were 2,757,900 parameters. The batch size was
27

64 and the learning rate was 0.00001 for RMSProp optimizer. We also set Z
dimension as 50.

Figure 16. CWGANs Architecture

The CNN-CNN architecture detail in this work is following as:

Generator model: there were four 1D Convolution (CON1D) layers, 512,


256, 128, 64 filters, respectively. Each the CON1D layer used and zero padding
kernel size at 5. Leaky Rectified Linear Unit, or Leaky ReLu, was used in each the
CON1D layer. Then, the Flatten layer was used with 0.2 Dropout. After that, 2 Dense
layers were used. The first dense layer was employed 220 units with the Leaky ReLu.
The Leaky ReLu value was set 0.01. The second dense layer was used with linear
activation function. The parameters of the generator were 1,369,391.

Critic/Discriminator model: the generator’s output was fed to the critic


model, there were three CON1D layers, 64, 128, 256 filters, respectively. Each the
CON1D layer used and zero padding kernel size at 5. Then, the Flatten layer was used
with 0.2 Dropout. After that, 2 Dense layers were used. The first dense layer was
employed 220 units with the Leaky ReLu. The Leaky ReLu value was set 0.01. The
second dense layer was used with linear activation function. The parameters of the
critic were 1,388,509.
28

5. Trading Operations

The initial wealth of a trader is fixed at W. In this trading operations, we assume


that there are no commission fee and taxes. A trader can take both long and short
positions on any stocks without any charges. Our trading rules were each time the
stock must be bought out and sold out. Here, the long position is where a trader buys a
stock to sell in the future. If the stock price moves up, a trader will receive a profit.
However, if the stock price moves down, a trader will lose money. In the short
position, a trader sells a stock first by borrowing the stock and buys the same stock
back in the future. A trader will gain money if the stock price goes down in the short
position case. Next, when a trader takes a long position, he or she will use all the
available wealth to buy a stock. Similarly, when a trader takes a short position, he or
she will be able to borrow a stock as much as his or her total wealth.

Our trading operations for LSTM model, we took a long position when the
predicted model price was more than the previous N-day closing price. After that, we
would close the long position when the predicted model price was less than the
previous N-day closing price. A short position would occur when the predicted model
price was less than the previous N-day closing price. Then, to close the short position
when the predicted model price was more than the previous N-day closing price. The
equation of LSTM trading strategy was defined as

𝐵𝑢𝑦 𝑆𝑖𝑔𝑛𝑎𝑙 ∶ 𝑃𝑡+1 > 𝑃𝑡−𝑁 , 𝑁 = 1, 2, 3, … ,19


𝐿𝑜𝑛𝑔 𝑃𝑜𝑠𝑖𝑡𝑖𝑜𝑛 = { (17)
𝑆𝑒𝑙𝑙 𝑆𝑖𝑔𝑛𝑎𝑙 ∶ 𝑃𝑡+1 < 𝑃𝑡−𝑁 , 𝑁 = 1, 2, 3, … ,19

𝐵𝑢𝑦 𝑆𝑖𝑔𝑛𝑎𝑙 ∶ 𝑃𝑡+1 < 𝑃𝑡−𝑁 , 𝑁 = 1, 2, 3, … ,19


𝑆ℎ𝑜𝑟𝑡 𝑃𝑜𝑠𝑖𝑡𝑖𝑜𝑛 = { (18)
𝑆𝑒𝑙𝑙 𝑆𝑖𝑔𝑛𝑎𝑙 ∶ 𝑃𝑡+1 > 𝑃𝑡−𝑁 , 𝑁 = 1, 2, 3, … ,19

where 𝑃𝑡+1 was predicted price for next day by LSTM model, 𝑃𝑡−𝑁 was the past N-
day closing price that were set from past 1-day to past 19-day.
29

In the same way of CWGANs trading strategy, but the CWGANs model used
the average of the number of predicted price possibilities compared to the past N-day
closing price. The number of predicted price possibilities were the number of the
latent space dimension. For a long position, a buy signal occurred when the average of
the number of predicted price possibilities were more than the previous N-day closing
price. On the contrary, a sell signal occurred when the average of the number of
predicted price possibilities were less than the previous N-day closing price. For a
short position, the buy signal occurred when the average of the number of predicted
price possibilities were less than the previous N-day closing price. The sell signal
occurred when the average of the number of predicted price possibilities were more
than the previous N-day closing price. Here, the equation of the CWGANs trading
strategy were defined as

∑𝑍
𝑖=1(𝑃𝑡+1 )𝑖
𝐵𝑢𝑦 𝑆𝑖𝑔𝑛𝑎𝑙 ∶ > 𝑃𝑡−𝑁 , 𝑁 = 1, 2, 3, … ,19
𝑍
𝐿𝑜𝑛𝑔 𝑃𝑜𝑠𝑖𝑡𝑖𝑜𝑛 = { (19)
∑𝑍
𝑖=1(𝑃𝑡+1 )𝑖
𝑆𝑒𝑙𝑙 𝑆𝑖𝑔𝑛𝑎𝑙 ∶ < 𝑃𝑡−𝑁 , 𝑁 = 1, 2, 3, … ,19
𝑍

∑𝑍
𝑖=1(𝑃𝑡+1 )𝑖
𝐵𝑢𝑦 𝑆𝑖𝑔𝑛𝑎𝑙 ∶ < 𝑃𝑡−𝑁 , 𝑁 = 1, 2, 3, … ,19
𝑍
𝑆𝑒𝑙𝑙 𝑃𝑜𝑠𝑖𝑡𝑖𝑜𝑛 = { (20)
∑𝑍
𝑖=1(𝑃𝑡+1 )𝑖
𝑆𝑒𝑙𝑙 𝑆𝑖𝑔𝑛𝑎𝑙 ∶ > 𝑃𝑡−𝑁 , 𝑁 = 1, 2, 3, … ,19
𝑍

where 𝑍 was the number of the latent space dimension, 𝑃𝑡−𝑁 was the past N-day
closing price that were set from past 1-day to past 19-day.
30

EXPERIMENTAL RESULTS AND DISCUSSION

1. Selecting Model

To select the model to use for testing profitability, we selected the model by
evaluation RMSE value and R-squared value. We repeated training the models several

times to ensure that the models were able to have reproducibility results. Table 1
showed the RMSE value and the R-squared value (training and testing samples)
results of model. Based on our models, the LSTM had least RMSE value in both
training and testing samples. Here, the CNN and the LSTM were applied as a base
structure of CWGANs. We found the RMSE value of CNN - CNN architecture as a
base structure of the CWGANs had the least RMSE values in the training samples and
testing samples based on our different architectural designs. The LSTM-LSTM and
CNN-LSTM architecture failed to converge. Therefore, this experiment selected the
CNN - CNN architecture based on the CWGANs as a stock price predictor.

Table 1 RMSE value summary of each model

CWGANs CWGANs
Root Mean Square
LSTM (LSTM- CNN) (CNN - CNN)
Error
(RMSE)
Training samples 7.48 22.57 12.35
Testing samples 13.5 28.8 19.13

Also, we computed coefficient of determination, or R-squared value to see


how much variance of the predicted data comparing with the real data. The computed
coefficient of determination performance is showed in the Table 2. The results
showed that the LSTM model had the highest R-squared value in both training and
testing samples. For the CWGANs, the CNN-CNN architecture had higher the R-
squared value than LSTM-CNN architecture in both training and testing samples.
31

Table 2 Coefficient of Determination Performance

Coefficient of CWGANs CWGANs


Determination LSTM (LSTM- CNN) (CNN - CNN)
(R-squared)
Training samples 0.9890 0.9726 0.9813

Testing samples 0.9686 0.9226 0.9670

2. Technical Indicators Setup

There are different default values in each technical indicator tool. We assume
that most traders or investors rarely change the default values of the technical
indicator tools provided by brokers, so the default values of the technical indictor
tools would be set following the standard values that brokers set.

Typically, the MACD indicator default setting are 12-period EMA, 26-period
EMA and 9-period EMA as a shorter-period, a longer-period, and a signal line,
respectively. There are a lot of time periods used in moving average. However, the
most common time periods that traders like to use in the MOV are 5, 10, 15, 20, 50,
100, and 200, our MOV setup selected 5-period as a short-period and 12-period as a
longer period for the default values. The Bollinger Bands value default, the number of
days in time-period and the standard deviation are 20 days and 2. The RSI indicator
uses the past 14 periods which is the default values. Also, the strategy of the MACD,
BB and RSI were applied to compare profitability with the stock price predictor
model. The trading strategies of each indicator were mentioned in Background
Section.

3. Stock Price Prediction Setup

After we selected the best model of the LSTM and the CWGANs model based
on the RMSE and the R-squared value. To evaluate the profitable model, both
selected models were separated into 2 cases, long-only case, and long-short case.
Then, the selected models would be tested for long-only and long-short execution
32

through back testing and forward testing by following condition in trading operation.
The back testing was applied to compare the viability between our models and the
technical indicators. To find an effective way to analyze the robustness of trading
strategy, we compared the predicted price with the past N days price. The past N days
were set from past 1-day to past 19-day. Therefore, there are the total 38 cases of each
selected model, 19 long-only cases, 19 long-short cases. After that, we chose the best
performance in long-only case and long-short case of the back testing to trade in the
forward testing.

4. Result Comparisons

We evaluated the profitability of the technical indicators and our approaches


by using back testing. The back testing tells us the effective trading strategy by
running historical data to see how much return on back testing can gain. Here, the
SET50 index data was classified into 3 periods. First period, we invested in the
SET50 index data between 3 July 2015 to 3 July 2019 in training dataset. Then, the
SET50 index data of the year 2020, between 1 January 2021 to 15 October 2021 was
tested as the second period and the third period, respectively. Each period was tested
by using the back testing to see return. The return is a profit on the investment. The
rate of return is defined as

𝑉𝐿 −𝑉𝑆
𝑅𝑂𝑅 =  100 (21)
𝑉𝑆

where 𝑅𝑂𝑅, 𝑉𝐿 and 𝑉𝑆 are the rate of return, last value of investment, and start value
of investment.

In facts, we should not consider only the return, but we should consider risk-
adjusted return as well. To do that, the four financial metrics, namely Sharpe ratio,
Sortino ratio, Maximum drawdown, and Calmar ratio were computed in the
experiments. The Sharpe ratio indicates how much wealth our approaches can earn
comparing to the volatility or the risk. However, the Sharpe ratio punishes the good
risk of the investment, the Sortino ratio punish only the bad risk of the investment.
The Sortino ratio is a type of the Sharpe ratio that focus on downside risk or negative
volatility. The Sharpe ratio and the Sortino ratio of portfolio were defined as
33

𝐸[𝑅𝑎 ]
𝑆ℎ𝑎𝑟𝑝𝑒𝑎 = (22)
𝜎𝑎

𝐸[𝑅𝑎 ]
𝑆𝑜𝑟𝑡𝑖𝑛𝑜𝑎 = (23)
𝜎𝑎(−)

where 𝐸[𝑅𝑎 ], 𝜎𝑎 ,and 𝜎𝑎(−) are expected return, standard deviation and standard
deviation of negative return. Here, the higher the Sharpe ratio or the Sortino ratio, the
better the portfolio is.

Another financial metric, Maximum drawdown (MDD), tells us how much the
maximum loss we might get. The MDD measures maximum loss in the past from a
peak to a trough of a portfolio as shown in Fig. 17.

Figure 17. Maximum drawdown

The MDD is defined as

𝑃−𝐿
𝑀𝐷𝐷 = (24)
𝑃

where 𝑃,and 𝐿 are peak value in interesting period, and trough value in interesting
period. Here, the lower the maximum drawdown, the lower the risk of portfolio is. To
clarify, we showed the Calmar ratio as well. The Calmar ratio indicates the
relationship between risk and return. It is useful for analyzing trading strategies or
models based on historical data. The Calmar ratio is defined as

𝐸[𝑅𝑎 ]
𝐶𝑎 = (25)
𝑀𝐷𝐷
34

where 𝐶𝑎 , 𝐸[𝑅𝑎 ], and 𝑀𝐷𝐷 are the Calmar ratio, expected return and maximum
drawdown.

The back testing results on the first period of each strategy, traditional
technical indicators, LSTM trading in long-only case, LSTM trading in long-short
case, CWGANs trading in long-only case and CWGANs trading in long-short case
were displayed in Table. 3, Table. 4, Table. 5, Table. 6, and Table. 7, respectively. As
demonstrated, the technical indicator, namely MOV, MACD, BB and RSI made not
satisfy return a profit. The MOV, the MACD and the RSI made a profit of 2%
approximately, but the BB made a loss of -2%. Also, they could not overcome the buy
& hold strategy. If investors or traders buy the SET50 index and hold it for 4 years,
they can get a profit of 17.94% in buy & hold strategy. However, our both models can
make the higher return than the buy & hold strategy and the technical indicators. The
LSTM trading strategy with past 8-day case, or LSTM-P8 for short, gain the profit of
21.26%, 15.32% in long-only and long-short case, respectively. For our CWGANs
trading strategy, most of the P cases were able to overcome the technical indicators in
long-only case. There are only P6, P8 case can overcome buy & hold strategy. The
trade execution processing the SET50 index data between 3 July 2015 to 3 July 2019
based on MOV, MACD, BB and RSI are shown in Fig. 18 respectively.

To find proper trading strategy way of our models with individual stocks or
testing samples, we picked the best ranking performance cases based on the
performance of investment. In Table 2, the P8 was chosen because they made the best
performance in long-only and long-short case. The P6, P8, P16 were picked in the
CWGANs trading strategy. Although, the P10 was the third ranking in the long-only
case, the P10 could not made the profitability in the long-short case. Thus, the P16
was picked instead of the P10. The examples of the trade execution processing of the
LSTM and CWGANs trading strategy cases were shown in Fig. 19- Fig.21,
respectively.
35

Table 3 Performance of Investment (1 July 2015 – 3 July 2019) based on traditional


technical indicators

3 July 2015 – 3 July 2019


Strategy\Period
Return ratio Sharpe Sortino Calmar
(%) ratio ratio MDD (%) ratio

Buy & Hold 17.94% 0.38 0.54 -22% 0.80

MOV 2.35% 0.11 0.16 -16% 0.15

MACD 2.13% 0.10 0.15 -10% 0.2

BB
-2.12% 0.004 0.006 -17.52% -0.12

RSI
2.72% 0.12 0.16 -20% 0.13
36

Table 4 Performance of Investment (1 July 2015 – 3 July 2019) based on LSTM


trading in long-only case

Period/ 3 July 2015 – 3 July 2019

Strategy
Past day Return ratio Sharpe Sortino MDD Calmar
(P) (%) ratio ratio (%) ratio
1 0.27% 0.05 0.06 -9.7 0.03

2 3.90% 0.20 0.30 -6.5 0.60

3 10.71% 0.46 0.68 -7 1.52


4 4.75% 0.21 0.30 -7 0.61

5 -3.03% -0.08 -0.11 -13 -0.23


6 7.60% 0.31 0.44 -10 0.80

7 2.94% 0.14 0.20 -9.1 0.32

8 21.26% 0.74 1.13 -6.8 3.12


LSTM long-only case

9 5.17% 0.22 0.32 -12.2 0.42

10 10.05% 0.40 0.57 -12.6 0.80

11 2.08% 0.11 0.16 -13.2 0.16

12 7.66% 0.31 0.44 -12.7 0.6


13 3.86% 0.17 0.24 -12.6 0.30

14 -3.85% -0.10 -0.14 -14.7 -0.26

15 6.30% 0.25 0.36 -16.3 0.38


16 8.93% 0.34 0.48 -14.3 0.62

17 13.50% 0.47 0.66 -13 1.04


18 15.33% 0.53 0.78 -11.6 1.32

19 17.35% 0.58 0.85 -12 1.44


37

Table 5 Performance of Investment (1 July 2015 – 3 July 2019) based on LSTM


trading in long-short case

Period/ 3 July 2015 – 3 July 2019

Strategy
Past day Return ratio Sharpe Sortino MDD Calmar
(P) (%) ratio ratio (%) ratio
1 -12.85% 0 0 -37.6 -0.34

2 -15.16% -0.08 -0.12 -36.4 -0.42

3 -10.30% -0.02 -0.03 -29.3 -0.35


4 -14.88% -0.08 -0.13 -40.8 -0.36

5 -12.40% -0.07 -0.11 -13 -0.32


6 0.13% 0.10 0.13 -29 0

7 0.18% 0.10 0.16 -24.5 0


LSTM long-short case

8 15.32% 0.27 0.45 -20.5 0.74

9 -1.75% 0.10 0.16 -29.2 -0.06

10 -3.60% 0.08 0.16 -29 -0.12

11 -13.65% -0.02 -0.05 -27.7 -0.50

12 -4.22% 0.06 0.11 -21.5 -0.20


13 3.86% -0.10 -0.15 -30 -0.56

14 -14.20% -0.04 -0.07 -32.7 -0.43

15 2.77% 0.15 0.26 -24.6 0.11


16 3.37% 0.15 0.25 -23.3 0.62

17 4.97% 0.17 0.27 -21.2 0.23


18 4.27% 0.15 0.28 -25 0.17

19 -3.22% 0.07 0.11 -31.6 -0.10


38

Table 6 Performance of Investment (1 July 2015 – 3 July 2019) based on CWGANs


trading in long-only case

1 July 2015 – 3 July 2019


Period/
Strategy Return ratio Sharpe Sortino MDD Calmar
Past day
(%) Ratio ratio (%) ratio
1 9.38% 0.41 0.62 -11 0.89
2 4.60% 0.35 0.57 -5 0.89
3 4.52% 0.33 0.53 -5 0.87
4 1.95% 0.12 0.17 -9 0.21
5 -10.91% -0.45 -0.59 -17 -0.66
6 19.68% 0.74 1.15 -7 2.91
7 16.50% 0.61 0.92 -9 1.85
CWGANs long-only case

8 19.36% 0.72 1.10 -7 2.62


9 15.07% 0.57 0.84 -13 1.20
10 16.56% 0.62 0.93 -10 1.58
11 5.96% 0.26 0.38 -13 0.47
12 1.30% 0.08 0.12 -14 0.09
13 9.81% 0.39 0.55 -11 0.87
14 -2.22% -0.05 -0.07 -14 -0.16
15 8.43% 0.33 0.47 -13 0.66
16 12.61% 0.47 0.68 -14 0.89
17 13.00% 0.47 0.67 -12 1.11
18 8.24% 0.32 0.46 -14 0.58
19 9.13% 0.35 0.49 -14 0.65
39

Table 7 Performance of Investment (1 July 2015 – 3 July 2019) based on CWGANs


trading in long-short case

1 July 2015 – 3 July 2019


Period/
Strategy Return ratio Sharpe Sortino MDD Calmar
Past day
(%) Ratio ratio (%) ratio
1 -3.09% 0.05 0.06 -33 -0.25
2 -14.51% 0.02 0.03 -52 -0.35
3 -17.11% -0.03 -0.04 -46 -0.39
4 -21.85% -0.12 -0.19 -48 -0.46
5 -21.69% -0.14 -0.23 -45 -0.48
6 11.72% 0.23 0.43 -24 0.49
7 7.73% 0.19 0.36 -21 0.36
CWGANs long-short case

8 16.69% 0.28 1.10 -20 0.74


9 -0.30% 0.06 0.84 -30 -0.18
10 -0.98% 0.07 0.93 -32 -0.17
11 -10.81% -0.04 0.38 -30 -0.48
12 -12.23% -0.03 -0.04 -30 -0.41
13 -2.46% 0.06 0.05 -30 -0.25
14 -2.22% -0.06 -0.10 -34 -0.47
15 9.17% 0.21 0.3 -28 0.15
16 10.98% 0.23 0.32 -23 0.29
17 -0.59% 0.47 0.10 -31 -0.15
18 -5.14% 0.06 0.02 -34 -0.26
19 -5.19% 0.04 -0.02 -33 -0.27
40

(a) MOV trading strategy

(b) MACD trading strategy

(c) BB trading strategy


41

(d) RSI trading strategy


Figure 18. The trading strategy of traditional technical indicators, (a) MOV strategy,
(b) MACD strategy, (c) BB strategy and (d) RSI strategy on SET50 index data
between 3 July 2015 to 3 July 2019.

(a) LSTM long-only P8 case


42

(b) LSTM long-short P8 case


Figure 19. Examples of LSTM trading strategy (a) Long-only case (past 8-day), (b)
Long-short case (past 8-day) on SET50 index data between 3 July 2015 to 3 July
2019.

(a) CWGANs long-only P6 case

(b) CWGANs long-only P8 case


43

(c) CWGANs long-only P16 case


Figure 20. Examples of the long-only trading strategy of CWGANs stock price
predictor (a) past 6-day (P6), (b) past 8-day (P8), (c) past 16-day (P16) on SET50
index data between 3 July 2015 to 3 July 2019.

(a) CWGANs long-short P6 case

(b) CWGANs long-short P8 case


44

(c) CWGANs long-short P16 case


Figure 21. Examples of the long-short trading strategy of CWGANs stock price
predictor (a) past 6-day (P6), (b) past 8-day (P8), (c) past 16-day (P16) on SET50
index data between 3 July 2015 to 3 July 2019.

After both models were back tested in the training dataset, we kept the past 8-
day case of both models to test in forward testing. Here, the forward testing was
separated into 4 parts as follow:

1. The first part period: the SET50 index data in year 2020
2. The second part period: the Top 100 stocks data in year 2020
3. The third part period: the top 100 stocks data between 1/1/2021 and
15/10/2021
4. The last part period: Picked Stock based on market capitalization between
1/1/2021 and 15/10/2021

4.1 First Part Period (Forward Testing)

The SET50 index data in year 2020


The annual total average return of each strategy was traded with the SET50
index data in 2020 demonstrated in Fig. 22. The MOV and the MACD strategy can
achieve the return of 12.25%, 18.57%, respectively. For the BB and RSI and buy &
hold trading strategy was not able to make the profit based on the SET50 index data in
2020. They yielded a loss profit. However, our methods can overcome and make
higher profit than the buy & hold, the MOV, the MACD, the BB, the RSI strategy in
both cases. For the long-only case, the LSTM and the CWGANs trading strategy can
45

take the return of 37.29%, 23.96%, respectively. Moreover, the long-short case of
both models can yield the most profitable case of 76.13% in the LSTM model, and
66.60% in the CWGANs model.

In this LSTM trading strategy case that we tried trading other past day case,
we found that the past 5-day (P5) case and the past 12-day (P12) case take a higher
profit than the P8 case. Thus, we kept the P5 and P12 for testing with the top 100 Thai
stocks experiment.

%Average return of each trading strategy in


SET50 index data in 2020
Buy & Hold MOV MACD
BB RSI LSTM long-only case
LSTM long-short case GAN long-only case GAN long-short case

100.00%
76.13%
80.00% 66.60%
60.00%
37.29%
40.00%
23.96%
18.57%
20.00% 12.25%

0.00%
.
-20.00% -12%
-15.77%
-40.00% -26.70%

Figure 22. Average return comparison between our models and traditional indicators
(SET50 index data in 2020)

Table 8 and Table 9 demonstrated the details of the investment performance of


four technical indicators and buy & hold strategy and our models based on the SET50
index data in 2020, respectively.
46

Table 8 Performance of investment in SET50 index data (1 January 2020 – 31


December 2020) based on four technical indicators and buy & hold strategy
1 January 2020 – 31 December 2020
Strategy\Period Return ratio Sharpe Sortino Calmar
(%) ratio ratio MDD (%) ratio
Buy & Hold -15.77 % -0.35 0.46 -0.37 -0.42
MOV 12.25 % 0.7 1.16 -0.21 0.59
MACD 18.57 % 1.03 1.88 -0.21 0.86
BB -12% -0.29 -0.38 -0.29 -0.4
RSI -26.70 % -0.95 -1.18 -0.37 -0.72

Table 9 Performance of investment SET50 index data (1 January 2020 – 31


December 2020) based on LSTM and CWGANs stock price predictor.

1 January 2020 – 31 December 2020


Period/
Strategy Return Sharpe Sortino MDD Calmar
Past day
ratio (%) Ratio ratio (%) ratio

5 37.29% 1.65 2.70 -19 1.95

LSTM long-only case 8 17.72% 0.98 1.63 -19 0.93

12 29.86% 1.55 2.66 -15 1.99

5 76.13% 1.73 3.82 -24 3.17

LSTM long-short case 8 53.36% 1.10 2.50 -22 2.42

12 53.71% 0.95 2.62 -31 1.73

6 9.18% 0.54 0.82 -24 0.38

CWGANs long-only case 8 23.96% 1.22 2.08 -20 1.20

16 14.23% 0.88 1.42 -18 0.81

6 19.42% 0.69 1.43 -26 0.74


CWGANs long-short
8 66.60% 1.14 2.99 -21 3.17
case
16 18.05% 0.55 1.42 -32 0.56
47

4.2 Second Part Period

The Top 100 stocks data in SET100 in 2020 (Forward Testing)

To employ the chosen past day of the LSTM and the CWGANs trading strategy
with the top 100 Thai stocks, the trading execution would be separated into 2 periods,
the year 2020 and 1/1/21-15/10/21. Since there is a covid-19 situation in 2020, a lot of
stocks price has fallen rapidly. Thus, we wanted to see how much wealth our
strategies could generate only in 2020.

Fig 23 shows the annual average return of each trading strategy in 2020. In
this experiment, the LSTM trading strategy in both cases outperforms all strategies
based on the experiments. The LSTM take the return of 26.55%, 49.28% from
investment in the Top 100 Thai stocks in long-short case and long-short case,
respectively. The GANs trading strategy cannot overcome some the traditional
technical indicators, it made the return less than the MACD in long-only case and
long-short case. However, the CWGANs trading strategy can overcome the buy &
hold, the BB, and the RSI strategy in both cases.

%Average return of each trading strategy in


Top 100 Thai stocks in 2020

Buy & Hold MOV MACD


BB RSI LSTM long-only case
LSTM long-short case GAN long-only case GAN long-short case

60.00%
49.28%
50.00%
40.00%
30.00% 25% 26.55%
20.75% 22.87%
17.17%
20.00%
10.00% 6.55%
0.83%
0.00%
-10.00% .
-20.00% -12.64%

Figure 23. Average return comparison between our models and traditional
indicators (Top 100 Thai stocks in 2020)
48

Table 10 Performance of investment in top 100 Thai stocks (1 January 2020 – 31


December 2020)

1 January 2020 – 31 December 2020 (Top 100 Thai stocks)


Strategy\Period Return Sharpe Sortino Calmar
ratio (%) ratio ratio MDD (%) ratio
Buy & Hold 0.83% 0.12 0.20 -45% 0.01
MOV 20.75% 0.65 1.14 -23% 0.90
MACD 25.00% 0.75 1.33 -35% 0.71
BB -2.50% 0.08 0.16 -35% -0.07
RSI -12.64% -0.21 -0.25 -40% -0.32

LSTM long-only (P5 case) 19.58% 0.70 1.26 -24% 0.81


LSTM long-short (P5 case) 27.56% 0.57 1.33 -39% 0.70
LSTM long-only (P8 case) 19.97% 0.66 1.15 -24% 0.83
LSTM long-short (P8 case)
31.73% 0.62 1.45 -38% 0.83
LSTM long-only (P12 case) 26.55% 0.77 1.33 -20% 1.32
LSTM long-short (P12 case) 49.28% 0.75 1.89 -39% 1.26

CWGANs long-only (P6 case) 12.34% 0.47 0.85 -24% 0.51


CWGANs long-short (P6
case) 10.86% 0.41 1.00 -41% 0.26
CWGANs long-only (P8 case) 17.17% 0.61 1.06 -24% 0.71
CWGANs long-short (P8
case) 22.87% 0.52 1.32 -40% 0.57
CWGANs long-only (P16
case) 18.63% 0.62 1.08 -21% 0.88
CWGANs long-short (P16
case) 25.91% 0.59 1.30 -43% 0.6
49

4.2 Third Part Period

The Top 100 stocks data in SET100 between 1/1/2021 and 15/10/2021

Fig 24. shows the average return of the top 100 Thai stocks in each strategy in
2021 (1/1/21-15/10/21). As shown, the technical indicators and our models cannot
defeat the buy & hold strategy. The investment performance details of each strategy in
2020 and 2021 are shown in Table 8- Table 9, respectively.

Table 11 Performance of investment in top 100 Thai stocks (1 January 2021 – 15


October 2021)

1 January 2021 – 15 October 2021 (Top 100 Thai stocks)


Strategy\Period Return Sharpe Sortino Calmar
ratio (%) ratio ratio MDD (%) ratio
Buy & Hold 11.17% 0.42 0.79 -23.50 0.62
MOV 2.53% 0.07 0.29 -14 0.40
MACD 4% 0.13 0.46 -14 0.70
BB 6.55 0.73 1.7 -10 1.34
RSI 6.18% 0.60 1.44 -12 1.14
LSTM long-only
(P8 case) 4.64% 0.23 0.63 -24 0.64
LSTM long-short
(P8 case) -0.56% 0.07 0.31 -12.7 0.17

CWGANs long-only
(P8 case) 5.62% 0.34 0.77 -12 0.69
CWGANs long-short
(P8 case) -0.44% 0.09 0.34 -25 0.18
50

%Average return of each trading strategy in Top 100 Thai


stocks between 1 January 2021 to 15 October 2021

Buy & Hold MOV MACD


BB RSI LSTM long-only case
LSTM long-short case GAN long-only case GAN long-short case

12.00% 11.17%

10.00%

8.00%
6.55%
6.18%
5.62%
6.00%
4.64%
4%
4.00%
2.53%
2.00%

0.00%
. -0.56% -0.44%
-2.00%

Figure 24 Average return comparison between our models and traditional


indicators (Top 100 Thai stocks between 1 Jan 2021 to 15 Oct 2021

4.4 Fourth Part Period

The picked stocks based on market capitalization between 1/1/2021 and


15/10/2021

In fact, no one invest 100 stocks at the same time. Also, there are a lot of
stocks gain benefit and loss benefit at the same time from the covid-19 situation.
Thus, the practical deployment would select 9 Thai stocks to do the profitability in
each strategy. To consider picking 9 Thai stocks, we selected by following market
capitalization. The market capitalization separates into three parts, Large-cap, Mid-
cap, and Small-cap. Additionally, the picked 9 stocks should build from different
sectors in a portfolio. The 9 picked stocks were evaluated the investment
performance. The picked stocks were followed as:

1. Stock in the SET50 as large cap: AOT (Transportation and logistics), BDMS
(Health care service), STGT (Personal product and pharmaceuticals), HMPRO
(Commerce)
51

2. Stock in the SET100 as mid cap: JMART (Info. and communication


technology), MAJOR (Media and publishing), STEC (Construction services)
3. Stock outside the SET100 as small cap: BEAUTY (Commerce), SAK
(Finance and securities)

Here, we assume that there were no commission fee and taxes. The initial wealth
was set at 1,000,000 baht in a simulated trading account. We started to collect data
from 1 January 2021 to 15 October 2021. Also, the stocks would action buy or sell
signal by following the rules in the trading operation of each strategy. The average
return of our investment result in this experiment is displayed in Fig.25. The LSTM
trading and the CWGANs trading outperform the MOV, the MACD, the BB, the RSI,
and the buy & hold strategy in both cases. Especially, the LSTM trading strategy in
long-short case can gain a profit of 29%. Its average return has doubled size as
compared with the technical indicators and the buy & hold strategy 2 times.

Table 12 and Table 13 show the investment performance details of the 9 picked
stocks of the traditional technical indicators and our models, respectively.

Table 12 Investment performance of the 9 picked stocks based on the traditional


technical indicators (1 January 2021 – 15 October 2021)
Period 1 January 2021 – 15 October 2021 (Practical Deployment)

%Average Return

Stock name/ Strategy Buy &Hold MOV MACD BB RSI

AOT 9.31% 13.10% 17.60% 1.60% 2.42%

Large BDMS 7.10% 0.37% -8.04% 17.23% 12.10%


cap HMPRO 4.38% -6.0% 12.36% 4.28% 17.85%

STGT -17.76% -10.52% -19.42% 0.00% 4.05%

JMART 81% 73.40% 71.35% -7% 17.52%


Mid
MAJOR 28.5% 21.5% 39.54% -5.90% -5.12%
cap
STEC 13.10% 13.30% 21.15% 34.05% 6.25%

BEAUTY 0.00% -4.95% -20.53% 29.60% 40.90%


Small
cap
SAK -10.62% 2.80% 3.95% -15.04% -5.38%
52

Table 13 Investment performance of the picked 9 stocks based on our stock price
predictors (1 January 2021 – 15 October 2021)

Period 1 January 2021 – 15 October 2021 (Practical Deployment)

%Average Return

Stock name/ Strategy LSTM-P8 LSTM-P8 CWGANs-P8 CWGANs-P8

(Long-only case) (Long-short case) (Long-only case) (Long-short case)

AOT 11.85% 30.65% 13.20% 25.10%

Large BDMS -4.90% 1.14% 0.40% 2.10%


cap HMPRO 3.80% 10.04% -1.0% 4.22%

STGT 4.70% 34.73% -3.56% 9.50%

JMART 62.83% 78.38% 65.93% 54.65%


Mid
MAJOR 28.34% 32.20% 37.22% 33.40%
cap
STEC 34.44% 54.90% 22.62% 30.15%

Small BEAUTY -9.98% -2.04% -9.95% -8.0%


cap SAK 7.52% 27.10% 7.60% 24.0%

%Average return of each trading strategy in


9 picked stocks (1/1/21-15/10/21)
Buy & Hold MOV MACD
BB RSI LSTM long-only case
LSTM long-short case GAN long-only case GAN long-short case

35.00%
29.19%
30.00%
25.00%
19.46%
20.00%
15.40% 14.72%
15.00% 12.78% 13.11%
11.44%
10.07%
10.00% 6.54%
5.00%
0.00%
.

Figure 25 Average return comparison between our models and traditional


indicators (9 picked stocks between 1 Jan 2021 to 15 Oct 2021)
53

We summarized the average return of all trading strategies for the four
forward testing cases in the Table 12. In the first part, the BB, RSI and Buy&Hold
could not make a profit. But the MACD, MOV can make a profit. Both models could
make a higher return than the four technical indicators, but there was the LSTM past
8day case earned only 1% less profit than the MACD. In the second part, the BB and
RSI could not make a profit as same the first part. The buy&hold hardly got a profit,
just 0.83%. But the MOV and the MACD could make a profit and overcome the
CWGANs model and the LSTM past8day a little bit in long-only case. However, the
LSTM Past 12 day in long-only and long-short case could make a higher the average
return than the MACD and MOV indicator.

Table 14 %Average return of each trading strategy in each forward testing

Top 100 stocks Picked 9 stocks


data data
SET50 index data Top 100 stocks data
Strategy/Part between 1/1/21- between 1/1/21-
in 2020 (First Part) in 2020 (Second Part)
15/10/21 15/10/21
(Third Part) (Fourth Part)

Buy&Hold -15.77% 0.83% 11.17% 12.78%

MOV 12.25% 20.75% 2.53% 11.44%

MACD 18.57% 25.00% 4% 13.11%

BB -12% -2.50% 6.55% 6.54%

RSI -26.70% -12.64% 6.18% 10.07%

37.20% (P5 case)


19.97% (P8 case)
LSTM long-only case 17.72% (P8 case) 4.64% (P8 case) 15.40% (P8 case)
26.55% (P12 case)
29.86% (P12 case)

76.13% (P5 case)


31.73% (P8 case)
LSTM long-short case 53.36% (P8 case) -0.56% (P8 case) 25.78% (P8 case)
49.28% (P12 case)
53.71% (P12 case)

CWGANs long-only
23.96% (P8 case) 17.17% (P8 case) 5.62% (P8 case) 14.72% (P8 case)
case
CWGANs long-short
69.68% (P8 case) 22.87% (P8 case) -0.44% (P8 case) 19.46% (P8 case)
case

In this third part period, the LSTM and GANs trading could not overcome
buy&hold, RSI and BB strategy. Both models could make a higher return than the
54

MACD and MOV in long-only case, but both models incurred a loss in the long-short
case. There was only the experiment in the third part where the LSTM trading earned
less profit than the GANs trading in the long-only case.

In the last period, both models can make higher the average return than four
technical indicators and buy&hold strategy. The LSTM past 8day case can make
higher return than the GANs past 8day case in long-only case and long-short case
Overall, the LSTM and GANs outperform four technical indicators. Mostly The
LSTM trading also can make a higher the average return than the GANs trading.
55

DISCUSSION

This thesis proposes stock trading by using deep learning algorithms, namely
LSTM, and GANs based on the Stock Exchange of Thailand market (SET). Both
models use the SET50 index data for training dataset. In the selecting GANs model,
we design the four patterns of the CWGANs architecture, CNN- CNN, CNN -LSTM,
LSTM-LSTM, and LSTM- CNN. The CNN - CNN architecture is better result than
the LSTM- CNN architecture based on our dataset. However, the CNN -LSTM and
the LSTM-LSTM architecture fails to converge. Thus, the CNN - CNN architecture of
the CWGANs is used for back testing. We also consider repeatability of LSTM and
CWGANs model to ensure that both models have ability to predict the future stock
prices. Then, the back testing performs to give scenarios of trading strategy. There are
38 scenarios, 19 long-only scenarios and 19 long-short scenarios. We choose the best
scenario in long-only and long-short cases of each model. After that, the best scenario
of each model is evaluated the profitability of the trading strategy in the testing
dataset. In addition, the Top 100 stocks are applied to evaluate the profitability of the
trading strategy of the models as well. This process is called “forward testing”. The
forward testing consists of 4 parts, SET50 index data in 2020, Top 100 stocks in 2020,
Top 100 stocks between 1 January 2021 to 15 October 2021, and Practical
deployment section. After we selected trading strategy from scenarios of the LSTM
and the CWGANs model, we would compare the profitability with the four technical
indicators, namely, MOV, MACD, BB and RSI and Buy & Hold strategies.

The back testing in the SET50 index data, the suitable trading strategy of
CWGANs and the LSTM model is P8 case in long-only and long-short cases. The
LSTM and CWGANs can overcome the four technical indicators and Buy & Hold
strategies. The long-only P8 case of the LSTM and CWGANs can make the profit of
21.26% and 19.36%. While the MOV, MACD, BB and RSI got 2.35%, 2.13%, -
2.12% and 2.72%, respectively. For the first part of forward testing of SET50 index
data in 2020, we found that the LSTM and the CWGANs still outperform the four
technical indicators and Buy & Hold strategies. The Buy & Hold, BB and RSI
strategy got loss of -15.77%, -12% and -26.70%. The CWGANs P8 case got profit of
56

23.96%, 66.60% in long-only and long-short cases, respectively. The LSTM P8 case
got profit of 17.72%, 53.30% in long-only and long-short cases, respectively.
However, the LSTM P5 case can make higher return than the LSTM P8 case, the
LSTM P5 case get the profit of 37.29%, 76.13% in long-only and long-short cases.
Another the forward testing is used with the top 100 Thai stocks of the SET100
market in 2020. The CWGANs P8 and the LSTM P8 case can have profitability.
Although, the return of the CWGANs is less than the MACD of 8%, the MDD of the
MACD is more than the MDD of CWGANs of 10%. Both models and the four
technical indicators cannot overcome the Buy & Hold strategy between 1 January
2021 to 15 October 2021. In the practical deployment section, we selected 9 stocks
based on market capitalization, the result showed that the average return of LSTM P8
case and the CWGANs P8 case outperform the MOV, MACD, BB and RSI and Buy
& Hold strategies. If we consider the return details of each stock, BEAUTY stock
cannot get a profit in both models. However, the RSI can yield 40.90% for BEAUTY
stock, the total average return of both models still outperforms the RSI.

Based on the experiments, the LSTM trading strategy can make higher return
than the CWGANs trading strategy in some cases. The LSTM trading strategy and
CWGANs trading strategy may help traders to gain profit from the Stock Exchange of
Thailand (SET).
57

CONCLUSION

Based on our objectives, the different CWGANs architecture, the CNN-CNN


architecture got the best result based on our dataset. Our both models, the LSTM and
the CWGANs models could learn market behavior to predict the future stock price
movement and make the profit. Moreover, both models are mostly promising and
outperforming four technical indicators, MOV, MACD, BB and RSI strategies. The
suitable trading strategy of CWGANs model found that CWGANs P8 case was
significant in the back testing and the forward testing. Even though, the LSTM P8
case did not make the highest return in comparing to its past N-day case, but the
LSTM trading strategy can still perform. The LSTM P8 case may be suitable trading
strategy. The overall, the LSTM and CWGANs could outperform the Buy & Hold and
the four technical indicator strategies. Furthermore, our models may allow traders to
gain profits from the Stock Exchange of Thailand (SET) market.
58
LITERATURE CITED

LITERATURE CITED

Adebiyi, A., Adewumi, A., & Ayo, C. (2014). Stock price prediction using the ARIMA
model.
Alqahtani, H., Kavakli-Thorne, M., & Kumar Ahuja, D. G. (2019). Applications of
Generative Adversarial Networks (GANs): An Updated Review. Archives of
Computational Methods in Engineering, 28. doi:10.1007/s11831-019-
09388-y
Arjovsky, M., Chintala, S., & Bottou, L. (2017). Wasserstein GAN.
Chouhan, L., Agarwal, N., Parmar, I., Saxena, S., Arora, R., Gupta, S., & Dhiman, H.
(2018). Stock Market Prediction Using Machine Learning.
de Souza, M. J. S., Ramos, D. G. F., Pena, M. G., Sobreiro, V. A., & Kimura, H.
(2018). Examination of the profitability of technical analysis based on moving
average strategies in BRICS. Financial Innovation, 4(1), 3.
doi:10.1186/s40854-018-0087-z
Eckerli, F., & Osterrieder, J. (2021). Generative Adversarial Networks in finance: an
overview.
Gurrib, I. (2014). The Moving Average Crossover Strategy: Does it Work for the
S&P500 Market Index? SSRN Electronic Journal. doi:10.2139/ssrn.2578302
Leangarun, T., Tangamchit, P., & Thajchayapong, S. (2018). Stock Price Manipulation
Detection using Generative Adversarial Networks.
Liu, S., Liao, G., & Ding, Y. (2018, 31 May-2 June 2018). Stock transaction
prediction modeling and analysis based on LSTM. Paper presented at the 2018
13th IEEE Conference on Industrial Electronics and Applications (ICIEA).
Nelson, D. M. Q., Pereira, A. M., & Oliveira, R. A. d. (2017). Stock market's price
movement prediction with LSTM neural networks. 2017 International Joint
Conference on Neural Networks (IJCNN), 1419-1426.
O'Shea, K., & Nash, R. (2015). An Introduction to Convolutional Neural Networks.
ArXiv e-prints.
Sahin, U., & Ozbayoglu, A. M. (2014). TN-RSI: Trend-normalized RSI Indicator for
Stock Trading Systems with Evolutionary Computation. Procedia Computer
Science, 36, 240-245. doi:https://doi.org/10.1016/j.procs.2014.09.086
Shah, N., & Manubhai, P. (2015). A Comparative Study on Technical Analysis by
Bollinger Band and RSI.
Vaidya, R. (2020). Moving Average Convergence-Divergence (MACD) Trading Rule:
An Application in Nepalese Stock Market &quot;NEPSE&quot. Quantitative
Economics and Management Studies, 1(6), 366-374.
doi:10.35877/454RI.qems197
Wang, Z., She, Q., & Ward, T. E. (2019). Generative Adversarial Networks: A Survey
and Taxonomy. ArXiv, abs/1906.01529.
Wei, D. (2019). Prediction of Stock Price Based on LSTM Neural Network. 2019
International Conference on Artificial Intelligence and Advanced Manufacturing
60

(AIAM), 544-547.
Zhang, K., Zhong, G., Dong, J., Wang, S., & Wang, Y. (2019). Stock Market
Prediction Based on Generative Adversarial Network. Procedia Computer
Science, 147, 400-406. doi:https://doi.org/10.1016/j.procs.2019.01.256
CURRICULU M VITAE

CURRICULUM VITAE
NAME Pattareeya Piravechsakul

DATE OF BIRTH 16 March 1994

BIRTH PLACE Bangkok

ADDRESS 51 Rama2 Road, Jomthong, Bangmod, Bangkok, 10150

EDUCATION Master of Engineering in Information and Communication


Technology for Embedded System
PUBLICATION Combining Technical Indicators and Deep Learning by
using LSTM Stock Price Predictor, 2021 18th
International Conference on Electrical
Engineering/Electronics, Computer, Telecommunications
and Information Technology (ECTI-CON)
SCHOLARSHIP Thailand Advanced Institute of Science and Technology –
Tokyo Institute of Technology (TAIST-Tokyo Tech)

You might also like