Search code examples
javascriptnode.jsalgorithmalgorithmic-tradingta-lib

How to arrange an unlabeled array into a labelled array?


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


Solution

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