mcpField
U
upstox

upstox

github.com/upstoxOther1217 installsUpdated 20w ago
Repository

About upstox

An agent skill for working with Upstox's Indian market APIs through the official `upstox-python-sdk`. It covers orders, portfolio and market data, with API-version guidance and safeguards for order operations.

Key features

  1. 01Place and manage orders
  2. 02Read portfolio and funds data
  3. 03Retrieve quotes and historical data
  4. 04Stream market and order updates
  5. 05Resolve instruments and validate lot sizes

Use cases

  1. 01Rehearse order workflows in the sandbox
  2. 02Check holdings, available funds, or market prices
  3. 03Look up instruments before placing an order
  4. 04Analyze options data and historical candles

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/upstox/upstox-skills --skill upstox

After installing, type/upstoxin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

config.json.example
examples/
bear_butterfly.py
bull_call_spread.py
historical_candles.py
instrument_search.py
market_quote.py
option_chain_analysis.py
place_equity_order.py
place_gtt_order.py
portfolio_summary.py
short_strangle.py
references/
errors.md
gtt-orders.md
instruments.md
kill-switch.md
margins.md
market-data.md
option-chain.md
orders.md
portfolio.md
websocket.md
scripts/
instrument_search.py
upstox_helpers.py
validate_order.py

Details

From the project README

Upstox Agent Skill

Trade on Upstox from an AI agent — place live orders, read portfolio data, stream market feeds, and access the full Indian market universe across NSE, BSE, and MCX through the Upstox Developer API and the official upstox-python-sdk.

All SDK signatures in this skill and its references are verified against the upstox-python SDK source. The single most common source of bugs is mixing up the v2 and v3 API classes — read the v2 vs v3 note below before writing order or quote code.


Setup

Install
pip install upstox-python-sdk
Credentials — environment variables only, never hardcode
export UPSTOX_ACCESS_TOKEN="your-daily-access-token"
export UPSTOX_SANDBOX_ACCESS_TOKEN="your-sandbox-access-token"
Initialize the client
import os
import upstox_client

configuration = upstox_client.Configuration()
configuration.access_token = os.environ["UPSTOX_ACCESS_TOKEN"]
api_client = upstox_client.ApiClient(configuration)

Prefer the shared helper so every call reuses one configured client:

from scripts.upstox_helpers import get_client
api_client = get_client()                 # reads UPSTOX_ACCESS_TOKEN (or sandbox)
Sandbox (paper trading)

Use the sandbox to place test orders without real money. The SDK exposes a flag — no separate base URL needed:

configuration = upstox_client.Configuration(sandbox=True)
configuration.access_token = os.environ["UPSTOX_SANDBOX_ACCESS_TOKEN"]

get_client(sandbox=True) does the same. Always rehearse order-placement workflows in the sandbox before going live.

Base URL

https://api.upstox.com/v2 (and /v3 for newer endpoints — the SDK handles this per class).


v2 vs v3 — read this before writing order/quote code

Upstox ships two generations of some APIs. They use different SDK classes and different method signatures. Picking the wrong one is the #1 cause of errors.

Concernv3 (preferred)v2 (legacy)
OrdersOrderApiV3 — place_order(body), modify_order(body), cancel_order(order_id) — no api_version argOrderApi — place_order(body, api_version), etc. — api_version required
GTT ordersOrderApiV3.place_gtt_order(body) (only v3)—
LTP / OHLC / greeksMarketQuoteV3Api — get_ltp(instrument_key=...) — no api_versionMarketQuoteApi — ltp(symbol, api_version)
Historical candlesHistoryV3Api.get_historical_candle_data(instrument_key, unit, interval, to_date, from_date=...)HistoryApi (older interval scheme)
Funds & marginUserApi.get_user_fund_margin_v3() — no argsUserApi.get_user_fund_margin(api_version, segment=...)

Default to v3 for orders, quotes, and historical data. Multi-order, order book, trades, brokerage, P&L, options, holidays, and convert-position remain on their v2 classes (OrderApi, ChargeApi, etc.).


Safety Guardrails — ALWAYS ENFORCE

This skill can place live, irreversible financial orders. Before any place_order, modify_order, cancel_order, place_gtt_order, exit_positions, or cancel_multi_order:

RuleBehaviour
Confirmation requiredShow a full, human-readable order preview and get explicit user confirmation before placing/modifying/cancelling. Run scripts/validate_order.py first.
Default order type: LIMITNever place a MARKET order unless the user explicitly asks for "market".
Default quantity1 share (equity) or 1 lot (F&O) when quantity is unspecified — never guess larger.
Lot-size validationReject F&O orders whose quantity is not a multiple of the instrument's lot_size.
Market Price ProtectionMARKET orders are auto-bounded by Upstox MPP; pass market_protection (%) to set your own price buffer so a market order can't fill at a wild price. See references/orders.md.
Kill switchA user can halt all trading in a segment via UserApi.update_kill_switch([{segment, action}]) — DISABLE cancels pending orders and blocks new ones. Warn about the ~12-hour re-enable lock before disabling. See references/kill-switch.md.
Sandbox firstRehearse in sandbox (Configuration(sandbox=True)) when the user is testing.
No hardcoded secretsAlways read tokens from os.environ.

Rate Limits

Breaching any limit returns HTTP 429 Too Many Requests — back off before retrying.

Combined rate limiting for Order Placement APIs

Applies across Place, Modify, Cancel, Multi Order, and GTT Order — combined.

Regular Algos — no algo registration needed:

Time durationRequest limit
Per second10 requests
Per minute500 requests
Per 30 minutes2000 requests

SEBI-Registered Algos — algo registration needed:

Time durationRequest limit
Per second50 requests
Per minute500 requests
Per 30 minutes2000 requests
Other Standard APIs

Holdings, positions, funds, historical candles, etc.

Time durationRequest limit
Per second50 requests
Per minute500 requests
Per 30 minutes2000 requests

Key Constants

# Transaction
BUY, SELL = "BUY", "SELL"

# Order types
MARKET, LIMIT, SL, SL_M = "MARKET", "LIMIT", "SL", "SL-M"

# Products
INTRADAY, DELIVERY, COVER, ONE_CANCELS_OTHER, MTF = "I", "D", "CO", "OCO", "MTF"

# Validity
DAY, IOC = "DAY", "IOC"
  • Exchanges (the exchange field): NSE, NFO, CDS, BSE, BFO, BCD, MCX
  • instrument_key segment prefixes: NSE_EQ, NSE_FO, NSE_INDEX, BSE_EQ, BSE_FO, BSE_INDEX, NCD_FO, BCD_FO, MCX_FO
instrument_key format
SEGMENT|IDENTIFIER
"NSE_EQ|INE002A01018"     # Reliance (equity → ISIN)
"NSE_INDEX|Nifty 50"      # index (→ name)
"NSE_FO|43919"            # F&O (→ numeric exchange token)

Resolve human names to instrument_key with scripts/instrument_search.py (the server-side Instrument Search API — no master-file downloads). Always resolve the instrument_key this way before placing an order; do not guess option/futures tokens — they change per expiry.

from scripts.instrument_search import resolve_instrument_key
inst = resolve_instrument_key("Reliance", exchanges="NSE", segments="EQ")
instrument_token = inst["instrument_key"]   # pass inst["lot_size"] to validate_order for F&O

Core Patterns

Place a limit buy (v3)
import os, upstox_client
from upstox_client.rest import ApiException

configuration = upstox_client.Configuration()
configuration.access_token = os.environ["UPSTOX_ACCESS_TOKEN"]
order_api = upstox_client.OrderApiV3(upstox_client.ApiClient(configuration))

body = upstox_client.PlaceOrderV3Request(
    quantity=1,
    product="D",                              # D = delivery/CNC
    validity="DAY",
    price=2500.0,
    tag="skill-order",
    instrument_token="NSE_EQ|INE002A01018",   # Reliance
    order_type="LIMIT",
    transaction_type="BUY",
    disclosed_quantity=0,
    trigger_price=0.0,
    is_amo=False,                             # required; True only for after-market orders
)

try:
    resp = order_api.place_order(body)        # NOTE: v3 has no api_version arg
    print("Order ID:", resp.data.order_ids)
except ApiException as e:
    print("Order failed:", e.body)
Get holdings (v2 PortfolioApi)
portfolio_api = upstox_client.PortfolioApi(upstox_client.ApiClient(configuration))
holdings = portfolio_api.get_holdings(api_version="2.0")
for h in holdings.data:
    print(h.trading_symbol, h.quantity, h.last_price, h.pnl)
Get LTP (v3 MarketQuoteV3Api)
mq = upstox_client.MarketQuoteV3Api(upstox_client.ApiClient(configuration))
resp = mq.get_ltp(instrument_key="NSE_EQ|INE002A01018")   # comma-separate for many
print(resp.data)

Reference Index — load on demand

TopicWhen to loadFile
Orders (place/modify/cancel/multi/exit)Any order operationreferences/orders.md
GTT orders (single & multi-leg)Conditional / good-till-triggered ordersreferences/gtt-orders.md
Portfolio (holdings, positions, convert, P&L)View/manage portfolioreferences/portfolio.md
Market data (LTP, OHLC, quotes, candles, status)Prices & historical datareferences/market-data.md
Option chain (greeks, OI, PCR, max pain)Options analysisreferences/option-chain.md
Margins, funds & brokerage chargesPre-trade margin/charge checksreferences/margins.md
Kill switch (halt trading in a segment)Risk control / disable a segmentreferences/kill-switch.md
WebSocket (live ticks, order updates)Streaming feedsreferences/websocket.md
Instruments (resolve names → tokens, lot size)Symbol lookupreferences/instruments.md
Errors (HTTP & UDAPI codes)Debugging, error codes, 429sreferences/errors.md

Example Prompts This Skill Handles

  • "Buy 10 SBIN at 820"
  • "Place a GTT to buy Wipro when it falls to 440"
  • "How much cash do I have free?"
  • "Last price of Bank Nifty"
  • "Pull the Nifty option chain for this week"

SDK Reference

Comments

Markdown supported