gpt4 book ai didi

google-api - 我们如何使用谷歌图表 API 创建带有移动平均线的 OHLC 图表

转载 作者:行者123 更新时间:2023-12-01 14:00:11 26 4
gpt4 key购买 nike

我正在使用 google API 创建图表。我能够创建 OHLC(烛台)图表。但我想为其添加移动平均线的叠加层。谁能指导我如何做到这一点?

提前致谢。

最佳答案

以下是如何向 CandlestickChart 添加移动平均线的示例:

function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Low');
data.addColumn('number', 'Open');
data.addColumn('number', 'Close');
data.addColumn('number', 'High');

var low, open, close = 45, high;
for (var i = 0; i < 30; i++) {
open = close;
close += ~~(Math.random() * 10) * Math.pow(-1, ~~(Math.random() * 2));
high = Math.max(open, close) + ~~(Math.random() * 10);
low = Math.min(open, close) - ~~(Math.random() * 10);
data.addRow([new Date(2014, 0, i + 1), low, open, close, high]);
}

// use a DataView to calculate an x-day moving average
var days = 5;
var view = new google.visualization.DataView(data);
view.setColumns([0, 1, 2, 3, 4, {
type: 'number',
label: days + '-day Moving Average',
calc: function (dt, row) {
// calculate average of closing value for last x days,
// if we are x or more days into the data set
if (row >= days - 1) {
var total = 0;
for (var i = 0; i < days; i++) {
total += dt.getValue(row - i, 3);
}
var avg = total / days;
return {v: avg, f: avg.toFixed(2)};
}
else {
// return null for < x days
return null;
}
}
}]);

var chart = new google.visualization.ComboChart(document.querySelector('#chart_div'));
chart.draw(view, {
height: 400,
width: 600,
chartArea: {
left: '7%',
width: '70%'
},
series: {
0: {
type: 'candlesticks'
},
1: {
type: 'line'
}
}
});
}
google.load("visualization", "1", {packages:["corechart"], callback: drawChart});

看到它在这里工作: http://jsfiddle.net/asgallant/74u6ox8b/

关于google-api - 我们如何使用谷歌图表 API 创建带有移动平均线的 OHLC 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25526361/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com