Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
534 views
in Technique[技术] by (71.8m points)

d3.js - Convert data to OHLC (Open, High, Low, Close) in JavaScript?

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

Here is the fiddle: https://jsfiddle.net/5dfjhnLw/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

Step by step explanation:

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

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

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

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

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

Finally, we return results.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...