Hey, everyone! In this blog post, I'm going to show you how I use Google Sheets to do some cool coding stuff with stock data. Specifically, I'm going to explain how I filter by column and calculate the closing averages over different time periods for different companies. Sounds fun, right?
First, let me tell you how I set up my spreadsheet. I have a sheet called "Indicators" where I store all the data I need. In row 2, I have the ticker symbols of the companies I'm interested in, sorted alphabetically from left to right. In row 13, I have a formula that uses the GOOGLEFINANCE function to get the closing prices of each stock for the past 185 trading days. For example, in cell H13, I have this formula:
=INDEX(GOOGLEFINANCE(H2,"PRICE",TODAY()-185,TODAY()),,2)
This formula gets the closing price of the stock in H2 (which is AAPL) for each day from today minus 185 days to today (Consider Weekends, if altering the number of days), and then returns only the second column of the result, which is the price. The two commas are used to exclude the ROW in the formula, thereby returning all the rows within the date range.
Next, in rows 14 to 17, I have four formulas that use the AVERAGE function to calculate the closing averages over 20, 45, 90, and 120 days for each stock. For example, in cell H14, I have this formula:
=IFERROR(AVERAGE(INDIRECT("H"&140-20):H$139),0)
This formula averages the values in cells H120 to H140, which are the closing prices of AAPL for the past 20 trading days. If there is an error (such as a missing value), it returns zero instead.
I repeat this process for the other time periods and stocks, so I end up with a table that looks like this:
| Ticker | AAPL | AMZN | GOOG | MSFT |
|--------|------|------|------|------|
| 20-day | 150 | 3200 | 2800 | 300 |
| 45-day | 145 | 3100 | 2700 | 290 |
| 90-day | 140 | 3000 | 2600 | 280 |
|120-day | 135 | 2900 | 2500 | 270 |
Now, here comes the fun part. I want to be able to quickly see the data for any stock I choose, without having to scroll through the whole table. So, I use a FILTER function to do that. In cell G1, I enter the ticker symbol of the stock I want to see. Then, in column F, I have this formula:
=FILTER($G2:FV140,$G$2:$FV$2=G1)
This formula filters the data in range G2:FV140 (which is my whole averages table range - Previous Columns have similar code but give me that averages of Indices) by matching the values in row 2 (which are the ticker symbols) with the value in G1 (which is my input). It then returns the matching column in column F. For example, if I enter GOOG in G1, I get this:
| Ticker | GOOG |
| 20-day Closing AVG| 138.91 |
| 45-day Closing AVG | 137.16 |
| 90-day Closing AVG | 135.73 |
|120-day Closing AVG | 135.57 |
This way, I can easily see how the closing price of GOOG has changed over different time periods. This is a useful technical indicator that can help me decide when to buy or sell a stock.
Of course, technical analysis is not everything. I always recommend doing your own research and looking at the fundamental ratios of a company before investing. But this Google Sheets trick can help you get a quick overview of the market trends and make informed decisions.
I hope you enjoyed this blog post and learned something new. If you have any questions or comments, feel free to leave them below. Thanks for reading!