使用以前的值填充时间序列数据数组 - Javascript

时间:2018-03-19 01:10:13

标签: javascript json date

我有股票价格的时间序列。数据并不总是连续的,有些日子不见了。我想用以前的价值填写这些天。我怎样才能有效地做到这一点?

可能有超过一千个市场有1000多天的数据。每天都有时间戳和定价的对象。

我想做的事情就是在某一天获得每个市场的价格并对它们进行分类。

我不确定通过在需要的地方插入时间戳和价格来填写所有遗失的日子是否更好? (它扩展了内存使用量,但可能更快地进行数据访问 - 我不知道这需要多少内存.1000个市场,1000天,有6个对象密钥。如果一个数字是8个字节,那么&#39 ;大约43Mb +有什么对象开销吗?)。

尝试返回上一个匹配,这需要搜索每个数组以查找匹配的时间戳。如果我想为每天的数据做这件事,这似乎很昂贵。

Sample data set (jsbin)



let data = {
  "name": "MARKET",
  "values": [{
    "time": 1440338400000,
    "close": 0.142163,
    "high": 0.152869,
    "low": 0.142163,
    "open": 0.152221,
    "volume": 14.2163,
    "marketCap": 0
  }, {
    "time": 1440511200000,
    "close": 0.158082,
    "high": 0.161828,
    "low": 0.15139,
    "open": 0.154371,
    "volume": 441.882,
    "marketCap": 0
  }, {
    "time": 1440597600000,
    "close": 0.162958,
    "high": 0.165483,
    "low": 0.15688,
    "open": 0.158235,
    "volume": 335.53,
    "marketCap": 0
  }, {
    "time": 1440684000000,
    "close": 0.16487,
    "high": 0.167594,
    "low": 0.160171,
    "open": 0.162908,
    "volume": 492.134,
    "marketCap": 0
  }, {
    "time": 1440770400000,
    "close": 0.162612,
    "high": 0.166171,
    "low": 0.162327,
    "open": 0.164979,
    "volume": 81.1436,
    "marketCap": 0
  }, {
    "time": 1440856800000,
    "close": 0.156701,
    "high": 0.161385,
    "low": 0.155568,
    "open": 0.161316,
    "volume": 514.921,
    "marketCap": 0
  }, {
    "time": 1440943200000,
    "close": 0.144589,
    "high": 0.157018,
    "low": 0.141987,
    "open": 0.156943,
    "volume": 635.154,
    "marketCap": 0
  }, {
    "time": 1441029600000,
    "close": 0.145655,
    "high": 0.146195,
    "low": 0.142581,
    "open": 0.144715,
    "volume": 10.1958,
    "marketCap": 0
  }, {
    "time": 1441116000000,
    "close": 0.142958,
    "high": 0.146204,
    "low": 0.142701,
    "open": 0.145595,
    "volume": 0.544275,
    "marketCap": 0
  }, {
    "time": 1441202400000,
    "close": 0.148987,
    "high": 0.148987,
    "low": 0.141326,
    "open": 0.142983,
    "volume": 58.1051,
    "marketCap": 0
  }, {
    "time": 1441288800000,
    "close": 0.15025,
    "high": 0.150856,
    "low": 0.148987,
    "open": 0.148987,
    "volume": 34.4072,
    "marketCap": 0
  }, {
    "time": 1441375200000,
    "close": 0.155935,
    "high": 0.156681,
    "low": 0.15228,
    "open": 0.152503,
    "volume": 0.155935,
    "marketCap": 0
  }, {
    "time": 1441461600000,
    "close": 0.159049,
    "high": 0.160032,
    "low": 0.155711,
    "open": 0.155836,
    "volume": 0.159049,
    "marketCap": 0
  }, {
    "time": 1441634400000,
    "close": 0.163826,
    "high": 0.16474,
    "low": 0.162861,
    "open": 0.164304,
    "volume": 327.651,
    "marketCap": 0
  }, {
    "time": 1441720800000,
    "close": 0.124591,
    "high": 0.16437,
    "low": 0.124427,
    "open": 0.163697,
    "volume": 1558.32,
    "marketCap": 0
  }, {
    "time": 1441807200000,
    "close": 0.115118,
    "high": 0.125665,
    "low": 0.115,
    "open": 0.124679,
    "volume": 110.436,
    "marketCap": 0
  }, {
    "time": 1441893600000,
    "close": 0.10203,
    "high": 0.115274,
    "low": 0.101593,
    "open": 0.115047,
    "volume": 1795.72,
    "marketCap": 0
  }, {
    "time": 1441980000000,
    "close": 0.101996,
    "high": 0.102003,
    "low": 0.101886,
    "open": 0.102003,
    "volume": 1795.14,
    "marketCap": 0
  }, {
    "time": 1442066400000,
    "close": 0.104842,
    "high": 0.106246,
    "low": 0.104305,
    "open": 0.106246,
    "volume": 2.09684,
    "marketCap": 0
  }, {
    "time": 1442152800000,
    "close": 0.105386,
    "high": 0.106185,
    "low": 0.103682,
    "open": 0.104886,
    "volume": 2192.04,
    "marketCap": 0
  }, {
    "time": 1442239200000,
    "close": 0.102747,
    "high": 0.114521,
    "low": 0.102498,
    "open": 0.105317,
    "volume": 21.0632,
    "marketCap": 0
  }, {
    "time": 1442325600000,
    "close": 0.0736399,
    "high": 0.105086,
    "low": 0.0697372,
    "open": 0.102713,
    "volume": 863.28,
    "marketCap": 0
  }, {
    "time": 1442412000000,
    "close": 0.0805882,
    "high": 0.0805882,
    "low": 0.0736,
    "open": 0.073644,
    "volume": 79.9028,
    "marketCap": 0
  }, {
    "time": 1442498400000,
    "close": 0.0882002,
    "high": 0.0885284,
    "low": 0.0805882,
    "open": 0.0805882,
    "volume": 52.9201,
    "marketCap": 0
  }, {
    "time": 1442584800000,
    "close": 0.102387,
    "high": 0.102535,
    "low": 0.0878508,
    "open": 0.0881813,
    "volume": 738.121,
    "marketCap": 0
  }, {
    "time": 1442671200000,
    "close": 0.102663,
    "high": 0.10276,
    "low": 0.102129,
    "open": 0.102387,
    "volume": 10.2663,
    "marketCap": 0
  }, {
    "time": 1442757600000,
    "close": 0.104525,
    "high": 0.106224,
    "low": 0.104265,
    "open": 0.106224,
    "volume": 72.0357,
    "marketCap": 0
  }, {
    "time": 1442844000000,
    "close": 0.109941,
    "high": 0.110287,
    "low": 0.104422,
    "open": 0.104472,
    "volume": 5.49706,
    "marketCap": 0
  }, {
    "time": 1442930400000,
    "close": 0.109915,
    "high": 0.110532,
    "low": 0.109467,
    "open": 0.110108,
    "volume": 5.49574,
    "marketCap": 0
  }, {
    "time": 1443016800000,
    "close": 0.115694,
    "high": 0.116243,
    "low": 0.113882,
    "open": 0.114151,
    "volume": 23.1389,
    "marketCap": 0
  }, {
    "time": 1443103200000,
    "close": 0.116584,
    "high": 0.116939,
    "low": 0.115274,
    "open": 0.115608,
    "volume": 23.3168,
    "marketCap": 0
  }, {
    "time": 1443189600000,
    "close": 0.119464,
    "high": 0.119717,
    "low": 0.118964,
    "open": 0.119519,
    "volume": 11.9464,
    "marketCap": 0
  }, {
    "time": 1443276000000,
    "close": 0.122498,
    "high": 0.122824,
    "low": 0.118839,
    "open": 0.119362,
    "volume": 12.2498,
    "marketCap": 0
  }, {
    "time": 1443362400000,
    "close": 0.125336,
    "high": 0.125755,
    "low": 0.122345,
    "open": 0.122539,
    "volume": 12.5336,
    "marketCap": 0
  }, {
    "time": 1443535200000,
    "close": 0.118549,
    "high": 0.119365,
    "low": 0.118333,
    "open": 0.119131,
    "volume": 1460.31,
    "marketCap": 0
  }, {
    "time": 1443621600000,
    "close": 0.120841,
    "high": 0.121297,
    "low": 0.118471,
    "open": 0.118521,
    "volume": 12.0841,
    "marketCap": 0
  }, {
    "time": 1443708000000,
    "close": 0.121266,
    "high": 0.121307,
    "low": 0.120696,
    "open": 0.120696,
    "volume": 12.1266,
    "marketCap": 0
  }, {
    "time": 1444053600000,
    "close": 0.124926,
    "high": 0.125369,
    "low": 0.122085,
    "open": 0.122188,
    "volume": 0.249852,
    "marketCap": 0
  }, {
    "time": 1444140000000,
    "close": 0.124018,
    "high": 0.12524,
    "low": 0.124018,
    "open": 0.124926,
    "volume": 0.248037,
    "marketCap": 0
  }, {
    "time": 1444312800000,
    "close": 0.127015,
    "high": 0.12717,
    "low": 0.126854,
    "open": 0.127056,
    "volume": 269.89,
    "marketCap": 0
  }, {
    "time": 1444399200000,
    "close": 0.129375,
    "high": 0.129437,
    "low": 0.126736,
    "open": 0.126915,
    "volume": 12.9375,
    "marketCap": 0
  }, {
    "time": 1444485600000,
    "close": 0.129097,
    "high": 0.129523,
    "low": 0.129063,
    "open": 0.12927,
    "volume": 12.9097,
    "marketCap": 0
  }]
};

data.values.forEach(value => {
  let date = new Date(value.time)
  console.log(date.toString())
})




2 个答案:

答案 0 :(得分:1)

有一种替代解决方案,重新采样数据。 看看https://content.pivotal.io/blog/time-series-analysis-part-3-resampling-and-interpolation

答案 1 :(得分:0)

我的解决方案是向后遍历数组并拼接丢失的时间帧数据。数据将是对最后看到的值的引用,这意味着时间戳将是不正​​确的,但由于间隔是统一的(每日),我可以使用数组索引来获取我正在追踪的正确数据。我假设这样的引用也会减少不必要的重复内存使用量。