A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data
Dear Jen LaVictoire,
Yes, you can use STOCKHISTORY inside LET or LAMBDA to perform calculations directly in Excel without first displaying all of the historical data in separate helper columns.
For example, STOCKHISTORY can retrieve historical information as an array, while LET can assign names to intermediate calculations and reuse them within the same formula. If you want to reuse the calculation throughout the workbook, LAMBDA can also be used to create a custom reusable function.
There are different ways to construct the formula depending on exactly what you want to return, such as a 50-day or 200-day Simple Moving Average (SMA), daily returns, historical volatility, or a complete dynamic array.
For example, if you want to calculate the latest 50-day SMA directly from STOCKHISTORY in a single cell, you could use:
=LET(data,STOCKHISTORY("XNAS:MSFT",TODAY()-120,TODAY(),0,0,1),prices,TOCOL(data),AVERAGE(TAKE(prices,-50)))
In this example:
- STOCKHISTORY retrieves the daily historical closing prices for MSFT.
- LET stores the returned historical prices as an in-memory array.
- TAKE (prices,-50) selects the latest 50 trading observations.
- AVERAGE calculates the 50-day SMA and returns a single result in the cell, rather than displaying the underlying historical data on the worksheet.
The same concept can be adapted for a 200-day SMA, daily returns, annualized historical volatility, or packaged into a reusable LAMBDA function depending on your specific requirement.
For additional details, I recommend referring to these Microsoft Support articles:
- LET function | Microsoft Support
- STOCKHISTORY function | Microsoft Support
- LAMBDA function | Microsoft Support
Hope this helps. Please feel free to reply below if you have any update or further concern.
Thank you very much for your valuable time and your cooperation.
If the answer is helpful, please click "Yes" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.