-1
2800: {Date: '2022-01-14', Open: 514.48999, High: 522.570007, Low: 511.269989, Close: 520.599976, …}
2801: {Date: '2022-01-18', Open: 510.390015, High: 519.390015, Low: 504.980011, Close: 513.340027, …}
2802: {Date: '2022-01-19', Open: 516.47998, High: 528.919983, Low: 515.299988, Close: 516.580017, …}
2803: {Date: '2022-01-20', Open: 522.380005, High: 532.030029, Low: 509.640015, Close: 510.850006, …}
2804: {Date: '2022-01-21', Open: 508.5, High: 513.859985, Low: 499.269989, Close: 499.910004, …}
2805: {Date: '2022-01-24', Open: 491.070007, High: 520.429993, Low: 483.309998, Close: 519.659973, …}
2806: {Date: '2022-01-25', Open: 505.51001, High: 514.26001, Low: 500.01001, Close: 502.720001, …}

I have the following historical price data of ADBE spanning more than 10 years. I am trying to get the min and max value every year according to their reporting date, period ending Nov each year.

I am trying to get the output in an array: [2010 min price, 2011 min price, 2012 min price ...]

const startDate = new Date("2010-12-01");
const endDate = new Date("2022-04-30");
    
const data2 = data.filter(a => {
    const date = new Date(a.Date);
      return (date >= startDate && date <= endDate);
    });
console.log(data)

    
function findMinMax(key) {
    const datas = data2.map((node) => node[key]);
        return {
          min: Math.min(...datas),
          max: Math.max(...datas),
        }
}
const checkit = findMinMax('Close')

As per the code, how can I do it without having to change the start and end date for each year just to get the min max value?

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
iuol
  • 7
  • 4
  • Please give an example of a sample input (condensed) and a sample output. – Som Shekhar Mukherjee Apr 30 '22 at 18:18
  • sorry not sure what input you are after? let input = [{Date: '2022-01-14', Open: 514.48999, High: 522.570007, Low: 511.269989, Close: 520.599976}, {Date: '2022-01-18', Open: 510.390015, High: 519.390015, Low: 504.980011, Close: 513.340027}] – iuol Apr 30 '22 at 18:51
  • And from this you want to calculate for every year what's the maximum & minimum values for "open", "high", "low" & "close"? – Som Shekhar Mukherjee Apr 30 '22 at 18:53
  • For any of them. Just close is fine – iuol Apr 30 '22 at 18:59
  • Okay! Got it, I can help you with the same, can you provide a big dataset so that I test my solution? – Som Shekhar Mukherjee Apr 30 '22 at 19:04
  • sure where do i upload it? – iuol Apr 30 '22 at 19:07
  • Checkout my answer, if it doesn't solve your problem, then you can share your dataset, you can share it on something like JSBin. – Som Shekhar Mukherjee Apr 30 '22 at 19:12
  • thanks it works. what if if the start date is not exactly the calender year. For example, ending November – iuol Apr 30 '22 at 19:33
  • how do I get it to work for the year starting 1-December instead of beginning in 1-Jan? – iuol Apr 30 '22 at 20:25
  • sorry its not working. What I mean is I want to calculate the min, max range for each year starting 1-December-Year to end-Nov-Year. Currently it only works for 1-Jan-Year to end-Dec-Year – iuol Apr 30 '22 at 20:46
  • The OP doesn't mention this, please phrase your question correctly! – Som Shekhar Mukherjee Apr 30 '22 at 20:55
  • .... I am trying to get the min and max value every year according to their reporting date, period "ending Nov" each year. Sorry if I did not make this very clear. But I did mention it – iuol Apr 30 '22 at 20:57
  • I really do appreciate the time you have spent helping me. I am new to javascript. Thank you very much – iuol Apr 30 '22 at 20:58
  • Let's say the year is 2022, then you want to only look into dates from 1st Jan 2022 to 30th Nov 2022, right? – Som Shekhar Mukherjee Apr 30 '22 at 20:59
  • it is based on the reporting period. So for this year 2022 it will be starting from 1-Dec-2021 to Today. For the last reporting period it will be 1-Dec-2020 - end of Nov 2021 – iuol Apr 30 '22 at 21:01
  • And where are you getting this reporting period from? – Som Shekhar Mukherjee Apr 30 '22 at 21:02
  • Their financial reports. They report for the full year ending nov. The months varies depending on the company's reporting period. For adobe it is for the full year ending Nov. – iuol Apr 30 '22 at 21:06
  • Yup finally got you buddy, I've updated my answer to calculate based on the reporting year, you can checkout the solution. In my example `01-05-2022` will be considered in reporting year `2021-2022` and `01-11-2021` will be considered in reporting year `2020-2021`. – Som Shekhar Mukherjee Apr 30 '22 at 21:16

1 Answers1

0

Checkout the solution below it calculates min/max for all fields based on the reporting month.

const data = [
  { Date: "2022-01-14", Open: 100, High: 500, Low: 50, Close: 1000 },
  { Date: "2021-12-05", Open: 200, High: 150, Low: 150, Close: 2000 },
  { Date: "2021-10-21", Open: 300, High: 550, Low: 70, Close: 200 },
  { Date: "2021-02-18", Open: 250, High: 600, Low: 10, Close: 500 },
];

const START_REPORTING_MONTH = 11; // 11 for December

const output = data.reduce((res, { Date: date, ...rest }) => {
  const d = new Date(date);
  const year = d.getFullYear();
  const month = d.getMonth();
  let reportingPeriod;

  if (month >= START_REPORTING_MONTH) {
    reportingPeriod = `${year}-${year + 1}`;
  } else {
    reportingPeriod = `${year - 1}-${year}`;
  }

  if (!res[reportingPeriod]) {
    res[reportingPeriod] = { min: { ...rest }, max: { ...rest } };
  }

  Object.entries(rest).forEach(([k, v]) => {
    if (v < res[reportingPeriod].min[k]) {
      res[reportingPeriod].min[k] = v;
    }
    if (v > res[reportingPeriod].max[k]) {
      res[reportingPeriod].max[k] = v;
    }
  });

  return res;
}, {});

console.log(output);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Som Shekhar Mukherjee
  • 4,701
  • 1
  • 12
  • 28