Predicting NSE Stocks using Prophet

FB data science team saw it forthcoming the age of data. They created their prophet to forecast data. While its use is not limited to stocks its a handy tool for anyone trying to understand number and movement. According to  them, Prophet was made to:

make it easier for experts and non-experts to make high-quality forecasts that keep up with demand

FB

Where Prophet shines

Not all forecasting problems can be solved by the same procedure. Prophet is optimized for the business forecast tasks we have encountered at Facebook, which typically have any of the following characteristics:

  • hourly, daily, or weekly observations with at least a few months (preferably a year) of history
  • strong multiple “human-scale” seasonalities: day of week and time of year
  • important holidays that occur at irregular intervals that are known in advance (e.g. the Super Bowl)
  • a reasonable number of missing observations or large outliers
  • historical trend changes, for instance, due to product launches or logging changes
  • trends that are non-linear growth curves, where a trend hits a natural limit or saturates

How Prophet works

At its core, the Prophet procedure is an additive regression model with four main components:

  • A piecewise linear or logistic growth curve trend. Prophet automatically detects changes in trends by selecting changepoints from the data.
  • A yearly seasonal component modeled using Fourier series.
  • A weekly seasonal component using dummy variables.
  • A user-provided list of important holidays.

y(t) = g(t) + s(t) + h(t) + ϵ

  • g(t) models trend, which describes a long-term increase or decrease in the data. Prophet incorporates two trend models, a saturating growth model, and a piecewise linear model, depending on the type of forecasting problem.
  • s(t) models seasonality with Fourier series, which describes how data is affected by seasonal factors such as the time of the year (e.g. more searches for eggnog during the winter holidays)
  • h(t) models the effects of holidays or large events that impact business time series ϵ. represents an irreducible error term

Using Prophet in Python

Setup

Start by importing all the necessary libraries. If you don’t already have Prophet installed, you can easily install it with pip. 

Pip install  fbprophet

If you are getting the following error while using Jupiter

Use command

conda install -c conda-forge fbprophet

Import the packages

import nsepy as nse 
 from nsetools import Nse
 import datetime
 import urllib3
 import random
 import numpy as np
 from fbprophet import Prophet
 import pandas as pd

Import Stock data for analysis

nsel = Nse()
 stock_list = nsel.get_stock_codes()
 stock_list= {v: k for k, v in stock_list.items()}
 Today = datetime.datetime.now()
 number_of_company = int(input('En|tert the number of company:\t')) # taking the number of companies to predict 
 company_list =[]
 for _ in range(number_of_company):
     name = input('Enter the Company to Predict\t:\t')
     company_list.append(name)
     numbered = list(company_list)
 stock_data = list(company_list)
 for i in range(number_of_company):
     # obtaining the data from NSE website
     stock_data[i] = nse.get_history(symbol=company_list[i], start=datetime.datetime(2017,1,1), end=datetime.datetime(2019,6,13))

Fitting the data into Prophet

For Prophet to run there are two essential columns required  are dy which is essentially date and y which is the value required

for each_stock in stock_data:
         each_stock.drop(['Trades','Volume','%Deliverble','VWAP','Turnover','Last','Prev Close','Series','Symbol'], axis =1,inplace =True)
         df = each_stock.sort_values('Date')
         df['date'] = pd.date_range(start=datetime.datetime(2017,1,1), periods=len(df),end=datetime.datetime(2019,6,13))
         df['y']=(df['High']+df['Low'])/2
         df['ds']=df['date']
         model = Prophet()
         model.fit(df);

Let Prophet do its work

future = model.make_future_dataframe(periods=180) #forecasting for 1 year from now.
        forecast = model.predict(future)
        figure=model.plot(forecast)
        
        fig2 = model.plot_components(forecast)
        

Here Fig2 is used to break down the output into its core compoonents

Output

Here the trend represents the overall trend of the stock. Weekly represents the cyclic nature in a weekly way and yearly tells us the cyclic nature in a year.

Full Code

import nsepy as nse 
from nsetools import Nse
import datetime
import urllib3
import random
import numpy as np
from fbprophet import Prophet
import pandas as pd
import requests
import import_ipynb
import pre as preprocessing
import matplotlib.pyplot as plt
import tensorflow as tf 
from keras.models import Sequential
from sklearn.preprocessing import MinMaxScaler
from keras.layers.recurrent import LSTM
from fbprophet.plot import plot_cross_validation_metric
from keras.layers import Dense, Dropout,Activation
from sklearn.metrics import mean_squared_error
import math
sess = requests.Session()
adapter = requests.adapters.HTTPAdapter(pool_connections=100, pool_maxsize=100)
sess.mount('http://', adapter)
resp = sess.get("http://www.nseindia.com")
nsel = Nse()
stock_list = nsel.get_stock_codes()
stock_list= {v: k for k, v in stock_list.items()}
Today = datetime.datetime.now()
number_of_company = int(input('En|tert the number of company:\t')) # taking the number of companies to predict 
company_list =[]
for _ in range(number_of_company):
    name = input('Enter the Company to Predict\t:\t')
    company_list.append(name)
    numbered = list(company_list)
stock_data = list(company_list)
for i in range(number_of_company):
    # obtaining the data from NSE website
    stock_data[i] = nse.get_history(symbol=company_list[i], start=datetime.datetime(2017,1,1), end=datetime.datetime(2019,6,13))
    for each_stock in stock_data:
        each_stock.drop(['Trades','Volume','%Deliverble','VWAP','Turnover','Last','Prev Close','Series','Symbol'], axis =1,inplace =True)
        df = each_stock.sort_values('Date')
        df['date'] = pd.date_range(start=datetime.datetime(2017,1,1), periods=len(df),end=datetime.datetime(2019,6,13))
        df['y']=(df['High']+df['Low'])/2
        df['ds']=df['date']
        model = Prophet()
        model.fit(df);
        
        future = model.make_future_dataframe(periods=180) #forecasting for 1 year from now.
        forecast = model.predict(future)
        figure=model.plot(forecast)
        
        fig2 = model.plot_components(forecast)