类似于从使用 C# 的日期、时间、价格,如何将基本交易数据转换为 OHLC(或开盘价、最高价、最低价、收盘价)的理论应用到这种不同的案例中?
Similar to create an OHLC data from Date, time, price using C#, how does one take the theory of converting basic trade data to OHLC (or Open, High, Low, Close) and apply it to this distinct case?
var data = [{ "tid": 283945, "date": 1384934366, "amount": "0.08180000", "price": "501.30" }, { "tid": 283947, "date": 1384934066, "amount": "0.06110000", "price": "490.66" }, ... ]; function convertToOHLC(data) { // What goes here? } convertToOHLC(data);这是小提琴:jsfiddle/5dfjhnLw/
推荐答案这是一个将数据转换为 OHLC 的工作函数:
This is a working function for converting the data to OHLC:
function convertToOHLC(data) { data.sort((a, b) => d3.ascending(a.date, b.date)); var result = []; var format = d3.timeFormat("%Y-%m-%d"); data.forEach(d => d.date = format(new Date(d.date * 1000))); var allDates = [...new Set(data.map(d => d.date))]; allDates.forEach(d => { var tempObject = {}; var filteredData = data.filter(e => e.date === d); tempObject.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); result.push(tempObject); }); return result; };这是您更新的小提琴:jsfiddle/mg9v89r2/
首先,我们按日期对原始数据数组进行排序:
First, we sort the original data array by the dates:
data.sort((a, b) => d3.ascending(a.date, b.date));当我们稍后处理 open 和 close 时,这是一个重要的步骤.
That's an important step when we deal with open and close later.
之后,我们将毫秒转换为日期,作为字符串:
After that, we convert the milliseconds to dates, as strings:
var format = d3.timeFormat("%Y-%m-%d"); data.forEach(d => d.date = format(new Date(d.date * 1000)));这样做,我们可以过滤属于给定日期的所有对象.首先,我们在您的数据中创建一个包含所有不同日期的数组:
Doing this, we can filter all objects belonging to a given day. First, we create an array with all different days in your data:
var allDates = [...new Set(data.map(d => d.date))];对于该数组的每一天,我们将调用一个函数来填充一个名为 results 的空数组:
For each day of that array, we will call a function that will populate an empty array, named results:
allDates.forEach(d => { var tempObject = {}; var filteredData = data.filter(e => e.date === d); tempObject.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); result.push(tempObject); });在上面的 forEach 中,我们创建了一个空对象,并为 allDates 数组中的每一天过滤数据:
In the above forEach, we create an empty object, and for each day in our allDates array, we filter the data:
var filteredData = data.filter(e => e.date === d);并用它填充一个临时对象:
And populate an temporary object with it:
var tempObject = {}; tempObject.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);每次迭代后,我们将该临时对象推入results:
After each iteration, we push that temporary object into results:
result.push(tempObject);最后,我们返回results.
令人惊讶的是,您小提琴中的庞大数据数组只有 2 天的数据.
That huge data array in your fiddle, surprisingly, has only 2 days of data.
更多推荐
在 JavaScript 中将数据转换为 OHLC(开盘价、最高价、最低价、收盘价)?
发布评论