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

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

问题描述

我正在尝试在中使用脚本将数据转换为JavaScript中的OHLC(打开,高,低,关闭)?,以将一些图表数据转换为OHLC.唯一的是,我的数据与原始数据有所不同( http://api.bitcoincharts.com/v1/csv/localbtcDKK.csv.gz -显示了丹麦曾经进行的所有比特币交易),那么我该如何修改代码?

I'm attempting to use the script in Convert data to OHLC (Open, High, Low, Close) in JavaScript? to convert some chart data to OHLC. The only thing is that my data differs a bit from the original (http://api.bitcoincharts.com/v1/csv/localbtcDKK.csv.gz - showing all bitcoin trades ever done in Denmark), so how would I have to go about modifying the code?

原始数据

var data = [{"tid": 283945, "date": 1384934366, "amount": "0.08180000", "price": "501.30"}, {"tid": 283947, "date": 1384934066, "amount": "0.06110000", "price": "490.66"}, ...];

新数据

// CSV format: UNIX timestamp, price, amount traded
// http://api.bitcoincharts.com/v1/csv/localbtcDKK.csv.gz
var data = `1366383202,748.680000000000,1.000000000000
1366471506,777.440000000000,2.700000000000
1368121200,685.740000000000,2.187400000000
...`

因为它是CSV,所以我已经加入 jquery-csv 来将数据转换为大批.这是我当前的代码( https://jsfiddle.net/askhflajsf/mg9v89r2/5/ ):

Since it's CSV I've included jquery-csv for turning the data into an array. Here's my current code (https://jsfiddle.net/askhflajsf/mg9v89r2/5/):

// CSV format: UNIX timestamp, price, amount traded
// http://api.bitcoincharts.com/v1/csv/localbtcDKK.csv.gz

var data = `1366383202,748.680000000000,1.000000000000
1366471506,777.440000000000,2.700000000000
1368121200,685.740000000000,2.187400000000
1375783458,619.500000000000,1.000000000000
...`

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
}

// Parse multi-line CSV string into a 2D array
// https://github.com/evanplaice/jquery-csv

var dataArray = $.csv.toArrays(data);

// jquery-csv's arrays are made up of strings, but we need them to be numbers

// Trim whitespace and then convert to Number

var dataArrayNumbers = dataArray.map(row => row.map(el => Number(el.trim())));

console.log(convertToOHLC(dataArrayNumbers));

推荐答案

您要做的最重要的事情是在数据中添加 headers .像这样:

The most important thing you have to do is adding headers to that data. Like this:

var data = `date,price,amount
1366383202,748.680000000000,1.000000000000
1366471506,777.440000000000,2.700000000000
//etc...

没有标题,这些属性都不起作用.

Without the headers, none of those properties will work.

此外,您在这里不需要jQuery.您可以简单地使用D3:

Also, you don't need jQuery here. You can simply use D3:

var parsedArray = d3.csvParse(data)

如果由于某种原因而无法更改数据以添加标题,则可以在代码中添加:

If, for whatever reason, you cannot change your data to add the headers, you can add then in the code:

var headers = ["date", "price", "amount"];
var parsedArray = d3.csvParse(headers + "\n" + data)

以下是具有这些更改的演示:

Here is the demo with those changes:

var csv = `date,price,amount
1366383202,748.680000000000,1.000000000000
1366471506,777.440000000000,2.700000000000
1368121200,685.740000000000,2.187400000000
1375783458,619.500000000000,1.000000000000`;

var originalData = d3.csvParse(csv)

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
}

console.log(convertToOHLC(originalData))

<script src="//d3js.org/d3.v4.min.js"></script>

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

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