Iex stocks¶
View a running version of this notebook. | Download this project.
IEX Stocks¶
In the previous notebook we saw how all the trades on the IEX stock exchange could be interactively visualized over the course of a whole day (Monday 21st of October 2019). Using datashader, all the trades are rasterized interactively to reveal the density of trades via a colormap.
When viewing a million trades at once for a whole day, it is extremely difficult to identify individual trades using a global view. In order to identify particular trades, it is necessary to zoom into a time window small enough that individual trades can be distinguished at which point the trade metadata can be inspected using the Bokeh hover tool.
What the global visualization helps reveal is the overall pattern of trades. In this notebook we will focus on interactively revealing the trading patterns for individual stocks by partitioning on a set of stock symbols selected with a widget.
Loading the data¶
First we will load the data as before, converting the integer timestamps into the correctly offset datetimes before counting the total number of events:
import datetime
import pandas as pd
df = pd.read_csv('./data/IEX_2019-10-21.csv')
df.timestamp = df.timestamp.astype('datetime64[ns]')
df.timestamp -= datetime.timedelta(hours=4)
print('Dataframe loaded containing %d events' % len(df))
Next we will identify the top ten most traded stocks on this day and compute how much of the trading volume (i.e summed over the `size column) that they account for:
symbol_volumes = df.groupby('symbol')['size'].sum()
top_symbol_volumes = symbol_volumes.sort_values()[-10:]
top_symbols = list(top_symbol_volumes.index)
top_symbol_info = (', '.join(top_symbols),
top_symbol_volumes.sum() /symbol_volumes.sum() * 100)
'The top ten symbols are %s and account for %.2f%% of trading volume' % top_symbol_info
The following dictionary below show the names of the companies that each of these ten symbols correspond to:
symbol_info = {
"PInterest":'PINS',
'Chesapeake Energy Corporation': 'CHK',
"Snap Inc": 'SNAP',
"NIO Inc": 'NIO',
"McDermott International": 'MDR',
"Teva Pharmaceutical Industries": 'TEVA',
"Hewlett Packard Enterprise":'HPE',
"Bank of America": 'BAC',
"GE": 'General Electric',
"Infosys":'INFY',
}
Before we can visualize each of these stocks individually, we will need to import the necessary libraries:
import holoviews as hv
from bokeh.models import HoverTool
import datashader as ds
from holoviews.operation.datashader import spikes_aggregate
hv.config.image_rtol = 10e-3 # Suppresses datetime issue at high zoom level
hv.extension('bokeh')