Bar Charts

Learn to retrieve historical data to create charts.

Aggregates endpoint

The aggregate endpoint available in Polygon provides aggregate bars of a security over a specified time window. We can use this data to analyze growth or decline in a security's values.

Press + to interact
The aggregates endpoint
The aggregates endpoint

The base URL for the aggregates endpoint is as follows:

https://api.polygon.io/v2/aggs/ticker/{ticker}/range/{multiplier}/{timespan}/{from}/{to}

Request parameters

The aggregates endpoint requires some mandatory and optional parameters to filter our output. The following table gives details about these parameters.

Name

Type

Category

Description

ticker

String

Mandatory

The ticker associated with the security we want.

timespan

String

Mandatory

The time over which the results are recorded.

Possible input: minute, hour, day, week, month, quarter, or year.

So, with a multiplier of two and a timespan of an hour, we will get data for every two hours.

multiplier

Integer

Mandatory

The size of the timespan multiplier.

from

String

Mandatory

This defines the starting date YYYY-MM-DD or timestamp in milliseconds of the time window.

to

String

Mandatory

This defines the ending date YYYY-MM-DD or timestamp in milliseconds of the time window.

adjusted

String

Optional

This is set to true if we want the result to be adjusted for splits. By default, it is set to true.

limit

Integer

Optional

This defines the number of results required. By default, it is set to 5000 and the maximum limit we can provide is 50000.

sort

String

Optional

This is used to sort the results based on timestamp.

Accepted input: asc or desc for ascending and descending, respectively.

Let’s try to call this endpoint in the following code:

Press + to interact
// Importing libraries here
import fetch from "node-fetch";
// Define API key here
const apiKey = '{{API_KEY}}';
//Required parameters
const ticker = "AAAU";
const multiplier = 1;
const timespan = 'day';
const from_ = '2021-07-07';
const to = '2021-07-14';
// Define endpoint URL here
const endpointUrl = new URL(`https://api.polygon.io/v2/aggs/ticker/${ticker}/range/${multiplier}/${timespan}/${from_}/${to}`);
// Define Header Parameters here
const headerParameters = {
authorization: `Bearer ${apiKey}`,
contentType: 'application/json',
};
// Setting API call options
const options = {
method: 'GET',
headers: headerParameters,
};
// Function to make API call
async function Aggregates() {
try {
const response = await fetch(endpointUrl, options);
// Custom function for printing the API response
printResponse(response);
} catch (error) {
// Custom function for printing the error message
printError(error);
}
}
// Calling function to make API call
Aggregates();

Here’s an explanation for the above code:

  • Lines 8–12: We define the required parameters.

  • Line 15: We define the endpoint URL.

  • Lines 18–21: We define the API key in the header parameter.

  • Lines 24–27: We define the options parameters and set the request type as GET.

  • Lines 30–40: We define the Aggregates function, which calls the endpoint.

  • Line 43: We call the Aggregates function.

Response fields

The following table provides details of the attributes in the response object.

Name

Type

Description

ticker

String

The ticker given in the request parameter.

queryCount

Integer

The number of aggregates used to generate the response.

resultsCount

Integer

The total number of results given.

adjusted

Boolean

This tells us if the response was adjusted for splits or not.

results

Object

This contains the details of our query.

results.v

Integer

The volume of the ticker in the given time window.

results.vw

Integer

The weighted average volume in the given time window.

results.o

Integer

The opening price of the ticker.

results.c

Integer

The closing price of the ticker.

results.h

Integer

The highest price of the ticker.

results.l

Integer

The lowest price of the ticker.

results.t

Integer

The time since the start of the aggregate window in milliseconds.

results.n

Integer

The total number of transactions in the aggregate window.

status

String

The status of our query.

request_id

String

The ID associated with our request.

count

Integer

The number of results returned.

Grouped daily endpoint

The grouped daily endpoint gives details about the opening, closing, highest, and lowest prices of the entire market on a specified date. This helps traders to compare the prices of different securities and make decisions based on them.

Press + to interact
The grouped daily endpoint
The grouped daily endpoint

The base URL for the grouped daily endpoint is given below:

https://api.polygon.io/v2/aggs/grouped/locale/{locale_type}/market/{security_type/{date}

Request parameters

The following table provides an overview of the parameters required to call the grouped daily endpoint:

Name

Type

Category

Description

locale_type

String

Mandatory

Filter results based on location.

Available options: us for stocks and global for cryptocurrency and forex.

security_type

String

Mandatory

The security whose data we want.

Possible input: stocks, fx, and crypto for stocks, forex, and cryptocurrency, respectively.

date

String

Mandatory

The starting date for the aggregate window.

adjusted

String

Optional

This is set to true if we want the result to be adjusted for splits. By default, it is set to true.

include_otc

Boolean

Optional

This parameter is only used in case our security_type is stocks. If true, the response includes OTC securities. By default, it is set to false.

Let’s try to call this endpoint in the code widget given below:

Press + to interact
// Importing libraries here
import fetch from "node-fetch";
// Define API key here
const apiKey = '{{API_KEY}}';
//Required paramenters
const locale_type = 'global';
const security_type = 'crypto';
const date = '2021-07-07';
// Define endpoint URL here
const endpointUrl = new URL(`https://api.polygon.io/v2/aggs/grouped/locale/${locale_type}/market/${security_type}/${date}`);
// Define Header Parameters here
const headerParameters = {
authorization: `Bearer ${apiKey}`,
contentType: 'application/json',
};
// Setting API call options
const options = {
method: 'GET',
headers: headerParameters,
};
// Function to make API call
async function GroupedDaily() {
try {
const response = await fetch(endpointUrl, options);
// Custom function for printing the API response
printResponse(response);
} catch (error) {
// Custom function for printing the error message
printError(error);
}
}
// Calling function to make API call
GroupedDaily();

Here’s an explanation for the above code:

  • Lines 8–10: We define the required parameters.

    • Line 8: We set the locale_type to global.

    • Line 9: We set the security_type to crypto to get results related to cryptocurrency.

    • Line 10: We set a date to specify the starting point of the aggregate window.

  • Line 13: We define the endpoint URL.

  • Lines 16–19: We define the API key in the header parameter.

  • Lines 22–25: We define the options parameters and set the request type as GET.

  • Lines 28–38: We define the GroupedDaily function, which calls the endpoint.

  • Line 41: We call the GroupedDaily function.

Change the required parameters’ values in lines 8–10 to get different results.

Response fields

The following table gives an overview of the attributes found in the response object of the grouped daily endpoint:

Name

Type

Description

queryCount

Integer

The number of aggregates used to generate a response to our query.

resultsCount

Integer

The total number of results for our query.

adjusted

Boolean

This tells us if the result is adjusted for splits or not.

results

Object

This object contains details about our query.

results.T

String

This is the given ticker.

results.v

Integer

The volume of the ticker in the given time window.

results.vw

Integer

The weighted average volume in the given time window.

results.o

Integer

The opening price of the ticker.

results.c

Integer

The closing price of the ticker.

results.h

Integer

The highest price of the ticker.

results.l

Integer

The lowest price of the ticker.

results.t

Integer

The time since the start of the aggregate window in milliseconds.

results.n

Integer

The total number of transactions in the aggregate window.

status

String

The response status to our query.

request_id

String

The ID assigned to our request by the server.

count

Integer

The number of results to our query.