Intraday candlestick | Highcharts
[
[1317888000000,372.5101,375,372.2,372.52],
[1317888060000,372.4,373,372.01,372.16],
[1317888120000,372.16,372.4,371.39,371.62],
[1317888180000,371.62,372.16,371.55,371.75],
[1317888240000,371.75,372.4,371.57,372],
[1317888300000,372,372.3,371.8,372.24],
[1317888360000,372.22,372.45,372.22,372.3],
[1317888420000,372.3,373.25,372.3,373.15],
[1317888480000,373.01,373.5,373,373.24],
[1317888540000,373.36,373.88,373.19,373.88],
米ドル/円(USDJPY)|為替レート・チャート|みんかぶFX
[[1497474000000, '109.554', '110.979', '109.258', '110.911'],
[1497560400000, '110.925', '111.415', '110.637', '110.89'],
[1497819600000, '110.841', '111.601', '110.704', '111.526'],
[1497906000000, '111.514', '111.782', '111.306', '111.454'],
[1497992400000, '111.434', '111.738', '111.035', '111.375'],
[1498078800000, '111.337', '111.446', '110.93', '111.319'],
[1498165200000, '111.32', '111.427', '111.142', '111.226'],
[1498424400000, '111.157', '111.939', '111.101', '111.86'],
[1498510800000, '111.82', '112.464', '111.459', '112.337'],
[1498597200000, '112.337', '112.416', '111.829', '112.28']]
import pandas as pd
import requests
from IPython.display import HTML
from IPython.display import display
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' }
r = requests.get('https://fx.minkabu.jp/api/v2/bar/USDJPY/daily.json?count=240', headers=headers)
r.raise_for_status()
r.json()[:10]
[[1497474000000, '109.554', '110.979', '109.258', '110.911'], [1497560400000, '110.925', '111.415', '110.637', '110.89'], [1497819600000, '110.841', '111.601', '110.704', '111.526'], [1497906000000, '111.514', '111.782', '111.306', '111.454'], [1497992400000, '111.434', '111.738', '111.035', '111.375'], [1498078800000, '111.337', '111.446', '110.93', '111.319'], [1498165200000, '111.32', '111.427', '111.142', '111.226'], [1498424400000, '111.157', '111.939', '111.101', '111.86'], [1498510800000, '111.82', '112.464', '111.459', '112.337'], [1498597200000, '112.337', '112.416', '111.829', '112.28']]
df = pd.DataFrame(data=r.json(), columns=['time', 'open', 'high', 'low', 'close'])
df.head()
#df.dtypes
#df.values.tolist()[:10]
time | open | high | low | close | |
---|---|---|---|---|---|
0 | 1497474000000 | 109.554 | 110.979 | 109.258 | 110.911 |
1 | 1497560400000 | 110.925 | 111.415 | 110.637 | 110.89 |
2 | 1497819600000 | 110.841 | 111.601 | 110.704 | 111.526 |
3 | 1497906000000 | 111.514 | 111.782 | 111.306 | 111.454 |
4 | 1497992400000 | 111.434 | 111.738 | 111.035 | 111.375 |
# [From Timestamps to Epoch](https://pandas.pydata.org/pandas-docs/stable/timeseries.html#from-timestamps-to-epoch)
utc = pd.to_datetime(df['time'], unit='ms')
utc3 = utc + pd.DateOffset(hours=3)
epoch = (utc3 - pd.Timestamp("1970-01-01")) / pd.Timedelta('1ms')
data1 = { 'time': epoch.astype('int64').values, 'open': df['open'].astype('float64').values, 'high': df['high'].astype('float64').values, 'low': df['low'].astype('float64').values, 'close': df['close'].astype('float64').values}
columns1 = ['time', 'open', 'high', 'low', 'close']
df2 = pd.DataFrame(data=data1, columns=columns1)
df2.head()
#df2.dtypes
#df2.values.tolist()[:10]
time | open | high | low | close | |
---|---|---|---|---|---|
0 | 1497484800000 | 109.554 | 110.979 | 109.258 | 110.911 |
1 | 1497571200000 | 110.925 | 111.415 | 110.637 | 110.890 |
2 | 1497830400000 | 110.841 | 111.601 | 110.704 | 111.526 |
3 | 1497916800000 | 111.514 | 111.782 | 111.306 | 111.454 |
4 | 1498003200000 | 111.434 | 111.738 | 111.035 | 111.375 |
template = """
<div id="container" style="height: 400px; min-width: 310px"></div>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
<script>
const plot = () => {{
if (typeof Highcharts !== 'object' ||
typeof Highcharts.stockChart !== 'function') {{
const timeoutID = setTimeout(plot, 1000);
return;
}}
// create the chart
Highcharts.stockChart('container', {{
rangeSelector: {{
selected: 1
}},
title: {{
text: '米ドル/円 daily'
}},
series: [{{
type: 'candlestick',
name: 'AAPL',
data: {data}
}}]
}});
}};
plot();
</script>
"""
display(HTML(template.format(data=df2.values.tolist())))