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.- Endpoint:
https://api.marketdata.app/v1/stocks/candles/{resolution}/{ticker}
- Stock Candles Documentation
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.
- 1 Minute: https://api.marketdata.app/v1/stocks/candles/1/AAPL?from=2022-01-01&to=2022-01-10
- 3 Minute: https://api.marketdata.app/v1/stocks/candles/3/AAPL?from=2022-01-01&to=2022-01-31
- Daily: https://api.marketdata.app/v1/stocks/candles/D/AAPL?from=2022-01-01&to=2022-12-31
- 4 Hour: https://api.marketdata.app/v1/stocks/candles/4H/AAPL?from=2022-01-01&to=2022-03-31
- 2 Weeks: https://api.marketdata.app/v1/stocks/candles/2W/AAPL?from=2020-01-01&to=2022-12-31
- 1 Month: https://api.marketdata.app/v1/stocks/candles/M/AAPL?from=2020-01-01&to=2022-12-31
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.
- Last 252 Trading Days: https://api.marketdata.app/v1/stocks/candles/daily/AAPL?to=today&countback=252
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.
- Limit: https://api.marketdata.app/v1/stocks/candles/daily/AAPL?limit=10
- Limit & Offset: https://api.marketdata.app/v1/stocks/candles/daily/AAPL?limit=10&offset=20
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.
- CSV Download: https://api.marketdata.app/v1/stocks/candles/4H/AAPL?from=2022-01-01&to=2022-12-31&dateformat=timestamp&format=csv
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.