DhanHQ Ruby SDK for Dhan API v2
A production-grade Ruby gem for the Dhan trading API. Build algorithmic trading systems, market data pipelines, and portfolio management tools for Indian markets (NSE, BSE, MCX) with clean Ruby abstractions, resilient WebSocket streaming, typed models, and safety-focused order workflows.
Quick Start
ruby
# Gemfile
gem 'DhanHQ'ruby
require 'dhan_hq'
DhanHQ.configure do |c|
c.client_id = ENV["DHAN_CLIENT_ID"]
c.access_token = ENV["DHAN_ACCESS_TOKEN"]
end
# Fetch positions
positions = DhanHQ::Models::Position.allFeatures
- Typed models for orders, positions, holdings, funds, and trades
- WebSocket market feed with auto-reconnect and exponential backoff
- WebSocket order updates — real-time execution events
- Token lifecycle management with automatic retry-on-401
- dry-validation contracts for every trading request
- Rails integration with ActionCable, config generators, and rake tasks
- Safety rails — validation before transport, no blind retries
- REST API — orders, super orders, positions, holdings, funds, instruments, option chain, historical data, and more
Next Steps
| Guide | Description |
|---|---|
| Installation & Setup | Configure the gem and authenticate |
| Orders API | Place, modify, cancel orders |
| WebSocket Market Feed | Stream live market data |
| Rails Integration | Use DhanHQ with Ruby on Rails |
| Technical Analysis | Built-in indicator helpers |
| GitHub | Source code and issues |
| RubyGems | Gem registry |
Community project. This is an independent gem and is not affiliated with, endorsed by, or supported by Dhan.