An Artificial Neural Network-based Stock Trading System Using Technical Analysis and Big Data Framework

# An Artificial Neural Network-based Stock Trading System Using Technical Analysis and Big Data Framework

###### Abstract

In this paper, a neural network-based stock price prediction and trading system using technical analysis indicators is presented. The model developed first converts the financial time series data into a series of buy-sell-hold trigger signals using the most commonly preferred technical analysis indicators. Then, a Multilayer Perceptron (MLP) artificial neural network (ANN) model is trained in the learning stage on the daily stock prices between 1997 and 2007 for all of the Dow30 stocks. Apache Spark big data framework is used in the training stage. The trained model is then tested with data from 2007 to 2017. The results indicate that by choosing the most appropriate technical indicators, the neural network model can achieve comparable results against the Buy and Hold strategy in most of the cases. Furthermore, fine tuning the technical indicators and/or optimization strategy can enhance the overall trading performance.

An Artificial Neural Network-based Stock Trading System Using Technical Analysis and Big Data Framework

 Omer Berat Sezer TOBB University of Economics and Technology Ankara, Turkey oberatsezer@etu.edu.tr
 A. Murat Ozbayoglu TOBB University of Economics and Technology Ankara, Turkey mozbayoglu@etu.edu.tr
 Erdogan Dogdu Georgia State University (adj.) Cankaya University Ankara, Turkey edogdu@cankaya.edu.tr

\@float

\end@float
• Algorithmic trading, trading strategy, machine learning, neural networks, stock market technical analysis

• Stock market, Artificial neural network, multi layer perceptron, algorithmic trading, technical analysis

Stock market forecasting and developing profitable trading models have always attracted researchers and practitioners [?]. However, it is very challenging to come up with a model that works reliably under different market conditions. In the last few decades, thanks to the advancements in computer and communications technologies, computational intelligence models started emerging as viable alternatives to the traditional decision support systems. Previous models are mostly based on static rules and analyses, hence can easily be outdated. At the same time, due to the excessive manual interactions, these models are not immune from human emotions, resulting in inconsistent, poor returns. Computational intelligence models on the other hand, such as neural networks [?, ?, ?], neuro-fuzzy models [?], support vector machines (SVM) [?], and genetic algorithms-based systems [?], demonstrated good performance achievements. Creamer utilized machine-learning algorithms to develop effective trading strategies and accordingly built automated trading agents. The agent they developed generated higher profits using Logitboost method than the simple “buy and hold" strategy [?]. But their experiments are very limited. They tested on two European index futures (FESX and FDAX) for only 21 trading days for March of 2009.

As implementing machine learning models using big data is becoming mainstream, such models started to emerge as part of algorithmic trading systems that now originates the majority of all transactions executed in NYSE. In this paper we aim to create such a profitable model using technical analysis indicators as features for a neural network model. Section 2 explains the model and the features we use, section 3 presents the method and the results are evaluated in section 4.

Technical analysis indicators have been used for identifying appropriate entry-exit points for trading models. Even though there are over 100 different indicators, some of them are more frequently used then the others, mostly because of easiness and/or effectiveness. Three of the most commonly used technical indicators are RSI, MACD and Williams %R. These are the features that we selected to use in our neuro-trading model due to their wide acceptance. Below we briefly explain these indicators.

Relative Strength Index (RSI) is a technical momentum indicator that shows historical strength or weakness of stock prices. It also compares losses and gains in a specified time period as follows.

 RSI=100–100(1+RS) (1)
 RS=AverageGainAverageLoss (2)

MACD is a technical indicator that illustrates the trend of the stock prices. It is equal to the difference of the 12-day Exponential Moving Average (EMA) and 26-day EMA.

 MACD=(12DaysEMA−26DaysEMA) (3)

Williams %R is momentum based technical indicator that shows the overbought and oversold conditions for stock prices.

 %R=(HighestHigh−CurrentClose)(HighestHigh−LowestLow)x−100 (4)

For big data analytics, commonly used open source tools are Apache Hadoop and Apache Spark. In our work, we used Spark. Apache Spark has a built-in machine learning library called MLlib implementing many algorithms. In general, for analyzing time series data and forecasting problems, Recurrent Neural Networks (RNN) are used in the literature. Meanwhile, Multilayer Perceptron (MLP) is also used for time series forecasting when appropriate feature processing is implemented. For this purpose, in our study, we used the aforementioned technical indicator outcomes as model features. Furthermore, we used Spark MLlib library’s MLP classifier to analyse time-series data.

In our approach, we aim to predict buy and sell (entry-exit) points of the stock prices by using MLP artificial neural network. There are three main phases in our model for predicting the buy-sell points from the stock prices. There is an extra Phase 4 for calculating the efficiency of the system, however, that is not part of the trading model. Algorithm 1 shows the steps of all phases.

In our study, the daily stock prices for Dow 30 stocks, which are obtained from finance.yahoo.com, are used as training and test datasets. In the first phase, open and close prices, daily high and low price values for each stock are obtained. Then, for each day, all daily close prices are labelled as “Hold", “Buy", or “Sell" by automatically analyzing the peak and valley points indicating highest and lowest points for a specified period. The peak points are marked as “Sell", the valley points are marked as “Buy" and the remaining points are marked as “Hold" (Figure An Artificial Neural Network-based Stock Trading System Using Technical Analysis and Big Data Framework)333Base graph is adapted from https://investing.com.

In the second phase, RSI, WilliamR and MACD values are calculated for each daily stock price. In our framework, we used TA4J (Techical Analysis For Java) library to calculate the RSI, WilliamR and MACD of the daily prices. Afterwards, corresponding label value, close price, RSI, WilliamR and MACD values are normalized in order to be suitable for the learning stage.

In addition, in the second phase, the data imbalance problem is also solved. Normally, the occurrence of “Hold" labels is much greater than the number of the “Sell" and “Buy" labels in the training data. This effects the learning stage such that the model only learns the majority classes better (Hold), ignoring the smaller classes causing misclassification and misprediction of data. There are different solutions in literature for this problem. We preferred the approach of resampling the minority classes. In other words, we created multiple copies of “Buy" and “Sell" labeled data and introduced those to the training dataset. Thus, the number of three class labels are approximately equal solving the class imbalance problem.

In the third phase, training and test data are fed to the multilayer perceptron (MLP) using Apache Spark. Our topology for MLP has four layers that consist of 4 nodes in the input layer, 5 nodes in the second layer, 4 nodes in the third layer and 3 nodes in the fourth layer (one for each output class Buy, Hold, and Sell). MLP is run with 200 epochs to train the learning model using the labeled training data. Figure An Artificial Neural Network-based Stock Trading System Using Technical Analysis and Big Data Framework depicts the whole process graphically.

As explained in Section An Artificial Neural Network-based Stock Trading System Using Technical Analysis and Big Data Framework, we used the three phases of the algorithm to train and test the model, and then measure the overall performance in Phase 4. The stock data obtained for Dow 30 is split into two sets, the training data is the stock prices between the dates of 1/1/1997 and 12/31/2006, and the test data is the stock prices from 1/1/2007 to 1/1/2017. We analyzed the performance of our framework with different criteria. Our focus is on the correct prediction of labels as an output of MLP model. Walmart (WMT) stock is chosen to provide an example for the evaluation. Table An Artificial Neural Network-based Stock Trading System Using Technical Analysis and Big Data Framework illustrates the confusion matrix for WMT, and Table An Artificial Neural Network-based Stock Trading System Using Technical Analysis and Big Data Framework shows the precision, recall and F1 scores. The overall prediction rate accuracy for WMT is 65.52%.

Moreover, we evaluated our system based on the success of our trading strategy. In our model, a stock is bought, sold or held according to its predicted label result. For instance, if the predicted label equals to "1" (buy), (the corresponding output neuron is activated) the stock is bought using the capital that exists at that particular point. We start with a total capital of $10,000. All available capital is used during each transaction. If the predicted label equals to "2" (sell), the stock is sold and we get back to an all cash position. If predicted label equals to "0" (hold), system does not do anything. As a result, during trading, if the same label is repeated one after another, only the first label gets triggered, the system ignores the repeating signals until the label changes. Also, in our scenario, we used a realistic trading environment that includes trading commission ($1 per transaction, 0.001 of the starting capital). Stop loss situations (%5) are also implemented in our scenario.

The box above shows a sample of JPM (JPMorgan) trades by our framework. There are 168 transactions for JMP between 2007-2017. Starting capital is $10,000 in 1/1/2007. The ending capital reaches$49,181.78 at the end.

We also applied “Buy and Hold" (BaH) as the base strategy for Dow 30 stocks for the same testing period. In BaH, a stock is bought at the beginning and sold at the end of the testing period. This is the preferred strategy for most long-term investors and works very well for bull markets, but not so good in trendless or bear markets.

Table An Artificial Neural Network-based Stock Trading System Using Technical Analysis and Big Data Framework shows the comparative performance of our model against the BaH for all Dow 30 stocks. Our proposed framework’s average annualized return is 10.3%, and the average annualized return of BaH strategy is 13.83%. Our proposed strategy’s annualized return performed better than BaH strategy’s annualized return in only 9 out of 29 (Visa stock [V] did not have enough data points in the same period). The average success percentage of all transactions (buy then sell) in our system is 67.33% indicating that every 2 out of 3 transactions resulted in a profit.

Generally it is very difficult to beat Buy and Hold during such a lengthy time period. However, our model provided mixed results (sometimes better, sometimes worse) in comparison to BaH. This is mainly because of using the same standard values for the chosen technical parameters for all of the stocks. Since, we have not implemented any new indicator and/or parameter optimization, the performances of the stocks vary accordingly. However, it is seen in previous studies that implementing such optimization techniques might increase the overall trading performance considerably [?]. Fine tuning the technical indicator parameters individually for each stock might improve the overall performance of the trading model.

In this paper, we presented a new stock trading and prediction model based on a MLP neural network utilizing technical analysis indicator values as features. Big data framework Apache Spark is used in implementation. The model is trained and tested on Dow 30 stocks in order to see the evaluate the model. The results indicate that comparable results are obtained against the baseline Buy and Hold strategy even without fine tuning and/or optimizing the model parameters. For future work, some optimization stages will be added to the model and deep learning models will be used in the learning stage.

• [1] R. Araujo and T. Ferreira. A morphological-rank-linear evolutionary method for stock market prediction. Information Sciences, 237:3–17, 2013.
• [2] G. S. Atsalakis and K. P. Valavanis. Surveying stock market forecasting techniques-part ii: Soft computing methods. Expert Systems with Applications, 36:5932–5941, 2009.
• [3] G. Creamer. Model calibration and automated trading agent for euro futures. Quantitative Finance, 12(4):531–545, 2012.
• [4] K. Kim. Financial time series forecasting using support vector machines. Neurocomputing, 55(1–2):307–319, 2003.
• [5] Z. Liao and J. Wang. Forecasting model of global stock index by stochastic time effective neural network. Expert Systems with Applications, 37(1):834–841, 2010.
• [6] A. Ozbayoglu and U. Erkut. Stock market technical indicator optimization by genetic algorithms. Intelligent Engineering Systems through Artificial Neural Networks, 20:589–596, 2010.
• [7] K. Park and H. Shin. Stock price prediction based on a complex interrelation network of economic factors. Engineering Applications of Artificial Intelligence, 26:1550–1561, 2013.
• [8] J. Wang, J. Wang, Z. Zhang, and S. Guo. Forecasting stock indices with back propagation neural network. Expert Systems with Apps., 38(11):14346–14355, 2011.
• [9] F. Zarandi, B. Rezaee, I. Turksen, and E. Neshat. Forecasting model of global stock index by stochastic time effective neural network. Expert Systems with Applications, 36:139–154, 2009.
You are adding the first comment!
How to quickly get a good reply:
• Give credit where it’s due by listing out the positive aspects of a paper before getting into which changes should be made.
• Be specific in your critique, and provide supporting evidence with appropriate references to substantiate general statements.
• Your comment should inspire ideas to flow and help the author improves the paper.

The better we are at sharing our knowledge with each other, the faster we move forward.
The feedback must be of minimum 40 characters and the title a minimum of 5 characters