Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## Introduction

The official Python client for communicating with the <a href="https://upstox.com/uplink/">Upstox API</a>.
The official Python client for communicating with the <a href="https://upstox.com/developer/api-documentation/open-api">Upstox API</a>.

Upstox API is a set of rest APIs that provide data required to build a complete investment and trading platform. Execute orders in real time, manage user portfolio, stream live market data (using Websocket), and more, with the easy to understand API collection.

Expand Down
46 changes: 38 additions & 8 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
# Python Sample Implementation
# Upstox Developer API – Example Code

## Websocket
This folder contains **ready-to-use Python samples** for the [Upstox API](https://upstox.com/developer/api-documentation/open-api). Each example shows how to call the API using the official [Upstox Python SDK](https://pypi.org/project/upstox-python-sdk/) (`upstox_client`).

### Market stream feed
## Why use these samples?

Python script to connect to the Upstox Websocket API for streaming live market data. It fetches market data for a list of instrument keys and decodes the incoming protobuf data to a JSON format.
- **Quick start** — Copy-paste examples for common flows (login, orders, market data, portfolio).
- **Correct usage** — Request/response patterns, error handling, and API version usage as recommended by Upstox.
- **Reference** — See how to structure `PlaceOrderRequest`, historical data params, and other API payloads.

[Market updates using Upstox's websocket](websocket/market_data/v3)
Use these samples to build trading apps, dashboards, or integrations without guessing request shapes or SDK usage.

### Porfolio stream feed
## Prerequisites

Python scripts to connect to the Upstox WebSocket API for streaming live order updates. It fetches the order updates and prints them to the console.
- **Python** 2.7 or 3.4+
- **SDK**: `pip install upstox-python-sdk`
- **Upstox developer account** and API credentials (client ID, client secret, redirect URI).
- **Access token** for authenticated APIs (obtain via [Login API](login/) samples).

[Order updates using Upstox's websocket](websocket/order_updates/)
For full setup, sandbox mode, and auth flow, see the main [Upstox Python SDK README](../README.md) in the repo root.

## Folder structure

Samples are grouped by API area. Each `.md` file contains one or more Python snippets you can run after replacing placeholders like `{your_access_token}` and `{your_client_id}`.

| Folder | Description |
|--------|-------------|
| [**login/**](login/) | Authentication: get token from auth code, access-token request, logout. |
| [**user/**](user/) | User profile, fund and margin details. |
| [**orders/**](orders/) | Order lifecycle: place (single/multi, v2 & v3), modify, cancel, order book, order details, order history, trades, historical trades, exit all positions. |
| [**portfolio/**](portfolio/) | Positions, holdings, MTF positions, convert positions. |
| [**market-quote/**](market-quote/) | LTP, full market quotes, OHLC (v2 & v3), option Greeks. |
| [**historical-data/**](historical-data/) | Historical and intraday candle data (v2 & v3). |
| [**option-chain/**](option-chain/) | Option contracts, put-call option chain. |
| [**expired-instruments/**](expired-instruments/) | Expiries, expired future/option contracts, expired historical candle data. |
| [**market-information/**](market-information/) | Exchange status, market timings, market holidays. |
| [**gtt-orders/**](gtt-orders/) | Place, modify, cancel, and get details for GTT (Good Till Triggered) orders. |
| [**margins/**](margins/) | Margin details. |
| [**charges/**](charges/) | Brokerage details. |
| [**trade-profit-and-loss/**](trade-profit-and-loss/) | P&amp;L report, report metadata, trade charges. |

## Documentation

- [Upstox API Documentation](https://upstox.com/developer/api-documentation)
- [Upstox Python SDK (PyPI)](https://pypi.org/project/upstox-python-sdk/)
10 changes: 10 additions & 0 deletions examples/charges/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Charges – Example code

Links to all charges-related examples in the `code/` folder.

## 1. Brokerage Details

- 1.1 [Get brokerage details for equity delivery orders](code/brokerage-details.md#get-brokerage-details-for-equity-delivery-orders)
- 1.2 [Get brokerage details for equity intraday orders](code/brokerage-details.md#get-brokerage-details-for-equity-intraday-orders)
- 1.3 [Get brokerage details for equity futures and options delivery orders](code/brokerage-details.md#get-brokerage-details-for-equity-futures-and-options-delivery-orders)
- 1.4 [Get brokerage details for equity futures and options intraday orders](code/brokerage-details.md#get-brokerage-details-for-equity-futures-and-options-intraday-orders)
102 changes: 102 additions & 0 deletions examples/charges/code/brokerage-details.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
## Get brokerage details for equity delivery orders

```python
import upstox_client
from upstox_client.rest import ApiException

configuration = upstox_client.Configuration()
configuration.access_token = '{your_access_token}'
api_version = '2.0'

api_instance = upstox_client.ChargeApi(upstox_client.ApiClient(configuration))
instrument_token = 'NSE_EQ|INE669E01016'
quantity = 10
product = 'D'
transaction_type = 'BUY'
price = 13.4

try:
# Brokerage details
api_response = api_instance.get_brokerage(instrument_token, quantity, product, transaction_type, price, api_version)
print(api_response)
except ApiException as e:
print("Exception when calling ChargeApi->get_brokerage: %s\n" % e)

```

## Get brokerage details for equity intraday orders

```python
import upstox_client
from upstox_client.rest import ApiException

configuration = upstox_client.Configuration()
configuration.access_token = '{your_access_token}'
api_version = '2.0'

api_instance = upstox_client.ChargeApi(upstox_client.ApiClient(configuration))
instrument_token = 'NSE_EQ|INE669E01016'
quantity = 10
product = 'I'
transaction_type = 'BUY'
price = 13.4

try:
# Brokerage details
api_response = api_instance.get_brokerage(instrument_token, quantity, product, transaction_type, price, api_version)
print(api_response)
except ApiException as e:
print("Exception when calling ChargeApi->get_brokerage: %s\n" % e)

```

## Get brokerage details for equity futures and options delivery orders

```python
import upstox_client
from upstox_client.rest import ApiException

configuration = upstox_client.Configuration()
configuration.access_token = '{your_access_token}'
api_version = '2.0'

api_instance = upstox_client.ChargeApi(upstox_client.ApiClient(configuration))
instrument_token = 'NSE_FO|35271'
quantity = 10
product = 'D'
transaction_type = 'BUY'
price = 1333.4

try:
# Brokerage details
api_response = api_instance.get_brokerage(instrument_token, quantity, product, transaction_type, price, api_version)
print(api_response)
except ApiException as e:
print("Exception when calling ChargeApi->get_brokerage: %s\n" % e)

```

## Get brokerage details for equity futures and options intraday orders

```python
import upstox_client
from upstox_client.rest import ApiException

configuration = upstox_client.Configuration()
configuration.access_token = '{your_access_token}'
api_version = '2.0'

api_instance = upstox_client.ChargeApi(upstox_client.ApiClient(configuration))
instrument_token = 'NSE_FO|35271'
quantity = 10
product = 'I'
transaction_type = 'BUY'
price = 1333.4

try:
# Brokerage details
api_response = api_instance.get_brokerage(instrument_token, quantity, product, transaction_type, price, api_version)
print(api_response)
except ApiException as e:
print("Exception when calling ChargeApi->get_brokerage: %s\n" % e)
```
19 changes: 19 additions & 0 deletions examples/expired-instruments/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Expired Instruments – Example code

Links to all expired-instruments-related examples in the `code/` folder.

## 1. Get Expiries

- 1.1 [Get Expiries for given instrument](code/get-expiries.md#get-expiries-for-given-instrument)

## 2. Get Expired Option Contracts

- 2.1 [Get Expired Option Contracts for given instrument with expiry date](code/get-expired-option-contracts.md#get-expired-option-contracts-for-given-instrument-with-expiry-date)

## 3. Get Expired Future Contracts

- 3.1 [Get Expired Future Contracts for given instrument with expiry date](code/get-expired-future-contracts.md#get-expired-future-contracts-for-given-instrument-with-expiry-date)

## 4. Get Expired Historical Candle Data

- 4.1 [Get Historical Candle Data for Expired Instruments](code/get-expired-historical-candle-data.md#get-historical-candle-data-for-expired-instruments)
15 changes: 15 additions & 0 deletions examples/expired-instruments/code/get-expired-future-contracts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Get Expired Future Contracts for given instrument with expiry date

```python
import upstox_client
from upstox_client.rest import ApiException

configuration = upstox_client.Configuration()
configuration.access_token = '{your_access_token}'
apiInstance = upstox_client.ExpiredInstrumentApi(upstox_client.ApiClient(configuration))
try:
response = apiInstance.get_expired_future_contracts("NSE_INDEX|Nifty 50", "2024-11-27")
print(response)
except ApiException as e:
print("Exception when calling expired instrument api: %s\n" % e)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Get Historical Candle Data for Expired Instruments

```python
import upstox_client
from upstox_client.rest import ApiException

configuration = upstox_client.Configuration()
configuration.access_token = '{your_access_token}'
apiInstance = upstox_client.ExpiredInstrumentApi(upstox_client.ApiClient(configuration))
try:
response = apiInstance.get_expired_historical_candle_data("NSE_FO|54452|24-04-2025", "1minute", "2025-04-24", "2025-04-24")
print(response)
except ApiException as e:
print("Exception when calling expired instrument api: %s\n" % e)
```
15 changes: 15 additions & 0 deletions examples/expired-instruments/code/get-expired-option-contracts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Get Expired Option Contracts for given instrument with expiry date

```python
import upstox_client
from upstox_client.rest import ApiException

configuration = upstox_client.Configuration()
configuration.access_token = '{your_access_token}'
apiInstance = upstox_client.ExpiredInstrumentApi(upstox_client.ApiClient(configuration))
try:
response = apiInstance.get_expired_option_contracts("NSE_INDEX|Nifty 50", "2025-04-30")
print(response)
except ApiException as e:
print("Exception when calling expired instrument api: %s\n" % e)
```
15 changes: 15 additions & 0 deletions examples/expired-instruments/code/get-expiries.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Get Expiries for given instrument

```python
import upstox_client
from upstox_client.rest import ApiException

configuration = upstox_client.Configuration()
configuration.access_token = '{your_access_token}'
apiInstance = upstox_client.ExpiredInstrumentApi(upstox_client.ApiClient(configuration))
try:
response = apiInstance.get_expiries("NSE_INDEX|Nifty 50")
print(response)
except ApiException as e:
print("Exception when calling expired instrument v3 api: %s\n" % e)
```
21 changes: 21 additions & 0 deletions examples/gtt-orders/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# GTT Orders – Example code

Links to all gtt-orders-related examples in the `code/` folder.

## 1. Place GTT Order

- 1.1 [Place Single Leg GTT Order](code/place-gtt-order.md#place-single-leg-gtt-order)
- 1.2 [Place Multiple Leg GTT Order](code/place-gtt-order.md#place-multiple-leg-gtt-order)

## 2. Modify GTT Order

- 2.1 [Modify Single Leg GTT Order](code/modify-gtt-order.md#modify-single-leg-gtt-order)
- 2.2 [Modify Multiple Leg GTT Order](code/modify-gtt-order.md#modify-multiple-leg-gtt-order)

## 3. Cancel GTT Order

- 3.1 [Cancel GTT Order](code/cancel-gtt-order.md#cancel-gtt-order)

## 4. Get GTT Order Details

- 4.1 [Get GTT Order Details](code/get-gtt-order-details.md#get-gtt-order-details)
18 changes: 18 additions & 0 deletions examples/gtt-orders/code/cancel-gtt-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## Cancel GTT Order

```python
import upstox_client
from upstox_client.rest import ApiException

configuration = upstox_client.Configuration()
configuration.access_token = '{your_access_token}'
api_instance = upstox_client.OrderApiV3(upstox_client.ApiClient(configuration))

body = upstox_client.GttCancelOrderRequest(gtt_order_id="GTT-C250303008840")

try:
api_response = api_instance.cancel_gtt_order(body=body)
print("GTT order canceled:", api_response)
except ApiException as e:
print("Exception when calling OrderApi->cancel_gtt_order: %s\n" % e)
```
16 changes: 16 additions & 0 deletions examples/gtt-orders/code/get-gtt-order-details.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## Get GTT Order Details

```python
import upstox_client
from upstox_client.rest import ApiException

configuration = upstox_client.Configuration()
configuration.access_token = '{your_access_token}'
api_instance = upstox_client.OrderApiV3(upstox_client.ApiClient(configuration))

try:
api_response = api_instance.get_gtt_order_details(gtt_order_id="GTT-C25030300128840")
print("GTT order details:", api_response)
except ApiException as e:
print("Exception when calling OrderApi->get_gtt_order_details: %s\n" % e)
```
55 changes: 55 additions & 0 deletions examples/gtt-orders/code/modify-gtt-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## Modify Single Leg GTT Order

```python
import upstox_client
from upstox_client.rest import ApiException

configuration = upstox_client.Configuration()
configuration.access_token = '{your_access_token}'
api_instance = upstox_client.OrderApiV3(upstox_client.ApiClient(configuration))

entry_rule = upstox_client.GttRule(strategy="ENTRY", trigger_type="ABOVE", trigger_price=7.3)
rules = [entry_rule]

body = upstox_client.GttModifyOrderRequest(
type="SINGLE",
gtt_order_id="GTT-C25270200137952",
rules=rules,
quantity=1
)

try:
api_response = api_instance.modify_gtt_order(body=body)
print("GTT order response:", api_response)
except ApiException as e:
print("Exception when calling OrderApi->modify_gtt_order: %s\n" % e)
```

## Modify Multiple Leg GTT Order

```python
import upstox_client
from upstox_client.rest import ApiException

configuration = upstox_client.Configuration()
configuration.access_token = '{your_access_token}'
api_instance = upstox_client.OrderApiV3(upstox_client.ApiClient(configuration))

entry_rule = upstox_client.GttRule(strategy="ENTRY", trigger_type="ABOVE", trigger_price=7.3)
target_rule = upstox_client.GttRule(strategy="TARGET", trigger_type="IMMEDIATE", trigger_price=7.64)
stoploss_rule = upstox_client.GttRule(strategy="STOPLOSS", trigger_type="IMMEDIATE", trigger_price=7.1)
rules = [entry_rule, target_rule, stoploss_rule]

body = upstox_client.GttModifyOrderRequest(
type="MULTIPLE",
gtt_order_id="GTT-C25280200137522",
rules=rules,
quantity=1
)

try:
api_response = api_instance.modify_gtt_order(body=body)
print("GTT order response:", api_response)
except ApiException as e:
print("Exception when calling OrderApi->modify_gtt_order: %s\n" % e)
```
Loading
Loading