Market Data Logo
Historical Stock Data API
Last Update: June 4, 2024

How To Use The Historical Stock Data API

Learn by example and get started with the historical stock data api. Both daily and intraday data is available going back decades.

Market Data offers a historical stock prices API that makes it easy for any user who needs access to daily or intraday historical U.S. stock market data via an API. The most common use for historical stock data is charts and that’s why our API uses the OHLCV candlestick format when returning prices. The API can be easily integrated with all of the most popular charting packages such as TradingView.

Get started below clicking on any of our examples and try it yourself. Our API can be used with the AAPL ticker with no authentication needed. No account is necessary. Register a free account only when you’d like to begin working with other tickers.

Throughout this post, the links for these API examples have been created using the dateformat=timestamp parameter to allow you to better read the output in your web browser. However, the default format is unix time.

User-Defined Aggregates For The Historical Stock Data API

In addition to the pre-defined intraday resolutions: 1 minute, 5 minutes, 15 minutes, etc., the API supports user-defined aggregate resolutions. That means you can define any aggregation period. Use 3 minute or 17 minute candlesticks if you wish; anything is possible. The same is true for larger resolutions. Group together the daily candlesticks however you wish: 2 days, 3 days, weekly, monthly, etc.

Use a single number for a minute-based resolution. Use a number then the letter “H” for an hourly resolution (i.e. 2H, 4H). Use “D” for a daily resolution (i.e. 2D, 4D). And finally, use W for weekly or M for monthly in the same manner.

Using Dates With The Historical Stock Data API

There are a number of different features that allow you to manage the date output of the historical stock data API.

Use From and To Together to Restrict Output Between Two Dates

The most common use of the API is to request candles between two dates. Use the from and to parameters to accomplish this. The API will output all data including the from date up to, but not including, the to date.

Use Countback and To When The Start Date Isn’t Known

Most times when using candlesticks, you know the dates you need data for. However, sometimes when charting the end-user hasn’t specified a specific date and is just opening a chart for the first time. Your charting software might need exactly n number of candles. Instead of requiring your application to calculate from and to dates, you can just use to and then specify the number of candles you need using the countback attribute. This is especially useful since you don’t need to worry about market hours or days when the market was closed. Just request a specific number of candles and the API will search backward from your to date until it has fetched the quantity of candles requested.

Use Relative Dates To Keep Current

Use English-language works like “today” and “yesterday” to generate API queries that will keep themselves up to date automatically based on the current day. A full list of supported relative date words can be found in our documentation.

Manage Pagination With Limit & Offset

The candles API can be configured for pagination using the limit and offset parameters. This allows your application to fetch only the candles you actually need and make follow-up queries as you change pages or scroll a chart. The first page will be the oldest results and the last page will be the newest results.

Download The Results in CSV Format

Need to import the data to a spreadsheet? Just add format=csv and the API will generate a downloadable CSV file that can be easily imported to whatever spreadsheet software you’re using. If you plan on using Google Sheets, we recommend our Sheets Add-On which supports querying our API via native formulas.

If you’re working with intraday data and the timestamp date format isn’t easy to work with, just use dateformat=spreadsheet to automatically format dates into Excel/Sheets date format (days after the Excel epoch). When you open the CSV the t column will appear as a set of numbers at first, but convert the column to datetime and that will make the data readable as a date and time.

Use The Historical Stock Data API For Free

Get started right away using the AAPL ticker. When you’re ready to move on to other tickers, Market Data’s Free Forever plan includes 100 free queries per day to help you get started. Feel free to use one of our 30 day free trials to demo our Starter or Trader plans which come with 10,000 or 50,000 daily queries.

On this page
    Add a header to begin generating the table of contents
    Share this article
    Comments & Questions
    Shopping Basket