Skip to content

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.all

Features

  • 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

GuideDescription
Installation & SetupConfigure the gem and authenticate
Orders APIPlace, modify, cancel orders
WebSocket Market FeedStream live market data
Rails IntegrationUse DhanHQ with Ruby on Rails
Technical AnalysisBuilt-in indicator helpers
GitHubSource code and issues
RubyGemsGem registry

Community project. This is an independent gem and is not affiliated with, endorsed by, or supported by Dhan.

Community project — not affiliated with Dhan. MIT License.