A high-frequency scalping bot for Bank Nifty options using the DhanHQ API. Built with Python's async/await architecture for efficient, event-driven trading.
This software is for educational purposes only. Trading in financial markets involves substantial risk of loss. Past performance is not indicative of future results. Use at your own risk.
- High-Frequency Scalping - Optimized for quick entry/exit on Bank Nifty options
- Async Event-Driven Architecture - Efficient processing with asyncio queues
- Dual Momentum Strategy - Combines EMA, RSI, and MACD for signal generation
- Real-Time Candle Building - Constructs OHLCV candles from tick data
- WebSocket Market Feed - Live streaming data via DhanHQ WebSocket
- Risk Management - Configurable stop-loss, targets, and trailing stops
- Paper Trading Mode - Test strategies without risking real money
- Rate Limit Handling - Respects API rate limits (25 orders/second)
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Market Feed │───▶│ Candle Builder │───▶│ Alpha Engine │
│ (Producer) │ │ (Aggregator) │ │ (Strategy) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│
▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Position Mgr │◀───│ Order Manager │◀───│ Signal Queue │
│ (Risk) │ │ (Executor) │ │ (Consumer) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
dhan-trader-bot/
├── main.py # Core orchestrator and trading engine
├── auth.py # OAuth authentication (run: python auth.py)
├── strategy.py # Dual momentum scalping strategy
├── indicators.py # Technical indicators (EMA, RSI, MACD, ATR)
├── candle_builder.py # Real-time OHLCV candle construction
├── order_manager.py # Order execution and position management
├── market_feed.py # DhanHQ WebSocket market data handler
├── config.py # Configuration and environment settings
├── models.py # Data models and structures
├── utils.py # Helper utilities and logging
└── requirements.txt # Python dependencies
- Python 3.10 or higher
- DhanHQ trading account with API access
- TA-Lib C library (for technical indicators)
git clone https://github.com/vishwamartur/dhan-trader-bot.git
cd dhan-trader-botWindows: Download and install from TA-Lib Windows
macOS:
brew install ta-libLinux (Ubuntu/Debian):
sudo apt-get install ta-lib
# Or build from source:
wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz
tar -xzf ta-lib-0.4.0-src.tar.gz
cd ta-lib/
./configure --prefix=/usr
make
sudo make installpip install -r requirements.txtOption A: Easy OAuth Login (Recommended)
python auth.pyThis opens your browser for Dhan login. After authentication, your token is saved automatically.
# Check authentication status
python auth.py --status
# Logout (remove saved token)
python auth.py --logoutOption B: Environment Variables
Set environment variables if you prefer manual configuration:
# Linux/macOS
export DHAN_CLIENT_ID="your_client_id"
export DHAN_ACCESS_TOKEN="your_access_token"
# Windows (PowerShell)
$env:DHAN_CLIENT_ID="your_client_id"
$env:DHAN_ACCESS_TOKEN="your_access_token"Or create a .env file:
DHAN_CLIENT_ID=your_client_id
DHAN_ACCESS_TOKEN=your_access_token
Paper Trading Mode (Recommended for Testing):
python main.pyLive Trading Mode:
python main.py --liveTest API Connection:
python main.py --test-connectionEdit config.py to customize:
| Parameter | Default | Description |
|---|---|---|
PAPER_TRADING |
True |
Enable/disable paper trading |
LOT_SIZE |
15 |
Bank Nifty lot size |
NUM_LOTS |
2 |
Number of lots per trade |
STOP_LOSS_POINTS |
20.0 |
Stop loss in points |
TARGET_POINTS |
40.0 |
Target profit in points |
MAX_DAILY_LOSS |
5000.0 |
Maximum daily loss (INR) |
CANDLE_TIMEFRAME_SECONDS |
60 |
Candle period (1 min) |
EMA_PERIOD |
9 |
EMA indicator period |
RSI_PERIOD |
14 |
RSI indicator period |
The bot uses a Dual Momentum Strategy:
- EMA Crossover - Price crossing above/below 9-period EMA
- RSI Filter - RSI > 60 for longs, RSI < 40 for shorts
- MACD Confirmation - MACD histogram direction alignment
| Signal | Conditions |
|---|---|
| LONG | Price > EMA(9) + RSI > 60 + MACD histogram positive |
| SHORT | Price < EMA(9) + RSI < 40 + MACD histogram negative |
- Stop Loss - Fixed points below entry
- Target - Fixed points above entry
- Trailing Stop - Dynamic stop adjustment as price moves favorably
- Maximum 1 position at a time
- Fixed stop-loss and target levels
- Daily loss limit enforcement
- Rate limit compliance (25 orders/second)
- Graceful shutdown with position closing
Logs are written to logs/trading.log with the following format:
2024-01-21 09:30:15 | INFO | 📊 Tick processor started
2024-01-21 09:30:15 | INFO | 🧠 Signal processor started
2024-01-21 09:30:15 | INFO | 💰 Order executor started
python -m pytest tests/- Async Queues - Tick, Candle, and Signal queues for decoupled processing
- Event-Driven - Callbacks for candle completion and signal generation
- Graceful Shutdown - Signal handlers for clean termination
This bot uses the official DhanHQ Python SDK:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- GitHub: @vishwamartur
Made with ❤️ for algorithmic traders