Author: Oscar Cronquist Article last updated on February 10, 2023

dynamic stock chart

This stock chart built in Excel allows you to change the date range and the chart is instantly updated.

 

1. Dynamic stock chart - Excel 365

Dynamic stock chart Excel 365

The image above demonstrates a dynamic stock chart, enter the stock quote in cell C2. The start and end date in cells C3 and C4, specify daily, weekly or monthly in cell C5.

The STOCKHISTORY function wants the corresponding number instead of daily, weekly, and monthly in order to work properly. We need a formula that converts the text to a number.

0 - daily
1 - weekly
2 - monthly

Formula in cell D5:

=MATCH(C5,{"Daily","weekly","monthly"},0)-1

Explaining the formula in cell D5

Step 1 - Populate arguments

The MATCH function returns the relative position of an item in an array that matches a specified value in a specific order.

Function syntax: MATCH(lookup_value, lookup_array, [match_type])

becomes

MATCH(C5,{"Daily","weekly","monthly"},0)

Step 2 - Evaluate MATCH function

MATCH(C5,{"Daily","weekly","monthly"},0)

becomes

MATCH("weekly",{"Daily","weekly","monthly"},0)

and returns 2. "weekly" is the second value in the array.

Step 3 - Subtract with one

MATCH(C5,{"Daily","weekly","monthly"},0)-1

becomes

2-1

and returns 1.

0 - daily
1 - weekly
2 - monthly

Dynamic stock chart1 Excel 365

Excel 365 dynamic array formula in cell B2:

=STOCKHISTORY(Sheet1!C2,Sheet1!C3,Sheet1!C4,Sheet1!D5,1,0,3,4,1)

Explaining the formula in cell B2

Step 1 - Populate arguments

The STOCKHISTORY function downloads stock prices based on a stock quote

Function syntax: STOCKHISTORY(stock, start_date, [end_date], [interval], [headers], [property0], [property1], [property2], [property3], [property4], [property5])

becomes

STOCKHISTORY(Sheet1!C2,Sheet1!C3,Sheet1!C4,Sheet1!D5,1,0,3,4,1)

Step 2 - Evaluate STOCKHISTORY function

STOCKHISTORY(Sheet1!C2,Sheet1!C3,Sheet1!C4,Sheet1!D5,1,0,3,4,1)

becomes

STOCKHISTORY("GOOGL",44197,44967,1,1,0,3,4,1)

and returns

{"Date","High","Low","Close";44193,89.4235,86.4,87.632;44200, ... ,95.01}

2. Dynamic stock chart - earlier Excel versions

dynamic stock chart

Here is how to build this chart.

2.1. Create a date range

stock chart - dynamic date range

I entered "Start:" and "End:" in cell range B16:B17. The date values in cells C16 and C17 will determine when the date range begins and ends.

Back to top

2.2. Copy data

I found the stock price data on the yahoo finance website, here is a link to S&P 500 historical prices.

  1. Press with left mouse button on the "Get to spreadsheet" link to the get file, you can find the link below historical data.
  2. Open the geted file in Excel.
  3. Copy data from the geted file.
  4. Paste to cell A20 on your worksheet.

stock chart - historical data

The data starts from the 1950s but I can't show all data, for obvious reasons.

Back to top

2.3. Build dynamic named ranges

We want the chart to change depending on the start and end date in cell range B16:B17. "Named ranges" is what we are looking for, it can return a cell range of variable size.

  1. Go to "Formulas" on the ribbon.
  2. Press with left mouse button on "Named Ranges".
  3. Press with left mouse button on the "New" button.
  4. Name it "Date".
  5. Refers to:
    =INDEX(Sheet1!$A$21:$A$810, MATCH(Sheet1!$C$16,Sheet1!$A$21:$A$810,-1)):INDEX(Sheet1!$A$21:$A$810, MATCH(Sheet1!$C$17, Sheet1!$A$21:$A$810, -1))

    stock chart - named ranges

  6. Press with left mouse button on "OK".
  7. Press with left mouse button on "New" button.
  8. Name it "High".
  9. Refers to:
    =INDEX(Sheet1!$C$21:$C$810, MATCH(Sheet1!$C$16,Sheet1!$A$21:$A$810, -1)):INDEX(Sheet1!$C$21:$C$810, MATCH(Sheet1!$C$17, Sheet1!$A$21:$A$810, -1))
  10. Press with left mouse button on OK.
  11. Press with left mouse button on "New".
  12. Name it "Low".
  13. Refers to:
    =INDEX(Sheet1!$D$21:$D$810, MATCH(Sheet1!$C$16, Sheet1!$A$21:$A$810, -1)):INDEX(Sheet1!$D$21:$D$810, MATCH(Sheet1!$C$17, Sheet1!$A$21:$A$810, -1))
  14. Press with left mouse button on OK.
  15. Press with left mouse button on "New".
  16. Name it "Close".
  17. Refers to:
    =INDEX(Sheet1!$E$21:$E$810, MATCH(Sheet1!$C$16, Sheet1!$A$21:$A$810, -1)):INDEX(Sheet1!$E$21:$E$810, MATCH(Sheet1!$C$17, Sheet1!$A$21:$A$810, -1))
  18. Press with left mouse button on OK.

You have now created 4 different named ranges which are to be used in the stock chart. You can find a formula explanation later in this post. But first, let's insert a stock chart above the date range.

Back to top

2.4. Insert a stock chart

  1. Select cell range C21:E29, column C:E contain high, low and close values.
  2. Go to tab "Insert" on the ribbon
  3. Press with left mouse button on "Stock chart" button

stock chart

The values are in an incorrect order, they need to be reversed.

  1. Press with right mouse button on on x axis
  2. Select "Format Axis..."
  3. Enable option "Categories in reverse order"

stock chart1

Back to top

2.5. Change series values

It is now time to use the dynamic named ranges we created earlier. They help us quickly change the cell range we want to be shown in the chart.

  1. Press with right mouse button on on the chart.
  2. Press with left mouse button on "Select Data...".
  3. Select Series1.
  4. Press with left mouse button on the "Edit" button.
  5. Series values:
    =Sheet1!High

    (Don't forget to type the sheet name before the named range.)
    stock chart - edit series

  6. Press with left mouse button on OK.
  7. Select Series2.
  8. Press with left mouse button on the "Edit" button.
  9. Series values:
    =Sheet1!Low
  10. Press with left mouse button on OK.
  11. Select Series3.
  12. Press with left mouse button on the "Edit" button.
  13. Series values:
    =Sheet1!Close
  14. Press with left mouse button on OK.
  15. Press with left mouse button on the "Edit" button below Horizontal (Category) Axis Labels.
  16. Axis label range:
    =Sheet1!Dates

Back to top

2.6. Explaining the named range (Date) formula

Step 1 - Find the position of value cell C16 in cell range A21:A810

The MATCH function returns a number representing the relative position of a given value in a cell range or array.

MATCH(lookup_value, lookup_array, [match_type])

MATCH(Sheet1!$C$16,Sheet1!$A$21:$A$810,-1)

becomes

MATCH(42248,Sheet1!$A$21:$A$810,-1)

and returns 2.

Step 2 - Calculate the first cell ref in the cell range

The INDEX function returns a value or a cell reference based on a row and column number.

INDEX(array[row_num][column_num])

INDEX(Sheet1!$A$21:$A$810, MATCH(Sheet1!$C$16,Sheet1!$A$21:$A$810,-1))

becomes

INDEX(Sheet1!$A$21:$A$810, 2)

and returns cell ref A22.

Step 3 - Find the position of value cell C17 in cell range A21:A810

The MATCH function returns a number representing the relative position of a given value in a cell range or array.

MATCH(lookup_value, lookup_array, [match_type])

MATCH(Sheet1!$C$17,Sheet1!$A$21:$A$810,-1)

becomes

MATCH(38353,Sheet1!$A$21:$A$810,-1)

and returns 130.

Step 4 - Calculate the second cell ref in the cell range

The INDEX function returns a value or a cell reference based on a row and column number.

INDEX(array[row_num][column_num])

INDEX(Sheet1!$A$21:$A$810, MATCH(Sheet1!$C$17, Sheet1!$A$21:$A$810, -1))

becomes

INDEX(Sheet1!$A$21:$A$810, 130)

and returns cell ref A151.

Step 5 - Combine cell refs to a cell range

You can concatenate two cell references using the colon character to create a cell reference to a cell range.

INDEX(Sheet1!$A$21:$A$810, MATCH(Sheet1!$C$16,Sheet1!$A$21:$A$810,-1)):INDEX(Sheet1!$A$21:$A$810, MATCH(Sheet1!$C$17, Sheet1!$A$21:$A$810, -1))

returns A22:A151.

Back to top

Get the Excel file


Dynamic-stock-chart.xlsx

Back to top

2.7. How to make the chart even more dynamic

stock chart - udf

This user defined function allows you to fetch past data for any quote on yahoo finance. You don't have to go to the yahoo finance website and copy/paste the data, this udf does it for you automatically. Just type the stock quote in cell D17 and press enter.

If you are looking for a particular company, index or commodity go to yahoo finance and use their "Quote lookup" to find the quote you are looking for.

Yes, you still need to manually adjust the y axis values on the chart every time you change the date range or stock quote unless you check out this post.

Back to top