Predicting Support and Resistance Indicators for Stock Market with Fibonacci Sequence in Long Short-Term Memory

.


Introduction
With rising number of data generated data analytics has a deep significance in areas that needs insights into future. These insights set trends, achieve goals, reduce costs, improve efficiency, increase productivity and sharpen the performance. Raw data with varied attributes, high dimension and multiple sets are searched and properties from them are extracted through mining techniques to form a structured set. Statistical tools are applied to these sets and to derive inferences from these formed structures, machine learning and deep learning algorithms are put to use. Out of the varied attributes, time is considered as a significant attribute in data analytics that is used in analyzing, predicting and forecasting -yet it has often been neglected. In order to make use of this attribute it is taken in the form of series usually taken at successive equally spaced points in time. This sequence is plotted via line charts usually as independent variable against any other attribute as dependent variable that is specific to the application. Time series analytical (Wei, 2006) methods are used for extracting the features and deducing essential statistical data while time series forecasting methods and models is adopted for prediction based on historical data.
Time series analysis and forecasting are used in industries, banking and financial services, cross marketing, medical diagnosis, marketing and Sales forecasting, security issues, collection analysis, risk management, fraud detection, underwriting, statistics, signal processing, pattern recognition, econometrics, mathematics, finance, weather forecasting, earthquake prediction, electroencephalography, control engineering, astronomy, communications engineering and largely in any domain of applied science and engineering which involves temporal measurements. For all the above mentioned time series applications, deep learning models (Heaton et al., 2016) are used for time series analysis (Gamboa, 2017) and time series forecasting (Qiu et al., 2014), it could also be combined with other techniques (Moews et al., 2019) and these models are proven to be effective than the other traditional methods.
Time series forecasting implemented by deep learning models is used to find the trends in share markets and there are many technical market indicators where a framework can be formed from which the investors can make two very important decisions-when to buy stocks and when to sell stocks. Support and resistance levels are one of the technical indicators that help investors to trace the past movements of share prices where these prices fluctuate with supply and demand. Support is where it is considered that there will be no further decline in prices. The break in rising of prices is considered as Resistance. Identifying these levels is an essential component for a successful technical analysis.
Most of the mathematicians and scientists accept Fibonacci sequence (Scott and Marketos, 2014) and Golden ratio (Grigas, 2013) as an amazing concept because of the presence of its sequence and pattern in objects and activities in nature. Nature uses this concept to maintain balance and seem to derive predictable patterns from atoms and astronomical objects. It is aesthetically pleasing and its mathematical properties are applied in many domains like mathematics, computer science, music and architecture to mention a few. This concept is also used as a technical tool and from a stock analyst's point of view Fibonacci sequence is used for following the price pattern and how the support and resistance levels are retraced. These help to know about the possible areas of trending of price waves where a trader can analyze and make decisions to sell or hold back the stocks. This paper examines how the Fibonacci sequence is developed as a retracement to know the support and resistance levels in share prices. This is then fitted in LSTM network model for future prediction and the accuracy is sought out. The organization of this paper is as follows. Section 2 examines the work carried out by other authors that is in compliance with Fibonacci sequence as a tool for prediction in stock markets, in addition a review about support and resistance is also presented. The papers that discusses about long shortterm memory is also been assessed. Section 3 elaborates about long short-term memory and about the facts related to Fibonacci sequence. The methodology is also discussed in detail here. The experimental results are put forth in section 4 and the conclusion about the paper is in section 5.

Related Work
There are many papers dedicated to emphasize the amazing characteristics of Fibonacci series, expose the hidden aesthetics and its usage in various applications. Few papers that are related to the application of the sequence in stock markets and the technical aspect of stock market have been discussed below. Recurrent Neural Networks (RNN) and Long Short-Term Memory (LSTM) are considered to be models of importance in deep learning for prediction and are applied in various domains where it is considered to be difficult to forecast. Here an attempt is made to expose few papers that discuss the application of the model Long Short-Term Memory in prediction of various attributes in share markets. Osler (2000) have defined about support and resistance which is a technical analysis calculated to find out the exchange rates that are interrupted or reversed. In this article the predictive power of support and resistance indicators have been examined with the data containing yen, mark and pound that was relative to dollar. The support and resistance level is taken as one of the criteria to provide information regarding foreign exchange market for indicating the ups and downs in prices. The findings reveal that the rate trends were affected by both support and resistance level more often than that was actually expected. All the six firms that were taken for study had almost similar results that lasted for a week. Kumar (2014) proposes a model in machine learning that can be used to remove the subjectivity that arises in domains like share market where decisions are made from predictions and specify it objectively. The author takes point of sale, money flow index, relative strength index and chaiken money flow as independent variables and a series of Fibonacci retracement of waves as dependent values. Only the important variables are extracted by Factor analysis and curtail the Fibonacci series to cardinality by two because there are actually a set of values. The study concludes that the wave 2 predicts better than the rest by the proposed model. Mohammad. (Alalaya and Almahameed, 2018) have taken Amman stock exchange Market prices as dataset and have analyzed the application of Fibonacci retracement and Elloit waves theory that determine the trading strategy, the time to buy and sell stocks and shares. They conclude that the sequence is helpful and helps to take decision to buy or hold. Pang et al. (2020) propose the deep Long Short-Term Memory neural network (LSTM) with embedded layer and the long short-term memory neural network with automatic encoder to predict the stock market for the Shanghai A-shares composite index. Comparison between LSTM and ELSTM was made and demonstrated that ELSTM model has higher accuracy than the LSTM. Roondiwala et al. (2017) uses Recurrent Neural Network (RNN) and Long Short-Term Memory to predict stock market indices because forecasting has been a difficult task. They have modeled and predicted for NIFTY 50, stock exchange of India. The model undergoes several stages and for analyzing the efficiency the RMSE was used. The model using RNN and LSTM was found to be efficient and helps the investors by forecasting efficiently. Nelson et al. (2017) use the LSTM networks to predict future trends of stock prices based on price from past history. A classification model was built based on LSTM using technical indicators as input rather than texts. A series of experiments were executed on datasets from Brazilian stock exchange. Metrics such as accuracy, precision, recall and Fmeasure were used to assess the model proposed. A statistical test was performed on top of the results obtained to further improve the accuracy obtained. Averagely the results had 55.9% of accuracy and determining uptrend or downtrend in the near future of a particular stock is promising.

Materials and Methods
Predicting the next move for a financial dataset using time series models is actually considered as a problem that is regression by nature. There are many algorithms that deal with time series problems but with the advent of deep learning models, there has been a shift from the traditional methods. LSTM is one of the deep learning models that are currently applied in many regression problems. The tracing of support and the resistance levels and its future reflections is a regression time series problem which is applied in two financial dataset and is dealt with LSTM through Fibonacci sequence.

Long Short Term Memory
Long Short Term Memory (LSTM) networks were introduced by Sepp Hochreiter and Jürgen Schmidhuber in 1997 that uses Constant Error Carousel (CEC) units to deal with the issue of vanishing gradient that arise in Recurrent Neural Networks (RNN). The basic architecture of LSTM network as shown in Fig. 1 comprises of a cell that has an input gate, output gate and a forget gate.  LSTM neurons keep a context of memory within their pipeline to allow for tackling sequential and temporal problems without the vanishing gradient issue affecting their performance. Their special behavior is retaining information for a long duration and LSTM have this similar chain like structure as RNN with a variation in the repeating module that has a different structure. So there are four interactive layers instead of a single network layer. LSTM is a powerful algorithm and has seen many upgradations in its architecture (Jozefowicz et al., 2015;Yao et al., 2015;Kalchbrenner et al., 2015;Greff et al., 2016) and that is used in a variety of application relating to traffic flow prediction (Fu et al., 2016), business process monitoring (Tax et al., 2017), speech recognition, language modeling, translation, image captioning, handwriting recognition, anomaly detection in network traffic and intrusion detection systems. The ability to preserve the long-term memory due to the gating mechanism is considered to be important to deal with problems arising in classification, clustering and predictions about data especially in applications related to time series, sequential problems, text classification and Natural Language Processing.

Fibonacci Sequence as a Financial Tool
Relationships and correlations within the data brings out Patterns that have a inevitable role in prediction and foresee what might or when something will happen that are easy for making decisions. Interesting kinds of patterns are sequences that can be understood as a list of elements recognized by a certain and distinctive order. Fibonacci sequence (Omotehinwa and Ramon, 2013) is a well known and widely used sequence in which each number is the sum of the two numbers that precede it and seems to be nature's underlying principle behind life's many events and phenomena. Apart from that, this sequence can then be broken down into ratios which some traders believe provide clues as to where a given financial market (Chatterjee et al., 2002) will move to. The ratios or percentages that traders use are 23.6, 38.2, 50, 61.8, 78.6, 100, 161.8, 261.8 and 423.6%. There are five types of trading tools that are based on Fibonacci`s discovery: Arcs, fans, retracements, extensions and time zones. Fibonacci Retracements are depicted as horizontal lines that specify areas of support and resistance. Forecasting the future support and resistance values can be considered as a time-series prediction based on regression problem where LSTM can be used to fit the model. The Long Short-Term Memory network (LSTM) network is used in deep learning because of its ability to train very large architectures successfully and designed to handle sequence dependence. Setting Fibonacci percentages in LSTM model thereby predicting the future flow of price trends helps in the identification of the stop-loss levels, the uptrend and downtrend cycle, place entry orders and set price targets.

Description of Dataset
The datasets that was used for forecasting in this study were taken from Kaggle an online community which offers a platform for dataset exchange and is a dataset repository. Two historical Equity stock datasets were taken with a total of 5021 instances each from Jan/3/2000 to Feb/28/2020. From this dataset 80% was taken for training purpose and 20% was taken for testing purpose. The attributes in each dataset comprises of date, symbol, series, previous closing price, opening price, high, low, last, closing price, volume, Volume Weighted Average Price (VWAP), Turnover, Trades, Deliverable Volume and % Deliverble. Table 1 lists certain features of these datasets and out of these attributes Closing Price was taken for tracing the support and resistance level using the Fibonacci sequence with the variable Date to forecast.

Methodology and Development
The problem of buying and holding shares is regarded as a regression problem and the model uses Fibonacci retracement fitted in LSTM with Time Steps. Scaling of data is done to make the data stationary to avert the problem of exploding gradient problems that occur in regression. This is taken as pre-processing of data so that comparison between different variables is done on equal footing. With time series data, the sequence of values is important and for assessing the ability of the model, the ordered dataset is split into train and test datasets after rescaling. So from the dataset 80% is split and taken as training datasets and the rest 20% for checking the accuracy as testing datasets. Fibonacci retracement is applied on the original datasets and Fig. 2 and 3 shows the retracement lines of closing price taken at 23.6, 38.2 and 61.8% as different color zones along with 0 and 100% for the minimum price level and maximum price level for dataset1 and dataset2 respectively.
To frame the problem as one time step for each instance, the training and testing input data are transformed into the specific array structure -[samples, time steps, features] as the LSTM network expects using numpy. reshape(). Now LSTM network is ready is be fitted for this problem. A stacked LSTM with two layers of 50 neurons or blocks each is used and the input for the next layer is the hidden output of the previous layer. The reshape input array is taken as 1 since our data has one time-step with one feature. The LSTM blocks uses sigmoid activation function since it is the default function in keras. The network is trained for 2 epoch and a batch size of 1 is used for dataset1 and 3 epoch with batch size 1 is used for dataset2. Once the model is fit, the performance of the model on the train and test datasets is estimated. Before generating predictions the error units are converted to the same units to be in par with the original data. Predictions are then made with the model with both the training and testing data and graphical presentations are generated.  The first three Fibonacci percentages along with the minimum and the maximum prices i.e., 0, 23.6, 38.2, 61.8 and 100% were set as levels for retracement in LSTM that aid to predict the future flow of trend in price levels. In the Fig. 4 and 5 it can be seen that the original training data is represented in green, the predictions for the training dataset in red and the predictions on the unseen test dataset in blue for dataset1 and dataset2 respectively. Finally Fibonacci retracement is plotted for the training data that is depicted as Fig. 6 for dataset1 and Fig. 7 for dataset2. The unseen testing data for the 20% of the data is plotted separately in Fig. 8 for dataset1 and Fig. 9 for dataset2 and the correlation between them is found to check the performance between the original price values and the predicted price values.

Experimental Results
Tracing the support and the resistance level values is one of the technical analyses performed to know the trend at which the price fluctuates and based on this observation the stock holder can get an idea of when the stocks may be sold or bought. As this is a prediction problem which can be applicable on past sequential data and can be solved by time series regression, two datasets of sequential nature are taken containing historical prices. To make an analysis of the result obtained by fitting the LSTM model with first three Fibonacci percentages along with the minimum and the maximum prices as lowest and highest percentages to trace the price trend, the error metric are used to seek the accuracy of the values that are actually taken for prediction. The results and its analysis are elaborated below. For analyzing the efficiency of the system the Root Mean Square Error (RMSE) is used. RMSE is the square root of the mean/average of the square of all of the error. The error or the difference between the target and the obtained output value is minimized by using RMSE value. RMSE is considered as one of the best error metric for predicting numerical values and is widely used generally. Mathematically it is denoted as (1) where yi is the observed or sample values, ŷi is the predicted value and n is the number of observations or samples: Out of 5021 sequences data, 4016 samples were taken for training purpose and 1005 samples for validation purpose. For training the model Adam is set as the optimizer. Google cloud engine was used as a training platform [Machine type: N1-standard-2 (2 vCPUs, 7.5 GB memory), CPU platform: Intel Core i5] and used Windows 7, Keras (Frontend) and Tensor flow (Backend) as the learning environment.
A good way to check the best forecasting model is to find the model with the smallest RMSE computed using time series cross-validation. It can be seen from Table 2 representing dataset1 and Table 3 representing dataset2 that the RSME values are lower than the range of the dependent variable i.e., closing price for each level of Fibonacci value and therefore the model has fitted both the training and the test datasets excellently. The values of both the training and the testing data along with the unseen data are plotted as Fig. 4 for dataset1 and as Fig. 5 for dataset2 that validates the result as seen in Tables 2 and 3 by overlapping lines of the test data and unseen data indicating that the model has forecasted excellently.
In Fig. 8, the support and the resistance level can be traced from the forecasted price of testing data for dataset1 and this can be compared with Fig. 6 where the support and resistance levels are traced for original price of testing data for dataset1. When comparing Fig. 8 with Fig. 6 it is seen that the peaks and troughs that are at Fibonacci levels are with minimal difference and helps identify the point at which the investor can decide the acquirement, retain or release the shares. Similarly for dataset2, Fig. 9 represents the tracing of support and resistance levels with Fibonacci percentages for forecasted data and when this is compared with Fig. 7 that represents the original price of testing data for the dataset2 it can be inferred that the indictors can be considered for selling or buying of the shares in future as both the predicted and the original retracement has minimal difference in closing price.
In Fig. 10 and 11, the uptrend and downtrend of the forecasted prices is depicted by different colors and candlestick pattern for dataset1 and dataset2 respectively. The recent 1000 data out of 5021 have been taken for this pattern to follow the major trend in prices for both the datasets. In both Fig. 10 and 11, the price values that coincide at the first three Fibonacci percentages i.e., 23.6, 38.2 and 61.8% along with the 0% set as swing low which represents the minimum price value and 100% set as swing high representing the maximum value is also depicted as different types of horizontal lines and in different colors. The Tables 2 and  3 given below show the error values for the two datasets along with the original and forecasted Fibonacci traced closing price values that were taken as dependant values. These values that fall in the Fibonacci levels are taken as points where the support and resistance levels are likely to retrace and a possible direction of trend is identified.  The major trend lines is sought in this study and by observing Fig. 10, the candlestick pattern follows uptrend, reaches the resistance level at Fibonacci level 38.2% then reverses to swing low. This pattern is generated at three different intervals and finally reaches swing high. From this the analyst can choose a point where the stocks can be sold because the resistance levels often acts as a trigger to sell where the traders can expect maximum supply. It can be seen from Fig. 11, that dataset2 follows the uptrend pattern and the price reverses at Fibonacci level of 23.6%, a resistance level and after the swing high it follows the downtrend pattern to swing low, a support level. A support level is achieved where there the traders expect maximum demand and it is point where stocks can be bought.

Conclusion
Stock price prediction depends on various factors and it is a difficult task for investors to get a right decision.
Share market has its techniques to assess the market situation and implementing them as models have actually reduced the burden of tedious manipulation for the investors. One of the major technical analysis is Support and Resistance level indicators which are taken here to forecast the future trend of the stock prices, where the investors may analyze the uptrend and downtrend to determine the buying or selling of shares and to identify these levels using the Fibonacci retracement is considered as an efficient tool which supports the analysis of traders. To develop this trading strategy there are machine learning and deep learning algorithms and LSTM is most preferred model when time series analysis is concerned when compared to other models. Implementing the Fibonacci retracement to forecast the unseen price values and the major trend lines has been successfully implemented using LSTM network in this research work. From the model proposed it can be inferred that the error results have been minimal and Daily candlestick chart of Axis Bank forecasted stocks close to the minimal value of the dependant variable by which the desired range is achieved. From the results implemented as figures it can be observed that the forecasted results are promising for the support and resistance level since accuracy has been achieved to an excellent level. In future, the work can be extended to add technical indicators like Relative Strength Index (RSI), Simple Moving Average (SMA), Money Flow Index (MFI) to the model to determine the trend lines. Stock market analysis has various factors to be considered to earn gains by selling at the right time. So the work can also be extended by combining the technical indicators with one of the fundamental indicators and analyzing the correlation between stocks for investing in multiple shares at the same time.