Open and Close
Learn to get the historical open, close, highest, and lowest prices of securities.
We'll cover the following
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.
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 |
| String | Mandatory | The ticker symbol of the stock or options contract to search. |
| String | Mandatory | This is the date we want to see the open and close price of securities. The date format is YYYY-MM-DD. |
| String | Optional | This is set to |
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.
// Importing libraries hereimport fetch from "node-fetch";// Define API key hereconst apiKey = '{{API_KEY}}';// Define endpoint URL hereconst endpointUrl = new URL(`https://api.polygon.io/v1/open-close/{{TICKER}}/{{date}}`);// Define Header Parameters hereconst headerParameters = {authorization: `Bearer ${apiKey}`};// Setting API call optionsconst options = {method: 'GET',headers: headerParameters,};// Function to make API callasync function DailyOpenClose() {try {const response = await fetch(endpointUrl, options);// Custom function for printing the API responseprintResponse(response);}catch (error) {// Custom function for printing the error messageprintError(error);}}// Calling function to make API callDailyOpenClose();
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 |
| String | The response to our query. |
| String | The date for which the data is requested. |
| String | The exchange symbol associated with our security. |
| Integer | The opening price of our security. |
| Integer | The highest price observed for our security on the given date. |
| Integer | The lowest price observed for our security on the given date. |
| Integer | The closing price of our security. |
| Integer | The volume observed by our security on the given date. |
| Integer | The closing price of the security in the after-hours market. |
| Integer | The opening price of the security in premarket trading. |
| Boolean | This is |
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.
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 |
| String | Mandatory | The |
| String | Mandatory | The |
| 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. |
| String | Optional | This is set to |
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.
// Importing libraries hereimport fetch from "node-fetch";// Define API key hereconst apiKey = '{{API_KEY}}';// Define endpoint URL hereconst endpointUrl = new URL(`https://api.polygon.io/v1/open-close/crypto/{{From}}/{{To}}/{{Date}}`);// Define Header Parameters hereconst headerParameters = {authorization: `Bearer ${apiKey}`};// Setting API call optionsconst options = {method: 'GET',headers: headerParameters,};// Function to make API callasync function CryptoOpenClose() {try {const response = await fetch(endpointUrl, options);// Custom function for printing the API responseprintResponse(response);} catch (error) {// Custom function for printing the error messageprintError(error);}}// Calling function to make API callCryptoOpenClose();
Here’s an explanation for the above code:
Line 8: We define the endpoint URL. We provide
from
,to
, anddate
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 |
| String | The symbol of the trading pair given in the query |
| Boolean | This is |
| String | The date given in the request parameter |
| Integer | The opening price of the given cryptocurrency |
| Integer | The closing price of the given cryptocurrency |
| 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. |
| 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.
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 |
| String | Mandatory | The ticker symbol associated with the stock or options contract we're looking for. |
| String | Optional | This is set to |
Let’s try to call the previous close endpoint in the following code. Try to change the value of the ticker by clicking “Edit.”
// Importing libraries hereimport fetch from "node-fetch";// Define API key hereconst apiKey = '{{API_KEY}}';// Define endpoint URL hereconst endpointUrl = new URL(`https://api.polygon.io/v2/aggs/ticker/{{TICKER}}/prev`);// Define Header Parameters hereconst headerParameters = {authorization: `Bearer ${apiKey}`};// //Define Query Parameters hereconst queryParameters = new URLSearchParams({adjusted: 'true',})// Setting API call optionsconst options = {method: 'GET',headers: headerParameters,};// Function to make API callasync function PreviousClose() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);// Custom function for printing the API responseprintResponse(response);} catch (error) {// Custom function for printing the error messageprintError(error);}}// Calling function to make API callPreviousClose();
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
totrue
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 |
| String | The ticker passed as an input parameter. |
| Integer | The number of aggregates used to give a response to our query. |
| Integer | The total number of results generated for our query. |
| Boolean | This is |
| Object | This contains information about the given ticker. |
| String | This is the given ticker. |
| Integer | The volume of the ticker in the given time window. |
| Integer | The weighted average volume in the given time window. |
| Integer | The opening price of the ticker. |
| Integer | The closing price of the ticker. |
| Integer | The highest price of the ticker. |
| Integer | The lowest price of the ticker. |
| Integer | The time since the start of the aggregate window in milliseconds. |
| Integer | The total number of transactions in the aggregate window. |
| String | The status of our query. |
| String | The ID associated with our query. |