Hello everyone,
When building trading or investment applications with DhanHQ, developers often need to decide between using Live Market Feed (WebSocket-based) and Market Quote (REST API-based). Each method serves distinct purposes based on your application’s specific needs.
When to Use Live Market Feed (WebSocket):
Real-Time Updates:
WebSocket provides continuous, real-time streaming of market data, which is ideal for:
- Trading Dashboards: For live updates where each price movement matters.
- Algo Trading: Essential for algorithms requiring immediate market data to execute trades swiftly.
- Live Charts & Technical Analysis: Real-time price movements are crucial for precise technical analysis.
- High-Frequency Applications: Applications where latency is critical, and delays could result in significant losses.
Pros:
- Immediate and continuous updates
- Best for real-time decision-making
- Ideal for high-frequency and latency-sensitive trading
Cons:
- Higher bandwidth consumption
- Requires continuous connection management
- Not efficient for occasional or single data fetches
Why Polling Single Data via WebSocket Is Not Useful:
WebSocket connections maintain a continuous, bidirectional communication channel. Polling individual data points using WebSocket is inefficient because it:
- Consumes unnecessary bandwidth by maintaining an open connection for occasional single data requests.
- Wastes server resources by keeping an idle connection alive when not continuously streaming.
- Is technically inefficient, as WebSockets are designed explicitly for real-time data streaming rather than occasional or on-demand queries.
When to Use Market Quote (REST API):
On-Demand Data Requests:
REST API provides data fetched at specific intervals or conditions, suitable for:
- Portfolio Valuations: Periodic snapshots where continuous updates are unnecessary.
- Snapshot fetch: Fetching market data at a fixed interval to compute values.
- Historical Data Queries: RESTful interactions are suited for data where immediate response isn’t mandatory.
Pros:
- Simple, stateless requests
- Efficient for low-frequency and occasional data retrieval
- Lower bandwidth usage and easier resource management
Cons:
- Not suitable for high-frequency trading
- Snapshot data, not updated continuously in real-time
Websocket Usage Limits:
Since the launch of websockets, we found that users were using Live Market Feed for fetching a single data packet, which is not only inefficient, but adds immense load on both client end and server end. This is why we have implemented rate-limiting on establishing new websocket connections. Specifically:
- Request Limits: More than 10 connection requests per minute from a single IP address will trigger IP blocking.
- Blocking Duration: The blocked IP will remain unable to make further requests for the next one minute.
To avoid this:
- Ensure correct parameters are passed while connecting to websocket.
- Shift to Market Quote APIs for fetching market snapshot data.
Understanding these differences helps optimize your algo’s performance, ensures compliance with DhanHQ’s usage policies, enhances experience, and manages operational costs effectively.
Happy Trading!