Stock Prediction using Prophet (Python)

Hareesh Pallathoor Balakrishnan
3 min readNov 16, 2021

Prophet is a procedure for forecasting time series data based on an additive model where non-linear trends are fit with yearly, weekly, and daily seasonality, plus holiday effects. It works best with time series that have strong seasonal effects and several seasons of historical data. Prophet is robust to missing data and shifts in the trend, and typically handles outliers well.

Introduction

Prophet from Meta (Facebook) is a forecasting procedure implemented in Python. We can use it to predict time series data. Since stock prices are time-series data and as we can easily get historic data, let's create a python script to predict US Stock prices.

Acquiring Stock Prices

Yahoo Finance has a Python library that can be used to fetch stock data based on the stock ticker. Yahoo Finance library can be added using

pip install yfinance

then to fetch all available data on let say Apple Inc [AAPL], we run the following code

we need only the close price so, we discard other columns, by using

hist = hist[[‘Close’]]

Now, Date is the index, we need to convert it to a column using reset_index, and also have to rename the columns to ds for timestamp and y for the value field, as per the Prophet requirement

now we have stock data, that can be passed to Prophet

Prophet

It can be installed by

pip install prophet

A new Prophet object can be initiated using

m = Prophet()

Any forecasting procedures are passed into this constructor, Prophet will by default fit weekly and yearly seasonalities, let's enable daily seasonalities.

m = Prophet(daily_seasonality=True)

Then we call the fit function and pass the historical data frame,

Note: The historical data frame should contain two columns, a timestamp column titled ds and a value column titled y.

m.fit(data)

A data frame with future predictions can be obtained by, where periods is the number of days.

future = m.make_future_dataframe(periods=365)

To summarise, adding these to our existing codes will be as follows

we can filter to the required data using

print(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail())

Plotting

Prophet has a built-in plot method that can be called using,

figure1 = m.plot(forecast)
figure2 = m.plot_components(forecast)

The following code would give a more interactive plot

from prophet.plot import plot_plotly, plot_components_plotlyplot_plotly(m, forecast)

Final Program

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Responses (2)

Write a response

how i know where's the stock going what's the mean line you mean the price under the line that mena the price down or what's you mean thanks

--

So you have a stock signal eh? Great. I'm willing to evaluate it in real time.

--