Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d3c7ec
commit e1aeb0c
Showing
3 changed files
with
195 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import pandas as pd\n", | ||
"import hvplot.pandas # noqa" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"`ohlc` is a useful chart type to visualize stock movements" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from bokeh.sampledata import stocks\n", | ||
"\n", | ||
"df = pd.DataFrame(stocks.AAPL)\n", | ||
"df['date'] = pd.to_datetime(df.date)\n", | ||
"\n", | ||
"df.iloc[-50:].hvplot.ohlc(grid=True)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"We can control the `neg_color`, `pos_color`, `line_color` and `bar_width`:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"df.iloc[-50:].hvplot.ohlc(neg_color='indianred', pos_color='chartreuse', line_color='gray', bar_width=0.9, grid=True)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"By default `ohlc` will assume the `index` OR the first datetime column should be mapped to the x-axis and the first four non-datetime columns correspond to the O (open), H (high), L (low) and C (close) components. The default call is therefore equivalent to:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"df.iloc[-50:].hvplot.ohlc('date', ['open', 'low', 'high', 'close'], grid=True)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Using the HoloViews `RangeToolLink` we can make it easy to scroll through the data while still seeing an overview of the overall timeseries:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from holoviews.plotting.links import RangeToolLink\n", | ||
"\n", | ||
"df_2012 = df[df.date > pd.to_datetime('2012')]\n", | ||
"\n", | ||
"ohlc = df_2012.hvplot.ohlc(ylabel='Price ($)', grid=True, xaxis=None)\n", | ||
"overview = df_2012.hvplot.ohlc(yaxis=None, height=150, fields={'date': 'Date'})\n", | ||
"volume = df_2012.hvplot.step('date', 'volume', height=100, xaxis=None)\n", | ||
"\n", | ||
"RangeToolLink(overview.get(0), ohlc.get(0))\n", | ||
"\n", | ||
"layout = (volume + ohlc + overview).cols(1)\n", | ||
"\n", | ||
"layout.opts(merge_tools=False)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.8" | ||
}, | ||
"widgets": { | ||
"application/vnd.jupyter.widget-state+json": { | ||
"state": {}, | ||
"version_major": 2, | ||
"version_minor": 0 | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters