Stock prices#

Building a simple plot that will display the first 25 data points for a selected stock. Display the stocks that can be changed with a drop-down menu.

Importing libraries and packages#

 1# Warnings
 2import warnings
 3
 4# Mathematical operations and data manipulation
 5import pandas as pd
 6
 7# Visualisation
 8from bokeh.plotting import figure, show
 9from bokeh.models.widgets import Panel, Tabs
10from ipywidgets import interact
11
12# Time
13from datetime import datetime
14
15# Output
16from bokeh.io import output_notebook
17
18output_notebook()
19warnings.filterwarnings("ignore")
20%matplotlib inline
Loading BokehJS ...

Set paths#

1# Path to datasets directory
2data_path = "./datasets"
3# Path to assets directory (for saving results to)
4assets_path = "./assets"

Loading dataset#

1dataset = pd.read_csv(f"{data_path}/stock_prices.csv")

Exploring dataset#

1# Shape of the dataset
2print("Shape of the dataset: ", dataset.shape)
3# Head
4dataset.head()
Shape of the dataset:  (851264, 7)
date symbol open close low high volume
0 2016-01-05 00:00:00 WLTW 123.430000 125.839996 122.309998 126.250000 2163600.0
1 2016-01-06 00:00:00 WLTW 125.239998 119.980003 119.940002 125.540001 2386400.0
2 2016-01-07 00:00:00 WLTW 116.379997 114.949997 114.930000 119.739998 2489500.0
3 2016-01-08 00:00:00 WLTW 115.480003 116.620003 113.500000 117.440002 2006300.0
4 2016-01-11 00:00:00 WLTW 117.010002 114.970001 114.089996 117.330002 1408600.0

Preprocessing#

 1# Mapping the date of each row to only the year-month-day format
 2
 3
 4def shorten_time_stamp(timestamp):
 5    shortened = timestamp[0]
 6
 7    if len(shortened) > 10:
 8        parsed_date = datetime.strptime(shortened, "%Y-%m-%d %H:%M:%S")
 9        shortened = datetime.strftime(parsed_date, "%Y-%m-%d")
10
11    return shortened
12
13
14dataset["short_date"] = dataset.apply(lambda x: shorten_time_stamp(x), axis=1)
15
16dataset.head()
date symbol open close low high volume short_date
0 2016-01-05 00:00:00 WLTW 123.430000 125.839996 122.309998 126.250000 2163600.0 2016-01-05
1 2016-01-06 00:00:00 WLTW 125.239998 119.980003 119.940002 125.540001 2386400.0 2016-01-06
2 2016-01-07 00:00:00 WLTW 116.379997 114.949997 114.930000 119.739998 2489500.0 2016-01-07
3 2016-01-08 00:00:00 WLTW 115.480003 116.620003 113.500000 117.440002 2006300.0 2016-01-08
4 2016-01-11 00:00:00 WLTW 117.010002 114.970001 114.089996 117.330002 1408600.0 2016-01-11

Visualisation#

 1# Method to build the tab-based plot
 2def get_plot(stock):
 3    stock_name = stock["symbol"].unique()[0]
 4
 5    line_plot = figure(
 6        title="Stock prices",
 7        x_axis_label="Date",
 8        x_range=stock["short_date"],
 9        y_axis_label="Price in $USD",
10    )
11    line_plot.line(stock["short_date"], stock["high"], legend_label=stock_name)
12    line_plot.xaxis.major_label_orientation = 1
13
14    circle_plot = figure(
15        title="Stock prices",
16        x_axis_label="Date",
17        x_range=stock["short_date"],
18        y_axis_label="Price in $USD",
19    )
20    circle_plot.circle(
21        stock["short_date"], stock["high"], legend_label=stock_name
22    )
23    circle_plot.xaxis.major_label_orientation = 1
24
25    line_tab = Panel(child=line_plot, title="Line")
26    circle_tab = Panel(child=circle_plot, title="Circles")
27    tabs = Tabs(tabs=[line_tab, circle_tab])
28
29    return tabs
1# Extracing all the stock names
2stock_names = dataset["symbol"].unique()
1# Creating the dropdown interaction and building the plot based on selection
2@interact(Stock=stock_names)
3def get_stock_for(Stock="AAPL"):
4    stock = dataset[dataset["symbol"] == Stock][:25]
5    show(get_plot(stock))