Open and Close

Learn to get the historical open, close, highest, and lowest prices of securities.

In this lesson, we’ll discuss two endpoints: the daily open-close endpoint and the previous close endpoint, which give details about the securities’ price at the opening and closing of the market.

The following table provides some tickers that can be used as input in the daily open-close and previous close endpoint:

Security

Market

Ticker

The Walt Disney Company

Stocks

DIS

Apple Inc.

OTC

O:AAPL221202C00050000

Thai Baht - South Korean Won

Forex

C:THBKRW

ApeCoin - United States Dollar

Cryptocurrency

X:APEUSD

Daily open-close

The daily open price indicates the value of a security when the trading started. Similarly, the closing price is the value of the security it last traded before the market closes. Polygon provides free access to open and close prices of stocks and options for any specific date within the previous two years with the daily open-close endpoint.

Press + to interact
The daily open-close endpoint
The daily open-close endpoint

The base URL for the daily open-close endpoint for stocks and options contracts is as follows:

https://api.polygon.io/v1/open-close/{ticker}/{date}

The daily open-close endpoint has some mandatory request parameters in addition to the API key. The following table provides details about these parameters:

Request Parameters

Name

Type

Category

Description

ticker

String

Mandatory

The ticker symbol of the stock or options contract to search.

date

String

Mandatory

This is the date we want to see the open and close price of securities. The date format is YYYY-MM-DD.

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.

Let’s try to retrieve open-close details for a stock in the following code. Try to change the value of the ticker and date by clicking “Edit.” Make sure to provide a date within the previous two years, and don’t forget to click “Save” after changing the values. Click the “Run” button to view the output.

Press + to interact
// Importing libraries here
import fetch from "node-fetch";
// Define API key here
const apiKey = '{{API_KEY}}';
// Define endpoint URL here
const endpointUrl = new URL(`https://api.polygon.io/v1/open-close/{{TICKER}}/{{date}}`);
// Define Header Parameters here
const headerParameters = {
authorization: `Bearer ${apiKey}`
};
// Setting API call options
const options = {
method: 'GET',
headers: headerParameters,
};
// Function to make API call
async function DailyOpenClose() {
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
DailyOpenClose();

Here’s an explanation for the above code:

  • Line 8: We define the endpoint URL and provide a ticker and date.

  • Lines 11–13: We define the API key in the header parameter.

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

  • Lines 22–32: We define the DailyOpenClose function, which calls the endpoint.

  • Line 35: We call the DailyOpenClose function.

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

Response Fields

Name

Type

Description

status

String

The response to our query.

from

String

The date for which the data is requested.

symbol

String

The exchange symbol associated with our security.

open

Integer

The opening price of our security.

high

Integer

The highest price observed for our security on the given date.

low

Integer

The lowest price observed for our security on the given date.

close

Integer

The closing price of our security.

volume

Integer

The volume observed by our security on the given date.

afterHours

Integer

The closing price of the security in the after-hours market.

preMarket

Integer

The opening price of the security in premarket trading.

otc

Boolean

This is true if the given query is for an over-the-counter ticker, meaning the asset is traded between two parties instead of going through a stock market. This attribute is not included in the response object if it is false.

Crypto open-close

Cryptocurrencies often include trading pairs, where one currency can be traded for another. These pairs are written in the from-to format. Some common trading pairs are ETH-BTC, BTC-USD, etc.

Press + to interact

From the previous two years, we can enquire about the daily open-close prices for a specific cryptocurrency on a particular day by utilizing the crypto open-close endpoint.

The base URL for this endpoint is as follows:

https://api.polygon.io/v1/open-close/crypto/{from}/{to}/{date}

This endpoint requires some mandatory request parameters. The following table includes an overview of the required and optional parameters.

Request Parameters

Name

Type

Category

Description

from

String

Mandatory

The from part of the cryptocurrency pair

to

String

Mandatory

The to part of the cryptocurrency pair

date

String

Mandatory

This is the date we want to see the open and close price of a cryptocurrency. The date format is YYYY-MM-DD.

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.

Let’s retrieve the daily open-close prices of the pair USD-BTC on 2022-07-07 in the following code. Try to change the input values by clicking the “Edit” button.

Press + to interact
// Importing libraries here
import fetch from "node-fetch";
// Define API key here
const apiKey = '{{API_KEY}}';
// Define endpoint URL here
const endpointUrl = new URL(`https://api.polygon.io/v1/open-close/crypto/{{From}}/{{To}}/{{Date}}`);
// Define Header Parameters here
const headerParameters = {
authorization: `Bearer ${apiKey}`
};
// Setting API call options
const options = {
method: 'GET',
headers: headerParameters,
};
// Function to make API call
async function CryptoOpenClose() {
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
CryptoOpenClose();

Here’s an explanation for the above code:

  • Line 8: We define the endpoint URL. We provide from, to, and date as request parameters in the URL.

  • Lines 11–13: We define the API key in the header parameter.

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

  • Lines 22–32: We define the CryptoOpenClose function, which calls the endpoint.

  • Line 35: We call the CryptoOpenClose function.

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

Response Fields

Name

Type

Description

symbol

String

The symbol of the trading pair given in the query

isUTC

Boolean

This is true if the timestamps are in the UTC timezone

day

String

The date given in the request parameter

open

Integer

The opening price of the given cryptocurrency

close

Integer

The closing price of the given cryptocurrency

openTrades

Object

This includes details about open trades such as condition codes, trade ID, price of the trade, volume or size of the trade, timestamp in milliseconds, and exchange market ID.

closeTrades

Object

This includes details about closed trades such as condition codes, trade ID, price of the trade, volume or size of the trade, timestamp in milliseconds, and exchange market ID.

Previous close

It’s often noted that the opening price of a security is not the same as its previous closing price. We can get the historical opening and closing prices for any date using the daily open-close endpoint. However, the previous close endpoint is used to fetch data for the last day.

Press + to interact
The previous close endpoint
The previous close endpoint

The base URL for this endpoint is as follows:

https://api.polygon.io/v2/aggs/ticker/{Ticker}/prev

The request parameters for this endpoint are as follows:

Request Parameters

Name

Type

Category

Description

ticker

String

Mandatory

The ticker symbol associated with the stock or options contract we're looking for.

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.

Let’s try to call the previous close endpoint in the following code. Try to change the value of the ticker by clicking “Edit.”

Press + to interact
// Importing libraries here
import fetch from "node-fetch";
// Define API key here
const apiKey = '{{API_KEY}}';
// Define endpoint URL here
const endpointUrl = new URL(`https://api.polygon.io/v2/aggs/ticker/{{TICKER}}/prev`);
// Define Header Parameters here
const headerParameters = {
authorization: `Bearer ${apiKey}`
};
// //Define Query Parameters here
const queryParameters = new URLSearchParams({
adjusted: 'true',
})
// Setting API call options
const options = {
method: 'GET',
headers: headerParameters,
};
// Function to make API call
async function PreviousClose() {
try {
endpointUrl.search = queryParameters;
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
PreviousClose();

Here’s an explanation for the above code:

  • Line 8: We define the endpoint URL.

  • Lines 11–13: We define the API key in the header parameter.

  • Lines 16–18: We set adjusted to true in the query parameters.

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

  • Lines 27–38: We define the PreviousClose function, which calls the endpoint.

  • Line 41: We call the PreviousClose function.

We can find details about the response attributes in the table given below.

Response Fields

Name

Type

Description

ticker

String

The ticker passed as an input parameter.

queryCount

Integer

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

resultsCount

Integer

The total number of results generated for our query.

adjusted

Boolean

This is true if the results are adjusted for splits.

results

Object

This contains information about the given ticker.

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 status of our query.

request_id

String

The ID associated with our query.