Skip to main content

DhanHQ-py : v1.3.3

PyPI

The official Python client for communicating with the Dhan API

DhanHQ-py Rest API is used to automate investing and trading. Execute orders in real time along with position management, historical data, tradebook and more with simple API collection.

Not just this, you also get real-time market data via DhanHQ Live Market Feed.

Dhan (c) 2024. Licensed under the MIT License

Documentation

v1.3.3 - Handling SSL

  • Handling of SSL verification along with requirement addition of pyOpenSSL.

v1.3.2 - Optimising marketfeed module

  • Adding upto 5000 instruments in a single list
  • Subscribing to multiple modes simultaneously
  • Websocket disconnect function
  • Removed Warning message

v1.3 - What's New

Live Market Feed data is now available across exchanges and segments via DhanHQ

  • Low latency websockets
  • 5000 instruments per socket
  • Establish upto 5 sockets per user

With Market Feed, you can subscribe data in below formats:

  • Ticker Data
  • Quote Data
  • Market Depth

Features

  • Order Management
    The order management APIs lets you place a new order, cancel or modify the pending order, retrieve the order status, trade status, order book & tradebook.

  • Live Market Feed
    Get real-time market data to power your trading systems, with easy to implement functions and data across exchanges.

  • Portfolio
    With this set of APIs, retrieve your holdings and positions in your portfolio.

  • Historical Data
    Get historical candle data for the desired scrip across segments & exchange, both Intraday 1 minute OHLC and Daily OHLC.

  • Get fund limits
    Get all information of your trading account like balance, margin utilised, collateral, etc.

  • eDIS Authorisation
    To sell holding stocks, one needs to complete the CDSL eDIS flow, generate T-PIN & mark stock to complete the sell action.

Quickstart

You can install the package via pip

pip install dhanhq

Hands-on API

from dhanhq import dhanhq

dhan = dhanhq("client_id","access_token")

# Place an order for Equity Cash
dhan.place_order(security_id='1333',   #hdfcbank
    exchange_segment=dhan.NSE,
    transaction_type=dhan.BUY,
    quantity=10,
    order_type=dhan.MARKET,
    product_type=dhan.INTRA,
    price=0)
    
# Place an order for NSE Futures & Options
dhan.place_order(security_id='52175',  #NiftyPE
    exchange_segment=dhan.NSE_FNO,
    transaction_type=dhan.BUY,
    quantity=550,
    order_type=dhan.MARKET,
    product_type=dhan.INTRA,
    price=0)
    
# Place an order for Currency
dhan.place_order(security_id= '10093',  #usdinr
    exchange_segment= dhan.CUR,
    transaction_type= dhan.BUY,
    quantity=1,
    order_type = dhan.MARKET,
    validity= dhan.DAY,
    product_type= dhan.INTRA,
    price=0)

# Place an order for BSE Equity
dhan.place_order(security_id='500180',   #hdfcbank
    exchange_segment=dhan.BSE,
    transaction_type=dhan.BUY,
    quantity=1,
    order_type=dhan.MARKET,
    product_type=dhan.INTRA,
    price=0,)

# Place an order for BSE Futures & Options
dhan.place_order(security_id='1135553',   #SensexPE
    exchange_segment=dhan.BSE_FNO,
    transaction_type=dhan.BUY,
    quantity=1,
    order_type=dhan.MARKET,
    product_type=dhan.INTRA,
    price=0,)
    
# Place an order for MCX Commodity    
dhan.place_order(security_id= '114',    #gold
    exchange_segment= dhan.MCX,
    transaction_type= dhan.BUY,
    quantity=1,
    order_type=dhan.MARKET,
    product_type= dhan.INTRA,
    price=0)
    
# Place Slice Order
dhan.place_slice_order(security_id='52175',  #NiftyPE
    exchange_segment=dhan.NSE_FNO,
    transaction_type=dhan.BUY,
    quantity=2000,              #nifty freeze quantity is 1800
    order_type=dhan.MARKET,
    product_type=dhan.INTRA,
    price=0)
   
# Place MTF Order
dhan.place_order(security_id='1333',   #hdfcbank
    exchange_segment=dhan.NSE,
    transaction_type=dhan.BUY,
    quantity=100,
    order_type=dhan.MARKET,
    product_type=dhan.MTF,
    price=0)
  
# Fetch all orders
dhan.get_order_list()

# Get order by id
dhan.get_order_by_id(order_id)

# modify order
dhan.modify_order(order_id, order_type, leg_name, quantity, price, trigger_price, disclosed_quantity, validity)

# Cancel order
dhan.cancel_order(order_id)

# Get order by correlation id
dhan.get_order_by_corelationID(corelationID)

# Get positions
dhan.get_positions()

# Get holdings
dhan.get_holdings()

# Intraday Minute Data
dhan.intraday_minute_data(security_id,exchange_segment,instrument_type)

# Historical Daily Data
dhan.historical_daily_data(symbol,exchange_segment,instrument_type,expiry_code,from_date,to_date)

# Time Converter
dhan.convert_to_date_time(Julian Date)

# Get trade book
dhan.get_trade_book(order_id)

# Get trade history
dhan.get_trade_history(from_date,to_date,page_number=0)

# Get fund limits
dhan.get_fund_limits()

# Generate TPIN
dhan.generate_tpin()

# Enter TPIN in Form
dhan.open_browser_for_tpin(isin='INE00IN01015',
    qty=1,
    exchange='NSE')

# EDIS Status and Inquiry
dhan.edis_inquiry()

Market Feed Usage

from dhanhq import marketfeed

# Add your Dhan Client ID and Access Token
client_id = "Dhan Client ID"
access_token = "Access Token"

# Structure for subscribing is (exchange_segment, "security_id", subscription_type)

instruments = 
    [(marketfeed.NSE, "1333", marketfeed.Ticker),
    (marketfeed.NSE, "1333", marketfeed.Quote),
    (marketfeed.NSE, "1333", marketfeed.Depth),
    (marketfeed.NSE, "11915", marketfeed.Ticker),
    (marketfeed.NSE, "11915", marketfeed.Ticker)]


# Ticker - Ticker Data
# Quote - Quote Data
# Depth - Market Depth

# In case subscription_type is left as blank, by default Ticker mode will be subscribed.

try:
    data = marketfeed.DhanFeed(client_id, access_token, instruments)
    while True:
        data.run_forever()
        response = data.get_data()
        print(response)

except Exception as e:
    print(e)

# Close Connection
data.disconnect()

# Subscribe instruments while connection is open
sub_instruments = 
    [(marketfeed.NSE, "14436", marketfeed.Ticker)]

data.subscribe_symbols(sub_instruments)

# Unsubscribe instruments which are already active on connection
unsub_instruments = 
    [(marketfeed.NSE, "1333", 16)]

data.unsubscribe_symbols(unsub_instruments)

Changelog

Check release notes

Metadata

Release files for dhanhq 1.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dhanhq 1.3.3
File Size Uploaded Provenance Actions
dhanhq-1.3.3.tar.gz 14.5 kB Without provenance Details for dhanhq-1.3.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dhanhq 1.3.3
File Interpreter ABI Platform Actions
dhanhq-1.3.3-py3-none-any.whl Python 3 none any Details for dhanhq-1.3.3-py3-none-any.whl

Total release size: 26.8 kB

Release files / dhanhq-1.3.3.tar.gz

Download URL dhanhq-1.3.3.tar.gz
Size 14.5 kB
Tags Source
SHA-256 checksum
How to use checksums
41b92c82509dc89373c5e7b0454749d4c9cee3118f36a326851584bd6964ce9f
BLAKE2b-256 checksum
How to use checksums
c52c523733b17b2ea2012df4b68937a19afcddcefec00d098ba5025265d5f05b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.6

Release files / dhanhq-1.3.3-py3-none-any.whl

Download URL dhanhq-1.3.3-py3-none-any.whl
Size 12.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
49b9bce280984bf49279a5b8b1b3209813068e8f80bacf457db9b70d393a3609
BLAKE2b-256 checksum
How to use checksums
17156c6ef81f114166943f1d8e8216479a1916cdebd522195a420488e3fc420d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.6

Security for dhanhq 1.3.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page