gpt4 book ai didi

javascript - 在 JavaScript 中将 CSV 转换为 OHLC(开盘价、最高价、最低价、收盘价)?

转载 作者:行者123 更新时间:2023-11-30 21:08:08 26 4
gpt4 key购买 nike

我正在尝试使用 Convert data to OHLC (Open, High, Low, Close) in JavaScript? 中的脚本将一些图表数据转换为 OHLC。唯一的问题是我的数据与原始数据有点不同(http://api.bitcoincharts.com/v1/csv/localbtcDKK.csv.gz - 显示了在丹麦进行的所有比特币交易),那么我该如何修改代码呢?

原始数据

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/):

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

最佳答案

您必须做的最重要的事情是向该数据添加标题。像这样:

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

没有 header ,这些属性都不起作用。

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

var parsedArray = d3.csvParse(data)

如果出于某种原因,您无法更改数据以添加 header ,则可以在代码中添加:

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

这是带有这些更改的演示:

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 - 在 JavaScript 中将 CSV 转换为 OHLC(开盘价、最高价、最低价、收盘价)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46420571/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com