Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save va2577/c8c06800ea90316fe4b22e6e9654d95e to your computer and use it in GitHub Desktop.
Save va2577/c8c06800ea90316fe4b22e6e9654d95e to your computer and use it in GitHub Desktop.
Jupyter Notebook pandas-highcharts candlestick
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# pandas-highcharts\n",
"\n",
"* [gtnx/pandas-highcharts: Beautiful charting of pandas.DataFrame with Highcharts](https://github.com/gtnx/pandas-highcharts)\n",
"* [Jupyter Notebook Viewer](http://nbviewer.jupyter.org/github/gtnx/pandas-highcharts/blob/master/example.ipynb)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"<script src=\"//code.highcharts.com/stock/highstock.js\"></script>\n",
"<script src=\"//code.highcharts.com/highcharts-more.js\"></script>\n",
"<script src=\"//code.highcharts.com/modules/exporting.js\"></script>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import pandas as pd\n",
"import requests\n",
"#from pandas_highcharts.core import serialize\n",
"from pandas_highcharts.display import display_charts"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1497474000000, '109.554', '110.979', '109.258', '110.911'],\n",
" [1497560400000, '110.925', '111.415', '110.637', '110.89'],\n",
" [1497819600000, '110.841', '111.601', '110.704', '111.526'],\n",
" [1497906000000, '111.514', '111.782', '111.306', '111.454'],\n",
" [1497992400000, '111.434', '111.738', '111.035', '111.375'],\n",
" [1498078800000, '111.337', '111.446', '110.93', '111.319'],\n",
" [1498165200000, '111.32', '111.427', '111.142', '111.226'],\n",
" [1498424400000, '111.157', '111.939', '111.101', '111.86'],\n",
" [1498510800000, '111.82', '112.464', '111.459', '112.337'],\n",
" [1498597200000, '112.337', '112.416', '111.829', '112.28']]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134' }\n",
"r = requests.get('https://fx.minkabu.jp/api/v2/bar/USDJPY/daily.json?count=240', headers=headers)\n",
"r.raise_for_status()\n",
"r.json()[:10]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>time</th>\n",
" <th>open</th>\n",
" <th>high</th>\n",
" <th>low</th>\n",
" <th>close</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1497474000000</td>\n",
" <td>109.554</td>\n",
" <td>110.979</td>\n",
" <td>109.258</td>\n",
" <td>110.911</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1497560400000</td>\n",
" <td>110.925</td>\n",
" <td>111.415</td>\n",
" <td>110.637</td>\n",
" <td>110.89</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1497819600000</td>\n",
" <td>110.841</td>\n",
" <td>111.601</td>\n",
" <td>110.704</td>\n",
" <td>111.526</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1497906000000</td>\n",
" <td>111.514</td>\n",
" <td>111.782</td>\n",
" <td>111.306</td>\n",
" <td>111.454</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1497992400000</td>\n",
" <td>111.434</td>\n",
" <td>111.738</td>\n",
" <td>111.035</td>\n",
" <td>111.375</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" time open high low close\n",
"0 1497474000000 109.554 110.979 109.258 110.911\n",
"1 1497560400000 110.925 111.415 110.637 110.89\n",
"2 1497819600000 110.841 111.601 110.704 111.526\n",
"3 1497906000000 111.514 111.782 111.306 111.454\n",
"4 1497992400000 111.434 111.738 111.035 111.375"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.DataFrame(data=r.json(), columns=['time', 'open', 'high', 'low', 'close'])\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>open</th>\n",
" <th>high</th>\n",
" <th>low</th>\n",
" <th>close</th>\n",
" </tr>\n",
" <tr>\n",
" <th>time</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2017-06-15</th>\n",
" <td>109.554</td>\n",
" <td>110.979</td>\n",
" <td>109.258</td>\n",
" <td>110.911</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2017-06-16</th>\n",
" <td>110.925</td>\n",
" <td>111.415</td>\n",
" <td>110.637</td>\n",
" <td>110.890</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2017-06-19</th>\n",
" <td>110.841</td>\n",
" <td>111.601</td>\n",
" <td>110.704</td>\n",
" <td>111.526</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2017-06-20</th>\n",
" <td>111.514</td>\n",
" <td>111.782</td>\n",
" <td>111.306</td>\n",
" <td>111.454</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2017-06-21</th>\n",
" <td>111.434</td>\n",
" <td>111.738</td>\n",
" <td>111.035</td>\n",
" <td>111.375</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" open high low close\n",
"time \n",
"2017-06-15 109.554 110.979 109.258 110.911\n",
"2017-06-16 110.925 111.415 110.637 110.890\n",
"2017-06-19 110.841 111.601 110.704 111.526\n",
"2017-06-20 111.514 111.782 111.306 111.454\n",
"2017-06-21 111.434 111.738 111.035 111.375"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# [From Timestamps to Epoch](https://pandas.pydata.org/pandas-docs/stable/timeseries.html#from-timestamps-to-epoch)\n",
"utc = pd.to_datetime(df['time'], unit='ms')\n",
"utc3 = utc + pd.DateOffset(hours=3)\n",
"data1 = { 'open': df['open'].astype('float64').values, 'high': df['high'].astype('float64').values, 'low': df['low'].astype('float64').values, 'close': df['close'].astype('float64').values}\n",
"columns1 = ['open', 'high', 'low', 'close']\n",
"index1 = utc3\n",
"df2 = pd.DataFrame(data=data1, columns=columns1, index=index1)\n",
"df2.head()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"<div id=\"usdjpy\"</div>\n",
" <script type=\"text/javascript\">new Highcharts.Chart({\"title\":{\"text\":\"\\u7c73\\u30c9\\u30eb\\/\\u5186\"},\"legend\":{\"enabled\":true},\"yAxis\":[{}],\"chart\":{\"renderTo\":\"usdjpy\",\"type\":\"line\"},\"xAxis\":{\"title\":{\"text\":\"time\"},\"type\":\"datetime\"},\"series\":[{\"yAxis\":0,\"name\":\"close\",\"data\":[[1497484800000,110.911],[1497571200000,110.89],[1497830400000,111.526],[1497916800000,111.454],[1498003200000,111.375],[1498089600000,111.319],[1498176000000,111.226],[1498435200000,111.86],[1498521600000,112.337],[1498608000000,112.28],[1498694400000,112.164],[1498780800000,112.362],[1499040000000,113.376],[1499126400000,113.261],[1499212800000,113.252],[1499299200000,113.208],[1499385600000,113.91],[1499644800000,114.024],[1499731200000,113.93],[1499817600000,113.149],[1499904000000,113.28],[1499990400000,112.528],[1500249600000,112.617],[1500336000000,112.056],[1500422400000,111.93],[1500508800000,111.893],[1500595200000,111.122],[1500854400000,111.081],[1500940800000,111.872],[1501027200000,111.169],[1501113600000,111.266],[1501200000000,110.671],[1501459200000,110.254],[1501545600000,110.387],[1501632000000,110.741],[1501718400000,110.045],[1501804800000,110.655],[1502064000000,110.729],[1502150400000,110.338],[1502236800000,110.056],[1502323200000,109.209],[1502409600000,109.173],[1502668800000,109.652],[1502755200000,110.662],[1502841600000,110.191],[1502928000000,109.553],[1503014400000,109.185],[1503273600000,108.958],[1503360000000,109.552],[1503446400000,109.032],[1503532800000,109.552],[1503619200000,109.322],[1503878400000,109.23],[1503964800000,109.738],[1504051200000,110.228],[1504137600000,109.96],[1504224000000,110.228],[1504483200000,109.696],[1504569600000,108.8],[1504656000000,109.178],[1504742400000,108.426],[1504828800000,107.836],[1505088000000,109.392],[1505174400000,110.15],[1505260800000,110.486],[1505347200000,110.214],[1505433600000,110.83],[1505692800000,111.57],[1505779200000,111.588],[1505865600000,112.21],[1505952000000,112.456],[1506038400000,111.99],[1506297600000,111.722],[1506384000000,112.21],[1506470400000,112.808],[1506556800000,112.322],[1506643200000,112.5],[1506902400000,112.738],[1506988800000,112.844],[1507075200000,112.75],[1507161600000,112.804],[1507248000000,112.6],[1507507200000,112.672],[1507593600000,112.436],[1507680000000,112.472],[1507766400000,112.272],[1507852800000,111.826],[1508112000000,112.182],[1508198400000,112.185],[1508284800000,112.92],[1508371200000,112.536],[1508457600000,113.508],[1508716800000,113.43],[1508803200000,113.892],[1508889600000,113.736],[1508976000000,113.97],[1509062400000,113.635],[1509321600000,113.166],[1509408000000,113.622],[1509494400000,114.178],[1509580800000,114.084],[1509667200000,114.042],[1509930000000,113.698],[1510016400000,114.002],[1510102800000,113.866],[1510189200000,113.454],[1510275600000,113.505],[1510534800000,113.62],[1510621200000,113.45],[1510707600000,112.876],[1510794000000,113.042],[1510880400000,112.085],[1511139600000,112.614],[1511226000000,112.435],[1511312400000,111.208],[1511398800000,111.199],[1511485200000,111.501],[1511744400000,111.082],[1511830800000,111.465],[1511917200000,111.906],[1512003600000,112.536],[1512090000000,112.164],[1512349200000,112.4],[1512435600000,112.579],[1512522000000,112.284],[1512608400000,113.082],[1512694800000,113.448],[1512954000000,113.545],[1513040400000,113.548],[1513126800000,112.526],[1513213200000,112.38],[1513299600000,112.581],[1513558800000,112.538],[1513645200000,112.884],[1513731600000,113.385],[1513818000000,113.315],[1513904400000,113.265],[1514250000000,113.226],[1514336400000,113.34],[1514422800000,112.856],[1514509200000,112.651],[1514854800000,112.273],[1514941200000,112.502],[1515027600000,112.732],[1515114000000,113.018],[1515373200000,113.082],[1515459600000,112.635],[1515546000000,111.424],[1515632400000,111.244],[1515718800000,110.996],[1515978000000,110.525],[1516064400000,110.438],[1516150800000,111.29],[1516237200000,111.092],[1516323600000,110.814],[1516582800000,110.922],[1516669200000,110.3],[1516755600000,109.218],[1516842000000,109.402],[1516928400000,108.589],[1517187600000,108.952],[1517274000000,108.774],[1517360400000,109.188],[1517446800000,109.382],[1517533200000,110.152],[1517792400000,109.064],[1517878800000,109.554],[1517965200000,109.302],[1518051600000,108.736],[1518138000000,108.77],[1518397200000,108.641],[1518483600000,107.81],[1518570000000,106.998],[1518656400000,106.118],[1518742800000,106.272],[1519002000000,106.58],[1519088400000,107.322],[1519174800000,107.77],[1519261200000,106.742],[1519347600000,106.858],[1519606800000,106.907],[1519693200000,107.32],[1519779600000,106.661],[1519866000000,106.222],[1519952400000,105.712],[1520211600000,106.204],[1520298000000,106.128],[1520384400000,106.059],[1520470800000,106.193],[1520557200000,106.814],[1520812800000,106.41],[1520899200000,106.564],[1520985600000,106.316],[1521072000000,106.327],[1521158400000,105.953],[1521417600000,106.084],[1521504000000,106.526],[1521590400000,106.046],[1521676800000,105.27],[1521763200000,104.712],[1522022400000,105.396],[1522108800000,105.328],[1522195200000,106.842],[1522281600000,106.418],[1522368000000,106.26],[1522627200000,105.877],[1522713600000,106.6],[1522800000000,106.774],[1522886400000,107.376],[1522972800000,106.915],[1523232000000,106.747],[1523318400000,107.188],[1523404800000,106.782],[1523491200000,107.304],[1523577600000,107.338],[1523836800000,107.106],[1523923200000,106.991],[1524009600000,107.225],[1524096000000,107.358],[1524182400000,107.616],[1524441600000,108.7],[1524528000000,108.808],[1524614400000,109.418],[1524700800000,109.294],[1524787200000,109.022],[1525046400000,109.328],[1525132800000,109.862],[1525219200000,109.826],[1525305600000,109.182],[1525392000000,109.056],[1525651200000,109.09],[1525737600000,109.126],[1525824000000,109.742],[1525910400000,109.388],[1525996800000,109.36],[1526256000000,109.652],[1526342400000,110.342],[1526428800000,110.394],[1526515200000,110.75],[1526601600000,110.736]]}]});</script>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Basic line plot\n",
"#chart = serialize(df2, render_to=\"my-chart\", title=\"My Chart\")\n",
"# Basic column plot\n",
"#chart = serialize(df, render_to=\"my-chart\", title=\"Test\", kind=\"bar\")\n",
"\n",
"#chart = display_charts(df2, render_to=\"usdjpy\", title=\"米ドル/円\", kind=\"candlestick\")\n",
"# ValueError: candlestick plots are not yet supported\n",
"\n",
"chart = display_charts(df2, render_to=\"usdjpy\", title=\"米ドル/円\", kind=\"line\", y=['close'])"
]
}
],
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment