IFERROR with STOCKH (Accounting for Weekends)

Hi,


I'm building a spreadsheet Using STOCKH to track my investments [STOCKH("SYMBOL", close, DATEXPR)], showing current prices, yesterday's close, a week ago's close back to 5 years ago's close. I'm using EDATE to calculate the date offsets from the current (today's) date. The problem is, if any of those dates falls on a weekend or a holiday, STOCKH returns an error (why STOCKH doesn't just copy the previous day's close into holidays and weekends is beyond me).


So, I want to use IFERROR to decrement the date value by 2 days if it's a holiday or weekend (I know it should be more complex than that), but I just can't figure out the syntax: I tried IFERROR(STOCKH("SYMBOL', close, DATEXPR), DATEXPR -"2d"), but no luck... Googling hasn't been helpful, either. Could somebody point me in the right direction?


Also, if someone could show me how to embed the current date into an expression, I'd be really grateful.


TIA

Posted on Aug 6, 2022 8:17 AM

Reply
Question marked as Top-ranking reply

Posted on Aug 7, 2022 7:17 AM

TimDanaher wrote:

I couldn't for the life of me, though, work out why STOCKH doesn't return Friday's closing price for Saturday and Sunday, because that's what the stock price is on Saturday and Sunday, but I see now that It allows me to preserve Friday's price change across the weekend.


Note how the 'My Stocks' template at File > New in your menu so neatly charts historical prices by using a filter on the the table for days that do not have price data.


For your specific problem, where you want historical prices on a specific dates, note that you can also use nested IFERRORs to "crawl back" if necessary to the latest previous day with price data.


All IFERROR does is try another calculation if the first one results in an error, as when STOCKH finds no price data.



For example, to crawl back from Saturday or Sunday to the previous Friday, you can do this:



=IFERROR(STOCKH("AAPL",0,B2),IFERROR(STOCKH("AAPL",0,B2−1),STOCKH("AAPL",0,B2−2)))


Or if you have a long weekend, this will crawl back from Monday to the previous Friday:


=IFERROR(STOCKH("AAPL",0,B2),IFERROR(STOCKH("AAPL",0,B2−1),IFERROR(STOCKH("AAPL",0,B2−2),STOCKH("AAPL",0,B2−3))))


If for some reason you have to handle cases where trading is shut down for longer than three consecutive days then you could simply nest more IFERRORs.


If you want it all in one formula you could substitute the relevant EDATE expression for B2.


SG




13 replies
Question marked as Top-ranking reply

Aug 7, 2022 7:17 AM in response to TimDanaher

TimDanaher wrote:

I couldn't for the life of me, though, work out why STOCKH doesn't return Friday's closing price for Saturday and Sunday, because that's what the stock price is on Saturday and Sunday, but I see now that It allows me to preserve Friday's price change across the weekend.


Note how the 'My Stocks' template at File > New in your menu so neatly charts historical prices by using a filter on the the table for days that do not have price data.


For your specific problem, where you want historical prices on a specific dates, note that you can also use nested IFERRORs to "crawl back" if necessary to the latest previous day with price data.


All IFERROR does is try another calculation if the first one results in an error, as when STOCKH finds no price data.



For example, to crawl back from Saturday or Sunday to the previous Friday, you can do this:



=IFERROR(STOCKH("AAPL",0,B2),IFERROR(STOCKH("AAPL",0,B2−1),STOCKH("AAPL",0,B2−2)))


Or if you have a long weekend, this will crawl back from Monday to the previous Friday:


=IFERROR(STOCKH("AAPL",0,B2),IFERROR(STOCKH("AAPL",0,B2−1),IFERROR(STOCKH("AAPL",0,B2−2),STOCKH("AAPL",0,B2−3))))


If for some reason you have to handle cases where trading is shut down for longer than three consecutive days then you could simply nest more IFERRORs.


If you want it all in one formula you could substitute the relevant EDATE expression for B2.


SG




Aug 7, 2022 6:01 AM in response to Ralf-F

Thanks, Ralf, I appreciate you taking the time to help me, here... mistake in my previous post: "Corrected date is the same, 08/08, because it is a workday..." (how do you edit posts on here?).


So I need to use WORKDAY in my dates table, too, to make sure that it returns Friday if the date falls on a weekend.


So I did:



Which converts current day to Friday if current day is on a weekend...


Then:



Which gives me the previous day's date + WORKDAY conversion, then 'B3-7' in the next cell to give me the previous week's date + WORKDAY conversion.


Then:



Which gives me the previous month's date + WORKDAY conversion then, in the next two cells 'A3-12' and 'A3-60' gives me the dates a year ago and five years ago respectively, + WORKDAY conversion.


Apologies if I'm teaching my grandmother to suck eggs, here, but I'm writing this all out mostly for my benefit... and, there we have it... I think, with your help, I've solved it...


I know I could put date calculation and stock price fetching in the same cell, but I did it this way just to keep everything clear in my head.


I couldn't for the life of me, though, work out why STOCKH doesn't return Friday's closing price for Saturday and Sunday, because that's what the stock price is on Saturday and Sunday, but I see now that It allows me to preserve Friday's price change across the weekend.


Thanks once again !!!

Aug 6, 2022 9:05 AM in response to TimDanaher

You don't need IFERROR to get the last working day for STOCKH.


You can use WORKDAY then Numbers will do all the calculations

B2 = WORKDAY($A2+1,−1) This will return the last workday, even the date is Saturday or Sunday


C2 = STOCKH("AAPL",0,WORKDAY($A2+1,−1))



Based on your region the , or the ; will be used to separate the different sections of a formula. If you write one thousand as 1,000.00 then the , is used as your formula separator. If you write one thousand as 1.000,00 then the ; is used as your formula separator.


Hope this will solve your question, please let me know if this worked for you or if something in unclear.


Ralf


Aug 6, 2022 11:24 AM in response to Ralf-F

Wow... I'm having real difficulty taking weekends into account...


I want to build a spreadsheet that shows current share price, then the price at -1 day, -1 week , -1 month, -1 year and -5 years. Then I can work out the changes in stock price between any period. At the moment I have this:



Which is fine: Today is Saturday 06/08 (I'm using TODAY in the top table to generate the current date), so WORKDAY corrects the date to Friday 05/08 then -1 day becomes Thursday 04/08. I'm using the top table to generate the dates, which I then use with STOCKH in the bottom table to get the share price.


BUT, if I go forward to Monday 08/08, I get:



Corrected date is the same, 08/08, because it's not a workday, but moving back a day then gives me Sunday07/08, when I want it to be Friday 05/08... can't figure out a way of doing this...


Am I right in assuming that if STOCKH just posted Friday's closing price for Saturday and Sunday, this problem would disappear ..?


TIA

Aug 6, 2022 2:25 PM in response to TimDanaher

The lower table should work on Tuesday / Monday night :-)


How would numbers know the stock price for Monday 08 Aug. ?

Monday is a workday, therefore it will not display the value for Friday.

But it will not get a result for this day and therefore it will give you an error.


I would recommend that you display the date that you want, with name of the date.

Also display the workday that is really used to pull the stock price.

This will help to understand if there is an error in your formulas.

As soon as everything is working you could hide the additional information.


Ralf

Aug 8, 2022 3:11 AM in response to TimDanaher

Seriously... I call up numbers Help from the 'Help' menu, type in 'WORKDAY' and get '(no results)'. I really need to know the syntax of the WORKDAY function. Googling 'Numbers.app WORKDAY syntax' just takes me to results for Microsoft Excel ( do the two functions work in exactly the same way?).


I need to know this because I want to generalise my spreadsheet to take into account holidays when the New York Stock Exchange is closed.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

IFERROR with STOCKH (Accounting for Weekends)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.