Chart Data
Understand how to obtain securities' indexes in the form of lists to be used in charts.
We'll cover the following
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 |
| optional | string | This parameter contains the tickers of the securities with whom we want to compare the security we are analyzing. |
| 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 |
| 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 |
| optional | string | This parameter represents the language in which we want the information; possible values include |
| optional | string | This parameter represents the region for which we want the information. The possible options for this parameter are |
| 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 |
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.
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 |
| This is an array that contains the lists for the number of shares of the security and the market indexes of the security. |
| This is the list of highest indexes for the given timestamps. |
| This is the list of the timestamps for which the data is provided. |
| This is the list of open indexes for the given timestamps. |
| This is the list of lowest indexes for the given timestamps. |
| This is the list of closing indexes for the given timestamps. |
| This is the list of the timestamps for which the data is provided. |
| This contains the indexes and volume just like |
Note: The response also contains other important information. The response fields can vary depending on the query parameter values.