0% found this document useful (0 votes)
23 views20 pages

Computation 07 00004

a

Uploaded by

Ivan Medić
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views20 pages

Computation 07 00004

a

Uploaded by

Ivan Medić
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

computation

Article
Advanced Markov-Based Machine Learning
Framework for Making Adaptive Trading System
Francesco Rundo 1, * , Francesca Trenta 2 , Agatino Luigi Di Stallo 3 and Sebastiano Battiato 2
1 STMicroelectronics ADG—Central R&D, 95121 Catania, Italy
2 IPLAB—Department of Mathematics and Computer Science, University of Catania, 95121 Catania, Italy;
[email protected] (F.T.); [email protected] (S.B.)
3 GIURIMATICA Lab, Department of Applied Mathematics and LawTech; 97100 Ragusa, Italy;
[email protected]
* Correspondence: [email protected]

Received: 14 November 2018; Accepted: 28 December 2018; Published: 3 January 2019 

Abstract: Stock market prediction and trading has attracted the effort of many researchers in
several scientific areas because it is a challenging task due to the high complexity of the market.
More investors put their effort to the development of a systematic approach, i.e., the so called
“Trading System (TS)” for stocks pricing and trend prediction. The introduction of the Trading
On-Line (TOL) has significantly improved the overall number of daily transactions on the stock
market with the consequent increasing of the market complexity and liquidity. One of the most
main consequence of the TOL is the “automatic trading”, i.e., an ad-hoc algorithmic robot able to
automatically analyze a lot of financial data with target to open/close several trading operations
in such reduced time for increasing the profitability of the trading system. When the number of
such automatic operations increase significantly, the trading approach is known as High Frequency
Trading (HFT). In this context, recently, the usage of machine learning has improved the robustness
of the trading systems including HFT sector. The authors propose an innovative approach based
on usage of ad-hoc machine learning approach, starting from historical data analysis, is able to
perform careful stock price prediction. The stock price prediction accuracy is further improved by
using adaptive correction based on the hypothesis that stock price formation is regulated by Markov
stochastic propriety. The validation results applied to such shares and financial instruments confirms
the robustness and effectiveness of the proposed automatic trading algorithm.

Keywords: LSTM; stock price; machine learning

1. Introduction
Stock price prediction is one of the main challenging tasks of the financial area. In literature,
there are several algorithms showing approaches for stock price prediction. Initially, the research
investigation focused its effort on methods based on traditional statistical algorithms, as well as on
the usage of soft computing techniques [1]. The recalled classical approaches show their limitation
related to inflexibility in dynamic variability and complexity. The major issue of stock price forecast is
related to high unpredictability, especially for long term prediction. Perhaps a valuable impact on stock
price variability is correlated to economic news or—for some stocks—to national and international
government policy. Countless research studies showed that a good and robust financial market model
for stock price forecast needs the development of algorithms able to process a lot of mathematical and
econometric variables; especially in the short term prediction of stock price. Nonetheless, there is a
huge amount of studies of various disciplines that have attempted to address the task of stock price
prediction, presenting a large variety of approaches, even though it seems, from general comparative

Computation 2019, 7, 4; doi:10.3390/computation7010004 www.mdpi.com/journal/computation


Computation 2019, 7, 4 2 of 20

results, that soft computing and more recent machine learning approaches exceed classical algorithms
in terms of performance. In the next sections, we describe some of the developed algorithms for stock
price prediction starting from classical ones based on regressive models, until the recent ones based on
Machine learning modelling.

1.1. Stock Price Prediction Methods Prior Art: Classical Pipelines


One of the first work about stock prediction is in Reference [2] where the authors presented a
buying and selling timing prediction system for stocks by using modular neural networks. Wang
et al. in Reference [3] presents a prediction system for stock market by utilizing a non-linear an
Autoregressive Integrated Moving Average (ARIMA) model. The authors reached effective results
in stock market prediction by using an Autocorrelation Function (ACF) to verify if a time series is
stationary or non-stationary in order to remove non-stationary behavior. The authors implement an
ARIMA-based recurrent network for stock market prediction. The experimental results confirmed
that the proposed approach seems quite good even though the prediction accuracy can be improved
by filtering stock price series in order to involve noise in it. In Reference [1], the authors propose
a Genetic Algorithms (GA) approach in order to predict a stock price index. They basically focus
on feature discretization in order to reduce the complexity in feature space. The approach consists
of the combination of a generic algorithm and together with an Artificial Neural Networks (ANN)
hybrid system in order to optimize the connection weights between layers and the thresholds for
feature discretization. Despite presenting some limitations, the approach seems very promising.
One limitation is that the performance of the model varies according to the number of the elements
in the hidden layers. Additionally, the weights and the thresholds must be determined accurately to
avoid the ANN overfitting problem. Another approach, based on neural networks, uses ANN [4].
The authors exploit ANN in order to select the most significant factors that affect the daily direction
of S&P 500 index. The results are promising even if it is difficult to determine the optimal network
parameters and select significant features. All the described methods suffer from a limited ability
to financial timeseries generalization of the used mathematical models. Significant improvements
in addressing the aforementioned issue occurred with the adoption of modern Machine Learning
techniques, as described in the next section.

1.2. Stock Price Prediction Methods Prior Art: Recent Machine and Deep Learning Pipelines
In Reference [5], the authors try to implement a Long Short-Term Memory (LSTM) model to
predict stock value movement. Specifically, Nelson et al. implement a LSTM classification model
in order to determine if the price of a particular stock will be higher or not than the current price
in 15 min in the future. The results show that the model based on LSTM is more accurate than
other machine learning models. A very interesting approach is described in Reference [6]: Zhang et
al. used, successfully, the Elman Recurrent Neural Network for predicting the open price of such
stocks. Specifically, the author described the Particle Swarm Optimization (PSO) algorithm for learning
optimization of the Elman Recurrent Neural Network. The experimental results confirmed that the
proposed approaches seems very promising, even though the prediction benchmark seems comparable
to classical BP Neural Network algorithms [6]. Another survey about usage of Machine Learning for
stock market prediction is reported in Reference [7]. The authors provide a detailed benchmark about
usage of Support Vector Machine (SVM), Random Forest, K-Nearest Neighbor (KNN), Naive Bayes,
and Softmax in stock prediction. The experimental results confirmed that Random Forest algorithm
performs the best for large datasets, and Naïve Bayesian Classifier is the best for small datasets.
The results also reveal that reduction in the number of technical indicators reduces the accuracies
of each algorithm. Anyway, from careful analysis of the recalled prior-art, the authors identified a
common baseline of almost of the Machine Learning algorithms: All methods perform a stock price
prediction by using historical data, and in some cases, data coming from sentiment analysis [8].
Computation 2019, 7, 4 3 of 20

1.3. The Proposed Stock Price Prediction Systems: Brief Overview of Underlying Algorithm
The work herein described, goes in the direction drawn by the Machine Learning methods above
detailed: We propose the usage of Long Short-Term Memory (LSTM) architecture [9] for performing
an adaptive profitable trading system for stock markets. The adoption of LSTM is correlated to a
specific study we have performed about financial time series dynamics. Starting from that statistic
analysis, we introduce some specific hypothesis on price formation dynamic. Basically, we assume
that the stock price formation is influenced by previous price timeserie, i.e., price forecast of such
stocks is affected by the previous trend of the same stock. In this regard, the LSTM have gained a lot
of credit because of their peculiarity, which allows time series forecasting starting from the previous
values of the series. The LSTMs are perfectly suited to this task as they are able to learn from past
collected information included in the stock price dynamic making a prediction based on that data.
In this context, the authors proposed a LSTM based algorithms for the prediction of stock close price
in a daily timeframe. Moreover, taking into account the above statistic hypothesis, the authors have
developed a further improvement of the LSTM stock price and trend prediction based on specific
assumption that share price formation satisfies the Markov statistical hypothesis. In order to show the
robustness of the proposed approach, we have tested the algorithm analyzing high volatile shares such
as bank stocks considering, perhaps, the current unstable macro-economic dynamics. We collected
about 18 years of historic bank stocks daily close price time series from October 2000 to October 2018,
downloaded from Yahoo-finance website, details follow.
The aim of this paper is to forecast “close price” of such stocks across a given timeframe in
order to elaborate ad-hoc trading algorithm based on that prediction. The remainder of the paper
is structured as follows. In Section 2 we introduce the forecasting problem and an overview of our
strategy and a brief introduction about Long Short-Term Memory neural networks and their structure.
We present, in Sections 3 and 4, our proposed method and an introduction about referred Markov
propriety. In Section 5, we show some experimental results about advantages and applications of our
method. In Section 6, we present a short discussion about future work.

2. The Overall Framework

2.1. Stock Price Forecasting Problem


Stock price forecasting can be defined as the prediction of current share price by using historical
data of same stock as well as correlated ones such market indexes, similar stocks, etc. Many regression
models are designed for explanatory purposes to understand the correlation between relevant
economic factors. Classical Regression models are often used for quantitative shares dynamic
forecasting. The desired target is to accurately predict the “future”. A reliable forecasting algorithm
will be able to identify risk factors, market indexes, and estimated rate of win/loss trades in
investment decisions, as well as the impact of critical events such as default rates and so on [10,11].
Specifically, the forecast of financial time series allows to understand stock market trend as it plays
a key role on making investment decisions. Moreover, the forecast financial times-series allows
for the identification of specific patterns useful to predict the best performing corporate shares or
market indexes. Those evaluations make time series analysis and forecasting an important area of
research. In this work, we propose a forecasting algorithm for stock price and trend prediction as well
as a correlated trading system based on those predictions. As recalled previously, a robust prediction
system should be able to forecast the current market trend so that we have implemented two pipelines
of LSTMs: One for global trend prediction and other for stock close price forecast.

2.2. Mathematical Background of LSTM


The LSTM is a particular type of Recurrent Neural Network (RNN) with effective performance
in regression problem such as forecasting time series. LSTM mode description is carefully detailed
in Reference [9]. LSTMs represent a considerable improvement of the classic RNN model because
Computation 2019, 7, 4 4 of 20

they are able to evaluate the hidden nonlinear correlations between and within input data that has
Computation 2018, 6, x FOR PEER REVIEW 4 of 20
led to increasing the performance of the time-series prediction algorithm. A classical LSTM main unit
Computation 2018, 6, x FOR PEER REVIEW 4 of 20
is composed of a cell and an input/output/forget gate. The cell is able to store (recall) values over
arbitrary time intervals, while the above gates are able to manage the input/output data flow of the
arbitrary
arbitrary
cell. time
timeintervals,
The diagram intervals,while
while
of a typical the
theaboveofgates
above
structure gates are
are able
a LSTM able to well
to
cell as manage
manage theinput/output
the input/output
as related data
data
equation model flow
flow
is of the
of the
reported
cell. The
in cell.
FigureThe1.diagram of a typical structure of a LSTM cell as well as related equation model is reported in
diagram of a typical structure of a LSTM cell as well as related equation model is reported
Figure 1. 1.
in Figure

Figure
Figure1.
Figure 1.1.Long
Long Short-Term
Long Short-Term Memory(LSTM)
Short-Term Memory (LSTM)model.
model.

More
Moredetails
detailsisisshown
shownininFigure
Figure22IIwhere
where aa prototype of
prototypeof
prototype LSTM
ofLSTM cell
cellisis
LSTMcell shown.
isshown.
shown.

Figure 2. Prototype LSTM cell.


Figure 2. Prototype LSTM cell.
Figure 2. Prototype LSTM cell.
Basically, the input of LSTM is a vector that represents the old memory and it passes through
theBasically,
Basically, thethat
forget layer
the input of LSTM
LSTM
decides
input of isinformation
whatis aa vector
vector that
that represents
it can the old
throw away
represents the old memory
from the celland
memory it
state.
and passes through
According
it passes to
through
the forget
thethis, the layer
forget thatgate
“forget
layer that decides what
layer”what
decides information
verifies the valueit
information can
it of throw
caneach away present
number
throw away from the
from theincell
cell 𝑪𝒕 𝟏 ,According
state.
cellstate. where ‘1’ to
According to
this, the “forget
represents gate
information layer”
to verifies
keep, while the
‘0’ value
representsof each number
information to present
forget (1). in cell
this, the “forget gate layer” verifies the value of each number present in cell Ct−1 , where𝒕 ‘1’ 𝑪 , where ‘1’
𝟏 represents
represents
informationinformation to keep,
to keep, while while𝑓‘0’=information
‘0’ represents represents information
𝜎(𝑊 ∙ ℎ to , 𝑥 forget
to forget (1).
+ 𝑏 ),(1). (1)
𝑓 = 𝜎(𝑊 ∙ ℎ , 𝑥 + 𝑏  ), (1)
Then, we decide what new information we’re going to store in the cell state using Equations (2)
f t σ W f ·[ht−1 , xt ] + b f , (1)
and (3) inwe
Then, order to merge
decide what the
newnew and the old
information memory,
we’re goingwhere 𝑪𝒕 inisthe
to store thecell
value thatusing
state can be added to(2)
Equations
the(3)
and cell
instates
order and 𝒊𝒕 identifies
to merge theand
activation values of the input 𝑪gates.
Then, we decide whatthe
newnew the old
information memory,
we’re to store 𝒕inisthe
goingwhere thecell
value
statethat canEquations
using be added (2)
to
the cell states and 𝒊 identifies the
and (3) in order to merge the new and𝑖the
𝒕 activation
= old
𝜎(𝑊values of the
∙ ℎ , 𝑥where
memory, input
), gates.
+ 𝑏 Ct is the value that can be added to the
e (2)
cell states and it identifies the activation values
𝜎(𝑊 of
𝐶 𝑖 ==𝑡𝑎𝑛ℎ(𝑊 ∙ ∙ℎthe input gates.
ℎ , 𝑥, 𝑥 ++𝑏𝑏), ), (3)(2)
𝐶 cell
The next step updates the old i=t =𝑡𝑎𝑛ℎ(𝑊 𝒕 h𝟏∙t−into
σ (W𝑪i ·[
state ℎ1 , xtthe ), state, called 𝑪𝒕 , as reported by(3)
b+i ),𝑏 cell
,]𝑥+new (2)
Equation (4).
The next step updates the old cell state 𝑪𝒕 𝟏 into the new cell state, called 𝑪𝒕 , as reported by
Cet = tanh(WC ·[ht−1 , xt ] + bC ),
Equation (4). 𝐶 = 𝑓 ∗ 𝐶 +𝑖 ∗𝐶 , (4) (3)
Finally, we generate the LSTM output
𝐶 = by𝑓 ∗merging
𝐶 + 𝑖the∗ 𝐶previous
, output, the input and the bias
(4)
vector according to the following Equations (5) and (6).
Finally, we generate the LSTM output by merging the previous output, the input and the bias
vector according to the following Equations (5) and (6).
Computation 2019, 7, 4 5 of 20

The next step updates the old cell state Ct−1 into the new cell state, called Ct , as reported by
Equation (4).
Ct = f t ∗ Ct−1 + it ∗ C
et , (4)

Finally, we generate the LSTM output by merging the previous output, the input and the bias
vector according to the following Equations (5) and (6).

ot = σ (Wo ·[ht−1 , xt ] + bo ), (5)

ht = ot ∗ tanh(Ct ), (6)

where:

• Wf , Wi , WC , Wo represent the LSTM weights


• bf , bi , bC , bo are the bias
• Ct is the cell state
• σ is the sigmoid function

Several research papers [12] confirmed that LSTM networks are well-suited for classifying,
analyzing and—as in this case—making time series forecasting as their mathematical model was
developed to deal with the exploding and vanishing gradient problems usually raised in the classical
RNNs architecture. For instance, one of the most recent work about LSTM-CNN based system for
classification is in Reference [13]. The authors present an interesting approach to improve classification
task, in particular, their method consists in reducing the execution time by 30%–42%. Thus, they prove
the effectiveness of LSTM neural network and their important contribution for specific tasks. Now,
after LSTM model introduction, we are ready to describe the proposed forecasting algorithm and
related trading system. Afterwards, we report several scenarios in which the proposed approach
shows high predictability and profitability.

3. The LSTMs Forecasting Framework: Description


Our prediction framework is composed by two LSTM networks: One for trend prediction and
another one for stock close price time-series regression. The first implemented LSTM network is used
for stock trend forecast: it is basically composed of an input layer in which we fed one stock close
price value at once; an hidden layer of 400 neurons and finally a fully connected output layer (one
neuron). The second LSTMs layer shows the same structure, but some configuration parameters and
training updates are different because it will be used for stock close price prediction. To validate
the method, we decided to select historical data (Daily timeframe) about bank stocks (e.g., Unicredit
[UCG.MI], Monte dei Paschi di Siena [MPS.MI], Intesa San Paolo [ISP.MI], and Credito Valtellinese
[CVAL.MI], etc.), as well as a corporate share i.e., Enel [ENEL.MI], all listed in the Milano Stock
Exchange Market. All daily data have been downloaded from Yahoo Finance website [14]. The
historical data have been collected from October 2000 to October 2018 and each entry is referred to
daily timeframe. Specifically, we collected for each share: Daily Open/Close/High/Low prices even
though we validate our system only for Close prediction. All collected financial values have been
arranged as part of training set and part as validation set. All values were normalized in the range (−1,
1), with classical MinMaxScaler algorithm. In Figure 3 we show an instance of the stock included in the
arranged dataset (Unicredit) in which the blue-colored part of the share timeserie represents the data
we collected as training set, while the remaining ones (red-colored) is used as a test and validation
set of the proposed approach. In more detail, in order to perform a valid and robust training session,
we considered about 12 years of the 18 collected that denotes about 2900 input data (highlighted by
green rectangle), as per previous description. The remaining six years (about 1600 data) are used as
the validation set of the proposed pipeline.
Computation 2019, 7, 4 6 of 20
Computation 2018, 6, x FOR PEER REVIEW 6 of 20
Computation 2018, 6, x FOR PEER REVIEW 6 of 20

Figure
Figure 3.
3. UCG.MI
UCG.MI trainingand
UCG.MI training andvalidation
validationset.
set.

InIn
In Figure
Figure
Figure 4,4,the
4,theauthors
the report
authors
authors an overall
report
report an representation
an overall
overall of the
representation
representation proposed
ofof pipeline
theproposed
the proposed (the part
pipeline
pipeline devoted
(the
(the part
part
to the
devoted stock trend
devotedtotothe
thestock and price
stocktrend prediction)
trend and
and price in order
price prediction) to
prediction) in show the
in order processing
ordertotoshow
showthe flow we
theprocessing have
processing designed
flow
flow wewe to
have
have
reach the to
designed
designed desired
toreach
reach targets:
the An efficient
thedesired
desired targets: stock
targets: price and
An efficient
An efficient trend
stock
stockpriceprediction
priceand trendalgorithm.
andtrend prediction
predictionalgorithm.
algorithm.

Figure4.4.Overall
Figure Overallscheme
scheme for
for the proposed
proposed stock
stocktrend/price
trend/priceprediction pipeline.
prediction pipeline.
Figure 4. Overall scheme for the proposed stock trend/price prediction pipeline.
TheTheproposed
proposedstock stock trend
trend and
and price prediction pipeline
price prediction pipelineisisthen thencomposed
composed byby thethe following
following
components:
components:
The proposed A data
A data pre-processing
pre-processing
stock block followed
trend and pricefollowedprediction by the
by the two pipeline
two pipeline
pipeline is thenLSTMLSTM
composed framework
frameworkby the (in which
(infollowing
which
oneonepipeline
pipelinefor
components: Afor Trend
dataTrend prediction
prediction and
pre-processing and
blockanother
another one for
one
followed for the
by stock
stock two price
price prediction).
prediction).
pipeline LSTMThe The first
first
framework block
block (data
(in(data
which
pre-processing)
pre-processing) isisable
able totonormalize
normalize input stock
stock price
price timeserie
timeserie data,
data,
one pipeline for Trend prediction and another one for stock price prediction). The first block (data asas well
well as asto to split
split input
input data
data
between
between training
training and
and validation,
validation, as
as above
above described.
described. The
The sosoprocessed
processed
pre-processing) is able to normalize input stock price timeserie data, as well as to split input data datadataare arefedfedinto LSTM
into LSTM
framework
framework
between ininwhich
training which andititwill
willbebe properly
validation,properly
as aboveprocessed
processed in
inorder
described. order Thetotoprovide
provide
so ananestimation
processed estimation
data are of of
thethe
fed mid-long
mid-long
into LSTM
term
term trend
trend (LSTM
(LSTM Trend
Trend direction
direction prediction),
prediction), asas wellasasofof
well the the stockprices
stock prices(LSTM
(LSTMPrediction
Predictionfrom fromreal
framework in which it will be properly processed in order to provide an estimation of the mid-long
real data).
data). About About this latest
this latest block, block, the proposed
the proposed stock stock close price prediction system is based on the
term trend (LSTM Trend direction prediction), asclose
well price
as of the prediction system
stock prices is based
(LSTM on the
Prediction usage
from
of usage
LSTM of LSTM
together together
with with
mathematicalmathematical
price price
correctioncorrection
approach approach
based based
on the on the
usage usage
of the of the
so-called
real data). About this latest block, the proposed stock close price prediction system is based on the
so-called
Markov Markov model. statistical model. We introduce nowoverview
a brief overview of the Markov model theory
usage of statistical
LSTM together We
with introduce
mathematical now price
a brief of the Markov
correction approach basedmodelon thetheoryusageneeded
of the
needed to understand the proposed enhanced of the LSTM prediction system. As reported in
to understand
so-called Markov thestatistical
proposedmodel.enhanced We of the LSTM
introduce nowprediction
a brief system. As
overview of reported
the Markov in Reference
model [15],
theory
Reference [15], the Markov Model based approaches for financial time-series forecasting shows
the Markov
needed Model
to understand based approaches
the proposed for financial
enhanced time-series forecasting shows promising results.
promising results. In Reference [15], for instance, of thethe LSTM
authors prediction
described system. method
a predicting As reportedbased in
In Reference
Reference [15], forMarkov
instance,Model
the authors described a predicting method based on usage of Hidden
on usage of Hidden Markov Model in order to improve the accuracy and a comparison of shows
[15], the based approaches for financial time-series forecasting the
Markov
promising Model
results.in order
Inbased to improve
Reference [15],the
for accuracy
instance, and
the aauthors
comparison of thea existing techniques basedbased
existing techniques on Machine and Deep learning. As is described
well known, predicting
a stochastic method
process can
on usage of Hidden Markov Model in order to improve the accuracy and a comparison of the
existing techniques based on Machine and Deep learning. As is well known, a stochastic process can
Computation 2019, 7, 4 7 of 20

on Machine and Deep learning. As is well known, a stochastic process can be defined as “Markov
Process” if the underlying dynamic satisfies the Markov propriety, i.e., the probability of an event—in
a well defined sequence—depends only on the state attained in the previous event. Formerly, in the
discrete-time domain, if we consider a random variable X with Markov propriety with a related
probabilistic sequence Xn , we have:

P ( X n + 1 = x n + 1 | X1 = x 1 , X2 = x 2 . . . . . . X k = x k . . . . . . , X n = x n ) = P ( X n + 1 = x n + 1 | X n = x n ) , (7)

P( X1 = x1 , X2 = x2 . . . . . . Xk = xk . . . . . . , Xn = xn ) > 0, (8)

The financial research area confirmed the fundamental role of probability theory and stochastic
analysis in the share price formation. As reported in Reference [15], in the context of stochastic
analysis the Markov theory (Markov Chain, Hidden Markov Mode, Markov propriety, etc.) shows
very promising results in addressing financial time series forecasting problem. More in detail, a very
effective Markov based approach for addressing financial data processing issues is the assumption that
price formation satisfies Markov propriety. Specifically applied in the proposed approach, the authors
perform a further processing of that assumption: We assume that if the price value at specific time yt+1
depends of previous stock price value only yt , the related prediction error et+1 keeps same statistics,
i.e., it satisfies Markov propriety so that the current stock price prediction error et+1 is correlated to
previous ones et only. We have applied this assumption to our prediction model based on LSTMs,
as described in what follows. Formerly, we consider stock close price prediction made by the second
LSTM pipeline, as pointed out in Figure 4.
 
−1
yt = f LSTM ytlstm , (9)

−1
In Equation (9), we have indicated ytlstm as the stock close price at previous time t – 1, while fLSTM
represents the second LSTM prediction functional model, and then, yt represents the forecasted stock
close price. In our theory framework, we define Y as the random variable describing the stock price
formation under the implicit hypothesis that stock price formation satisfies Markov propriety, i.e.,
under the following mathematical correlation:
 
P(Yt = yt Y1 = y1 , Y2 = y2 . . . , Yt−1 = yt−1 = P(Yt = yt Yt−1 = yt−1 , (10)

According to Equation (10), we have similarly supposed that the error prediction dynamic satisfies
the same Markov propriety. Formerly, if we consider E as the random variable describing the prediction
error evolution, the same correlation reported in Equation (9) is satisfied:
 
P( Et = et E1 = e1 , E2 = e2 . . . ., Et−1 = et−1 = P( Et = et Et−1 = et−1 , (11)

Equation (11) means, in few words, that under the adopted hypothesis (stock price prediction
error dynamic satisfies the Markov propriety) we have the great advantage to use the previous
prediction error et to correct the current stock price forecasting process. Therefore, according to this
clear consequence, we proceed correcting the stock close price prediction performed by our LSTM
system yt as per Equation (9) with a part of previous stock close price prediction error, as reported
below:      
−1
yt = f LSTM ytlstm + p·et−1 = f LSTM ytlstm
−1 t −1
+ p· yreal −1
− ytlstm (12)

where, with the parameter p, we have defined a learning adaptive factor heuristically determined
−1
(we have configured p = 0.5 for this work), while we have defined ytreal as the real stock close price,
t−1
and with ylstm , the predicted ones by second LSTM pipeline (see Equation (9)). Consequently, after
an appropriate training session, this part of the proposed pipeline, will be used as stock close price
forward predictor. Specifically, during market evolution we select a stock and then use collected actual
Computation 2019, 7, 4 8 of 20

stock close price value at time t for forecasting the same share close price at time t + 1. By means of the
made assumption, that price formation satisfies Markov propriety, the so obtained prediction (made
by the second LSTM) will be furtherly corrected by means of the previous prediction error, as per
Equation (12). Meanwhile, the first LSTM pipeline performs mid-long term trend forecast. Considering
that we collect stock market prices at real time (according to adopted timeframe) we have decide to
improve LSTM predictability to update the second LSTM pipeline with already collected real stock
Computation 2018, 6, x FOR PEER REVIEW 8 of 20
close prices. In this way, we improve the prediction accuracy of the second LSTM pipeline because we
update the internal representation of the stock timeserie (acquired during the training session) with
second LSTM pipeline because we update the internal representation of the stock timeserie
real-time data being collected. Afterwards, considering that we have predicted the mid-long stock
(acquired during the training session) with real-time data being collected. Afterwards, considering
trend as well as the next stock close price, we are ready to decide what type of financial operation to be
that we have predicted the mid-long stock trend as well as the next stock close price, we are ready to
performed.
decide whatFor type
this reason, the sooperation
of financial predictedtostock prices (from
be performed. Forsecond LSTM)the
this reason, as well as the predicted
so predicted stock
trend information (first LSTM) will be fed into the Trading System Algorithm
prices (from second LSTM) as well as the predicted trend information (first LSTM) will block to make a decision
be fed into
about
the the tradeSystem
Trading to be done (LONGblock
Algorithm vs SHORT
to makevs aNull Operation)
decision about asthewell as to
trade to estimate
be done related
(LONG trade
vs
parameters
SHORT vssuch NullasOperation)
Take Profitas(pre-defined amount related
well as to estimate of profittrade
for the related trade),
parameters such as Stop Loss
Take (max
Profit
accepted loss for
(pre-defined the analyzed
amount trade),
of profit for theand so on.trade),
related In Figure
Stop5,Loss
an overall scheme loss
(max accepted of the
forwhole proposed
the analyzed
financial
trade), pipeline
and so on. isInreported.
Figure 5, an overall scheme of the whole proposed financial pipeline is reported.

Figure5.5.Overall
Figure Overallscheme
scheme of
of the
the whole proposed
proposedfinancial
financialpipeline.
pipeline.

The Thepipeline
pipeline described
describedininFigure
Figure55isisaa clear
clear graphical representationofofthe
graphical representation thealgorithm
algorithm described
described
tilltill
now, i.e.,i.e.,
now, thethetwo twoLSTM
LSTMpipelines forfor
pipelines mid-long
mid-long term
term trend
trendprediction
prediction(Self
(SelfPredictions
PredictionsLSTM
LSTMasasthe
deeptheneural network
deep neural performs
network mid-long
performs term term
mid-long trendtrend
prediction by recurrent
prediction learning
by recurrent of stock
learning price
of stock
timeserie without without
price timeserie update with realwith
update data real
being collected)
data and stockand
being collected) closestock
priceclose
predictions (Predictions
price predictions
from(Predictions
real data as fromthisreal data as
second this second
LSTM—as LSTM—as
said above—will said perform
above—will performupdate
a real-time a real-time update
of each of
internal
each internal by
representation representation by using
using real stock realdata
prices stock prices
being data being
collected collected
during marketduring market
session) andsession)
the new
andnamed
block the new block named
“Trading System“Trading System
Algorithm” Algorithm”
block, which willblock,bewhich
betterwill be better
described in described in the
the next section.
next section.
4. The Trading System Algorithm Block
4. The Trading System Algorithm Block
In this section, we describe the latest block of the proposed pipeline reported in Figure 5. In the
following In this
Figure section,
6, thewe describe the
functional latest block
pipeline embeddedof the in
proposed pipeline
the trading reported
system blockin is Figure
shown.5. In the
following Figure 6, the functional pipeline embedded in the trading system block is shown.
Computation 2019, 7, 4 9 of 20
Computation 2018, 6, x FOR PEER REVIEW 9 of 20

Figure 6. Trading
Figure 6. System Pipeline.
Trading System Pipeline.

The Thefirst target


first targetofofthis
thisblock
blockisisthe
theanalysis
analysis of of the mid-longterm
the mid-long termtrend trendprediction
prediction made
made bybythethe
first LSTM.
first LSTM. This analysis
This based
analysis on mathematical
based on mathematical check of theofLSTM
check the LSTMoutputoutput(simple derivative
(simple analysis
derivative
based on the first LSTM output curve in order to understand if this curve
analysis based on the first LSTM output curve in order to understand if this curve shows growing or shows growing or decreasing
dynamic)
decreasing willdynamic)
be able to provide
will be ablethe to type of operation
provide the type oftooperation
be performed: LONG (buy
to be performed: LONGthe stock
(buy as thewe
suppose
stock as thewe price will grow
suppose in the
the price future)
will growor inSHORT
the future) (i.e.,or
short
SHORT selling as short
(i.e., we sell the stock
selling as webecause
sell thewe
stock because
suppose the pricewe suppose
will decreasetheinprice
the will decrease
future). Now,intothe future).
confirm theNow,
decided to confirm
LONG the decidedoperation,
or SHORT LONG
or SHORT operation, we proceed evaluating if the value of the predicted
we proceed evaluating if the value of the predicted stock close price (by second LSTM pipeline plus stock close price (by second
LSTMcorrection)
Markov pipeline plus Markov
is greater (orcorrection)
not) than theis greater (or not)
actual stock openthanpricethebeing
actualcollected
stock open fromprice being
real markets.
collected
This comparisonfrom real markets.
is crucial This comparison
to confirm, or not, the is defined
crucial to confirm, or
operation, as not, the defined
previously operation,
described. as
Basically,
previously described. Basically, we performed a LONG operation
we performed a LONG operation if and only if the first LSTM suggests LONG mid-long trend and if and only if the first LSTM
thesuggests
predicted LONG
stockmid-long
close price trend and the
is greater thanpredicted
the actual stock close price
collected open is greater
price than the
otherwise, theactual
system
collected open price otherwise, the system performs a SHORT trade.
performs a SHORT trade. The reason of the above algorithm is very simple to understand: We have a The reason of the above
algorithm is very simple to understand: We have a confirmation of a LONG operation if predicted
confirmation of a LONG operation if predicted stock close price is greater than the stock open price,
stock close price is greater than the stock open price, which basically means that we suppose the
which basically means that we suppose the stock dynamic currently pricing as per open price will
stock dynamic currently pricing as per open price will grow in the future, as the predicted close
grow in the future, as the predicted close price is greater than the open ones. Same reasoning but in the
price is greater than the open ones. Same reasoning but in the opposite sense, for SHORT operation.
opposite sense, for SHORT operation. If there is a mismatch between the LSTM estimated mid-long
If there is a mismatch between the LSTM estimated mid-long term trend and close to open price
term trend andthe
comparison, close to opentrading
proposed price comparison,
system does the not proposed
perform any trading system
operation does
(Null not perform
Operation). Thisany
operation
decision(Null
will beOperation).
done by the This decision
“Decision will beblock
System” doneof bythethepipeline
“Decision System”
reported block of
in Figure the pipeline
6. Finally, in
reported
case of market operation (LONG or SHORT), the proposed pipeline computes the Take Profit pipeline
in Figure 6. Finally, in case of market operation (LONG or SHORT), the proposed (T/P),
computes the Take
i.e., the amount of Profit
money(T/P), i.e., the
you intend amount
to gain in theofopened
moneytrade.you Thisintend to gain will
estimation in the be opened
made in the trade.
This estimation
“T/P Estimation” willblock.
be made in the “T/P
We estimate Estimation”
the T/P by specifying block.
the We estimate
amount of gaintheatT/P
which by values
specifyingof thethe
amount
systemofwillgainclose
at which values
the trade. In of the to
order system willthe
estimate closeTaketheProfit
trade.ofInthe order to estimate
performed trade, theweTake Profit
set the
of following
the performed trade, we set the following variables:
variables:

• • CRV
CRV (InvestmentAmount):
(Investment Amount): Total
Total amount
amount ofof invested
invested money
moneyfor forbuying
buyingororshort selling
short thethe
selling
analyzed
analyzed stocks.
stocks.
• Ns (stock numbers): Number of shares. It is obtained by means of the following formula in
• Ns (stock numbers): Number of shares. It is obtained by means of the following formula in which
which we have indicated, with yopen, the actual stock open price (13):
we have indicated, with yopen , the actual stock open price (13):
𝑪𝑹𝑽 !
𝑵𝒔 = 𝒓𝒐𝒖𝒏𝒅 , (13)
𝒚𝒐𝒑𝒆𝒏
CRV
Ns = round , (13)
• Cs (trade commissions): applied commissions requested yopen by the broker or bank engaged for
executing/managing requested trades. It is often defined as a percentage of the CRV. In our
• (trade commissions):
Csexperiments we supposedapplied commissions
a classical requested
value of 0.19% to beby the broker
applied or bank
to CRV engaged
both during thefor
executing/managing requested
opening and the closing trades. Ittrade.
of the requested is often defined as a percentage of the CRV. In our
experiments we supposed a classical value of 0.19% to be applied to CRV both during the opening
After above steps, we compute the related take profit (TP) of the requested trade at time (t), by
and the closing of the requested trade.
using the following equation set (14):
After above steps, we compute
𝑇𝑃 = 𝜃the∙ 𝑦 related take profit
𝑦 (TP) of∙ 𝑁the requested trade at time
(14) (t),
by using the following equation set (14):
𝑇𝑃 = 𝜃 ∙ 𝑦 𝑦 ∙𝑁 2𝐶 (15)
 
t t
TP = θ · yclose− predicted − yopen−real · Ns (14)
Computation 2019, 7, 4 10 of 20

 
TPnet = θ · ytclose− predicted − ytopen−real · Ns − 2Cs (15)

where θ is heuristic parameter to manage the level of profit, we defined as 0.5 in our experiments.
Clearly, in (15) we report the net profit of the trade according to the broker applied commissions.
After above evaluation, if all described constraints are satisfied, the proposed algorithm decides to open
the trade and then to perform monitoring of the current stock price dynamic. The “P/L Computation”
block is in charge to perform a time-by-time real-time Profit and Loss monitoring of the opened trade
proceeding with an operation closure if the estimated take profit (TP) is reached. In case the TP is not
reached, the proposed trading system performs an automatic closing of the opened trade ad the last
available stock close price taking into account the related stock exchange market (in our experiment we
have referred to Milan Stock Exchange Market, which close at 17.15 p.m. CET time). The above trading
algorithm may be applied to any stocks or markets as well as to any timeframe. Our experiments,
reported in the following section, have been conducted by selecting some stocks exchanged in the
Milan Stock Exchange Market with Daily timeframe.

5. Experimental Results
In our experiments, to implement the described LSTMs framework we used Keras framework [16].
Keras is a high-level neural networks software framework running on top of Google’s Tensorflow
API framework [17]. Moreover, we decided to use Python library for implementing LSTM model.
The experiments were executed on a PC Intel i5 quad-core with 16 Gb of RAM and 1 Tb of
storage. Several experiments were carried out in order to predict the trend direction of each stock.
In the following figures we report several benchmark visual comparisons of the proposed system.
In each figure, we report the stock timeserie, in the analyzed period, with such analytic financial
indicators (better described in the following paragraphs), as well as with first LSTM mid-long term
trend prediction.
In Figures 7–11 the authors report, for each diagram, the real stock trend (blue time-serie) with the
predicted trend-line (red curve). For the sake of comparison, we reported such common mathematical
heuristic financial indicator such as the Simple Moving Average (SMA) over different periods in order
to perform a correct discrimination of the medium and long-term stock trend. As is well known,
prediction of the stock price trend, especially mid-long term trend prediction, is a great challenge as
per the complexity of that market usually affected by chaotic and unpredictable dynamics. For the
reasons previously described, a zero-fault pipeline that is able to predict stock trend is very difficult
to obtain. In that field, the proposed approach shows promising results even though there is, of
course, a great amount of work to be done for improving the obtained performance in trend prediction.
In our experiments, we have noted that for such stock the mid-long term trend prediction seems
accurate (see Figures 8 and 9), while the remaining ones (Figure 7, Figure 10, and Figure 11) shows a
real trend not very compliant with the predicted ones likely due to such financial complex dynamic
and market information not yet included in the LSTM learning, which is basically based to price
movement dynamic only. Anyway, the trading system we have described in Section 4, is able to
compensate the above displacements in trend prediction, as confirmed by the good results we report
in the following figures. As described in the previous Figure 5, the proposed pipeline includes another
LSTM based block for stock price prediction. In the next Figures 12–17, we report experimental
results we obtained on forecasting analyzed stocks time series (close price) by means of second LSTM
layer with prediction price correction based on the assumption of that share price formation satisfies
Markov propriety. In order to show the advantages of the proposed Markov-based price prediction
correction with respect to classical LSTM based prediction system, in Figures 13 and 15 the authors
show the same stock price prediction reported in Figures 12 and 14, respectively, but without the
described Markov-based correction. Moreover, we report such benchmark comparisons for showing
the robustness and effectiveness of the proposed prediction pipeline.
Computation 2019, 7, 4 11 of 20
Computation 2018, 6, x FOR PEER REVIEW 11 of 20
Computation 2018, 6, x FOR PEER REVIEW 11 of 20

Figure
Figure 7.
7.UCG.MI
UCG.MITrend
TrendDirection
Direction(red) and
(red) SMA.
and SMA.
Figure 7. UCG.MI Trend Direction (red) and SMA.

Figure 8.
Figure BMPS.MITrend
8.BMPS.MI TrendDirection
Direction(red)
(red) and
and SMA.
SMA.

Figure 8. BMPS.MI Trend Direction (red) and SMA.


Computation 2018, 6, x FOR PEER REVIEW 12 of 20
Computation 2019, 7, 4 12 of 20
Computation 2018, 6, x FOR PEER REVIEW 12 of 20

Figure
Figure 9.
Figure CVAL.MI
9. CVAL.MI Trend
CVAL.MITrend
Trend Direction
Direction
Direction (red)
(red) and
andand
(red) SMA.
SMA.
SMA.

Figure 10. ISP.MI Trend Direction (red) and SMA.

Figure 10. ISP.MI Trend


Trend Direction
Direction (red)
(red)and
andSMA.
SMA.
Computation 2018, 6, x FOR PEER REVIEW 13 of 20
Computation 2019, 7, 4 13 of 20
Computation 2018, 6, x FOR PEER REVIEW 13 of 20
Computation 2018, 6, x FOR PEER REVIEW 13 of 20

Figure 11. ENEL.MI Trend Direction (red) and SMA.

Figure11.
Figure
Figure 11.ENEL.MI
11. ENEL.MI Trend
ENEL.MI Trend Direction (red)
Trend Direction (red)and
andSMA.
SMA.

Figure 12. BMPS.MI Close Price time-serie (red: predicted; blue: real value).
Figure 12.BMPS.MI
Figure12. BMPS.MIClose
Close Price
Price time-serie (red:predicted;
time-serie (red: predicted;blue:
blue:real
realvalue).
value).
Figure 12. BMPS.MI Close Price time-serie (red: predicted; blue: real value).

Figure 13. BMPS.MI Close Price time-serie (red: predicted without Markov correction; blue: real value).
Figure
Figure 13.13.BMPS.MI
BMPS.MIClose
ClosePrice
Price time-serie
time-serie (red: predicted
predicted without
withoutMarkov
Markovcorrection;
correction;blue: real
blue: real
value).
value).
Figure 13. BMPS.MI Close Price time-serie (red: predicted without Markov correction; blue: real
value).
Computation 2019, 7, 4 14 of 20
Computation 2018,
Computation 2018, 6,
6, xx FOR
FOR PEER
PEER REVIEW
REVIEW 14 of
14 of 20
20

Figure 14.
Figure 14. ISP.MI
ISP.MI Close
Close Price
Price time-serie
time-serie (red: predicted;
predicted; blue: real
real value).
Figure 14. ISP.MI Close Price time-serie (red:
(red: predicted;blue:
blue: realvalue).
value).

Figure
Figure 15.
15.15.
Figure ISP.MI
ISP.MI
ISP.MI ClosePrice
Close
Close Pricetime-serie
Price time-serie(red:
time-serie (red: predicted
(red: predicted without
predicted without Markov
withoutMarkov correction;
Markovcorrection; blue:
correction;blue: real
blue: value).
real
real value).
value).

Figure16.
Figure
Figure 16.CVAL.MI
16. CVAL.MIClose
CVAL.MI ClosePrice
Close Price time-serie
Price time-serie (red:
time-serie (red: predicted;
(red:predicted; blue:
predicted;blue: real
blue:real value).
realvalue).
value).
Computation 2019, 7, 4 15 of 20
Computation 2018, 6, x FOR PEER REVIEW 15 of 20

Figure 17.UCG.MI
Figure17. UCG.MIClose
ClosePrice
Price time-serie (red: predicted;
time-serie (red: predicted;blue:
blue:real
realvalue).
value).

AsAspreviously
previouslymentioned,
mentioned,one oneof of the
the challenging targetfor
challenging target forthe
thepipeline
pipelineherein
hereindescribed
described is to
is to
provide an efficient mechanism for stock price prediction. The proposed
provide an efficient mechanism for stock price prediction. The proposed method shows satisfactory method shows satisfactory
results when
results when doing
doingshares price
shares forecasting
price forecasting so that we decided
so that we decided to report such such
to report type of numerical
type metrics
of numerical
in order
metricstoinexhibit
order totheexhibit
proper thebenchmark comparison
proper benchmark of that capability.
comparison of that capability.
The analyzed stock price prediction benchmark metrics are reported in Tables 1 and 2. More in
The
detail, in analyzed
Table 1, we stock
reportprice prediction
the average Rootbenchmark
Mean Squaremetrics are (RMSE)
Error reportedfor in Tables 1 and
the single 2. Morestock
analyzed in
detail, in Table 1, we report the average Root Mean Square Error (RMSE)
both in case of we apply the correction based on the assumption of price formation satisfies Markov for the single analyzed
stock both
propriety (RMSE in case of)we apply the correction based on the assumption of price formation satisfies
update and without that correction, i.e., as it is performed by the trained LSTM only.
Markov propriety (RMSE
From a simple analysis of the update) and without that correction, i.e., as it is performed by the trained
data reported in Table 1, the assumption made by the author improves,
LSTM only. From a simple analysis of the data reported in Table 1, the assumption made by the
significantly, the overall prediction performance of the proposed approach. In Table 2, the authors
author improves, significantly, the overall prediction performance of the proposed approach. In
report a further statistical comparisons index: The variance computed for the target stock price, as well
Table 2, the authors report a further statistical comparisons index: The variance computed for the
as for the corresponding predicted ones (both with proposed Markov based correction “update” —and
target stock price, as well as for the corresponding predicted ones (both with proposed Markov
without that- “no update”), for each analyzed stock, in order to confirm how effective is our prediction
based correction “update” —and without that- “no update”), for each analyzed stock, in order to
model. As for Table 1, Table 2 reported statistic evaluations, confirm the robustness of the proposed
confirm how effective is our prediction model. As for Table 1, Table 2 reported statistic evaluations,
approach.
confirm The benchmarks
the robustness of reported
the proposedin Tables 1 and 2The
approach. provide such statistic
benchmarks reported comparisons
in Tables 1related
and 2 to
stock price such
provide forecasting.
statisticIncomparisons
order to improve related that
to benchmark
stock price comparisons,
forecasting. Inweorder decided to evaluate
to improve thatthe
performance of the proposed approach by means of common robust
benchmark comparisons, we decided to evaluate the performance of the proposed approach byindexes used in scientific literature
formeans
this purpose.
of common Therobust
first comparison
indexes usedindex we have
in scientific used isfor
literature described in Reference
this purpose. The first[18] where the
comparison
authors
index presented
we have used an innovative
is described LSTM-based
in Reference approach to predict
[18] where financialpresented
the authors indexes with comparable
an innovative
prediction
LSTM-basederrors. In Reference
approach [18],financial
to predict the authors showed
indexes with an interesting
comparable benchmark
prediction comparison
errors. In Reference index
suitable to authors
[18], the evaluateshowed
the stockan price prediction
interesting accuracy.
benchmark In orderindex
comparison to evaluate
suitable the
toproposed
evaluate theprediction
stock
price prediction
algorithm of ups (LONGaccuracy. In order
trend to evaluate
also knows the proposed
as “Bullish”) prediction
and downs (SHORTalgorithm
trend of also
ups known
(LONG as
trend alsothe
“Bearish”), knows
authorsas “Bullish”)
decided toand downs data
calculate (SHORT trend by
accuracy also known
using the asset“Bearish”),
of equation thereported
authors in
decided (16)
Equations to calculate
and (17).data accuracy by ( using the set of equation reported in Reference (16) and
Reference (17). 1, (yi+1 − yi ) > 0
y= (16)
0, (𝑦 otherwise
1, 𝑦) 0
𝑦= (16)
0, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
1 N
DA =
DA = ∑∑ 𝑎ai (17)(17)
N i =1
where 𝒚𝒊 , 𝒚𝒊 𝟏 represents the real share price at time i and i + 1 respectively and, than, 𝒚
where yi , yi+1 represents the real share price at time i and i + 1 respectively and, than, y represents
represents the actual financial trend of the analyzed stock. We apply the same clustering as per
the actual financial trend of the analyzed stock. We apply the same clustering as per Equation (16) to
Reference (16) to the set of predicted values. Obviously, the term 𝒂𝒊 (1: if the predicted trend is
thecorrect
set of 0:
predicted
otherwise) values. Obviously,
represents an average term ai (1: metric
the benchmark if the predicted trend trends.
of the predicted is correct 0: otherwise)
represents an average benchmark metric of the predicted trends.
Computation 2019, 7, 4 16 of 20

Table 1. ENEL.MI, UCG.MI, BMPS.MI, CVAL.MI, and ISP.MI RMSE normalized in [0, 1] while enclosed
in bracket corresponding not normalized.

ENEL.MI UCG.MI BMPS.MI CVAL.MI ISP.MI


RMSEno_update 0.030 (0.08132) 0.0064 (0.75) 0.0053 (30.0152) 0.0067 (0.068) 0.027 (0.06787)
RMSEupdate 0.028 (0.07633) 0.0060 (0.70) 0.0052 (28.069) 0.0060 (0.064) 0.025 (0.0631)

Table 2. ENEL.MI, UCG.MI, BMPS.MI, CVAL.MI, and ISP.MI Variance normalized in [0, 1] while
enclosed in bracket corresponding not normalized

ENEL.MI UCG.MI BMPS.MI CVAL.MI ISP.MI


VARIANCEtarget 0.0923 (0.66) 0.00362 (47,6) 0.004569 (147786) 0.0055 (0.27) 0.0732 (0.4542)
VARIANCEno_update 0.092 (0.65) 0.00357 (46.5) 0.00463 (147812) 0.0054 (0.2451) 0.0729 (0.4504)
VARIANCEupdate 0.09221 (0.66) 0.00360 (46.7) 0.00462 (147619) 0.0055 (0.2456) 0.0730 (0.4509)

The accuracy reached by authors in Reference [18] is in the range between 43% and 58% applied
in the Shanghai Eastern market. The prediction accuracy of the proposed method is reported in Table 3.
With a benchmark DA index in the range 50% to 99% our method confirms the robustness and the
efficiency on stock price prediction.

Table 3. ENEL.MI, UCG.MI, BMPS.MI, CVAL.MI, and ISP.MI Data Accuracy (DA) results.

ENEL.MI UCG.MI BMPS.MI CVAL.MI ISP.MI


Data Accuracy 0.501813 0.999412 0.510281 0.99402 0.50

At the end, the authors provide a further robustness benchmarks usually used in the financial
market, i.e., the si called drawdown. It is so defined in Reference [19]: “A drawdown is the
peak-to-trough decline during a specific recorded period of an investment, fund or commodity security.
A drawdown is usually quoted as the percentage between the peak and the subsequent trough.”
We performed a drawdown measure check of the proposed pipeline. The results are reported in
Table 4.

Table 4. ENEL.MI, UCG.MI, BMPS.MI, CVAL.MI, and ISP.MI. Maximum Drawdown (%) results.

ENEL.MI UCG.MI BMPS.MI CVAL.MI ISP.MI


Maximum Drawdown −9.2350 −3.3345 −11.0911 −4.0011 −10.9876

If we compare Maximum Drawdown results reported in Table 4 with similar ones reported in a
recent algorithm based on Machine Learning technique [20], we easily understand that our proposed
approach shows acceptable drawdown as it exposes almost 11 % of the total amount of investment
which is a very promising result in the field of financial sustainable trading system [20]. Finally,
the results we have obtained in stock price prediction by means of the proposed pipeline confirms the
robustness and the effectiveness of the approach are herein described, getting up the overall accuracy
of the proposed prediction system based on the LSTM only. The benchmark comparison reported
in Tables 1–4 confirms the mentioned advantages. Now, in order to validate the performance of the
overall proposed system including the trading pipeline block, we have applied, as described in the
previous section, our proposed pipeline for trading several stocks in the Milan Stocks Exchange Market.
In Figures 18–22, the reported results of classical Profit and Loss Diagram (P&L) computed in EURO
currency (y axes) are shown.
Computation 2019, 7, 4 17 of 20
Computation
Computation 2018,
Computation 2018, 6,
2018, 6, xxx FOR
6, FOR PEER
FOR PEER REVIEW
PEER REVIEW
REVIEW 17
17 of
17 of 20
of 20
20

Figure
Figure 18.
Figure 18. UCG.MI
18. UCG.MI Profit
UCG.MI Profit and
Profit and Loss
and Loss Diagram.
Loss Diagram.
Diagram.

Figure
Figure 19.
Figure 19. CVAL.MI
19. CVAL.MI Profit
CVAL.MI Profit and
Profit and Loss
and Diagram.
Loss Diagram.
Loss Diagram.

Figure
Figure 20.
20. BMPS.MI
20. BMPS.MI Profit
Profit and
Profit and Loss
and Loss Diagram.
Loss Diagram.
Diagram.
Computation 2019, 7, 4 18 of 20
Computation 2018,
Computation 2018, 6,
6, xx FOR
FOR PEER
PEER REVIEW
REVIEW 18 of 20
18 of 20

Figure
Figure 21.
Figure 21. ISP.MI
21. ISP.MI Profit and
ISP.MI Profit and Loss
Loss Diagram.
Diagram.

Figure 22.
Figure 22. ENEL.MI
ENEL.MI Profit
Profit and
and Loss
Loss Diagram.
Diagram.

The
The above
The abovefigures
above figuresshow
figures show
showa very promising
aa very
very P/L trend
promising
promising with an
P/L trend
P/L trend encouraging
with
with profit trend-line
an encouraging
an encouraging together
profit trend-line
profit trend-line
with a contained
together
together with drawdown
with aa contained
contained as reported
drawdown
drawdown in Tablein
as reported
as reported 4. Table
in Both 4.
Table results
Both obtained
4. Both for such
results obtained
results obtained for
fordifferent stocks
such different
such different
confirm
stocks the
confirmrobustness
the and
robustnessthe effectiveness
and the of the
effectiveness proposed
of the approach
proposed because
approach
stocks confirm the robustness and the effectiveness of the proposed approach because it is able it is
because able
it to
is findto
able a
to
good trade-off
find aa good
find between
good trade-off profit
trade-off between and
between profitrisk as
profit and the
and risk maximum
risk as
as the drawdown
the maximum is
maximum drawdown really
drawdown is sustainable
is really for
really sustainablethis type
sustainable for for
of investment.
this type of investment.
this type of investment.
6. Conclusions and
6. Conclusions
Conclusions and Future
Future Works
Works
6. and Future Works
In this
In this paper,
this paper, we
paper, we presented
presented aaa Markov
we presented Markov Propriety-based
Propriety-based machine
machine learning method
learning method
method for for stock
for stock price
stock price
price
In Markov Propriety-based machine learning
close prediction
close prediction
prediction withwith related
with related trading
related trading system.
trading system. Specifically,
system. Specifically, we
Specifically, we implemented
we implemented a robust
implemented aa robust algorithm
algorithm the
robust algorithm for for
close for
stock
the trend
stock and
trend price
and prediction
price predictionby using
by LSTM
using LSTM neural networks
neural networksto show
to show that Machine
that Machine Learning
Learning is
the stock trend and price prediction by using LSTM neural networks to show that Machine Learning
very effective
is very
very effective to extract
effective to useful
to extract
extract usefulinformation
useful information from
information from stock market.
from stock The
stock market. contribution
market. The The contributionof our
contribution of work
of our reaches
our work
work
is
multiple
reaches goals. Firstly,
multiple goals. we defined
Firstly, we a propera stochastic
defined proper methodmethod
stochastic to improve to the accuracy
improve the of the LSTM
accuracy of the
reaches multiple goals. Firstly, we defined a proper stochastic method to improve the accuracy of the
prediction
LSTM capability
prediction (by means
capability (by of the of
means usedthe Markov
used based based
Markov correction). Moreover,
correction). we have
Moreover, we used
have
LSTM prediction capability (by means of the used Markov based correction). Moreover, we have
LSTM
used LSTMcapability
LSTM to addto
capability more
addinformation about mid-long
more information
information term trend.
about mid-long
mid-long term Intrend.
this way, we have
In this
this way,improved
we have
have
used capability to add more about term trend. In way, we
drastically
improved the robustness
drastically the of the proposed
robustness of approach
the as
proposedwell as we
approachhave as reduced,
well as significantly,
we have the risk
reduced,
improved drastically the robustness of the proposed approach as well as we have reduced,
of the single trade
significantly, as theofsystem
the risk
risk performs
the single
single tradetheasfinancial
the systemoperations
system performsundertheappropriate conditions under
financial operations
operations (same
significantly, the of the trade as the performs the financial under
direction
appropriate between predicted
conditions trend direction
(same and openedbetween
trade). The applications
predicted trend in which the proposed
and opened
opened method
trade). The
appropriate conditions (same direction between predicted trend and trade). The
can be applied
applications in are really
which the so many
proposed and they
method can
cangobebeyond
applied the financial
are really market
so many field.
and We
they want
can to
go
applications in which the proposed method can be applied are really so many and they can go
apply the
beyond the proposed
the financial pipeline
financial market for
market field. improving
field. We
We want trading
want to system
to apply
apply the profitability
the proposed of
proposed pipeline many
pipeline for banking
for improvingand financial
improving trading
trading
beyond
institutions, as well asofformany
system profitability
profitability stockbanking
and financial indexes forecasting
and financial
financial because
institutions, as well
wellthose predicted
as for
for stock data
stock and are often
and financial
financial
system of many banking and institutions, as as
indexes forecasting
indexes forecasting because
because those
those predicted
predicted data
data are
are often
often used
used for
for company
company market market analysis
analysis or or for
for
Computation 2019, 7, 4 19 of 20

used for company market analysis or for taking main decisions in the field of economic and monetary
policy. In order to improve the availability of the proposed approach, the authors are working to port
the proposed algorithms within the STM32 based embedded platform [21] in order to have a standalone
device for performing the aforementioned market analysis. Anyway, the proposed approach may be
further improved in terms of predication capability and profitability. For this reason, the authors are
evaluating such bio-inspired algorithms successfully used in some medical applications [22–24] for
extracting ad-hoc features from financial time-series. The preliminary results are very encouraging and
we hope to present such results in a following work. As previously reported, a methodology that could
improve significantly the prediction capability of the proposed approach is based on social sentiment
analysis, as described in Reference [8]. The authors observed that sentiment signals can influence the
market price movements. An idea on which the authors are working consists in considering daily
news in order to predict the stock pricing. Preliminary results confirm that this strategy can drastically
improve the overall stock price and trend predictability. According to the results herein obtained,
ad-hoc financial sentiment analysis algorithm is being developed to be added to our pipeline in order
to improve the overall profitability and predictability of the proposed pipeline. Future works will
focalize on intelligent integration between the analytic pipelines, herein described with sentiment
analysis ones. One of the most recent work about this topic is described in Reference [25] from which
the authors is started for current development.

Author Contributions: Conceptualization & Investigation, F.R.; Validation, F.T.; Writing-Review & Editing, S.B.
and A.L.D.S.
Funding: This research received no external funding.
Conflicts of Interest: The authors declare no conflicts of interest.

References
1. Kim, K.; Han, I. Genetic Algorithms Approach to Feature Discretization in Artificial Neural Networks for
the prediction of Stock Price Index. Expert Syst. Appl. 2000, 19, 125–132. [CrossRef]
2. Kimoto, T.; Asakawa, K.; Yoda, M. Stock Market Prediction System with Modular Neural Networks.
In Proceedings of the 1990 IJCNN International Joint Conference on Neural Networks, San Diego, CA,
USA, 17–21 June 1990.
3. Wang, J.-H.; Leu, J.-Y. Stock market trend prediction using ARIMA-based neural networks. In Proceedings
of the International Conference on Neural Networks (ICNN’96), Washington, DC, USA, 3–6 June 1996.
4. Niaki, S.T.A.; Hoseinzade, S. Forecasting s&p 500 index using artificial neural networks and design of
experiments. J. Ind. Eng. Int. 2013, 9, 1.
5. Nelson, D.M.Q.; Pereira, A.C.M.; de Oliveira, R.A. Stock market’s price movement prediction with LSTM
neural networks. In Proceedings of the 2017 International Joint Conference on Neural Networks (IJCNN),
Anchorage, AK, USA, 14–19 May 2017.
6. Zhang, Z.; Shen, Y.; Zhang, G.; Song, Y.; Zhu, Y. Short-term Prediction for Opening Price of Stock
Market Based on Self-adapting Variant PSO-Elman Neural Network. In Proceedings of the 2017 8th IEEE
International Conference on Software Engineering and Service Science (ICSESS), Beijing, China, 24–26
November 2017.
7. Kumar, I.; Dogra, K.; Utreja, C.; Yadav, P. A Comparative study of supervised machine learning algorithms
for stock market trend prediction. In Proceedings of the 2018 Second International Conference on Inventive
Communication and Computational Technologies (ICICCT), Coimbatore, India, 20–21 April 2018.
8. Bollen, J.; Mao, H. Twitter mood as a stock market predictor. Computer 2011, 44, 91–94. [CrossRef]
9. Gers, F.A.; Schmidhuber, J. LSTM Recurrent Neural Networks Learn Simple Context Free and Context
Sensitive Languages. IEEE Trans. Neural Netw. 2001, 12, 1333–1340. [CrossRef] [PubMed]
10. Diebold, F.X. Elements of Forecasting, 4th ed.; Thomson Learning: Belmont, CA, USA, 2007.
11. Granger, C.; Newbold, P. Forecasting Transformed Series. J. R. Stat. Soc. 2001, 38, 189–203. [CrossRef]
12. Gers, F.A.; Schraudolph, N.N.; Schmidhuber, J. Learning precise timing with LSTM recurrent networks.
J. Mach. Learn. Res. 2003, 3, 115–143.
Computation 2019, 7, 4 20 of 20

13. Savvopoulos, A.; Kanavos, A.; Mylonas, P.; Sioutas, S. LSTM Accelerator for Convolutional Object
Identification. Algorithms 2018, 11, 157. [CrossRef]
14. Yahoo Finance. Available online: https://it.finance.yahoo.com/ (accessed on 24 October 2018).
15. Somani, P.; Talele, S.; Sawant, S. Stock Market Prediction Using Hidden Markov Model. In Proceedings of
the 7th Joint International Information Technology and Artificial Intelligence Conference, Chongqing, China,
20–21 December 2014.
16. Keras Documentation. Available online: https://keras.io/ (accessed on 24 October 2018).
17. Tensorflow.org. Available online: https://www.tensorflow.org/ (accessed on 24 October 2018).
18. Pang, X.; Zhou, Y.; Wang, P.; Lin, W.; Chang, V. An innovative neural network approach for stock market
prediction. J. Supercomput. 2018, 1–21. [CrossRef]
19. Investopedia. Available online: https://www.investopedia.com/terms/d/drawdown.asp (accessed on
17 December 2018).
20. Reid, D.; Hussain, A.J.; Tawfik, H. Spiking Neural Networks for Financial Data Prediction. In Proceedings of
the 2013 International Joint Conference on Neural Networks (IJCNN), Dallas, TX, USA, 4–9 August 2013.
21. Artificial Intelligence (AI)—STMicroelectronics. Available online: https://www.st.com/content/st_com/
en/about/innovation---technology/artificial-intelligence.html (accessed on 17 December 2018).
22. Rundo, F.; Conoci, S.; Ortis, A.; Battiato, S. An Advanced Bio-Inspired PhotoPlethysmoGraphy (PPG) and
ECG Pattern Recognition System for Medical Assessment. Sensors 2018, 18, 405. [CrossRef] [PubMed]
23. Rundo, F.; Ortis, A.; Battiato, S.; Conoci, S. Advanced Bio-Inspired System for Noninvasive Cuff-Less Blood
Pressure Estimation from Physiological Signal Analysis. Computation 2018, 6, 46. [CrossRef]
24. Rundo, F.; Conoci, S.; Banna, G.L.; Ortis, A.; Stanco, F.; Battiato, S. Evaluation of Levenberg–Marquardt
neural networks and stacked autoencoders clustering for skin lesion analysis, screening and follow-up.
IET Comput. Vis. 2018, 12, 957–996. [CrossRef]
25. Ortis, A.; Farinella, G.M.; Torrisi, G.; Battiato, S. Visual Sentiment Analysis Based on Objective Text
Description of Images. In Proceedings of the International Conference on Content-Based Multimedia
Indexing (CBMI), La Rochelle, France, 4–6 September 2018.

© 2019 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access
article distributed under the terms and conditions of the Creative Commons Attribution
(CC BY) license (http://creativecommons.org/licenses/by/4.0/).

You might also like