An Artificial Neural Network-based Stock Trading System Using Technical Analysis and Big Data Framework
O. B. Sezer M. Ozbayoglu E. Dogdu
cs.CE q-fin.TR stat.ML 68TXX







2017 



































3 




















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
  


    December 30, 2023
==================================================================================================================================================================================================================================================================================================================================================================================================================


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.







Algorithmic trading, trading strategy, machine learning, neural networks, stock market technical analysis





§ INTRODUCTION



Stock market forecasting and developing profitable trading models have always attracted researchers and practitioners <cit.>. 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 <cit.>, neuro-fuzzy models <cit.>, support vector machines (SVM) <cit.>, and genetic algorithms-based systems <cit.>, 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 <cit.>. 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.



§ MODEL FEATURES



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)


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)



    RS= Average Gain/Average Loss




 §.§ Moving Average Convergence and Divergence (MACD)


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)




 §.§ Williams %R


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

    %R = (Highest High - CurrentClose)/ (Highest High - Lowest Low) x -100




§ METHOD







For big data analytics, commonly used open source tools are Apache Hadoop[http://hadoop.apache.org] and Apache Spark[http://spark.apache.org]. 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 <ref> 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 <ref>)[Base 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[https://github.com/mdeverdelhan/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. 



[ht]
Predicting label of Dow30 Stocks using MLP








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 <ref> depicts the whole process graphically.






§ EVALUATION





As explained in Section <ref>, 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 <ref> illustrates the confusion matrix for WMT, and Table <ref> 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. 



Transaction Number, Interval, Gain, Instant Capital

1.(21-25) => -516.19 Capital: $9481.81

..

62.(831-836) => -1532.45 Capital: $19428.23

..



168.(2463-2465) => 1061.5 Capital: $49181.78



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 <ref> 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 <cit.>. Fine tuning the technical indicator parameters individually for each stock might improve the overall performance of the trading model.  



§ CONCLUSION



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.









abbrv
  










