Skip to main content

How to get historical prices

Use Twelve Data to fetch historical OHLCV data across instruments and time intervals

Team avatar
Written by Team
Updated over 3 weeks ago

Through Twelve Data, you can also fetch the data for various intervals. The following time frames are supported: 1min, 5min, 15min, 30min, 45min, 1h, 2h, 4h, 8h, 1day, 1week, 1month.

At this stage, it is important to find out:

  • Difference between intraday and daily intervals

  • What is the earliest available data date

  • Method to access historical prices

Intraday vs. daily intervals

At Twelve Data, all intervals can be divided into two categories: intraday and daily data. The main difference lies in the amount of historical data available. Note that intraday data is generally available for the past couple of years. Additionally, the 1-minute OHLC data is available only from February 10, 2020, at market open (09:30 AM EST), and historical data prior to this date is not provided.

Daily intervals include 1day, 1week, and 1month and contain data for the complete history since the first trading date for most symbols.

Intraday intervals include 1min, 5min, 15min, 30min, 45min, 1h, 2h, 4h, 5h, and 8h, and typically span multiple years for most symbols.

Earliest available date

To check the earliest available date, use the /earliest_timestamp API endpoint.

The depth of historical data varies significantly by instrument type and interval. For equities, daily and higher intervals provide a complete history dating back to the first trading date, while intraday intervals typically range from a few months to a few years.

Forex and cryptocurrencies also offer extensive daily historical data, with intraday data available for multiple years.

How to get the prices

Once you know the first available date, and you want to extract the corresponding information, you can use two methods.

Option 1: outputsize

Specify the number of records to be returned with the outputsize parameter. This is the simplest and the most efficient approach; however, the maximum value to be set is 5000.

Option 2: start_date and end_date

Filter data using start_date and end_date parameters. This provides great flexibility for manipulating the data, but it has some nuances, such as the correct parameter order, the "sliding window" approach, and a few others.

start_date

Using this parameter alone has no effect unless start_date exceeds the range specified by outputsize.

Example

https://api.twelvedata.com/time_series?start_date=2020-05-06&outputsize=5&symbol=aapl&interval=1day&apikey=xxx

This request will return the most recent 5 records, as the condition is met. However, if the start_date falls within the range between the current day and the outputsize limit, truncation will occur.

end_date

This parameter defines the maximum possible datetime value for the time series.

Example
โ€‹https://api.twelvedata.com/time_series?end_date=2020-05-06&outputsize=5&symbol=aapl&interval=1day&apikey=xxx

In this case, the output will include the last five records: [2020-05-05, 2020-05-04, 2020-05-01, 2020-04-30, 2020-04-29]. The number of rows is entirely determined by the outputsize parameter.

Combined usage

When start_date and end_date are used together, these parameters set the lower and upper boundaries for the response.

Example
โ€‹https://api.twelvedata.com/time_series?start_date=2020-01-06&end_date=2020-05-06&symbol=aapl&interval=1day&apikey=xxx

This request returns all values between the specified dates. Note that the outputsize parameter is omitted in this case; including it would restrict the output.

๐Ÿ’กTip: When requesting historical data, the response time might increase. Try to get it once, cache and update it later with the real-time data.

Examples

Earliest historical date for AAPL for EOD data

Minute-by-minute price for the entire day

Weekly prices for the last 9 months

Read more

Did this answer your question?