Interactive Crypto Dashboard Powered by Julia

A fun way to visualize historical market data

Vikas Negi
Towards Data Science

--

Photo by Bermix Studio on Unsplash

First of all, a very important disclaimer: This tutorial is for educational purpose only, and should NOT be considered as trading advice. Cryptocurrency markets are highly volatile and you are likely to lose your investment. Please understand the risks and do your own research before you decide to trade in any of the cryptocurrencies discussed in this article.

Previously, I had explored the use of Julia for COVID-19 data visualization. The plots that we made in that tutorial were insightful, albeit limited in interactivity. I recently learned that Dash announced support for Julia, and therefore decided to try it out. Dash uses a react based framework which allows us to build scalable web applications. Python and R users must be already familiar with the Dash ecosystem. Now, with Dash.jl, we can finally make use of Julia as a powerful backend for our web app.

Keeping things simple, the focus of this tutorial will be to build an app that allows the user to monitor historical price data and calculate some key technical indicators for various cryptocurrencies. I chose this use case partly due to my interest in blockchain technologies, and due to all the exciting price action happening over the last few months. Such analysis can of course also be done for stock market data.

Let’s get started

I use VS Code, for which you need to install and configure the Julia extension. It is fairly straightforward, detailed instructions can be found here. You can also run the app directly from the Julia REPL. Clone this repository and follow the instructions given there. All relevant packages will be installed automatically. In case you do use VS Code, you can select the Julia environment by clicking on the bottom left (Julia env button) of your window and then selecting the location (CryptoDashApp directory) from the top panel. I am using Julia 1.6.0, the app will also work with older versions (tested on 1.5 and 1.4). However, I strongly recommend to upgrade.

Obtaining market data

We need a way to access historical market data for a number of cryptocurrencies. Popular exchanges such as Binance and Coinbase usually provide APIs for such purposes. However, creating an account with them is tedious, not to mention that most of these APIs are Python-based. Thankfully, there are a number of other market data providers who also give free access, with some limitations. Alpha Vantage is one such option. A big shout-out to Dean Markwick who did an amazing job creating the AlphaVantage.jl package, which we will be using in our app. Go ahead and get your free API access key here. Do note that the number of API calls are restricted to five per minute and 500 requests per day (more than enough for our app). We will try to circumvent the former limitation with some workarounds.

Project directory

All relevant code can be found here. The .toml files lie in the main directory, which shares the same name as the module. App configuration options are present in module file ‘CryptoDashApp.jl’, rest of the Julia functions are also present in the ‘src’ directory. We will download and save the market data for current date to the ‘data’ folder. In order to avoid clutter, our app will also perform a cleanup action by deleting data files from a previous date. See code gist below:

Delete data files except from the current day

Using Alpha Vantage API

We will create various helper functions to retrieve and dump market data into a Julia DataFrame. In case you are not familiar with DataFrames or need a quick introduction, here’s a handy guide. Since the number of API calls are limited, we only want to retrieve data once for a given cryptocurrency on a given day. Therefore, first step is to check if a data file already exists, if not, we download and save it to the disk. This is implemented in the function get_price_data_single as shown below:

Download and save data to disk for current date

If you look carefully at the code snippet above, we also did some post-processing to the ‘raw’ data obtained directly via an API call. For this, we make use of a function raw_to_df, which creates a nice DataFrame with every row indicating a day and column names indicating the corresponding data type.

Convert raw data to a more readable DataFrame

The resulting DataFrame should look like the following in the REPL:

ETH market data available within a DataFrame

In order to obtain the average data for a given day, we simply take the mean of the first four columns (open, high, low, close). These four columns also provide input to generate candlestick plots. Volume data is obtained via the volume column. These operations are grouped into the functions average_price_df and vol_df:

Group average price, candlestick and volume data into separate DataFrames

All the above are combined into the function get_price_data_single shown before, which can be called for any given cryptocurrency and returns separate DataFrames. Similar operations can be performed for another set of metrics which tell us about the market health of an asset. In the case of cryptocurrencies, they are user activity, developer behavior, and market maturity, which are provided by Flipside Crypto. The function get_ratings_data returns these metrics.

Calculating moving averages

Moving average is an important technical indicator, which is frequently used by traders to make buy/sell decisions. The three most important ones are Simple Moving Average (SMA), Weighted Moving Average (WMA) and Exponential Moving Average (EMA). I won’t go into the details, you can check this link for additional explanation. We calculate all three (check function moving_averages), and plot them together with the daily average price data. The desired averaging window can be selected from the web interface.

Plotting data

We make use of PlotlyJS.jl which is the Julia interface to plotly.js library. To pass data to the Plot function, we need to create PlotlyJS trace objects from our DataFrame. See the function plot_price_vol_data as an example:

Creating PlotlyJS trace objects for price and volume data

These traces will be used (shown later) in the callback! section within the run_app function.

Building the Dash app

The UI of this app consists of: (1) Drop-down list (to select a given cryptocurrency or to select a plotting mode) (2) Checkbox (to select averaging window or a time frame to display historical data). Such elements can be easily added as shown in the code gist below:

Set label and range for UI elements

Every time a user interacts with the app UI, a callback is triggered, which will update the input (think duration of data, averaging window etc.) to our plotting function.

Callback section within the run_app function

The resulting new plot is then displayed in the browser window. Check out the run_app function in CryptoDashApp.jl to see how all of this comes together.

Using the app

Instructions on how to use the web app are given on the CryptoDashApp GitHub page. To summarize, you need to: (1) Get a free API key here (2) Clone the repo (3) Activate and Instantiate the environment to download all necessary packages (only needed for the first time!) (4) Provide a port number and API key to the ‘run_app’ function. (5) Head over to link-1 or link-2

If all goes well, you should see something like the following:

Daily average + SMA, WMA and EMA (10-day window) for BTC price data over last 180 days

First row of checkbox controls the averaging window for calculating the various moving averages, whereas second row controls the duration of historical data (in days) that should be selected for display. Changing the mode (first drop-down list) will allow you to see a different set of data. See example for Ethereum (ETH) below:

Candlestick and volume data for ETH over the last 180 days

As ETH prices go up, trading exchanges start to see more activity. That’s probably why the average daily volume for ETH appears to increase during Jan-Feb 2021. However, trading volumes are comparatively lower during most of March, 2021. This could mean more investors are holding (keep calm and HODL!) ETH in their wallets, rather than moving it to/from an exchange. Litecoin (LTC) data shows similar behavior, with increase in trading volumes correlated with price action, see below:

Candlestick and volume data for LTC over the last 270 days

You can also check FCAS data for LTC by selecting that mode from the first drop-down list. It shows an overall rating of ‘Attractive’, since the average scores lies between 750–899 (more info). This is not at all surprising, given the fact that LTC is quite a mature project, and has been around for many years now.

FCAS rating for LTC

The plots are interactive, which means you can hover your cursor over the data points to see their value, and also zoom into a particular region. There is also an option to download the plot as a png file. By clicking on the legends, you can turn off the display for that data, which is useful for example when you want to look at only a certain type of data as shown below:

Daily average + EMA data for ETH over last 180 days

Conclusion

Dash.jl provides an easy-to-use framework to develop scalable web apps based on Julia. The official Plotly tutorial is also a great place to start. I enjoyed doing this exercise, and will continue to explore more interesting use cases. Meanwhile, feel free to develop on top of this app in case you have some ideas of your own. Full code can be found here. Thank you for taking the time to read this post! In case you want to connect, here’s my LinkedIn.

References

  1. https://github.com/vnegi10/CryptoDashApp.git
  2. https://dash-julia.plotly.com/
  3. Another great example of a web app that visualizes COVID-19 data: https://github.com/mbauman/CovidCountyDash.jl.git
  4. https://www.alphavantage.co/documentation/
  5. https://dm13450.github.io/2021/03/27/CryptoAlphaVantage.html

--

--