gpt4 book ai didi

javascript - 将数据刻度为分钟/小时 OHLC Javascript

转载 作者:行者123 更新时间:2023-12-03 01:18:29 27 4
gpt4 key购买 nike

我有一组刻度数据,如下所示:

var data = [{
"tid": 283945,
"code": "0001",
"time": 2018-08-02T04:24:53Z,
"volume": "12000",
"price": "501.30",
"mode": "B"
}, {
"tid": 283947,
"code": "0001",
"date": 2018-08-02T04:24:53Z,
"amount": "1200000",
"price": "490.66",
"mode": "B"
},
...
];

我已经编写了代码来创建每日 OHLC,如下所示:

function convertToOHLC(data) {
data.sort((a, b) => d3.ascending(a.time, b.time));
var result = [];
data.forEach(d => {
d.date = new Date(d.time).toISOString().substring(0,10);
});

var allDates = [...new Set(data.map(d => d.date))];
allDates.forEach(d => {
var tempObject = {};
var filteredData = data.filter(e => e.date === d);
var filteredBData = data.filter(e => e.date === d && e.mode === 'B');
var filteredSData = data.filter(e => e.date === d && e.mode === 'S');
tempObject.code = filteredData[0].code;
tempObject.time = new Date(d);
tempObject.open = filteredData[0].price;
tempObject.close = filteredData[filteredData.length - 1].price;
tempObject.high = d3.max(filteredData, e => e.price);
tempObject.low = d3.min(filteredData, e => e.price);
tempObject.volume = d3.sum(filteredData, e => e.volume);
tempObject.bVolume = d3.sum(filteredBData, e => e.volume);
tempObject.sVolume = d3.sum(filteredSData, e => e.volume);
result.push(tempObject);
});
return result;
};

但是,我生活在一个并不是每一分钟都可以交易的国家,因此有些分钟可能是空的。然而,记录它们可能仍然很有值(value)。除此之外,交易时间为9:30-12:30和14:30-17:30。如何在有或没有交易的情况下为交易时间的每一分钟创建 OHLC。

请查看previous post ,很抱歉之前没有问清楚。感谢您的宝贵时间!

最佳答案

当您编写小型应用程序时,效率并不重要。现在重要了。新的解决方案避免了多次循环数据。现在速度快如闪电。

function convertToOHLC(data) {
var code = data[0].code;
data.sort((a, b) => d3.ascending(a.time, b.time));
var result = [];
data.forEach(d => {
d.date = new Date(d.time).toISOString().substring(0,10);
});

var allDates = [...new Set(data.map(d => d.date))];
allDates.forEach(d => {
var minuteCounter = 0;
var hourCounter = 9;
var halfTime = new Date(new Date(d).setHours(12,30));
var halfTimeBegin = new Date(new Date(d).setHours(14,30));
var fullTime = new Date(new Date(d).setHours(17,00));
while ( new Date(new Date(d).setHours(hourCounter,minuteCounter)) < fullTime) {
if (new Date(new Date(d).setHours(hourCounter,minuteCounter)) < halfTime ||
new Date(new Date(d).setHours(hourCounter,minuteCounter)) >= halfTimeBegin) {
var currentTime = new Date(new Date(d).setHours(hourCounter,minuteCounter));
var nextMinute = new Date(new Date(d).setHours(hourCounter,minuteCounter));
nextMinute.setMinutes(nextMinute.getMinutes() + 1);
var tempObject = {};
var filteredData = [];
var filteredBData = [];
var filteredSData = [];
for (var i = 0; i < data.length; i++) {
if (data[i].time >= currentTime && data[i].time <= nextMinute) {
filteredData.push(data[i]);
if (data[i].mode === 'B') {
filteredBData.push(data[i]);
} else if (data[i].mode === 'S') {
filteredSData.push(data[i]);
}
} else {
break;
}
}
var matchedLength = filteredData.length;
data = data.splice(matchedLength);
tempObject.code = code;
tempObject.time = currentTime;
tempObject.open = filteredData[0] ? filteredData[0].price : 0;
tempObject.close = filteredData[filteredData.length - 1] ? filteredData[filteredData.length - 1].price : 0;
tempObject.high = d3.max(filteredData, e => e.price) || 0;
tempObject.low = d3.min(filteredData, e => e.price) || 0;
tempObject.volume = d3.sum(filteredData, e => e.volume) || 0;
tempObject.bVolume = d3.sum(filteredBData, e => e.volume) || 0;
tempObject.sVolume = d3.sum(filteredSData, e => e.volume) || 0;
result.push(tempObject);
}

if (minuteCounter == 59) {
hourCounter += 1;
minuteCounter = 0;
} else {
minuteCounter += 1;
}
}

})
return result;
};

关于javascript - 将数据刻度为分钟/小时 OHLC Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51882575/

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