2018-09-17-LP_solution

Real-time Intraday Stock Data Visualization

27 Sep 2018

The above interactive graph displays the real-time intraday stock data for one of my favored tech corporation Square, Inc. Thanks to the awesome tools Alpha Vantage and Plotly, we could freely and easily complete the graph online with python.

In this article I will demonstrate how to fulfil the task with jupyter notebook. Before start, alpha_vantage and plotly should be installed with the pip install []. And you shold also claim a free alpha-vantage API key. Then all the initial preparations are done. Let's start!

Stock Data Acessing

Apply the alpha_vantage API TimeSeries() to acess the real-time intraday stock data, detailed API functionality is provided in the documentation. The argument we choose is stock code and time interval (stock frequency).

I specifically pick up the SQ for Square Inc., with 1-minute and 5-minute as frequency. Note the 5-miute frequency is added to make the candlestick pattern more prominent.

Visualization

Plotly is a powerful online data visualization library easily communicating with jupyter notebook. I use the plotly to enable both interactive and real-time functionality. By posting my graph online into the plotly repository, it could be shared online and autonomously updated.

To keep the data up-to-date, we need to update the current date. And Python datetime do the task.

 

Plotly in Python is basically consisted of two major components: the data argument which is a list of trace object stroing graph data (x and y), and the layout argument which defines the formatting of the plot and magic functionality like animation, interactivity... A full tutorial is well presented for comprehensive understanding. Here I present a interactive graph with customized button.

To begin with, I create the trace items to specifically store candlestick, closed price, low price and high price plot. There is also a default Candlestick plot in plotly. Lastly I gather them together in a list called data.

I set up updatemenus for the customized button objects, which is a specific feature in layout. xanchor and yanchor with x and y are included to make the button properly located the graph. Layout is then completed with desired feature.

 

Finally, a fig dictionary is combined with data and layout, thus calling iplot to draw the graph. And we uploaded this jupyter notebook file into the user's plotly repository, then the plotly graph will be automatically generated in the website.

By using the sharing link at the bottom of plotly website, the iframe or html code could be derived for online share.

In this way, an online interactive real-time stock graph is cooked. Hurray!