A Python-based algorithmic trading system for the Indian stock market (NSE). It focuses on NIFTY and BANKNIFTY options/futures trading with real-time data processing and strategy execution.
- Features
- Prerequisites
- Installation
- Configuration
- Running the System
- Project Structure
- Architecture
- Security Notes
- Troubleshooting
- Real-time Market Data: WebSocket-based live data streaming from GDFL
- Multiple Timeframes: 30s, 60s, 180s, 300s data resampling
- Options Trading Strategies: NIFTY and BANKNIFTY options strategies
- IV Calculation: Uses Black-Scholes model for implied volatility
- Risk Management: Stop-loss, trailing stops, expiry-based exits
- Telegram Notifications: Real-time alerts for trades and system status
- Scheduled Execution: Time-based triggers using APScheduler
- Python 3.7 or higher
- MongoDB (installed and running)
- Access to GDFL market data API
- Zerodha API credentials (optional, for alternative data source)
- Telegram Bot Token and Channels (for notifications)
git clone <repository-url>
cd Trading-Algorithm# Windows
python -m venv venv
venv\Scripts\activate
# Linux/Mac
python3 -m venv venv
source venv/bin/activatepip install -r requirements.txtNote: If requirements.txt is not available, install the following packages:
pip install motor pandas numpy asyncio websocket-client mibian APScheduler python-telegram-bot python-dotenv schedule dateutilpip install python-dotenvCopy the example configuration file:
copy appConfig.example.py appConfig.pyLinux/Mac:
cp appConfig.example.py appConfig.pyCreate a .env file in the project root directory with your credentials:
# MongoDB Configuration
MONGODB_USER=your_mongodb_username
MONGODB_PASSWORD=your_mongodb_password
MONGODB_HOST=127.0.0.1
MONGODB_PORT=27016
MONGODB_DATABASE=credencewall
# Telegram Configuration
TELEGRAM_BOT_TOKEN=your_telegram_bot_token
TELEGRAM_CHANNEL_MONITOR=@your_monitor_channel
TELEGRAM_CHANNEL_SE=@your_se_channel
TELEGRAM_CHANNEL_CP=@your_cp_channel
TELEGRAM_CHANNEL_ID=@your_id_channel
# GDFL API Configuration
GDFL_API_KEY=your_gdfl_api_key
GDFL_WEBSOCKET_ENDPOINT=ws://your-gdfl-endpoint:port
# Zerodha API Configuration (Optional)
ZERODHA_USER_ID=your_zerodha_user_id
ZERODHA_API_KEY=your_zerodha_api_key
ZERODHA_API_SECRET=your_zerodha_api_secret
ZERODHA_PASSWORD=your_zerodha_password
ZERODHA_PIN=your_zerodha_pin
# MySQL Configuration (If used)
MYSQL_HOST=your_mysql_host
MYSQL_PORT=3306
MYSQL_USER=your_mysql_user
MYSQL_PASSWORD=your_mysql_password
MYSQL_DATABASE=your_mysql_databaseEdit appConfig.py and ensure it's configured to load environment variables. The file should already be set up to use python-dotenv if available.
The system consists of multiple scripts that need to be run in a specific order or as separate processes.
Run this first to update exchange data and instruments:
python main_001_GDailyInit.pyAlternative (Zerodha):
python main_001_ZDailyInit.pyStart collecting real-time market data:
python main_002_GDailyLiveTicks.pyAlternative (Zerodha):
python main_002_ZDailyLiveTicks.pyConvert raw ticks into OHLCV candles:
30-second resampling:
python main_003_GDataResample_30Sec_v5.py60-second resampling:
python main_004_GDataResample_60Sec_v2.pyRun trading strategies:
Short Expiry Strategy (300-second intervals):
python main_005_GSolverSE_300Sec_v2.pyCall/Put Strategy (180-second intervals):
python main_006_GSolverCP_180Sec_v2.pyIntraday ID Strategy:
python main_007_QStrategy_ID_v1.pyFor production, use systemd services (if configured):
# Start services
sudo systemctl start qchGDFL-Live.service
sudo systemctl start qchGDFL-LiveResample30Sec.service
sudo systemctl start qchGDFL-LiveResample60Sec.service
sudo systemctl start qchGDFL-LiveStrategySE300Sec.service
sudo systemctl start qchGDFL-LiveStrategyCP180Sec.service
# Enable auto-start on boot
sudo systemctl enable qchGDFL-Live.serviceTrading-Algorithm/
├── main_001_GDailyInit.py # Daily initialization
├── main_002_GDailyLiveTicks.py # Live data collection
├── main_003_GDataResample_30Sec_v5.py # 30s resampling
├── main_004_GDataResample_60Sec_v2.py # 60s resampling
├── main_005_GSolverSE_300Sec_v2.py # SE strategy execution
├── main_006_GSolverCP_180Sec_v2.py # CP strategy execution
├── main_007_QStrategy_ID_v1.py # ID strategy execution
├── appConfig.py # Configuration (use .env)
├── appConfig.example.py # Configuration template
├── .env # Environment variables (create this)
├── .gitignore # Git ignore rules
├── Sources/ # Data source implementations
│ ├── GDFL/ # GDFL data source
│ └── ZERO/ # Zerodha data source
├── SolverStrategies/ # Strategy framework
│ ├── BaseStrategy.py # Base strategy engine
│ ├── Strategies.py # Strategy implementations
│ ├── Rules.py # Trade rule definitions
│ └── Enums.py # Trading enums
├── Channels/ # Notification channels
│ └── TelegramChannel.py # Telegram integration
├── Utils/ # Utility modules
│ └── BrokerUtils/ # Broker utilities
└── Linux/ # Systemd service files
The system follows a multi-stage pipeline:
- Daily Initialization: Updates exchange data and instruments from GDFL
- Live Tick Collection: Subscribes to real-time market data via WebSocket
- Data Resampling: Converts raw ticks into OHLCV candles (30s, 60s intervals)
- Strategy Execution: Implements entry/exit rules, stop-loss, trailing stops
- Notifications: Sends alerts via Telegram
Market Data (GDFL/Zerodha)
↓
Raw Ticks (MongoDB)
↓
Resampled Candles (30s, 60s)
↓
Strategy Evaluation
↓
Trade Signals → Telegram Notifications
-
Never commit
.envfile: The.envfile contains sensitive credentials and is excluded via.gitignore -
Rotate exposed credentials: If any credentials were previously committed to version control, rotate them immediately
-
Use environment variables: All credentials should be stored in the
.envfile, never hardcoded in the code -
Review git history: If this code was previously committed with credentials, clean the git history
-
Production deployment: For production, consider using:
- AWS Secrets Manager
- HashiCorp Vault
- Azure Key Vault
- Or similar secrets management solutions
See the Configuration section above for the complete list of required environment variables.
Problem: Cannot connect to MongoDB
Solution:
- Ensure MongoDB is running:
mongod --version - Check MongoDB connection settings in
.env - Verify network connectivity to MongoDB host
- Check MongoDB logs for authentication errors
Problem: Telegram notifications not working
Solution:
- Verify bot token is correct
- Ensure bot is added as administrator to channels
- Check bot has "Post Messages" permission
- Verify channel usernames are correct (include
@symbol)
Problem: Cannot connect to GDFL WebSocket
Solution:
- Verify GDFL API key is correct
- Check WebSocket endpoint URL
- Ensure network connectivity to GDFL servers
- Verify API key permissions
Problem: ModuleNotFoundError when running scripts
Solution:
- Ensure virtual environment is activated
- Install all requirements:
pip install -r requirements.txt - Check Python path is correct
- Verify all dependencies are installed
Problem: Database name errors or collection not found
Solution:
- Check
MONGODB_DATABASEin.envfile - Verify database exists in MongoDB
- Run initialization script first:
python main_001_GDailyInit.py - Check database collections are created
- Telegram Setup Guide - Detailed guide for setting up Telegram channels
- Security Guide - Security best practices
- Rename Summary - Details about recent changes
[Add your license information here]
[Add contributing guidelines if applicable]
[Add support contact information if applicable]
Note: This is a production trading system. Always test thoroughly in a paper trading environment before using with real funds. Use at your own risk.