# 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

#### 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:
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()
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:
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)

``````

## 12 thoughts on “Predicting NSE Stocks using Prophet”

1. Many thanks for your post. I’d prefer to comment that the cost of car insurance differs from one insurance policy to another, for the reason that there are so many different facets which bring about the overall cost. For example, the model and make of the motor vehicle will have a tremendous bearing on the price tag. A reliable aged family automobile will have a less expensive premium compared to a flashy sports vehicle.

1. Rahula raj says:

Yes, I agree when one takes into account different segment there are both event-based and historical effects. However, I would point out that using the prophet we are trying to find out the cyclicity of the data which tends to account for all the effects and it also takes into account the sudden changes by using dummy variables.

2. I also believe that mesothelioma is a rare form of melanoma that is normally found in these previously exposed to asbestos. Cancerous tissues form from the mesothelium, which is a shielding lining which covers most of the body’s internal organs. These cells usually form inside the lining of the lungs, stomach, or the sac that really encircles one’s heart. Thanks for discussing your ideas.

3. I wish to show thanks to the writer for bailing me out of this predicament. As a result of browsing through the the net and obtaining proposals which were not productive, I assumed my life was gone. Being alive minus the solutions to the problems you’ve sorted out by way of the write-up is a crucial case, as well as the kind that might have in a negative way affected my entire career if I hadn’t discovered your web page. That understanding and kindness in handling a lot of things was invaluable. I don’t know what I would’ve done if I had not come across such a subject like this. I can at this point relish my future. Thanks a lot very much for your expert and result oriented guide. I won’t hesitate to propose your blog post to anybody who needs tips on this subject matter.

1. Rahula raj says:

Thanks for your praise! If you want any post on python regarding any subject feel free to comment.

4. Prashr88 says:

Hello nice code. Thanks. How long does it take to run this code in anaconda ?

1. Rahula raj says:

A couple of minutes

5. Keep up the good work! You know, many people are looking around for this information online, you could help them greatly.

1. Rahula raj says:

Thanks and do let me know if there are other topics that I should cover

6. Thanks for your article. I would like to say a health insurance specialist also utilizes the benefit of the coordinators of the group insurance plan. The health insurance professional is given a listing of benefits desired by an individual or a group coordinator. What any broker will is seek out individuals or coordinators that best fit those desires. Then he offers his advice and if all sides agree, the particular broker formulates binding agreement between the 2 parties.

1. Rahula raj says:

Great insight! I am sure that health insurance is an area where data analytics can really explode. It is similar to financial risk management with an exposure amount and a probability of default. I had written an article on it , do check it out.