在JavaScript中将数据转换为OHLC(开放,高,低,关闭)? [英] Convert data to OHLC (Open, High, Low, Close) in JavaScript?

查看:202
本文介绍了在JavaScript中将数据转换为OHLC(开放,高,低,关闭)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于创建OHLC数据使用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);

这是小提琴: https://jsfiddle.net/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;
};

这是你的更新小提琴: https://jsfiddle.net/mg9v89r2/

Here is your updated fiddle: https://jsfiddle.net/mg9v89r2/

首先,我们按日期对原始数据数组进行排序:

First, we sort the original data array by the dates:

data.sort((a, b) => d3.ascending(a.date, b.date));

当我们处理 open 和关闭以后。

之后,我们将毫秒转换为日期,作为字符串:

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))];

对于该数组的每一天,我们将调用一个函数来填充一个空数组,名为结果

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);

每次迭代后,我们将该临时对象推送到结果

After each iteration, we push that temporary object into results:

result.push(tempObject);

最后,我们返回结果

令人惊讶的是,您的小提琴中的巨大数据阵列只有2天的数据。

That huge data array in your fiddle, surprisingly, has only 2 days of data.

这篇关于在JavaScript中将数据转换为OHLC(开放,高,低,关闭)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆