- Excel Version
- 365
The relatively new function STOCKHISTORY is terrific in most respects. Of course, it’s not without deficiencies. Principally, it delivers data only for dates that the source possesses, that source being Refinitiv. When a security did not trade for whatever reason or Refinitiv does not have the data, the function reports NA. This NA error is most troublesome when making calculations across dates and securities.
Take for example a portfolio of stocks going across columns and dates and another column spilling on down the rows. If the dates reported by STOCKHISTORY are different for the various securities then they won’t line up! If a stock is halted for a few days, the problem is just worsened. How frustrating.
Note that it is not possible to simply use STOCKHISTORY copied down to all rows of a long list of dates for each security because Excel will quickly report a #CALC! error as there eventually occurs too many calls on the source.
What can be done? Here is a complex formula that accounts for every single calendar day between the relevant dates, even weekends, holidays and no-trade days. As long as the Start date does not cause an error, this formula fills in all the data. The formula reports the Data Type data for the day in the row if available OR it reports the last valid data. If only Microsoft would include a new argument in function STOCKHISTORY that could if desired report the all dates and the last valid data between the Start Date and the End date this mind-bending formula would be unnecessary.
Take for example a portfolio of stocks going across columns and dates and another column spilling on down the rows. If the dates reported by STOCKHISTORY are different for the various securities then they won’t line up! If a stock is halted for a few days, the problem is just worsened. How frustrating.
stock spill.xlsx | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | |||
2 | 12/30/2022 | Start | ||||||||
3 | 1/12/2023 | End | ||||||||
11 | ||||||||||
12 | CANADIAN NATURAL RESOURCES LIMITED (XTSE:CNQ) | Light Sweet Crude Oil | ||||||||
13 | Date | Close | Date | Close | ||||||
14 | 12/30/2022 | $ 75.19 | 12/30/2022 | $ 80.26 | ||||||
15 | 1/3/2023 | $ 71.29 | 1/2/2023 | #N/A | ||||||
16 | 1/4/2023 | $ 70.47 | 1/3/2023 | $ 76.93 | ||||||
17 | 1/5/2023 | $ 70.62 | 1/4/2023 | $ 72.84 | ||||||
18 | 1/6/2023 | $ 73.29 | 1/5/2023 | $ 73.67 | ||||||
19 | 1/9/2023 | $ 73.88 | 1/6/2023 | $ 73.77 | ||||||
20 | 1/10/2023 | $ 74.37 | 1/9/2023 | $ 74.63 | ||||||
21 | 1/11/2023 | $ 74.76 | 1/10/2023 | $ 75.12 | ||||||
22 | 1/12/2023 | $ 76.09 | 1/11/2023 | $ 77.41 | ||||||
23 | 1/12/2023 | $ 78.39 | ||||||||
Sheet2 |
Cell Formulas | ||
---|---|---|
Range | Formula | |
D13:E22 | D13 | =STOCKHISTORY(D12,A2,A3) |
G13:H23 | G13 | =STOCKHISTORY(G12,A2,A3) |
Dynamic array formulas. |
Note that it is not possible to simply use STOCKHISTORY copied down to all rows of a long list of dates for each security because Excel will quickly report a #CALC! error as there eventually occurs too many calls on the source.
What can be done? Here is a complex formula that accounts for every single calendar day between the relevant dates, even weekends, holidays and no-trade days. As long as the Start date does not cause an error, this formula fills in all the data. The formula reports the Data Type data for the day in the row if available OR it reports the last valid data. If only Microsoft would include a new argument in function STOCKHISTORY that could if desired report the all dates and the last valid data between the Start Date and the End date this mind-bending formula would be unnecessary.
stock spill.xlsx | |||||||||
---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | |||
2 | 12/30/2022 | Start | |||||||
3 | 1/12/2023 | End | |||||||
27 | |||||||||
28 | CANADIAN NATURAL RESOURCES LIMITED (XTSE:CNQ) | Light Sweet Crude Oil | |||||||
29 | Fri 12/30/2022 | $ 75.19 | $ 80.26 | ||||||
30 | Sat 12/31/2022 | $ 75.19 | $ 80.26 | ||||||
31 | Sun 1/1/2023 | $ 75.19 | $ 80.26 | ||||||
32 | Mon 1/2/2023 | $ 75.19 | $ 80.26 | ||||||
33 | Tue 1/3/2023 | $ 71.29 | $ 76.93 | ||||||
34 | Wed 1/4/2023 | $ 70.47 | $ 72.84 | ||||||
35 | Thu 1/5/2023 | $ 70.62 | $ 73.67 | ||||||
36 | Fri 1/6/2023 | $ 73.29 | $ 73.77 | ||||||
37 | Sat 1/7/2023 | $ 73.29 | $ 73.77 | ||||||
38 | Sun 1/8/2023 | $ 73.29 | $ 73.77 | ||||||
39 | Mon 1/9/2023 | $ 73.88 | $ 74.63 | ||||||
40 | Tue 1/10/2023 | $ 74.37 | $ 75.12 | ||||||
41 | Wed 1/11/2023 | $ 74.76 | $ 77.41 | ||||||
42 | Thu 1/12/2023 | $ 76.09 | $ 78.39 | ||||||
Sheet2 |
Cell Formulas | ||
---|---|---|
Range | Formula | |
A29:A42 | A29 | =SEQUENCE(A3-A2+1,,A2) |
D29:D42,G29:G42 | D29 | =XLOOKUP($A29#,FILTER(INDEX(STOCKHISTORY(D28,$A$2,$A$3,0,0),,1),ISNUMBER(INDEX(STOCKHISTORY(D28,$A$2,$A$3,0,0),,2))),FILTER(INDEX(STOCKHISTORY(D28,$A$2,$A$3,0,0),,2),ISNUMBER(INDEX(STOCKHISTORY(D28,$A$2,$A$3,0,0),,2))),,-1) |
Dynamic array formulas. |