我正在为印度股票市场做一个交易机器人。我有这种形式的股票数据。
data = [[ 1627875900, 434.75, 435.8, 432.55, 434.9, 1750806 ], [ 1627876800, 434.75, 435.2, 432.7, 433, 905388 ], [ 1627877700, 432.9, 433.75, 431.8, 433.55, 689338 ],...........]按顺序表示为
[[time stamp, Open Price, High Price, Low Price, Close Price, Volume], [.....]]我想把它安排在标签数组中
{ open: [...], close: [...], high: [...], low: [...], volume: [...] }我使用的是TALIB,它使用这种形式的数据来使用指示器。
我看到这是用Python和熊猫做的,但是需要用javascript.来解决。
谢谢
发布于 2021-08-26 11:12:38
这里是数据操作的一个例子。
const data = [[ 1627875900, 434.75, 435.8, 432.55, 434.9, 1750806 ], [ 1627876800, 434.75, 435.2, 432.7, 433, 905388 ], [ 1627877700, 432.9, 433.75, 431.8, 433.55, 689338 ]];
const open = [], close = [], high = [], low = [], volume = [];
data.forEach(elm => {
open.push(elm[1])
high.push(elm[2])
low.push(elm[3])
close.push(elm[4])
volume.push(elm[5])
});
const result = {open, high, low, close, volume};
console.log(result);
发布于 2021-08-26 11:15:03
试试这个:
const data = [
[1627875900, 434.75, 435.8, 432.55, 434.9, 1750806],
[1627876800, 434.75, 435.2, 432.7, 433, 905388],
[1627877700, 432.9, 433.75, 431.8, 433.55, 689338]
]
const result = {
open: [],
high: [],
low: [],
close: [],
volume: []
}
data.forEach(item => {
result.open.push(item[1])
result.high.push(item[2])
result.low.push(item[3])
result.close.push(item[4])
result.volume.push(item[5])
})
console.log(result)
发布于 2021-08-26 11:35:31
可以使用zip函数按索引压缩嵌套数组,并将结果传递给预期数据形状的构造函数。
const zip = (...r) => [...r[0]].map((_, c) => r.map(s => s[c]));
const Data = ([_timestamp, open, close, high, low, volume]) => ({ open, close, high, low, volume });
// usage
const data = [[1627875900, 434.75, 435.8, 432.55, 434.9, 1750806], [1627876800, 434.75, 435.2, 432.7, 433, 905388], [1627877700, 432.9, 433.75, 431.8, 433.55, 689338]];
const result = Data(zip(...data));
console.log(result);.as-console-wrapper { max-height: 100% !important; top: 0; }
https://stackoverflow.com/questions/68937294
复制相似问题