今回は、D3.jsを使用してMACD(Moving Average Convergence Divergence)を表示しようと思います。
MACDの計算は、
短期EMA – 長期EMA
シグナルの計算は、
MACDのSMA
となります。
OANDA JapanのMetaTrader 5のチャートと比較します。
データを取得した業者が提示したレートと若干違いはありますが、ほぼ同じ形をしています。
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, viewport-fit=cover"> <meta http-equiv="Content-Security-Policy" content="default-src * data: gap: https://ssl.gstatic.com; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'"> <script src="components/loader.js"></script> <script src="lib/onsenui/js/onsenui.min.js"></script> <script src="js/moment.min.js"></script> <script src="js/d3.min.js"></script> <script src="js/d3chart.js"></script> <script src="js/app.js"></script> <link rel="stylesheet" href="components/loader.css"> <link rel="stylesheet" href="lib/onsenui/css/onsenui.css"> <link rel="stylesheet" href="lib/onsenui/css/onsen-css-components.css"> <link rel="stylesheet" href="css/style.css"> </head> <body> <ons-page> <ons-toolbar> <div class="center">D3.js USDJPY 日足</div> </ons-toolbar> <div id="chart"></div> <div id="macd"></div> </ons-page> </body>> </html> |
style.css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
#chart { width: 100%; height: 170px; } #d3-chart { background-color: #000; } .axis text { font-size: 10px; color: #fff; } .axis path, .axis line { fill: none; stroke: #ccc; stroke-width: 1px; } .tick line { opacity: 0.3; stroke-width: 1px; } /* candlestick */ .candlestick-yosen { fill: #f00; stroke: #f00; stroke-width: 1px; } .candlestick-insen { fill: #0ff; stroke: #0ff; stroke-width: 1px; } /* sma */ .sma-yellow { fill: none; stroke: #ff0; stroke-width: 2px; } .sma-lime { fill: none; stroke: #0f0; stroke-width: 2px; } /* ema */ .ema-orange { fill: none; stroke: #ffa500; stroke-width: 2px; } .ema-green { fill: none; stroke: #008000; stroke-width: 2px; } #macd { width: 100%; height: 70px; } #d3-macd { background-color: #000; } .macd-silver { fill: none; stroke: #c0c0c0; stroke-width: 2px; } .signal-red { fill: none; stroke: #ff0000; stroke-width: 1px; } |
app.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
"use strict"; var sma = function (data, index, pos, period) { var avg = null; if ((index - period) >= 0) { for (var i = 0; i < period; i++) { if (data[index - i][pos]) { avg += data[index - i][pos]; } else { return null; } } avg = Math.floor((avg / period) * 1000) / 1000; } return avg; }; var ema = function (data, index, period, ix) { var avg = null; if ((index - period) == 0) { for (var i = 0; i < period; i++) { avg += data[index - i][4]; } avg = Math.floor((avg / period) * 1000) / 1000; } else if ((index - period) > 0) { avg = Math.floor((data[index - 1][ix] + (2 / (period + 1)) * (data[index][4] - data[index - 1][ix])) * 1000) / 1000; } return avg; }; ons.ready(function () { var i = 0; var data = [ ["20200401", 107.534, 107.934, 106.920, 107.163], ["20200402", 107.119, 108.091, 107.011, 107.857], ["20200403", 107.800, 108.678, 107.781, 108.460], ["20200406", 108.388, 109.382, 108.388, 109.200], ["20200407", 109.188, 109.263, 108.670, 108.796], ["20200408", 108.775, 109.099, 108.500, 108.845], ["20200409", 108.809, 109.061, 108.206, 108.493], ["20200410", 108.430, 108.583, 108.321, 108.398], ["20200413", 108.395, 108.483, 107.499, 107.749], ["20200414", 107.712, 107.759, 106.982, 107.210], ["20200415", 107.148, 107.872, 106.922, 107.432], ["20200416", 107.346, 108.082, 107.161, 107.859], ["20200417", 107.862, 108.082, 107.300, 107.580], ["20200420", 107.507, 107.950, 107.453, 107.631], ["20200421", 107.564, 107.893, 107.280, 107.757], ["20200422", 107.735, 107.938, 107.515, 107.718], ["20200423", 107.761, 108.043, 107.346, 107.620], ["20200424", 107.580, 107.759, 107.370, 107.452], ["20200427", 107.498, 107.621, 106.992, 107.257], ["20200428", 107.209, 107.337, 106.560, 106.861], ["20200429", 106.820, 106.892, 106.350, 106.581], ["20200430", 106.587, 107.495, 106.406, 107.155], ["20200501", 107.125, 107.404, 106.604, 106.930], ["20200504", 106.760, 107.069, 106.634, 106.721], ["20200505", 106.698, 106.895, 106.416, 106.531], ["20200506", 106.483, 106.574, 105.988, 106.088], ["20200507", 106.015, 106.655, 105.986, 106.266], ["20200508", 106.244, 106.743, 106.220, 106.735], ["20200511", 106.604, 107.768, 106.558, 107.627], ["20200512", 107.612, 107.689, 107.120, 107.146], ["20200513", 107.085, 107.276, 106.738, 107.031], ["20200514", 106.969, 107.364, 106.775, 107.235], ["20200515", 107.217, 107.434, 106.855, 107.170], ["20200518", 107.070, 107.506, 107.056, 107.301], ["20200519", 107.277, 108.087, 107.222, 107.711], ["20200520", 107.637, 107.984, 107.337, 107.544], ["20200521", 107.513, 107.849, 107.501, 107.598], ["20200522", 107.602, 107.760, 107.323, 107.608], ["20200525", 107.526, 107.781, 107.524, 107.708], ["20200526", 107.666, 107.924, 107.401, 107.548], ["20200527", 107.511, 107.946, 107.365, 107.715], ["20200528", 107.674, 107.901, 107.570, 107.615], ["20200529", 107.605, 107.894, 107.078, 107.875], ["20200601", 107.666, 107.857, 107.374, 107.580], ["20200602", 107.535, 108.771, 107.515, 108.681], ["20200603", 108.621, 108.984, 108.421, 108.925], ["20200604", 108.844, 109.197, 108.614, 109.165], ["20200605", 109.104, 109.850, 109.044, 109.616], ["20200608", 109.568, 109.686, 108.233, 108.440], ["20200609", 108.394, 108.541, 107.621, 107.743], ["20200610", 107.706, 107.874, 106.991, 107.146], ["20200611", 107.067, 107.235, 106.570, 106.862], ["20200612", 106.795, 107.553, 106.584, 107.407], ["20200615", 107.336, 107.555, 107.000, 107.353], ["20200616", 107.363, 107.638, 107.211, 107.291], ["20200617", 107.278, 107.442, 106.951, 106.999], ["20200618", 106.939, 107.129, 106.667, 106.973], ["20200619", 106.951, 107.059, 106.767, 106.845], ["20200622", 106.748, 107.010, 106.725, 106.863], ["20200623", 106.844, 107.222, 106.073, 106.529], ["20200624", 106.435, 107.068, 106.384, 107.058], ["20200625", 107.003, 107.452, 106.953, 107.174], ["20200626", 107.173, 107.357, 106.798, 107.199], ["20200629", 107.081, 107.885, 107.041, 107.580], ["20200630", 107.510, 107.983, 107.505, 107.932], ["20200701", 107.905, 108.164, 107.362, 107.455], ["20200702", 107.420, 107.724, 107.332, 107.498], ["20200703", 107.479, 107.568, 107.436, 107.485], ["20200706", 107.460, 107.773, 107.258, 107.385], ["20200707", 107.326, 107.791, 107.247, 107.538], ["20200708", 107.475, 107.711, 107.202, 107.253], ["20200709", 107.231, 107.398, 107.097, 107.201], ["20200710", 107.169, 107.264, 106.639, 106.930], ["20200713", 106.861, 107.319, 106.787, 107.283], ["20200714", 107.227, 107.427, 107.119, 107.236], ["20200715", 107.205, 107.309, 106.666, 106.939], ["20200716", 106.895, 107.401, 106.833, 107.272], ["20200717", 107.250, 107.360, 106.938, 106.986], ["20200720", 107.011, 107.537, 106.970, 107.279], ["20200721", 107.193, 107.367, 106.681, 106.803], ["20200722", 106.789, 107.288, 106.705, 107.153], ["20200723", 107.124, 107.231, 106.711, 106.865], ["20200724", 106.826, 106.872, 105.681, 106.052], ["20200727", 105.996, 106.056, 105.115, 105.373], ["20200728", 105.372, 105.686, 104.956, 105.088], ["20200729", 105.032, 105.244, 104.769, 104.940], ["20200730", 104.902, 105.295, 104.680, 104.739], ["20200731", 104.692, 106.055, 104.184, 105.895], ["20200803", 105.768, 106.468, 105.576, 105.955], ["20200804", 105.959, 106.196, 105.638, 105.731], ["20200805", 105.612, 105.873, 105.320, 105.590], ["20200806", 105.521, 105.697, 105.301, 105.528], ["20200807", 105.491, 106.056, 105.481, 105.948] ]; var data2 = []; for (i = 0; i < data.length; i++) { var s = ema(data, i, 12, 5); var l = ema(data, i, 26, 6); var macd = (s && l) ? ((s * 100000) - (l * 100000)) / 100000 : null; data[i].push(s); data[i].push(l); data2.push([data[i][0], macd]); // macd data2[i].push(sma(data2, i, 1, 9)); // signal } var month = data.filter(function (value) { return (value[0].substr(0, 6) == "202007"); }); var month2 = data2.filter(function (value) { return (value[0].substr(0, 6) == "202007"); }); window.fn.chart.createChart("chart", month); window.fn.chart.createMacd("macd", month2); }); |
d3chart.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
"use strict"; //************************************************************ // module: d3chart.js // author: otak-lab // use: d3.min.js //************************************************************ if (!window.hasOwnProperty("fn")) { window.fn = {}; } window.fn.chart = {}; //************************************************************ // create candlestick chart //************************************************************ window.fn.chart.createChart = function (id, data) { var elem = document.getElementById(id); if (!elem) return; if (data.length == 0) return; // margin var margin = { top: 10, right: 45, bottom: 7, left: 10 }; // width, height var w = elem.clientWidth - (margin.left + margin.right); var h = elem.clientHeight - (margin.top + margin.bottom); // min, max var min = Math.min.apply(null, data.map(function (d) { return Math.min(d[2], d[3]); })); var max = Math.max.apply(null, data.map(function (d) { return Math.max(d[2], d[3]); })); // y var y = d3.scaleLinear() .domain([min, max]) .nice() .range([h, 0]); // x var x = d3.scaleBand() .domain(data.map(function (d) { return d[0]; })) .range([0, w]); // adjust line x positon var pos = (x(data[1][0]) - x(data[0][0])) / 2; // axis var yAxis = d3.axisRight(y) .tickSizeInner(-w) .tickSizeOuter(0) .tickPadding(4) .tickFormat(function (d) { return d3.format(".3f")(d); }) .ticks(7); var yRange = yAxis.scale().domain(); var xAxis = d3.axisBottom(x) .tickValues(x.domain()) .tickSizeInner(-h) .tickSizeOuter(0) .tickPadding(8) .tickFormat(function (d, i) { //var result = Number(d.substr(6, 2)).toString(); //if (i == 0) result = Number(d.substr(4, 2)).toString() + "/" + result; //return result; return null; }); var xRange = xAxis.scale().domain(); // remove chart d3.select("#d3-chart").remove(); // create svg var svg = d3.select(elem) .append("svg") .attr("id", "d3-chart") .attr("width", elem.clientWidth.toString()) .attr("height", elem.clientHeight.toString()) .append("g") .attr("transform", "translate(" + margin.left.toString() + ", " + margin.top.toString() + ")"); // create x svg.append("g") .attr("class", "axis") .attr("transform", "translate(0, " + h.toString() + ")") .call(xAxis); // create y svg.append("g") .attr("class", "axis") .attr("transform", "translate(" + w.toString() + ", 0)") .call(yAxis); // candlestick body var cbw = Math.abs(x(xRange[1]) - x(xRange[0])) - 3; var cb = svg.selectAll("rect.b") .data(data) .enter() .append("rect"); cb.attr("class", function (d) { var cl = "candlestick-yosen"; if (d[1] > d[4]) { cl = "candlestick-insen"; } return cl; }) .attr("x", function (d) { return (x(d[0]) + pos - (cbw / 2)); }) .attr("y", function (d) { return y((d[1] > d[4]) ? d[1] : d[4]); }) .attr("width", cbw) .attr("height", function (d) { var top = (d[1] > d[4]) ? d[1] : d[4]; var bottom = (d[1] > d[4]) ? d[4] : d[1]; return Math.abs(y(top) - y(bottom)); }); // candlestick shadow var csw = 1; var cs = svg.selectAll("rect.s") .data(data) .enter() .append("rect"); cs.attr("class", function (d) { var cl = "candlestick-yosen"; if (d[1] > d[4]) { cl = "candlestick-insen"; } return cl; }) .attr("x", function (d) { return (x(d[0]) + pos - (csw / 2)); }) .attr("y", function (d) { return y(d[2]); }) .attr("width", csw) .attr("height", function (d) { return Math.abs(y(d[2]) - y(d[3])); }); // sma var sma5 = d3.line() .x(function (d) { return (x(d[0]) + pos); }) .y(function (d) { return y(d[5]); }) .curve(d3.curveCardinal); var sma20 = d3.line() .x(function (d) { return (x(d[0]) + pos); }) .y(function (d) { return y(d[6]); }) .curve(d3.curveCardinal); // 5sma svg.append("path") .attr("class", "sma-yellow") .attr("d", sma5(data)); // 20sma svg.append("path") .attr("class", "sma-lime") .attr("d", sma20(data)); // ema var ema5 = d3.line() .x(function (d) { return (x(d[0]) + pos); }) .y(function (d) { return y(d[5]); }) .curve(d3.curveCardinal); var ema20 = d3.line() .x(function (d) { return (x(d[0]) + pos); }) .y(function (d) { return y(d[6]); }) .curve(d3.curveCardinal); // 5ema svg.append("path") .attr("class", "ema-orange") .attr("d", ema5(data)); // 20ema svg.append("path") .attr("class", "ema-green") .attr("d", ema20(data)); }; //************************************************************ // create macd //************************************************************ window.fn.chart.createMacd = function (id, data) { var elem = document.getElementById(id); if (!elem) return; if (data.length == 0) return; // margin var margin = { top: 5, right: 45, bottom: 20, left: 10 }; // width, height var w = elem.clientWidth - (margin.left + margin.right); var h = elem.clientHeight - (margin.top + margin.bottom); // min, max var min = Math.min.apply(null, data.map(function (d) { return Math.min(d[1], d[2]); })); var max = Math.max.apply(null, data.map(function (d) { return Math.max(d[1], d[2]); })); // y var y = d3.scaleLinear() .domain([min, max]) .nice() .range([h, 0]); // x var x = d3.scaleBand() .domain(data.map(function (d) { return d[0]; })) .range([0, w]); // adjust line x positon var pos = (x(data[1][0]) - x(data[0][0])) / 2; // axis var yAxis = d3.axisRight(y) .tickSizeInner(-w) .tickSizeOuter(0) .tickPadding(4) .tickFormat(function (d) { return d3.format(".4f")(d); }) .ticks(4); var yRange = yAxis.scale().domain(); var xAxis = d3.axisBottom(x) .tickValues(x.domain()) .tickSizeInner(-h) .tickSizeOuter(0) .tickPadding(8) .tickFormat(function (d, i) { var result = Number(d.substr(6, 2)).toString(); if (i == 0) result = Number(d.substr(4, 2)).toString() + "/" + result; return result; }); var xRange = xAxis.scale().domain(); // remove chart d3.select("#d3-macd").remove(); // create svg var svg = d3.select(elem) .append("svg") .attr("id", "d3-macd") .attr("width", elem.clientWidth.toString()) .attr("height", elem.clientHeight.toString()) .append("g") .attr("transform", "translate(" + margin.left.toString() + ", " + margin.top.toString() + ")"); // create x svg.append("g") .attr("class", "axis") .attr("transform", "translate(0, " + h.toString() + ")") .call(xAxis); // create y svg.append("g") .attr("class", "axis") .attr("transform", "translate(" + w.toString() + ", 0)") .call(yAxis); // macd var macd = d3.line() .x(function (d) { return (x(d[0]) + pos); }) .y(function (d) { return y(d[1]); }) .curve(d3.curveCardinal); var signal = d3.line() .x(function (d) { return (x(d[0]) + pos); }) .y(function (d) { return y(d[2]); }) .curve(d3.curveCardinal); // macd svg.append("path") .attr("class", "macd-silver") .attr("d", macd(data)); // 20ema svg.append("path") .attr("class", "signal-red") .attr("d", signal(data)); }; |