0

I am making a trading bot for Indian Stock market. I have stock data in this form

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 ],...........]

which is represented according to order as

[[time stamp, Open Price, High Price, Low Price, Close Price, Volume], [.....]]

I want to arrange it in labelled array as

{ open: [...], close: [...], high: [...], low: [...], volume: [...] }

I am using TALIB Library which uses data in this form to use indicator.

I have seen this to be done using Python and pandas but need solution in javascript.

Thanks

4 Answers4

0

Here is an example of the data manipulation.

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);
evolutionxbox
  • 3,932
  • 6
  • 34
  • 51
Adil Bimzagh
  • 224
  • 1
  • 9
0

Try this:

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)
Lars Flieger
  • 2,421
  • 1
  • 12
  • 34
0

You can use a zip function to zip your nested arrays by index and pass the result to a constructor for your expected data shape.

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; }
pilchard
  • 12,414
  • 5
  • 11
  • 23
0

This is very easily built atop some reusable helper functions. transpose flips a matrix so that the columns turn into rows. zipObj takes an array of keys and and array of values and produces an object with the corresponding values associated with each key. With those, we can write this almost trivially:

const transpose = (xs) => 
  xs [0] .map ((_, i) => xs .map (r => r[i]))
  
const zipObj = (keys) => (vals) => 
  Object .fromEntries (transpose ([keys, vals]))

const restructure = (data) => 
  zipObj (['timestamp', 'open', 'high', 'low', 'close', 'volume']) (transpose (data))

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]];

console .log (restructure (data))
.as-console-wrapper {max-height: 100% !important; top: 0}

This includes the timestamp values. If you don't want them, then we can remove each with another helper (tail just returns an array without its first value) and then simply not include timestamp in the list of keys. You can see this by expanding the following snippet:

const tail  = (xs) => xs .slice (1)

const transpose = (xs) => 
  xs [0] .map ((_, i) => xs .map (r => r[i]))
  
const zipObj = (keys) => (vals) => 
  Object .fromEntries (transpose ([keys, vals]))

const restructure = (data) => 
  zipObj (['open', 'high', 'low', 'close', 'volume']) (transpose (data .map (tail)))

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]];

console .log (restructure (data))
.as-console-wrapper {max-height: 100% !important; top: 0}
Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103