Convert tick data to 1 minute OHLC (Open, High, Lo

2019-09-24 17:10发布

Similar to create an OHLC data from Date, time, price using Javascript, how does one take the theory of converting basic trade data to OHLC (or Open, High, Low, Close) and apply it to generating 1 minute OHLC? Beside that, I also have problem adapting epoch timestamp to ISODate with the provided code.

var data = [{
    "tid": 283945,
    "date": 2018-08-02T04:24:53Z,
    "amount": "0.08180000",
    "price": "501.30"
}, {
    "tid": 283947,
    "date": 2018-08-02T04:24:53Z,
    "amount": "0.06110000",
    "price": "490.66"
},
...
];

function convertToOHLC(data) {
    // What goes here?
}
convertToOHLC(data);

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

1条回答
姐就是有狂的资本
2楼-- · 2019-09-24 17:58

I created an example, according to your requirements how to convert data to ohlc:

function convertToOHLC(data) {
    var parsedData = JSON.parse(data),
        pointStart = parsedData[0].date,
        range = [],
        low,
        high,
        ranges = [],
        dataOHLC = [],
        interval = 60 * 1000;

    parsedData.sort(function(a, b) {
        return a.date - b.date
    });

    $.each(parsedData, function(i, el) {
        if (pointStart + interval < el.date) {
            ranges.push(range.slice());
            range = [];
            range.push(el);
            pointStart = pointStart + interval;
        } else {
            range.push(el);
        }

        if (i === parsedData.length - 1) {
            ranges.push(range);
        }
    });

    $.each(ranges, function(i, range) {
        low = range[0].price;
        high = range[0].price;

        $.each(range, function(i, el) {
            low = Math.min(low, el.price);
            high = Math.max(high, el.price);
        });

        dataOHLC.push({
            x: range[0].date + 30 * 1000,
            open: Number(range[0].price),
            high: high,
            low: low,
            close: Number(range[range.length - 1].price)
        });
    });

    return dataOHLC
}

Live demo: https://jsfiddle.net/BlackLabel/qwvrgy93/

查看更多
登录 后发表回答