Historical Data

Learn to get the trends of a security for a specified interval.

The past does not dictate how a security will perform in the future but it does tell us how a security copes up with the external factors affecting the stock markets.

We can use the YH Finance API to retrieve historical data about securities for a specified interval. This historical data can help us show the trend of that particular security to traders.

The base URL https://yfapi.net/v8/finance/spark is used to get this data.

Request parameters

The following three query parameters can be used with this endpoint:

Parameters

Category

Type

Description

symbols

required

string

This is the symbol of the security we want to search for. We can search for multiple stocks as well by separating the symbols of the securities using a comma. The table in the Appendix contains some symbols that we can use to test this endpoint.

interval

optional

string

This sets the period of time between two datasets. The intervals have to be in minutes, days, weeks, or months. The units used for minutes, days, weeks, and months will be m, d, w, and mo respectively.

range

optional

string

This shows how far back we want the starting data to be. This range has to be defined in days, months, or years. The units used for days, months, and years are d, mo, and y respectively. We can also use max in place of these ranges to get our data from as far back as possible.

The code below shows how we can use this endpoint to get five days' data for Apple Inc. in one-day intervals. Click "Run" to call this endpoint and see the retrieved information.

Press + to interact
const endpointUrl = new URL('https://yfapi.net/v8/finance/spark');
const queryParameters = new URLSearchParams({
interval: '1d',
range: '5d',
symbols: 'AAPL' //AAPL is the symbol for Apple Inc.
});
const headerParameters = {
'X-API-KEY': '{{API_KEY}}'
}
const options = {
method: 'GET',
headers: headerParameters
};
async function fetchHistoricalData() {
try {
endpointUrl.search = queryParameters;
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
fetchHistoricalData();

Let's look at the code in the widget above:

  • Line 1: We set the URL to https://yfapi.net/v8/finance/spark.

  • Lines 3–7: We define the query parameters.

  • Lines 18–26: We define a function fetchHistoricalData() that calls the API and prints the response.

We can replace AAPL in line 6 with the symbol of any other security to get data for that security. Change the interval and range parameters to specify the amount of data to retrieve.

Response fields

The response contains timestamps for the closing time of the last five days and the price of Apple Inc. stocks at those times. Some important response elements are listed in the table below.

Response fields

Description

timestamp

This is the list of the timestamps for which the data is provided.

close

This is the list of the closing indexes of the security at the given timestamps.

Note: Only some of the possible elements of response have been discussed here to keep the lesson concise.