Get Trade History
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 'Accept: application/json' \
--header 'Authorization: Bearer {your_access_token}'
import requests
url = 'https://api.upstox.com/v2/charges/historical-trades'
headers = {
'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 = {
'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("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 => [
'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 'Accept: application/json' \
--header 'Authorization: Bearer {your_access_token}'
import requests
url = 'https://api.upstox.com/v2/charges/historical-trades'
headers = {
'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 = {
'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("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 => [
'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();
}
}
}