みんかぶ FX のチャートのレートを取得してみました

みんかぶ FX のチャート のレートを取得してみました。

環境

  • Chart.js Version 2.7.2

米ドル/円

米ドル/円(USDJPY)の例で進めてみます。

1 分足

$ curl \
-A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299' \
-e 'https://fx.minkabu.jp/pair/USDJPY' \
-o 1m.json \
'https://fx.minkabu.jp/api/v2/bar/USDJPY/1m.json?count=240'
[
  
  [1525168680000,"109.622","109.629","109.621","109.627"],
  [1525168740000,"109.628","109.635","109.627","109.634"],
  [1525168800000,"109.634","109.634","109.621","109.623"],
  [1525168860000,"109.625","109.633","109.621","109.63"],
  [1525168920000,"109.631","109.633","109.621","109.623"]
]

結果は省略して、最後の 5 行だけ表示しています。

count=240 を変えると任意の期間のデータが取れそうです。

各行のデータはそれぞれ次のようになっているようです。

  1. 日時: Unix エポック
  2. Open: 始値
  3. High: 高値
  4. Low: 安値
  5. Close: 終値

-A で User-Agent をつけて、 -e で Referer をつけています。 それから、 -o で保存するファイル名をつけています。

5 分足

$ curl \
-A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299' \
-e 'https://fx.minkabu.jp/pair/USDJPY' \
-o 5m.json \
'https://fx.minkabu.jp/api/v2/bar/USDJPY/5m.json?count=240'
[
  
  [1525167600000,"109.63","109.644","109.619","109.629"],
  [1525167900000,"109.63","109.63","109.6","109.606"],
  [1525168200000,"109.606","109.652","109.598","109.632"],
  [1525168500000,"109.632","109.64","109.614","109.634"],
  [1525168800000,"109.634","109.634","109.621","109.633"]
]

10 分足

$ curl \
-A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299' \
-e 'https://fx.minkabu.jp/pair/USDJPY' \
-o 10m.json \
'https://fx.minkabu.jp/api/v2/bar/USDJPY/10m.json?count=240'
[
  
  [1525166400000,"109.583","109.6","109.559","109.594"],
  [1525167000000,"109.594","109.656","109.586","109.63"],
  [1525167600000,"109.63","109.644","109.6","109.606"],
  [1525168200000,"109.606","109.652","109.598","109.634"],
  [1525168800000,"109.634","109.634","109.621","109.633"]
]

15 分足

$ curl \
-A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299' \
-e 'https://fx.minkabu.jp/pair/USDJPY' \
-o 15m.json \
'https://fx.minkabu.jp/api/v2/bar/USDJPY/15m.json?count=240'
[
  
  [1525165200000,"109.57","109.642","109.563","109.605"],
  [1525166100000,"109.605","109.614","109.559","109.594"],
  [1525167000000,"109.594","109.656","109.586","109.629"],
  [1525167900000,"109.63","109.652","109.598","109.634"],
  [1525168800000,"109.634","109.637","109.621","109.636"]
]

30 分足

$ curl \
-A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299' \
-e 'https://fx.minkabu.jp/pair/USDJPY' \
-o 30m.json \
'https://fx.minkabu.jp/api/v2/bar/USDJPY/30m.json?count=240'
[
  
  [1525161600000,"109.472","109.522","109.454","109.476"],
  [1525163400000,"109.476","109.584","109.472","109.57"],
  [1525165200000,"109.57","109.642","109.559","109.594"],
  [1525167000000,"109.594","109.656","109.586","109.634"],
  [1525168800000,"109.634","109.639","109.621","109.638"]
]

1 時間足

$ curl \
-A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299' \
-e 'https://fx.minkabu.jp/pair/USDJPY' \
-o 1h.json \
'https://fx.minkabu.jp/api/v2/bar/USDJPY/1h.json?count=240'
[
  
  [1525154400000,"109.392","109.458","109.374","109.428"],
  [1525158000000,"109.429","109.483","109.407","109.475"],
  [1525161600000,"109.472","109.584","109.454","109.57"],
  [1525165200000,"109.57","109.656","109.559","109.634"],
  [1525168800000,"109.634","109.652","109.621","109.65"]
]

日足

$ curl \
-A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299' \
-e 'https://fx.minkabu.jp/pair/USDJPY' \
-o daily.json \
'https://fx.minkabu.jp/api/v2/bar/USDJPY/daily.json?count=240'
[
  
  [1524603600000,"108.808","109.455","108.783","109.418"],
  [1524690000000,"109.418","109.467","109.065","109.294"],
  [1524776400000,"109.288","109.537","108.97","109.022"],
  [1525035600000,"109.063","109.452","109.02","109.328"],
  [1525122000000,"109.332","109.4","109.234","109.38"]
]

週足

$ curl \
-A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299'\
-e 'https://fx.minkabu.jp/pair/USDJPY' \
-o weekly.json \
'https://fx.minkabu.jp/api/v2/bar/USDJPY/weekly.json?count=240'
[
  
  [1522594800000,"106.237","107.493","105.657","106.915"],
  [1523199600000,"106.937","107.78","106.616","107.338"],
  [1523804400000,"107.468","107.856","106.882","107.616"],
  [1524409200000,"107.768","109.537","107.659","109.022"],
  [1525014000000,"109.063","109.656","109.02","109.641"]
]

月足

$ curl \
-A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299' \
-e 'https://fx.minkabu.jp/pair/USDJPY' \
-o monthly.json \
'https://fx.minkabu.jp/api/v2/bar/USDJPY/monthly.json?count=240'
[
  
  [1514732400000,"112.691","113.387","108.282","109.188"],
  [1517410800000,"109.188","110.482","105.55","106.661"],
  [1519830000000,"106.661","107.293","104.636","106.26"],
  [1522508400000,"106.237","109.537","105.657","109.328"],
  [1525100400000,"109.332","109.656","109.234","109.644"]
]

Chart.js

このデータを、前回使った Chart.js の折れ線グラフで表示してみます。

次のように記述しました。

<div height="320" width="640">
  <canvas id="canvas1"></canvas>
</div>

<script>
  (() => {
    const dailyjson = [[1496005200000,"111.295","111.467","111.124","111.222"],[1496091600000,"111.265","111.317","110.657","110.827"],[1496178000000,"110.845","111.225","110.484","110.766"],[1496264400000,"110.768","111.481","110.65","111.347"],[1496350800000,"111.334","111.711","110.326","110.43"],[1496610000000,"110.315","110.73","110.303","110.452"],[1496696400000,"110.451","110.512","109.218","109.383"],[1496782800000,"109.411","109.881","109.096","109.81"],[1496869200000,"109.811","110.381","109.378","109.972"],[1496955600000,"109.983","110.812","109.721","110.323"],[1497214800000,"110.311","110.44","109.624","109.931"],[1497301200000,"109.925","110.27","109.786","110.06"],[1497387600000,"110.045","110.343","108.82","109.572"],[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"],[1498683600000,"112.28","112.925","111.804","112.164"],[1498770000000,"112.167","112.6","111.71","112.362"],[1499029200000,"112.1","113.471","112.054","113.376"],[1499115600000,"113.375","113.451","112.743","113.261"],[1499202000000,"113.26","113.686","112.821","113.252"],[1499288400000,"113.255","113.469","112.88","113.208"],[1499374800000,"113.204","114.179","113.056","113.91"],[1499634000000,"113.855","114.298","113.831","114.024"],[1499720400000,"114.025","114.495","113.714","113.93"],[1499806800000,"113.918","113.967","112.925","113.149"],[1499893200000,"113.152","113.522","112.86","113.28"],[1499979600000,"113.275","113.573","112.255","112.528"],[1500238800000,"112.455","112.865","112.321","112.617"],[1500325200000,"112.615","112.682","111.677","112.056"],[1500411600000,"112.015","112.229","111.551","111.93"],[1500498000000,"111.942","112.417","111.48","111.893"],[1500584400000,"111.898","112.079","111.011","111.122"],[1500843600000,"111.106","111.32","110.618","111.081"],[1500930000000,"111.039","111.958","110.828","111.872"],[1501016400000,"111.878","112.193","111.06","111.169"],[1501102800000,"111.158","111.711","110.781","111.266"],[1501189200000,"111.225","111.33","110.547","110.671"],[1501448400000,"110.653","110.773","110.214","110.254"],[1501534800000,"110.25","110.589","109.919","110.387"],[1501621200000,"110.36","110.984","110.283","110.741"],[1501707600000,"110.7","110.829","109.855","110.045"],[1501794000000,"110.031","111.051","109.841","110.655"],[1502053200000,"110.697","110.919","110.647","110.729"],[1502139600000,"110.73","110.829","110.25","110.338"],[1502226000000,"110.33","110.363","109.559","110.056"],[1502312400000,"110.055","110.179","109.153","109.209"],[1502398800000,"109.182","109.404","108.718","109.173"],[1502658000000,"109.153","109.802","109.024","109.652"],[1502744400000,"109.603","110.848","109.603","110.662"],[1502830800000,"110.656","110.949","110.03","110.191"],[1502917200000,"110.182","110.369","109.446","109.553"],[1503003600000,"109.548","109.599","108.603","109.185"],[1503262800000,"109.313","109.422","108.636","108.958"],[1503349200000,"108.967","109.649","108.844","109.552"],[1503435600000,"109.567","109.826","108.924","109.032"],[1503522000000,"109.041","109.605","108.849","109.552"],[1503608400000,"109.554","109.844","109.112","109.322"],[1503867600000,"109.154","109.413","109.026","109.23"],[1503954000000,"109.234","109.902","108.267","109.738"],[1504040400000,"109.63","110.439","109.542","110.228"],[1504126800000,"110.228","110.671","109.88","109.96"],[1504213200000,"109.96","110.465","109.562","110.228"],[1504472400000,"109.498","109.933","109.383","109.696"],[1504558800000,"109.676","109.835","108.63","108.8"],[1504645200000,"108.8","109.398","108.448","109.178"],[1504731600000,"109.178","109.264","108.046","108.426"],[1504818000000,"108.43","108.486","107.318","107.836"],[1505077200000,"108.168","109.506","108.128","109.392"],[1505163600000,"109.39","110.254","109.239","110.15"],[1505250000000,"110.154","110.691","109.903","110.486"],[1505336400000,"110.484","111.04","110.06","110.214"],[1505422800000,"110.216","111.334","109.546","110.83"],[1505682000000,"111.056","111.663","110.993","111.57"],[1505768400000,"111.57","111.881","111.201","111.588"],[1505854800000,"111.586","112.532","111.096","112.21"],[1505941200000,"112.21","112.716","112.135","112.456"],[1506027600000,"112.456","112.556","111.653","111.99"],[1506286800000,"112.148","112.531","111.475","111.722"],[1506373200000,"111.72","112.475","111.498","112.21"],[1506459600000,"112.218","113.257","112.218","112.808"],[1506546000000,"112.808","113.202","112.254","112.322"],[1506632400000,"112.33","112.737","112.213","112.5"],[1506891600000,"112.646","113.058","112.436","112.738"],[1506978000000,"112.744","113.196","112.654","112.844"],[1507064400000,"112.842","112.937","112.323","112.75"],[1507150800000,"112.748","112.918","112.413","112.804"],[1507237200000,"112.804","113.44","112.6","112.6"],[1507496400000,"112.558","112.747","112.33","112.672"],[1507582800000,"112.674","112.827","111.989","112.436"],[1507669200000,"112.44","112.586","112.08","112.472"],[1507755600000,"112.476","112.521","112.126","112.272"],[1507842000000,"112.276","112.305","111.69","111.826"],[1508101200000,"111.748","112.288","111.653","112.182"],[1508187600000,"112.182","112.48","112.036","112.185"],[1508274000000,"112.188","113.05","112.136","112.92"],[1508360400000,"112.924","113.147","112.296","112.536"],[1508446800000,"112.532","113.57","112.461","113.508"],[1508706000000,"113.882","114.1","113.245","113.43"],[1508792400000,"113.436","114.022","113.249","113.892"],[1508878800000,"113.9","114.244","113.48","113.736"],[1508965200000,"113.74","114.077","113.342","113.97"],[1509051600000,"113.972","114.452","113.635","113.635"],[1509310800000,"113.723","113.836","113.026","113.166"],[1509397200000,"113.17","113.732","112.958","113.622"],[1509483600000,"113.627","114.28","113.602","114.178"],[1509570000000,"114.178","114.218","113.538","114.084"],[1509656400000,"114.086","114.426","113.626","114.042"],[1509919200000,"114.001","114.734","113.696","113.698"],[1510005600000,"113.698","114.342","113.698","114.002"],[1510092000000,"114.002","114.008","113.395","113.866"],[1510178400000,"113.874","114.068","113.091","113.454"],[1510264800000,"113.47","113.635","113.221","113.505"],[1510524000000,"113.454","113.713","113.245","113.62"],[1510610400000,"113.622","113.912","113.308","113.45"],[1510696800000,"113.448","113.496","112.478","112.876"],[1510783200000,"112.872","113.333","112.734","113.042"],[1510869600000,"113.038","113.143","111.945","112.085"],[1511128800000,"112.101","112.718","111.886","112.614"],[1511215200000,"112.614","112.704","112.174","112.435"],[1511301600000,"112.435","112.493","111.142","111.208"],[1511388000000,"111.21","111.381","111.068","111.199"],[1511474400000,"111.199","111.622","111.194","111.501"],[1511733600000,"111.488","111.692","110.842","111.082"],[1511820000000,"111.08","111.64","110.931","111.465"],[1511906400000,"111.468","112.15","111.375","111.906"],[1511992800000,"111.91","112.638","111.735","112.536"],[1512079200000,"112.526","112.874","111.406","112.164"],[1512338400000,"112.792","113.09","112.365","112.4"],[1512424800000,"112.392","112.867","112.378","112.579"],[1512511200000,"112.578","112.633","111.992","112.284"],[1512597600000,"112.286","113.158","112.219","113.082"],[1512684000000,"113.08","113.59","113.077","113.448"],[1512943200000,"113.528","113.69","113.243","113.545"],[1513029600000,"113.552","113.751","113.371","113.548"],[1513116000000,"113.542","113.575","112.46","112.526"],[1513202400000,"112.534","112.883","112.065","112.38"],[1513288800000,"112.38","112.744","112.032","112.581"],[1513548000000,"112.72","112.83","112.31","112.538"],[1513634400000,"112.534","113.079","112.511","112.884"],[1513720800000,"112.884","113.466","112.839","113.385"],[1513807200000,"113.384","113.638","113.199","113.315"],[1513893600000,"113.315","113.448","113.24","113.265"],[1514239200000,"113.285","113.332","113.119","113.226"],[1514325600000,"113.23","113.381","113.147","113.34"],[1514412000000,"113.342","113.344","112.662","112.856"],[1514498400000,"112.856","112.969","112.472","112.651"],[1514844000000,"112.691","112.701","112.055","112.273"],[1514930400000,"112.273","112.608","112.172","112.502"],[1515016800000,"112.504","112.864","112.461","112.732"],[1515103200000,"112.734","113.303","112.717","113.018"],[1515362400000,"113.116","113.387","112.882","113.082"],[1515448800000,"113.08","113.18","112.366","112.635"],[1515535200000,"112.634","112.785","111.271","111.424"],[1515621600000,"111.426","111.876","111.044","111.244"],[1515708000000,"111.244","111.698","110.915","110.996"],[1515967200000,"111.081","111.184","110.328","110.525"],[1516053600000,"110.521","110.983","110.246","110.438"],[1516140000000,"110.444","111.347","110.192","111.29"],[1516226400000,"111.271","111.482","110.696","111.092"],[1516312800000,"111.09","111.129","110.49","110.814"],[1516572000000,"110.551","111.223","110.512","110.922"],[1516658400000,"110.914","111.179","110.252","110.3"],[1516744800000,"110.302","110.337","108.968","109.218"],[1516831200000,"109.212","109.702","108.5","109.402"],[1516917600000,"109.402","109.771","108.282","108.589"],[1517176800000,"108.685","109.203","108.51","108.952"],[1517263200000,"108.948","109.205","108.414","108.774"],[1517349600000,"108.772","109.446","108.596","109.188"],[1517436000000,"109.188","109.751","109.094","109.382"],[1517522400000,"109.388","110.482","109.281","110.152"],[1517781600000,"110.196","110.286","108.992","109.064"],[1517868000000,"109.078","109.653","108.456","109.554"],[1517954400000,"109.552","109.712","108.919","109.302"],[1518040800000,"109.309","109.784","108.58","108.736"],[1518127200000,"108.734","109.31","108.046","108.77"],[1518386400000,"108.842","108.885","108.437","108.641"],[1518472800000,"108.641","108.781","107.408","107.81"],[1518559200000,"107.812","107.903","106.723","106.998"],[1518645600000,"106.991","107.0","106.035","106.118"],[1518732000000,"106.118","106.399","105.55","106.272"],[1518991200000,"106.328","106.727","106.096","106.58"],[1519077600000,"106.582","107.377","106.557","107.322"],[1519164000000,"107.322","107.903","107.278","107.77"],[1519250400000,"107.759","107.773","106.594","106.742"],[1519336800000,"106.743","107.133","106.512","106.858"],[1519596000000,"107.15","107.154","106.376","106.907"],[1519682400000,"106.907","107.676","106.784","107.32"],[1519768800000,"107.32","107.528","106.565","106.661"],[1519855200000,"106.661","107.205","106.163","106.222"],[1519941600000,"106.22","106.298","105.25","105.712"],[1520200800000,"105.51","106.238","105.351","106.204"],[1520287200000,"106.204","106.463","105.852","106.128"],[1520373600000,"106.134","106.221","105.456","106.059"],[1520460000000,"106.063","106.315","105.892","106.193"],[1520546400000,"106.186","107.05","106.15","106.814"],[1520802000000,"106.632","106.972","106.31","106.41"],[1520888400000,"106.408","107.293","106.254","106.564"],[1520974800000,"106.569","106.749","106.064","106.316"],[1521061200000,"106.318","106.415","105.785","106.327"],[1521147600000,"106.324","106.374","105.602","105.953"],[1521406800000,"105.905","106.307","105.677","106.084"],[1521493200000,"106.089","106.608","105.929","106.526"],[1521579600000,"106.528","106.64","105.881","106.046"],[1521666000000,"106.048","106.089","105.261","105.27"],[1521752400000,"105.273","105.411","104.636","104.712"],[1522011600000,"104.766","105.482","104.707","105.396"],[1522098000000,"105.398","105.903","105.316","105.328"],[1522184400000,"105.329","107.012","105.325","106.842"],[1522270800000,"106.838","106.933","106.267","106.418"],[1522357200000,"106.418","106.537","106.122","106.26"],[1522616400000,"106.237","106.45","105.657","105.877"],[1522702800000,"105.877","106.657","105.693","106.6"],[1522789200000,"106.598","106.848","105.99","106.774"],[1522875600000,"106.774","107.493","106.711","107.376"],[1522962000000,"107.374","107.46","106.776","106.915"],[1523221200000,"106.937","107.203","106.616","106.747"],[1523307600000,"106.754","107.399","106.619","107.188"],[1523394000000,"107.19","107.253","106.65","106.782"],[1523480400000,"106.777","107.43","106.7","107.304"],[1523566800000,"107.294","107.78","107.204","107.338"],[1523826000000,"107.468","107.609","107.035","107.106"],[1523912400000,"107.104","107.213","106.882","106.991"],[1523998800000,"106.991","107.386","106.984","107.225"],[1524085200000,"107.223","107.517","107.181","107.358"],[1524171600000,"107.354","107.856","107.352","107.616"],[1524430800000,"107.768","108.754","107.659","108.7"],[1524517200000,"108.698","109.201","108.543","108.808"],[1524603600000,"108.808","109.455","108.783","109.418"],[1524690000000,"109.418","109.467","109.065","109.294"],[1524776400000,"109.288","109.537","108.97","109.022"],[1525035600000,"109.063","109.452","109.02","109.328"],[1525122000000,"109.332","109.4","109.234","109.38"]];
    const date = dailyjson.map(x => moment(x[0]).format('YYYY/MM/DD'));
    const close = dailyjson.map(x => x[1]);
    const ctx = document.getElementById('canvas1').getContext('2d');
    const data = {
      datasets: [{
        data: close,
        fill: false,
        label: '米ドル/円'
      }],
      labels: date
    };
    const options = {};
    const c = new Chart(ctx, {
      data: data,
      options: options,
      type: 'line'
    });
  })();
</script>

ここに折れ線グラフを表示しました。

ちゃんと表示されているかな。

日時の書式を設定をするのに、 const date = dailyjson.map(x => moment(x[0]).format('YYYY/MM/DD')); のように、読み込ませるデータ自体に対して書式を設定しました。 読み込ませるデータ自体はそのままに、表示するときに書式を設定することもできるようですが、綺麗な間隔で日時が表示されず、認識が不可能なくらい詰まって表示されてしまったので、読み込ませるデータ自体に書式を設定しました。 もっとうまく表示することができるのかな。

終わり

JSON の API があるサービスは便利です。

最初、 Chart.js に表示するときに、直接みんかぶ FX からデータを取ってこようと思ったのですが、 CORS の関係でできませんでした。 よろしくないことをしようとしてたみたいです。 公式に一般に公開されている API ではないと思うので、ほどほどにしておきます。