今回は、前回の「【Monaca】D3.jsを使用してドル円の日足チャートに単純移動平均線を表示する」のコードを流用して、指数平滑移動平均線を表示しようと思います。
指数平滑移動平均線の計算は、1日目の場合、
期間分の終値を加算して期間で割る。
2日目以降の場合、
前日の指数+(2/(期間+1)×(当日の終値-前日の指数)
となります。
OANDA JapanのMetaTrader 5のチャートと比較します。
データを取得した業者が提示したレートと若干違いはありますが、ほぼ同じ形をしています。
※例として6/18の取得したデータは陽線、MT5は陰線。
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 |
<!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/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">USDJPY 日足</div> </ons-toolbar> <div id="chart"></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 |
#chart { width: 100%; height: 270px; } #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; } |
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 |
"use strict"; var sma = function (data, index, period) { 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; } 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] ]; for (i = 0; i < data.length; i++) { data[i].push(sma(data, i, 5)); data[i].push(sma(data, i, 20)); data[i].push(ema(data, i, 5, 7)); data[i].push(ema(data, i, 20, 8)); } var month = data.filter(function (value) { return (value[0].substr(0, 6) == "202006"); }); window.fn.chart.createChart("chart", month); }); |
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 |
"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: 15, right: 45, bottom: 25, 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; }); 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[7]); }) .curve(d3.curveCardinal); var ema20 = d3.line() .x(function (d) { return (x(d[0]) + pos); }) .y(function (d) { return y(d[8]); }) .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)); }; |