Excel STOCKHISTORY Never Falls On Weekend Or Holiday - 2468

If you like this content, please consider visiting the video on YouTube and subscribe to the MrExcel Channel to show your support!
This video has been published on Feb 13, 2022.
TKH is using the new STOCKHISTORY function in Excel. He is looking for stock prices from 7, 14, 30, and 60 days ago. When you are asking for a single day, the STOCKHISTORY function returns a #VALUE error if the market is closed on the desired day.
This video shows how to use the old WORKDAY function to make sure your date never ends on a holiday or weekend.
maxresdefault.jpg


Transcript of the video:
Run STOCKHISTORY for a day that falls after a weekend or holiday.
This is our episode 2468.
Hey, welcome back to the MrExcel netcast. I am Bill Jelen. Today's question from TKH.
Trying to track previous stock prices by either 7, 14, 30, or 90 days from today's date.
It just so happens that I happen to be running this on Sunday the 13th.
And when I tried to do the STOCKHISTORY for Microsoft MSFT for today minus seven, that evaluates to Sunday, February 6th.
And there was no prices that day, so it returns a #VALUE! error, right?
So the question is how can we go today minus seven today minus 30 today minus 90 and make sure that we never end up on a Saturday, Sunday or a market holiday?
And it's a great old function that I haven't used in a while called the WORKDAY function.
The WORKDAY function is designed to return Monday through Friday dates.
You can also optionally have it leave holidays out right? So starting on the inside: TODAY() minus 7.
TODAY() minus seven will get me back to Sunday, February 6th.
From there I want to go forward one day, this 1 right here says I'm going forward one day.
But, unfortunately on a Monday that will end up getting me Tuesday's result.
So I have to start from today minus seven, and go back 1 day and then go forward 1 day.
I tried it without this and it was ending up on Sunday.
WORKDAY doesn't advance if that second item there is not positive.
Right, so you have to, for the “number of days” you have to put a one in to get it to actually move up to the next day. Alright, so right there we would be done.
There's an optional argument here called holidays.
Eventually you're going to run into like 30 days after Christmas or 30 days after New Years or something like that.
You're going to have a market holiday.
So just somewhere - anywhere - in your spreadsheet, put all of the market holidays. I had to Google this and type them in here.
And so for me, it's E8:E28.
And that optional holidays argument, makes sure that if 7, 14, 30, or 90 days ago falls on a holiday, it will go forward one day.
OK, so the solution then.
I put the numbers 7, 14, 30, an d90 down the left hand side here. Stock ticker up in A1 is MSFT.
I put the headings in.
The STOCKHISTORY will give you headings, but then it gives you 2 rows all the time, so I figure it's easier just to put the headings in myself and then let STOCKHISTORY Just return the numbers.
So taking a look at this STOCKHISTORY of Microsoft, actually, that should be A1, shouldn't it?
And then WORKDAY from TODAY, minus the date over in A4, which is 7, 14, 30, or 90. I'll go back one day from that.
Go forward one work day from that.
And oh, by the way, ignore these holidays right here This comma comma?
That's for the optional end date since you only want the price for one date, you leave that blank.
Interval is daily. And then what fields do we want?
You can put these in any order and you don't have to put them all.
So zero is the date, 1 is closed, 2 is open, 3 is high, 4 is low, and 5 is volume, like that.
And then just copy it down to the next three rows and you should be good to go.
We should be able to come up here and put in Coca Cola – KO - and it will retrieve those dates.
Should work even if there was a market holiday, whether it falls on a Saturday, Sunday, Monday or anything.
If you like these videos, please down below, Like, Subscribe, and Ring the bell.
Feel free to post any questions or comments down in the comments below. Alright hey, I want to thank you for stopping by.
We'll see you next time for another netcast from MrExcel.
If you love Excel, check out my new courses on the Retrieve platform.
They are video courses, but you just type what you're looking for.
It takes you right to that spot in the video and there's a complete transcript in several languages. It's a super fast way to learn.
 
Bill, good video! I would like to add some observations if I may. This STOCKHISTORY function is terrific in most respects. Having stock prices update in Excel has long been the dream of every investor. But there are problems.

The function is tough to manage when comparing stocks over long periods or across markets because the dates frequently do not line up. This happens because the markets can have differing weekends, differing holidays (invariably between the TSX and NASDAQ), or the stock of interest has had no trades on a day or has been halted for any number of days. Errors arise and stymie our portfolio analysis.

three stocks normalised.xlsx
HIJKLMNO
7start12/21/2021
8end2/1/2022
9
10TSX-Toronto Stock Exchange 300 Composite IndexNASDAQ Composite IndexPAN ORIENT ENERGY CORP. (XTSX:POE)
11DateCloseDateCloseDateClose
1212/21/2021$ 20,924.8712/21/2021$ 15,341.0912/21/2021$ 1.17
1312/22/2021$ 21,070.0512/22/2021$ 15,521.8912/22/2021#N/A
1412/23/2021$ 21,218.9312/23/2021$ 15,653.3712/23/2021$ 1.15
1512/24/2021$ 21,229.6812/27/2021$ 15,871.2612/24/2021$ 1.29
Sheet1
Cell Formulas
RangeFormula
H10H10=C10
K10K10=D10
N10N10=E10
H12:I39H12=STOCKHISTORY(H10,I7,I8,,0)
K12:L40K12=STOCKHISTORY(K10,I7,I8,,0)
N12:O39N12=STOCKHISTORY(N10,I7,I8,,0)
Dynamic array formulas.
Cells with Conditional Formatting
CellConditionCell FormatStop If True
K8Cell Value<0textNO
K8Cell Value>0textNO


So what's needed is a way to produce every single date between our start date and end date and then have the function retrieve the prices for those dates. If the date does not exist, get the last valid price. But you cannot just have STOCKHISTORY try to retrieve the prices for a long list of dates and many stocks because a #CALC or a #BUSY error will occur as the system gets overwhelmed. The green formula in B12 here that gets copied down eventually causes a crash when the dates get too far apart.

What to do? Surprisingly, there is a complex formula does not cause the system to crash. Using XLOOKUP and INDEX within STOCKHISTORY, we can get the last valid price for every date between the start and end dates. We can then have a set of columns containing each stock and the rows contain all the dates and prices. We can add things up horizontally and total the portfolio values. Triumph!

But, of course, there is always a problem: STOCKHISTORY reports #NA when the stock did not trade on a date the Exchange was open! Here Pan Orient was halted and so the errors creep in. At least this algorithm works for most stocks and indexes.

three stocks normalised.xlsx
ABCDE
7start12/21/2021
8end2/1/2022
9
10PAN ORIENT ENERGY CORP. (XTSX:POE)TSX-Toronto Stock Exchange 300 Composite IndexNASDAQ Composite IndexPAN ORIENT ENERGY CORP. (XTSX:POE)
11Datesprice on each datelast valid price on each datelast valid price on each datelast valid price on each date
1212/21/2021$ 1.17$ 20,924.87$ 15,341.09$ 1.17
1312/22/2021#N/A$ 21,070.05$ 15,521.89#N/A
1412/23/2021$ 1.15$ 21,218.93$ 15,653.37$ 1.15
1512/24/2021$ 1.29$ 21,229.68$ 15,653.37$ 1.29
1612/25/2021#VALUE!$ 21,229.68$ 15,653.37$ 1.29
1712/26/2021#VALUE!$ 21,229.68$ 15,653.37$ 1.29
Sheet1
Cell Formulas
RangeFormula
A12:A54A12=SEQUENCE(B8-B7+1,,B7)
C12:E54C12=XLOOKUP($A12#,INDEX(STOCKHISTORY(C10,$B$7,$B$8,,0),,1),INDEX(STOCKHISTORY(C10,$B$7,$B$8,,0),,2),,-1)
B12:B17B12=STOCKHISTORY($B$10,$A12,,,0,1)
Dynamic array formulas.
 
Hi Don
Thanks for this great idea on how to successfully use STOCKHISTORY. That is a great technique!

Bill
 
Dear Don,


Thank you very much for your response.

I do work with your great Feb 14th idea (though without using the SEQUENCE function). Actually it is implemented in a named function fcn.WeeklyPrice which is defined as follows:

=LAMBDA(SYMBOL,END_DATE,LET(
_start_date_,END_DATE-6,
_array_,STOCKHISTORY(SYMBOL,_start_date_,END_DATE,0,0),
_qualified_,_array_*--ISNUMBER(INDEX(_array_,,2)),
_price_,XLOOKUP(END_DATE,INDEX(_qualified_,,1),INDEX(_qualified_,,2),,-1,1),
IFERROR(_price_,-ERROR.TYPE(_price_))

))

I am concerned with negative (i.e., error) returns. When error numbers represent #N/A (#7, negative), it is fine. I know what happened.

Unfortunately, I could find no data pertaining to minus 10 (which indicate Excel #CONNECT! error). This became even more frustrating when I found out that F2+Enter solves the problem.

I appreciate your help. THX!

 

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top