Get Trade History
The Trade History API provides users with access to their historical trade and transaction data, allowing them to retrieve details of orders executed through Upstox platform. This API enables various use cases, including reviewing past month's trade activity, maintaining records for compliance or analysis, and other potential use case. Currently this API will give you data only for last 3 financial years.
Request
curl --location 'https://api.upstox.com/v2/charges/historical-trades?segment=EQ&start_date=2022-04-01&end_date=2023-03-31&page_number=1&page_size=100' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer {your_access_token}'
For additional samples in various languages, please refer to the Sample code section on this page.
Query Parameters
| Name | Required | Type | Description |
|---|---|---|---|
| segment | false | string | Segment for which data is requested can be from the following options, If not provide, will consider all the segment. EQ - Equity FO - Futures and Options COM - Commodity CD - Currency Derivatives MF - Mutual funds |
| start_date | true | string | Date from which data needs to be fetched. it should be within the last 3 financial years. Date format: YYYY-mm-dd. |
| end_date | true | string | Date till data needs to be fetched. it should be within the last 3 financial years. Date format: YYYY-mm-dd. |
| page_number | true | integer | Page number, the pages are starting from 1. |
| page_size | true | integer | Page size for pagination. |
Responses
- 200
- 4XX
Response Body
- EQ
- FO
- COM
- CD
- MF
{
"status": "success",
"data": [
{
"exchange": "NSE",
"segment": "EQ",
"option_type": "",
"quantity": 1,
"amount": 2252.35,
"trade_id": "75217259",
"trade_date": "2023-03-28",
"transaction_type": "BUY",
"scrip_name": "RELIANCE",
"strike_price": "0.0",
"expiry": "",
"price": 2252.35,
"isin": "INE002A01018",
"symbol": "RELIANCE",
"instrument_token": "NSE_EQ|INE002A01018"
}
],
"errors": null,
"meta_data": {
"page": {
"page_number": 1,
"page_size": 15,
"total_records": 15,
"total_pages": 1
}
}
}
{
"status": "success",
"data": [
{
"exchange": "NFO",
"segment": "FO",
"option_type": "CE",
"quantity": 25,
"amount": 15237.5,
"trade_id": "430927903",
"trade_date": "2023-03-31",
"transaction_type": "SELL",
"scrip_name": "BANKNIFTY",
"strike_price": "40000.0",
"expiry": "2023-04-06",
"price": 609.5,
"isin": "",
"symbol": "BANKNIFTY",
"instrument_token": ""
}
],
"errors": null,
"meta_data": {
"page": {
"page_number": 1,
"page_size": 15,
"total_records": 15,
"total_pages": 1
}
}
}
{
"status": "success",
"data": [
{
"exchange": "MCX",
"segment": "COM",
"option_type": "",
"quantity": 2,
"amount": 721000,
"trade_id": "110116226",
"trade_date": "2023-03-31",
"transaction_type": "BUY",
"scrip_name": "SILVERM",
"strike_price": "0.0",
"expiry": "2023-04-28",
"price": 72100,
"isin": "",
"symbol": "",
"instrument_token": ""
}
],
"errors": null,
"meta_data": {
"page": {
"page_number": 1,
"page_size": 15,
"total_records": 15,
"total_pages": 1
}
}
}
{
"status": "success",
"data": [
{
"exchange": "CDS",
"segment": "CD",
"option_type": "CE",
"quantity": 1,
"amount": 385,
"trade_id": "420354",
"trade_date": "2023-03-27",
"transaction_type": "SELL",
"scrip_name": "USDINR",
"strike_price": "82.0",
"expiry": "2023-03-28",
"price": 0.385,
"isin": "",
"symbol": "",
"instrument_token": ""
}
],
"errors": null,
"meta_data": {
"page": {
"page_number": 1,
"page_size": 15,
"total_records": 15,
"total_pages": 1
}
}
}
{
"status": "success",
"data": [
{
"exchange": "BMF",
"segment": "MF",
"option_type": "",
"quantity": 13,
"amount": 999.9852,
"trade_id": "",
"trade_date": "2023-02-09",
"transaction_type": "BUY",
"scrip_name": "AXIS SMALL CAP FUND - DIRECT P",
"strike_price": "0.0",
"expiry": "",
"price": 71.9,
"isin": "INF846K01K35",
"symbol": "",
"instrument_token": "BMF_MF|INF846K01K35"
}
],
"errors": null,
"meta_data": {
"page": {
"page_number": 1,
"page_size": 15,
"total_records": 15,
"total_pages": 1
}
}
}
| Name | Type | Description |
|---|---|---|
| status | string | A string indicating the outcome of the request. Typically success for successful operations. |
| data | object[] | Response data for historical trade reports |
| data[].exchange | string | Exchange to which the order is associated. Valid exchanges can be found in the Exchange Appendix |
| data[].segment | string | Segment to which the order is associated. Possible values: EQ, FO, CD, COM, MF. |
| data[].option_type | string | Option type of the option contracts. Possible values: CE, PE. Option type is available only in case of FO and CD segment. |
| data[].quantity | integer | Quantity with which the order was placed. |
| data[].amount | float | Total amount at which order is bought/sold. |
| data[].trade_id | string | Trade ID generated from exchange towards traded transaction |
| data[].trade_date | string | The date on which the order was bought/sold |
| data[].transaction_type | string | Indicates whether its a buy or sell order. Possible values: BUY, SELL. |
| data[].scrip_name | string | Name of the scrip traded |
| data[].strike_price | float | The strike price for the option. |
| data[].expiry | string | Expiry date (for derivatives). Data format is YYYY-mm-dd. |
| data[].price | float | Price at which the traded quantity is traded. |
| data[].isin | string | This represents the standard ISIN for stocks listed on multiple exchanges. ISIN is available in case of EQ and MF segment. |
| data[].symbol | string | Shows the trading symbol of the instrument. Symbol is available in case of EQ and FO segment. |
| data[].instrument_token | string | Key of the instrument. For the regex pattern applicable to this field, see the Field Pattern Appendix. Instrument token is available in case of EQ and MF segment. |
| metadata | object | Meta data for historical trade data |
| metadata.page | object | Meta data for page. |
| metadata.page.page_number | integer | Page number for pagination |
| metadata.page.page_size | integer | Page size |
Error codes
| Error code | Description |
|---|---|
| UDAPI1066 | The segment is invalid - The provided segment value isn't recognized or accepted. |
| UDAPI1091 | The page_number is required - You need to specify the page number for the data you're trying to retrieve. |
| UDAPI1092 | The page_size is required - The page size, indicating the number of results per page, must be provided. |
| UDAPI1093 | The start_date and end_date exceed the financial year limit. - Ensure your date ranges lies within the last 3 financial years. |
| UDAPI1094 | end_date must be greater than or equal to start_date and Date should be in valid format: yyyy-MM-dd - Ensure your date ranges and formats are correct. |
| UDAPI1120 | The page_size should be greater than or equal to 1: page_size must be at least 1 |
| UDAPI1121 | The page_size should be less than or equal to 5000: page_size must be no more than 5000 |
| UDAPI1122 | The page_number should be greater than or equal to 1: page_number must be at least 1 |
Sample Code
Get trade history for equity segment
- Curl
- Python
- Node.js
- Java
- PHP
- Python SDK
- Node.js SDK
- Java SDK
curl --location 'https://api.upstox.com/v2/charges/historical-trades?segment=EQ&start_date=2022-04-01&end_date=2023-03-31&page_number=1&page_size=100' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer {your_access_token}'
import requests
url = 'https://api.upstox.com/v2/charges/historical-trades'
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer {your_access_token}'
}
params = {
'segment': 'EQ',
'start_date': '2022-04-01',
'end_date': '2023-03-31',
'page_number': '1',
'page_size': '100'
}
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
data = response.json()
print(data)
else:
print(f"Error: {response.status_code} - {response.text}")
const axios = require('axios');
const url = 'https://api.upstox.com/v2/charges/historical-trades';
const headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer {your_access_token}'
};
const params = {
'segment': 'EQ',
'start_date': '2022-04-01',
'end_date': '2023-03-31',
'page_number': '1',
'page_size': '100'
};
axios.get(url, { headers, params })
.then(response => {
console.log(response.data); // or do something with the data
})
.catch(error => {
console.error('Error:', error.response.data);
});
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) throws IOException {
String baseUrl = "https://api.upstox.com/v2/charges/historical-trades";
String accessToken = "{your_access_token}";
String segment = "EQ";
String startDate = "2022-04-01";
String endDate = "2023-03-31";
int pageNumber = 1;
int pageSize = 100;
URI uri = URI.create(String.format("%s?segment=%s&start_date=%s&end_date=%s&page_number=%d&page_size=%d",
baseUrl, segment, startDate, endDate, pageNumber, pageSize));
URL url = uri.toURL();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Authorization", "Bearer " + accessToken);
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String responseBody = reader.lines().collect(Collectors.joining(System.lineSeparator()));
System.out.println(responseBody);
reader.close();
} else {
System.out.println("Error: " + responseCode + " - " + connection.getResponseMessage());
}
connection.disconnect();
}
}
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.upstox.com/v2/charges/historical-trades?segment=EQ&start_date=2022-04-01&end_date=2023-03-31&page_number=1&page_size=100',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Accept: application/json',
'Authorization: Bearer {your_access_token}'
],
]);
$response = curl_exec($curl);
if ($response === false) {
echo 'Error: ' . curl_error($curl);
} else {
echo $response;
}
curl_close($curl);
?>
import upstox_client
from upstox_client.rest import ApiException
configuration = upstox_client.Configuration()
configuration.access_token = "{your_access_token}"
api_instance = upstox_client.PostTradeApi(upstox_client.ApiClient(configuration))
param = {
'segment': "EQ"
}
try:
api_response = api_instance.get_trades_by_date_range("2023-04-01", "2025-03-31",1,1000,**param)
print(api_response)
except ApiException as e:
print("Exception when calling OrderApi->get trades_by_date_range: %s\n" % e.body)
let UpstoxClient = require('upstox-js-sdk');
let defaultClient = UpstoxClient.ApiClient.instance;
var OAUTH2 = defaultClient.authentications['OAUTH2'];
OAUTH2.accessToken = "{your_access_token}";
apiInstance = new UpstoxClient.PostTradeApi();
opts = {
segment: "EQ"
}
apiInstance.getTradesByDateRange("2023-04-01","2024-08-30",1,1000,opts,(error, data, response) => {
if (error) {
console.error(error.response.text);
} else {
console.log('API called successfully. Returned data: ' + JSON.stringify(data));
}
});
import com.upstox.ApiClient;
import com.upstox.ApiException;
import com.upstox.Configuration;
import com.upstox.api.TradeHistoryResponse;
import com.upstox.auth.OAuth;
import io.swagger.client.api.PostTradeApi;
public class Main {
public static void main(String[] args) {
ApiClient defaultClient = Configuration.getDefaultApiClient();
OAuth OAUTH2 = (OAuth) defaultClient.getAuthentication("OAUTH2");
OAUTH2.setAccessToken("{your_access_token}");
PostTradeApi apiInstance = new PostTradeApi();
try {
TradeHistoryResponse result = apiInstance.getTradesByDateRange("2023-04-01","2025-08-01",1,1000,"EQ");
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling OrderApi#getTradesByDateRange= " + e.getResponseBody());
e.printStackTrace();
}
}
}
Get trade history for futures and options segment
- Curl
- Python
- Node.js
- Java
- PHP
- Python SDK
- Node.js SDK
- Java SDK
curl --location 'https://api.upstox.com/v2/charges/historical-trades?segment=FO&start_date=2022-04-01&end_date=2023-03-31&page_number=1&page_size=100' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer {your_access_token}'
import requests
url = 'https://api.upstox.com/v2/charges/historical-trades'
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer {your_access_token}'
}
params = {
'segment': 'FO',
'start_date': '2022-04-01',
'end_date': '2023-03-31',
'page_number': '1',
'page_size': '100'
}
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
data = response.json()
print(data)
else:
print(f"Error: {response.status_code} - {response.text}")
const axios = require('axios');
const url = 'https://api.upstox.com/v2/charges/historical-trades';
const headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer {your_access_token}'
};
const params = {
'segment': 'FO',
'start_date': '2022-04-01',
'end_date': '2023-03-31',
'page_number': '1',
'page_size': '100'
};
axios.get(url, { headers, params })
.then(response => {
console.log(response.data); // or do something with the data
})
.catch(error => {
console.error('Error:', error.response.data);
});
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) throws IOException {
String baseUrl = "https://api.upstox.com/v2/charges/historical-trades";
String accessToken = "{your_access_token}";
String segment = "FO";
String startDate = "2022-04-01";
String endDate = "2023-03-31";
int pageNumber = 1;
int pageSize = 100;
URI uri = URI.create(String.format("%s?segment=%s&start_date=%s&end_date=%s&page_number=%d&page_size=%d",
baseUrl, segment, startDate, endDate, pageNumber, pageSize));
URL url = uri.toURL();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Authorization", "Bearer " + accessToken);
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String responseBody = reader.lines().collect(Collectors.joining(System.lineSeparator()));
System.out.println(responseBody);
reader.close();
} else {
System.out.println("Error: " + responseCode + " - " + connection.getResponseMessage());
}
connection.disconnect();
}
}
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.upstox.com/v2/charges/historical-trades?segment=FO&start_date=2022-04-01&end_date=2023-03-31&page_number=1&page_size=100',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Accept: application/json',
'Authorization: Bearer {your_access_token}'
],
]);
$response = curl_exec($curl);
if ($response === false) {
echo 'Error: ' . curl_error($curl);
} else {
echo $response;
}
curl_close($curl);
?>
import upstox_client
from upstox_client.rest import ApiException
configuration = upstox_client.Configuration()
configuration.access_token = "{your_access_token}"
api_instance = upstox_client.PostTradeApi(upstox_client.ApiClient(configuration))
param = {
'segment': "FO"
}
try:
api_response = api_instance.get_trades_by_date_range("2023-04-01", "2025-03-31",1,1000,**param)
print(api_response)
except ApiException as e:
print("Exception when calling OrderApi->get trades_by_date_range: %s\n" % e.body)
let UpstoxClient = require('upstox-js-sdk');
let defaultClient = UpstoxClient.ApiClient.instance;
var OAUTH2 = defaultClient.authentications['OAUTH2'];
OAUTH2.accessToken = "{your_access_token}";
apiInstance = new UpstoxClient.PostTradeApi();
opts = {
segment: "FO"
}
apiInstance.getTradesByDateRange("2023-04-01","2024-08-30",1,1000,opts,(error, data, response) => {
if (error) {
console.error(error.response.text);
} else {
console.log('API called successfully. Returned data: ' + JSON.stringify(data));
}
});
import com.upstox.ApiClient;
import com.upstox.ApiException;
import com.upstox.Configuration;
import com.upstox.api.TradeHistoryResponse;
import com.upstox.auth.OAuth;
import io.swagger.client.api.PostTradeApi;
public class Main {
public static void main(String[] args) {
ApiClient defaultClient = Configuration.getDefaultApiClient();
OAuth OAUTH2 = (OAuth) defaultClient.getAuthentication("OAUTH2");
OAUTH2.setAccessToken("{your_access_token}");
PostTradeApi apiInstance = new PostTradeApi();
try {
TradeHistoryResponse result = apiInstance.getTradesByDateRange("2023-04-01","2025-08-01",1,1000,"FO");
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling OrderApi#getTradesByDateRange= " + e.getResponseBody());
e.printStackTrace();
}
}
}
Loading...