dhan-rs 0.1.7

Unofficial Rust client library for the DhanHQ Broker API v2 — orders, market data, WebSocket feeds, and more
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# dhan-rs

[![Crates.io](https://img.shields.io/crates/v/dhan-rs.svg)](https://crates.io/crates/dhan-rs)
[![Documentation](https://docs.rs/dhan-rs/badge.svg)](https://docs.rs/dhan-rs)
[![CI](https://github.com/SPRAGE/dhan-rs/actions/workflows/ci.yml/badge.svg)](https://github.com/SPRAGE/dhan-rs/actions/workflows/ci.yml)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)

An **unofficial** Rust client library for the [DhanHQ Broker API v2](https://dhanhq.co/docs/v2/).

> [!CAUTION]
> **⚠️ AI-GENERATED CODE — USE AT YOUR OWN RISK**
>
> This entire crate was generated by AI (GitHub Copilot / Claude). It includes
> local contract and recovery hardening, but **has not been extensively tested
> against the live API**. Before using this in production or with real money:
>
> - Review the source code thoroughly
> - Write your own integration tests against DhanHQ's sandbox/live API
> - Validate all order placement, modification, and cancellation flows
> - Verify WebSocket market feed parsing against real data
> - This is **not** an official DhanHQ product and is not endorsed by DhanHQ
>
> **The authors accept no responsibility for financial losses incurred through
> the use of this library.**

## Audit and design documentation

- [Current implementation status and operating boundaries]docs/implementation-status.md
- [Sandbox testing and safety boundary]docs/sandbox-testing.md
- [Current WebSocket stability guide]docs/websocket-stability.md
- [Historical DhanHQ API v2 compliance audit]docs/dhan-v2-compliance-audit.md
- [Historical WebSocket stability design]docs/websocket-stability-design.md

The audit and design document the pre-remediation baseline and remain useful
evidence of Dhan documentation conflicts. The implementation-status and
stability pages describe the current code, deterministic local tests, and the
boundaries that still require authenticated validation.

---

## Features

- **Broad API coverage** — Typed REST modules including Data APIs, Global
  Stocks, multi-order/eDIS additions, and instrument-master downloads; see
  the [implementation status]docs/implementation-status.md for conflicts
  and live-validation limits
- **WebSocket streaming** — Low-level market/order streams, a supervised
  standard-feed manager, a managed order-update supervisor, and separate
  20-level/200-level Full Market Depth streams
- **Typed models** — Serde-backed request and response types across implemented modules
- **Async/await** — Built on `tokio` and `reqwest` for async-first design
- **Native binary parsing** — Little-endian standard-feed packets parsed without an external binary parsing crate
- **Ergonomic error handling** — Rich `DhanError` enum with API error codes, HTTP errors, JSON errors, and WebSocket errors

## Supported API Modules

| Module | Description |
|---|---|
| **Authentication** | Access token generation, renewal, consent flows (individual & partner) |
| **Orders** | Place, modify, cancel, slice orders; order book & trade book |
| **Super Orders** | Multi-leg bracket/cover orders with stop-loss and target |
| **Forever Orders** | GTT (Good Till Triggered) and OCO (One Cancels Other) orders |
| **Conditional Triggers** | Alert-based conditional order placement |
| **Portfolio** | Holdings, positions, position conversion, exit all |
| **eDIS** | T-PIN generation, eDIS form, delivery inquiry |
| **Trader's Control** | Kill switch, P&L-based auto-exit |
| **Funds** | Margin calculator (single & multi), fund limits |
| **Statements** | Ledger reports, trade history |
| **Market Quotes** | LTP, OHLC, full market depth snapshots (REST) |
| **Historical Data** | Daily and intraday OHLCV candles |
| **Option Chain** | Option chain data with Greeks, expiry lists |
| **Postback** | Webhook payload deserialization types |
| **WebSocket: Market Feed** | Real-time ticker, quote, and Full packets on the standard binary protocol |
| **WebSocket: Managed Market Feed** | Lazy multi-socket ownership, retry, health, gap acknowledgement, and graceful shutdown |
| **WebSocket: Order Updates** | Real-time order status changes (JSON protocol), plus an optional managed/reconciling supervisor |
| **Full Market Depth** | Separate caller-polled 20-level and 200-level protocols; no depth reconnect manager yet |
| **Data APIs** | Rolling expired options, technical metrics, market movers, and company information |
| **Global Stocks** | Orders, estimates, margin, trades, market status, holdings, and fund limits |
| **Instruments** | Compact/detailed public CSV and authenticated segment CSV downloads |

For the managed market feed, create parsed receivers before subscribing.
Otherwise a live packet with no receiver is intentionally reported as a
durable data-quality gap rather than discarded silently.

## Installation

Add to your `Cargo.toml`:

```toml
[dependencies]
dhan-rs = "0.1"
tokio = { version = "1", features = ["full"] }
```

## Quick Start

### REST API — Place an Order

```rust,no_run
use dhan_rs::DhanClient;
use dhan_rs::types::orders::PlaceOrderRequest;
use dhan_rs::types::enums::*;

#[tokio::main]
async fn main() -> dhan_rs::Result<()> {
    let client = DhanClient::new("your-client-id", "your-access-token");

    let req = PlaceOrderRequest {
        dhan_client_id: "your-client-id".into(),
        correlation_id: Some("example-order-1".into()),
        transaction_type: TransactionType::BUY,
        exchange_segment: ExchangeSegment::NSE_EQ,
        product_type: ProductType::INTRADAY,
        order_type: OrderType::LIMIT,
        validity: Validity::DAY,
        security_id: "1333".into(),    // HDFC Bank
        quantity: 1,
        disclosed_quantity: None,
        price: Some(1500.0),
        trigger_price: None,
        after_market_order: Some(false),
        amo_time: None,
        bo_profit_value: None,
        bo_stop_loss_value: None,
    };

    let response = client.place_order(&req).await?;
    println!("Order placed: {:?}", response);
    Ok(())
}
```

### REST API — Get Holdings

```rust,no_run
use dhan_rs::DhanClient;

#[tokio::main]
async fn main() -> dhan_rs::Result<()> {
    let client = DhanClient::new("your-client-id", "your-access-token");

    let holdings = client.get_holdings().await?;
    for h in &holdings {
        println!("{}: {} shares @ ₹{:.2}",
            h.trading_symbol.as_deref().unwrap_or("?"),
            h.total_qty.unwrap_or(0),
            h.avg_cost_price.unwrap_or(0.0),
        );
    }
    Ok(())
}
```

### REST API — Market Quotes

```rust,no_run
use dhan_rs::DhanClient;
use dhan_rs::types::market_quote::MarketQuoteRequest;

#[tokio::main]
async fn main() -> dhan_rs::Result<()> {
    let client = DhanClient::new("your-client-id", "your-access-token");

    let mut req = MarketQuoteRequest::new();
    req.insert("NSE_EQ".into(), vec![1333, 11536]);

    let ltp = client.get_ltp(&req).await?;
    println!("LTP data: {:?}", ltp);
    Ok(())
}
```

### WebSocket — Live Market Feed

```rust,no_run
use dhan_rs::ws::market_feed::{MarketFeedStream, Instrument};
use dhan_rs::types::enums::FeedRequestCode;
use futures_util::StreamExt;

#[tokio::main]
async fn main() -> dhan_rs::Result<()> {
    let mut stream = MarketFeedStream::connect("your-client-id", "your-access-token").await?;

    let instruments = vec![
        Instrument::new("NSE_EQ", "1333"),   // HDFC Bank
        Instrument::new("NSE_EQ", "11536"),  // TCS
    ];
    stream.subscribe(FeedRequestCode::SubscribeTicker, &instruments).await?;

    while let Some(event) = stream.next().await {
        match event {
            Ok(e) => println!("{e:?}"),
            Err(e) => eprintln!("Error: {e}"),
        }
    }
    Ok(())
}
```

### WebSocket — Live Order Updates

```rust,no_run
use dhan_rs::ws::order_update::OrderUpdateStream;
use futures_util::StreamExt;

#[tokio::main]
async fn main() -> dhan_rs::Result<()> {
    let mut stream = OrderUpdateStream::connect(
        "your-client-id",
        "your-access-token",
    ).await?;

    while let Some(msg) = stream.next().await {
        match msg {
            Ok(update) => println!(
                "Order {} → {}",
                update.Data.OrderNo.as_deref().unwrap_or("?"),
                update.Data.Status.as_deref().unwrap_or("?"),
            ),
            Err(e) => eprintln!("Error: {e}"),
        }
    }
    Ok(())
}
```

### Option Chain with Greeks

```rust,no_run
use dhan_rs::DhanClient;
use dhan_rs::types::option_chain::OptionChainRequest;

#[tokio::main]
async fn main() -> dhan_rs::Result<()> {
    let client = DhanClient::new("your-client-id", "your-access-token");

    let req = OptionChainRequest {
        UnderlyingScrip: 13,
        UnderlyingSeg: "IDX_I".into(),
        Expiry: "2026-02-26".into(),
    };

    let chain = client.get_option_chain(&req).await?;
    println!("NIFTY spot: {:?}", chain.data.last_price);

    for (strike, data) in &chain.data.oc {
        if let Some(ce) = &data.ce {
            println!("Strike {strike} CE — LTP: {:?}, IV: {:?}, Delta: {:?}",
                ce.last_price, ce.implied_volatility,
                ce.greeks.as_ref().map(|g| g.delta));
        }
    }
    Ok(())
}
```

## Architecture

```
dhan-rs/
├── src/
│   ├── lib.rs              # Crate root, re-exports
│   ├── client.rs           # DhanClient — HTTP client with auth
│   ├── error.rs            # DhanError enum, Result alias
│   ├── constants.rs        # Base URLs, WebSocket URLs, rate limits
│   ├── types/              # Request/response structs
│   │   ├── enums.rs        # 22+ shared enums (ExchangeSegment, OrderType, etc.)
│   │   ├── orders.rs       # Order types
│   │   ├── super_order.rs  # Super Order types
│   │   ├── forever_order.rs # Forever/GTT Order types
│   │   ├── conditional.rs  # Conditional trigger types
│   │   ├── portfolio.rs    # Holdings, Positions
│   │   ├── funds.rs        # Margin calculator, fund limits
│   │   ├── historical.rs   # OHLCV candle types
│   │   ├── option_chain.rs # Option chain + Greeks
│   │   ├── market_quote.rs # LTP, OHLC, depth quotes
│   │   ├── postback.rs     # Webhook payload type
│   │   └── ...             # auth, data, global_stocks, instruments, etc.
│   ├── api/                # Endpoint implementations (impl DhanClient)
│   │   ├── orders.rs       # 9 order endpoints
│   │   ├── portfolio.rs    # Holdings, positions, convert, exit
│   │   ├── auth.rs         # Token generation, consent flows
│   │   └── ...             # 18 endpoint modules total
│   └── ws/                 # WebSocket streaming
│       ├── market_feed.rs  # Standard binary feed parser + Stream impl
│       ├── manager.rs      # Supervised standard-feed owner
│       ├── order_update.rs # JSON stream and managed order supervisor
│       └── depth.rs        # Separate 20-level and 200-level depth streams
```

`DhanClient` methods are grouped into inherent `impl DhanClient` blocks across
the `api/` modules. The current implementation status page is the authoritative
coverage guide; the surface is deliberately not presented as complete Dhan API
parity.
WebSocket streams implement `futures_util::Stream` for integration with async
combinators.

## API Reference

Full documentation is available on [docs.rs](https://docs.rs/dhan-rs).

### DhanClient Methods

<details>
<summary><strong>Orders</strong> (9 methods)</summary>

| Method | Description |
|---|---|
| `place_order(req)` | Place a new order |
| `modify_order(order_id, req)` | Modify a pending order |
| `cancel_order(order_id)` | Cancel an open order |
| `slice_order(req)` | Slice a large order into smaller ones |
| `get_orders()` | Get all orders for the day |
| `get_order(order_id)` | Get a specific order by ID |
| `get_order_by_correlation_id(id)` | Get order by external correlation ID |
| `get_trades()` | Get all trades for the day |
| `get_trades_for_order(order_id)` | Get trades for a specific order |

</details>

<details>
<summary><strong>Super Orders</strong> (5 methods)</summary>

| Method | Description |
|---|---|
| `place_super_order(req)` | Place a bracket/cover order |
| `modify_super_order(order_id, req)` | Modify a super order |
| `cancel_super_order(order_id, leg)` | Cancel a super order leg |
| `cancel_super_order_no_content(order_id, leg)` | Cancel when the HTML empty-response contract applies |
| `get_super_orders()` | Get all super orders |

</details>

<details>
<summary><strong>Forever Orders</strong> (5 methods)</summary>

| Method | Description |
|---|---|
| `create_forever_order(req)` | Create a GTT/OCO order |
| `modify_forever_order(order_id, req)` | Modify a forever order |
| `delete_forever_order(order_id)` | Delete a forever order |
| `get_all_forever_orders()` | Get all forever orders |
| `get_all_forever_orders_openapi()` | List through the linked OpenAPI route |

</details>

<details>
<summary><strong>Conditional Triggers</strong> (6 methods)</summary>

| Method | Description |
|---|---|
| `place_conditional_trigger(req)` | Create a conditional trigger |
| `modify_conditional_trigger(id, req)` | Modify a trigger |
| `delete_conditional_trigger(id)` | Delete a trigger |
| `get_conditional_trigger(id)` | Get a specific trigger |
| `get_all_conditional_triggers()` | Get all triggers |
| `place_multi_order(req)` | Place the OpenAPI multi-order batch |

</details>

<details>
<summary><strong>Portfolio</strong> (4 methods)</summary>

| Method | Description |
|---|---|
| `get_holdings()` | Get demat holdings |
| `get_positions()` | Get open positions |
| `convert_position(req)` | Convert position product type |
| `exit_all_positions()` | Exit all open positions |

</details>

<details>
<summary><strong>Funds & Margin</strong> (3 methods)</summary>

| Method | Description |
|---|---|
| `calculate_margin(req)` | Calculate margin for a single order |
| `calculate_multi_margin(req)` | Calculate margin for multiple orders |
| `get_fund_limit()` | Get available fund limits |

</details>

<details>
<summary><strong>Market Data</strong> (7 methods)</summary>

| Method | Description |
|---|---|
| `get_ltp(req)` | Get last traded price |
| `get_ohlc(req)` | Get OHLC data |
| `get_quote(req)` | Get full market depth quote |
| `get_daily_historical(req)` | Get daily OHLCV candles |
| `get_intraday_historical(req)` | Get intraday candles |
| `get_option_chain(req)` | Get option chain with Greeks |
| `get_expiry_list(req)` | Get expiry dates |

</details>

<details>
<summary><strong>Selected additional methods</strong></summary>

| Method | Description |
|---|---|
| `generate_access_token(...)` | Generate JWT access token |
| `renew_token()` | Renew expiring token |
| `generate_consent(...)` / `consume_consent(...)` | API key consent flow |
| `partner_generate_consent(...)` / `partner_consume_consent(...)` | Partner consent flow |
| `get_profile()` | Get user profile |
| `set_ip(req)` / `modify_ip(req)` / `get_ip()` | Static IP management |
| `generate_tpin()` / `generate_edis_form(req)` / `generate_bulk_edis_form(req)` / `inquire_edis(isin)` | eDIS |
| `manage_kill_switch(status)` / `get_kill_switch_status()` | Kill switch |
| `set_pnl_exit(req)` / `stop_pnl_exit()` / `get_pnl_exit()` | P&L-based exit |
| `get_ledger_response(from, to)` / `get_ledger(from, to)` / `get_trade_history(from, to, page)` | Statements |

</details>

## Rate Limits

DhanHQ enforces the following rate limits:

| Category | Per Second | Per Minute | Per Hour | Per Day |
|---|---|---|---|---|
| Orders | 10 | 250 | 1,000 | 7,000 |
| Data (REST) | 5 | — | — | 100,000 |
| Historical | 1 | — | — | — |
| Instruments | 20 | — | — | — |

- Max 25 modifications per order
- Option Chain: 1 request per 3 seconds
- Market Quote: up to 1,000 instruments per request
- WebSocket: 5 connections per user, 5,000 instruments each

> **Note:** This library does **not** enforce rate limits automatically. You are
> responsible for staying within the limits.

## Dependencies

| Crate | Purpose |
|---|---|
| `reqwest` | Async HTTP client (rustls-tls) |
| `serde` / `serde_json` | JSON serialization |
| `tokio` | Async runtime |
| `tokio-tungstenite` | WebSocket client (rustls-tls) |
| `thiserror` | Error type derivation |
| `chrono` | Declared date/time serde support; currently unused by `src/` |
| `tracing` | Structured logging |
| `url` | URL parse errors represented by `DhanError` |
| `futures-util` | Stream/Sink traits for WebSocket |
| `bytes` | Raw market-frame buffers |

## Requirements

- Rust 2024 edition (1.85+)
- A DhanHQ trading account with API access
- Access token from [DhanHQ Developer Portal]https://dhanhq.co/docs/v2/

## License

This project is licensed under the [MIT License](LICENSE).

## Disclaimer

This is an **unofficial**, **AI-generated** client library. It is **not**
affiliated with, endorsed by, or supported by DhanHQ or Dhan. Use at your own
risk. See [DISCLAIMER.md](DISCLAIMER.md) for full details.

Trading in financial markets involves substantial risk of loss. This software
is provided "as is" without warranty of any kind. The authors are not
responsible for any financial losses incurred through the use of this library.