npx skills add ...
npx skills add dhan-oss/dhanhq-skills --skill dhanhq
Use when the user mentions DhanHQ, Dhan API, or wants to trade on Indian exchanges (NSE, BSE, MCX). Triggers for: place, modify, or cancel stock/F&O/commodity orders on Dhan; fetch portfolio holdings or positions; get live or historical market data; access option chains with Greeks; check fund limits or margin; build any trading automation for Indian markets; resolve NSE/BSE instrument IDs; stream live WebSocket market feeds or order updates. Also trigger for general questions about programmatic trading on Indian exchanges if Dhan is the user's broker.
npx skills add dhan-oss/dhanhq-skills --skill dhanhq
Stable install:
Use the current SDK branch when you need newer v2 capabilities such as 200-level depth or the latest helper coverage:
Minimal initialization:
Environment-variable setup:
If generating scripts for this repo, prefer:
LIMIT orders unless the user explicitly wants MARKET.Rs. 50,000.CNC or MTF for F&O, commodity, or currency segments.modify_order, cancel_order, kill_switch, or any multi-leg live execution.Before using the account for live work, verify:
dhan_login.user_profile(...) or GET /profile shows the needed account setup.dataPlan is active for quote/history/feed/option-chain use.Useful profile fields:
tokenValidityactiveSegmentddpimtfdataPlandataValidity| Category | Constant | Value |
|---|---|---|
| Exchange | dhanhq.NSE | NSE_EQ |
dhanhq.BSE | BSE_EQ | |
dhanhq.NSE_FNO | NSE_FNO | |
dhanhq.BSE_FNO | BSE_FNO | |
dhanhq.MCX | MCX_COMM | |
dhanhq.CUR | NSE_CURRENCY | |
dhanhq.INDEX | IDX_I | |
| Transaction | dhanhq.BUY | BUY |
dhanhq.SELL | SELL | |
| Order Type | dhanhq.LIMIT | LIMIT |
dhanhq.MARKET | MARKET | |
dhanhq.SL | STOP_LOSS | |
dhanhq.SLM | STOP_LOSS_MARKET | |
| Product | dhanhq.CNC | CNC |
dhanhq.INTRA | INTRADAY | |
dhanhq.MARGIN | MARGIN | |
dhanhq.MTF | MTF | |
| Validity | dhanhq.DAY | DAY |
dhanhq.IOC | IOC |
| Task | Method |
|---|---|
| Place order | dhan.place_order() |
| Slice large order | dhan.place_slice_order() |
| Modify order | dhan.modify_order() |
| Cancel order | dhan.cancel_order() |
| Order book | dhan.get_order_list() |
| Order by ID | dhan.get_order_by_id() |
| Order by correlation ID | dhan.get_order_by_correlationID() |
| Trade book | dhan.get_trade_book() |
| Trade history | dhan.get_trade_history() |
| Ledger | dhan.ledger_report() |
| Super orders | place_super_order(), modify_super_order(), cancel_super_order(), get_super_order_list() |
| Forever orders | place_forever(), modify_forever(), cancel_forever(), get_forever() |
| Holdings | dhan.get_holdings() |
| Positions | dhan.get_positions() |
| Convert position | dhan.convert_position() |
| eDIS | dhan.generate_tpin(), dhan.open_browser_for_tpin(), dhan.edis_inquiry() |
| Fund limits | dhan.get_fund_limits() |
| Margin calculator | dhan.margin_calculator() |
| Daily history | dhan.historical_daily_data() |
| Minute history | dhan.intraday_minute_data() |
| Expired options data | dhan.expired_options_data() |
| Market quote snapshot | dhan.ticker_data(), dhan.ohlc_data(), dhan.quote_data() |
| Expiry list | dhan.expiry_list() |
| Option chain | dhan.option_chain() |
| Security master | dhanhq.fetch_security_list() |
| Live market feed | MarketFeed |
| Live order updates | OrderUpdate |
| Full market depth | FullDepth |
| Kill switch | dhan.kill_switch(), dhan.status_kill_switch() |
{"status": "success"|"failure", "remarks": ..., "data": ...}. Response shapes vary by endpoint — success payloads differ significantly (arrays, flat objects, nested dicts) depending on the API.ce_ltp, ce_oi, ce_iv are repo-defined names — not raw Dhan field names.intraday_minute_data(...) is the current SDK method. Do not reference historical_minute_data().expiry_code with [0, 1, 2, 3], but Dhan's v2 annexure documents 0, 1, 2. Prefer the documented values unless Dhan updates the API docs.1 request/sec.data["oc"]. Use repo helpers for analysis-friendly rows.| Segment | Allowed Product Types |
|---|---|
NSE_EQ, BSE_EQ | CNC, INTRADAY, MARGIN, MTF |
NSE_FNO, BSE_FNO, MCX_COMM, NSE_CURRENCY, BSE_CURRENCY | INTRADAY, MARGIN |
Use the security master as the primary source for:
security_idlot_sizetick_sizeQuick-reference index underlyings:
| Underlying | security_id | Underlying Segment |
|---|---|---|
| NIFTY 50 | 13 | IDX_I |
| BANK NIFTY | 25 | IDX_I |
| FINNIFTY | 27 | IDX_I |
| MIDCPNIFTY | 442 | IDX_I |
| SENSEX | 51 | IDX_I |
When generating scripts in this repo, prefer:
get_client() for SDK bootstrappingresolve_symbol() for cash-market lookupresolve_derivative() for contract lookupfetch_chain_df() for option-chain normalizationfind_atm_row() for ATM selectioncheck_margin() for pre-flight margin checkspreview_order() for readable confirmation| API Category | Per Second | Per Minute | Per Hour | Per Day |
|---|---|---|---|---|
| Order APIs | 10 | 250 | 1000 | 7000 |
| Data APIs | 5 | - | - | 100000 |
| Quote APIs | 1 | Unlimited | Unlimited | Unlimited |
| Non-Trading APIs | 20 | Unlimited | Unlimited | Unlimited |
Dhan APIs cover execution, quotes, OHLC, option chain, and portfolio. For fundamental data (PE, EPS, revenue), technical indicators (RSI, MACD), or shareholding patterns not available via Dhan, use ScanX — see references/scanx-data.md.
| Need | File |
|---|---|
| Orders, super orders, forever orders | references/orders.md |
| Holdings, positions, eDIS | references/portfolio.md |
| Daily/minute history, quotes, expired options | references/market-data.md |
| Option-chain usage and normalization | references/option-chain.md |
| Fund limits and margin checks | references/funds.md |
| Live feeds and depth | references/live-feed.md |
| Error handling and subscription troubleshooting | references/error-codes.md |
| Instrument resolution | references/instruments.md |
| Multi-step execution patterns | references/common-workflows.md |
| Options analytics | references/options-analysis-patterns.md |
| Backtesting patterns | references/backtesting-with-dhan.md |
| PE ratio, RSI, financials, screeners — data Dhan does not provide | references/scanx-data.md |
If the user gets DH-902 or 806:
web.dhan.coMy Profile -> Access DhanHQ APIsdataPlan is activeticker_data() or ohlc_data()import os
from dhanhq import DhanContext, dhanhq
dhan_context = DhanContext(
os.environ["DHAN_CLIENT_ID"],
os.environ["DHAN_ACCESS_TOKEN"],
)
dhan = dhanhq(dhan_context)from scripts.dhan_helpers import get_client
dhan, dhan_context = get_client()from dhanhq import DhanLogin
dhan_login = DhanLogin("YOUR_CLIENT_ID")
profile = dhan_login.user_profile("YOUR_ACCESS_TOKEN")
print(profile["dataPlan"])
print(profile["dataValidity"])data = dhan.historical_daily_data(
security_id="2885",
exchange_segment=dhanhq.NSE,
instrument_type="EQUITY",
from_date="2024-01-01",
to_date="2024-12-31",
)
if data["status"] == "success":
candles = data["data"]
timestamps = [dhan.convert_to_date_time(ts) for ts in candles["timestamp"]]from scripts.dhan_helpers import fetch_chain_df, find_atm_row
chain_df, spot = fetch_chain_df(dhan, under_security_id=13, expiry="2025-03-27")
atm = find_atm_row(chain_df, spot)
print(spot)
print(atm["strike"])
print(atm["ce_security_id"], atm["ce_ltp"])from scripts.dhan_helpers import check_margin
margin = check_margin(
dhan,
security_id="2885",
exchange_segment=dhanhq.NSE,
transaction_type=dhanhq.BUY,
quantity=10,
product_type=dhanhq.CNC,
price=2450.0,
)
print(margin["sufficient"], margin["total_margin"], margin["available_balance"])from dhanhq import MarketFeed
instruments = [
(MarketFeed.NSE, "2885", MarketFeed.Ticker),
(MarketFeed.NSE_FNO, "49081", MarketFeed.Full),
]
feed = MarketFeed(dhan_context, instruments, "v2")
feed.run_forever()
print(feed.get_data())