https://github.com/marketcalls/dhan-20depth
https://github.com/marketcalls/dhan-20depth
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/marketcalls/dhan-20depth
- Owner: marketcalls
- License: mit
- Created: 2025-01-16T06:09:31.000Z (9 months ago)
- Default Branch: master
- Last Pushed: 2025-01-16T07:24:22.000Z (9 months ago)
- Last Synced: 2025-01-16T07:45:02.688Z (9 months ago)
- Language: JavaScript
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Dhan 20 Depth Market Data Viewer
A real-time market depth visualization tool for Dhan Trading API that displays 20-level market depth data for NSE stocks.
## Features
- Real-time 20-level market depth data visualization
- WebSocket-based live data streaming
- Clean and responsive UI using TailwindCSS and DaisyUI
- Animated bid/offer quantity updates
- Secure credential management through environment variables## Prerequisites
- Python 3.8+
- Dhan Trading Account
- Dhan API Credentials (Token and Client ID)## Installation
1. Clone the repository:
```bash
git clone https://github.com/marketcalls/dhan-20depth.git
cd dhan-20depth
```2. Install required Python packages:
```bash
pip install -r requirements.txt
```3. Create a `.env` file in the project root and add your Dhan credentials:
```env
DHAN_TOKEN=your_dhan_token
DHAN_CLIENT_ID=your_client_id
```## Configuration
The application uses the following environment variables:
- `DHAN_TOKEN`: Your Dhan API token
- `DHAN_CLIENT_ID`: Your Dhan client IDYou can also modify the stock being tracked by changing the `SecurityId` in `market-depth.js`:
```javascript
{
"ExchangeSegment": "NSE_EQ",
"SecurityId": "2885", // Change this to your desired stock's security ID
"BidAskBoth": true,
"Mode": "20DEPTH"
}
```## Usage
1. Start the Flask server:
```bash
python app.py
```2. Open your browser and navigate to:
```
http://localhost:5000
```## Technical Details
### Backend (`app.py`)
- Flask-based server handling WebSocket connections and credential management
- Asynchronous WebSocket client for Dhan's market depth feed
- Binary message parsing for market depth data### Frontend
- `market-depth.js`: Handles WebSocket connection, data processing, and UI updates
- `index.html`: Responsive UI layout with real-time data display
- Uses TailwindCSS and DaisyUI for styling### WebSocket Message Format
Market depth data is received in binary format:
- Header (12 bytes):
- Message Length (2 bytes)
- Feed Code (1 byte): 41 for Bid, 51 for Ask
- Exchange Segment (1 byte)
- Security ID (4 bytes)
- Message Sequence (4 bytes)
- Data (20 levels × 16 bytes each):
- Price (8 bytes, double)
- Quantity (4 bytes, uint32)
- Orders (4 bytes, uint32)## Error Handling
- Automatic WebSocket reconnection on disconnection
- Ping/Pong mechanism to detect connection health
- Comprehensive error logging for debugging
- Graceful handling of malformed messages## Contributing
1. Fork the repository
2. Create your feature branch (`git checkout -b feature/AmazingFeature`)
3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request## License
This project is licensed under the MIT License - see the LICENSE file for details.
## Acknowledgments
- [Dhan API](https://api.dhan.co) for providing the market data feed
- [TailwindCSS](https://tailwindcss.com) and [DaisyUI](https://daisyui.com) for the UI components
- [Flask](https://flask.palletsprojects.com) for the web framework## Support
For support, please raise an issue in the GitHub repository or contact the maintainers.