AI algorithms and models are around for quite some time now. I got curious to check how good they are in predicting stock prices. Are they any better in recommending BUY and SELL signals compared to technical analysis used today?

Stock trading community over the past decade has invented multiple technical analysis to predict stock price without worrying about underlying fundamentals and financials of the company. In a way, these technical analysis algorithms are like AI which could predict stock prices in the future. These algorithms are rule-based algorithms and could analyse based on a fixed set of relations only. On the other hand, ML and Deep Learning algorithms are devised to identify hidden complex patterns in data which are hard to identify and difficult to comprehend compute for a human mind.

By doing my limited research (googling) on time series models I shortlisted three models:

  • Prophet
  • Long Short-Term Memory (LSTM)
  • Convolutional Neural Network (CNN)

Across multiple articles, people mention that stock price prediction as one of the ideal use cases for LSTM but not many details on how good it is. I came across a paper "NSE Stock Market Prediction Using Deep-Learning Models" ( which compares results from various models and concludes CNN is better than LSTM. Prophet model from Facebook seems like a new kid on the block and Facebook claims it is a much efficient model for time series prediction.

I decided to check out Prophet model first, primarily because a stock prediction library, Stocker, is readily available with this model. I had to make a few changes to 'Stocker' library for 'yahoo finance' data source and function parameters.

In this example, I am using "Asian Paints" stock price from NSE stock exchange. I am not just trying to predict the price in future, but to give out recommendations to BUY and SELL.

Exploring price movement

Lets plot the price movement and volume variations in this stock in from 2016 years.

In this particular stock, we see the "Price Movement", fig 1, price dipped by late 2016, moved sideways in 2017 and goin through volatality in 2018. On volume data, fig 2, we see occational spike in volume and most of the time the volume traded are between 1M to 2M.

In the last 6 years, this stock has delived 260% profit. And in last 3 years this stock has delivered about 60% profit.

Setting Baselines

Profit by Long Term Investment

If we have invested in this stock for a long term knowing that it will fair well for sure in the longer term, the following would be the total profit.

Price at start of 2018: 1135.55, Price Today: 1400.95 
Profit %: 23.37

Profit by Time Machine

Lets say if have a time machine and using which we know exactly when the price will raise and fall. In such a case if we have invested in every possible BUY and sold at the correct SELL point, following would be the total profit.

Also, since we are talking about machines calling the shots, lets put a cap on exposure duration to minimize the risk. In this example I have used 45 days as the cap, meaning a trade will be open no more than 45 days.

Buy on: 2018-01-01, Sell on:2018-01-05, Profit: 41.60
Buy on: 2018-02-07, Sell on:2018-04-16, Profit: 57.75
Buy on: 2018-04-17, Sell on:2018-06-19, Profit: 93.20
Buy on: 2018-06-20, Sell on:2018-07-24, Profit: 196.45
Buy on: 2018-09-24, Sell on:2018-11-29, Profit: 78.35
Buy on: 2018-11-30, Sell on:2019-02-01, Profit: 111.25
Total Profit with time machine from year 2018: 46.59

Profit by 'Prophet'

Lets see if we can device a model which would have got us better results than the "Profit by Long Term Investment" numbers and setting "Profit by Time Machine" maximum extent.

We want to identify optimal BUY and SELL points for the stock so that, cumulative trade gives better returns. Its not just about beating long term investment, its about minimising the risk. Since the trade possition is held only for 45 days max, the risk exposure it also minimised.

Changepoint Tuning

By Setting changepoint_prior_scale to 0.00010 for this stock, model seems to be giving better results compared to default values.


Prediction Range: 2018-03-07 to 2019-03-07.

Predicted price on 2019-03-06 = $1343.49.
Actual price on    2019-03-06 = $1393.70.

Average Absolute Error on Training Data = $41.72.
Average Absolute Error on Testing  Data = $70.93.

When the model predicted an increase, the price increased 51.59% of the time.
When the model predicted a  decrease, the price decreased  55.00% of the time.

The actual value was within the 80% confidence interval 48.18% of the time.

The absolute error on testing data is arround Rs. 70, not a bad prediction. But the increase and decrese prediction is just about 50%, way below the expectation, it is as good a coin flip. So lets not use the direction to predict the BUY/SELL recomendation but rather use the price prediction for n days to make the call.

Predict BUY & SELL

Lets train the model using price movement till 2017 and predict the BUY and SELL recomendation for duration Jan-2018 to Feb-2019.

Buy on: 2018-01-02, Sell on:2018-01-09, Profit: 50.30
Buy on: 2018-01-24, Sell on:2018-02-05, Profit: -16.95
Buy on: 2018-02-06, Sell on:2018-04-13, Profit: 51.95
Buy on: 2018-04-16, Sell on:2018-06-18, Profit: 103.20
Buy on: 2018-06-19, Sell on:2018-07-18, Profit: 123.80
Buy on: 2018-09-03, Sell on:2018-11-09, Profit: -66.00
Buy on: 2018-11-12, Sell on:2018-11-20, Profit: 28.05

Profit %: 23.462913123976044


Using 'Prophet' to recommend BUY/SELL triggers, we would have got about 23% profit in the last year. This is effectively the returns we would have got from long term investment.

Even though this model doesn't fare any better than the long term investment, this would minimise the risk by reducing the exposure time. So there is some advantage from this model but not as compelling to take this recommendation any seriously.

Let's check out how other models fare for this data set.



Stock Trade Recommendation Using Prophet