Predicting Cryptocurrency Prices using Facebook Prophet

Or how to deploy a Forecasting Web Application using Python, Streamlit & Heroku

Michael Tuijp
Analytics Vidhya

--

Photo by Nick Chong on Unsplash

With the recent resurgence of both the crypto hype and adoption, many have been speculating on where the space and especially the price could go. Having come across Facebook Prophet, the popularity gaining & easy-to-use forecasting module (for Python and R), it seems like a potentially interesting tool for predicting what lies ahead.

Even more interesting is the Streamlit module, a very straightforward package which enables anyone with basic Python coding skills to create a decent looking web application! In this case it’s a live prediction tool for estimating future cryptocurrency prices, however it could run any type of application for any type of use-case!

Since we’re eager to know whether prices will go up in the near future, let’s build a forecasting application using Fabebook Prophet & Streamlit! If we want to openly show the app, it needs to be deployed (in the cloud) so it can be accessed by anyone from anywhere. From the most popular cloud services & deployment methods I’ve come across, using Heroku seems the easiest platform to create a simple application deployment with. For a more professional & production grade deployment, Azure, AWS and Google Cloud are probably your best picks.

First things first, let’s build our app!

Building the Streamlit Crypto Forecaster

As always, the first step in building an application is importing the required packages. Note that using Python 3.7 & installing the package pystan is required for installing the Facebook Prophet package. All in all we need the below packages:

$pip install pandas 
$pip install streamlit
$pip install plotly
$pip install cryptocmd
$pip install pystan
$pip install fbprophet

Running a Streamlit application is very easy. Simply go to the folder you saved your Python file in using your command prompt (with the command “cd C:\your\folder”) and use the command “streamlit run your_app_name.py”. This will open up a local Streamlit app in your browser, from where you can see the exact output of your script. The cool thing is that you can make changes & save the script, which will enable your Streamlit app to rerun the changes automatically! A little further down the line I’ll show what this looks like.

The Data

To build a crypto price forecaster app, we’re first of all going to need up-to-date price data. We could build our own web scraping tool to scrape the price data from price feeds like Coinmarketcap.com. Having said that, why reinvent the wheel when we can build on the shoulders of giants? Such a price scraping Python package has already been built after all, called cryptocmd. So lets use the input generated from the scraper using the code below, by selecting a ticker, initializing the scraper & loading the data.

The Graphs

So now that we have the data, we want to create an interactive plot to see the historical price of our cryptocurrency. With the code below we create an option to plot all available data for the selected ticker, on a regular scale or log scale (measuring percentage difference in price, rather absolute prices).

Now that we have the data & plot, let’s see what this looks like in Streamlit.

Streamlit app overview with current price data & time series plot

Facebook Prophet

Awesome! Now that we have an app which shows the data & plot of our selected crypto ticker, it’s finally time to create our Facebook Prophet forecasting model.

Alright, our Streamlit Crypto Forecaster is ready! After having done some renditions of the app, I’ve added & tweaked some parameters and styling to make it more customizable and better looking. The full code for the application can be found on my Github.

Deployment on Heroku

With the app ready, you can now play around with it locally. However, if you like other people to access your application, it needs to be deployed (on the cloud or local server). Most production-grade applications are mostly deployed on one of the big 3 cloud platforms (AWS, Azure & Google Cloud), for which I’ll write a future article on deployment on these platforms. For a quick & easy deployment however, Heroku is a great place to start!

Required Files

In order to deploy the application on Heroku, we need some additional files. First we need a requirements.txt file, which contains all the required Python packages & their versions required to run the application. Next to that a setup.sh file is required, which specifies the basic setup requirements for the deployment, like the file below.

mkdir -p ~/.streamlit/echo “\
[server]\n\
port = $PORT\n\
enableCORS = false\n\
headless = true\n\
\n\
“ > ~/.streamlit/config.toml

Finally, we need to execute the setup & python file, for which a Procfile is required and looks like the command below.

web: sh setup.sh && streamlit run streamlit_crypto_forecaster.py

Heroku

Now that all files are ready, we’re ready to deploy the app on Heroku. You do need a Github & Heroku account, both of which can be created for free. For getting started with creating a repository on Github, check out this article. Once you have an account for Heroku as well, you can start by selecting Create New App, give it a unique name & select the region closest to you. After your app is created you’ll see the deployment overview below.

Heroku deployment overview, connect with Github as deployment method

From here we select Github as the deployment method. After that it’s simply a matter of filling in the path to your Github repository, (optionally) Enable Automatic Deploys (which will automatically deploy any changes you commit to your Github repository to your Heroku app), and finalize by clicking Deploy Branch.

Connect with your Github repository path & deploy your app!

Congratulations! Your Heroku app is now being deployed & is accessible for anyone you’d like to share it with! You can now explore the Streamlit Crypto Forecaster from anywhere, at any time! Again, if you’d like to see or clone the code of the full application, check it out on my Github. At the time of writing, it would seem the model is pretty bullish for the forseeable future!

Prediction for the next 365 days using Facebook Prophet

Final Note

This article was written out of excitement for the latest developments in the crypto space, the latest Python packages & applications, and the easy-to-use Cloud platforms of today. These articles helped me out with coming up with & building the application, so give them a read as well! (1) (2) (3)

It’s now easier than ever to create whatever type of (Data Science) application you like & deploy it very quick and easy! I’m very excited to explore & work on similar projects in the future, to enable anyone in building their own applications!

P.S. None of the forecasts or mentions of price is financial advise ;)

--

--

Michael Tuijp
Analytics Vidhya

I’m a Data Scientist and enthusiast of leveraging the latest tools in Data Science, Engineering & DevOps to improve processes, businesses & our lives overall!