Chart Data

Understand how to obtain securities' indexes in the form of lists to be used in charts.

Charts can help us compile large amounts of data and show it to the user in an easy-to-understand form. They can help the user better understand the trends of a security.

The YH API provides us an endpoint that can give us the information we can use to create charts to give the traders a better insight. The base URL of this endpoint is https://yfapi.net/v8/finance/chart/{ticker}. The {ticker} parameter at the end of the endpoint will be replaced with the ticker of the security we want to search for when making an API call. For example, if we want to get chart data for Apple Inc., our endpoint will be https://yfapi.net/v8/finance/chart/AAPL.

It can provide us with more detailed information when compared to similar endpoints. It can also provide us with comparative data, which can be plotted to compare the performance of multiple securities.

Request Parameters

This endpoint has the following six query parameters:

Parameters

Category

Type

Description

comparisons

optional

string

This parameter contains the tickers of the securities with whom we want to compare the security we are analyzing.

events

optional

string

This parameter is used if we want to get information about splits or dividends with the other chart data. The possible values for this parameter are split, div, or split,div.

interval

optional

string

This sets the interval windows for which we want the data. The intervals must be in minutes, days, weeks, or months. The units used for minutes, days, weeks, and months will be m, d, w, and mo respectively.

lang

optional

string

This parameter represents the language in which we want the information; possible values include en, es, de, fr,it, and zh. The table in the Appendix shows which language each of these symbols represents.

region

optional

string

This parameter represents the region for which we want the information. The possible options for this parameter are AU, CA, DE, ES, FR, GB, HK, IN, IT, and US. The table in the Appendix shows which region each of these symbols represents.

range

optional

string

This indicates how far back we want the data to start. 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 chart data for Apple. We're also getting data for Airbnb Inc. to be compared with the data of our primary security. Click "Run" to retrieve this data.

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

Let's look at our code:

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

  • Lines 4–8: We define the query parameters.

  • Lines 19–27: We define a function named fetchChartData() that calls the API and prints the response.

We can change the AAPL in line 1 with the ticker of any other security we want to get chart data for. To get comparative data for security other than Airbnb Inc., we can replace its ticker ABNB in line 5 with the ticker of that security. Changing the values of interval and range in lines 6–7 will change the amount of data we'll get in response.

Response fields

The code segment shown in the widget above returns the open, close, highest, and lowest prices of Apple Inc. for the last five days. It also returns the same data for Airbnb Inc. Some important response fields are mentioned in the table below:

Response fields

Description

quote

This is an array that contains the lists for the number of shares of the security and the market indexes of the security.

high

This is the list of highest indexes for the given timestamps.

volume

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

open

This is the list of open indexes for the given timestamps.

low

This is the list of lowest indexes for the given timestamps.

close

This is the list of closing indexes for the given timestamps.

timestamp

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

comparisons

This contains the indexes and volume just like quote but this data is for the security with which we are comparing the primary security.

Note: The response also contains other important information. The response fields can vary depending on the query parameter values.