An automated trading system for Zerodha Kite API that manages authentication, orders, and notifications with focus on ESM stocks.
TradeMaster is a Python-based trading automation tool that interfaces with Zerodha's Kite API to facilitate automated trading strategies. The system handles authentication, order management, and sends notifications about trade executions and system status. It is specifically designed to automate the placement of market or limit CNC orders for ESM Stage 1 or 2 stocks exactly at 9:30 AM to improve chances of order execution.
- Automated Authentication: Manages Zerodha Kite API tokens with auto-renewal
- Order Management: Places, modifies, and tracks orders
- Notification System: Alerts via various channels for trade executions and system events
- Stock Configuration: Customizable stock watch list through CSV configuration
- ESM Stock Order Rules Compliance: Ensures orders follow ESM stock restrictions
- Scheduled Execution: Places orders exactly at 9:30 AM IST
- Robust Error Handling: Includes retry logic and comprehensive logging
- Python 3.7+
- Google Chrome (for automated authentication)
- Windows OS (also supports Linux/Mac)
- Clone this repository:
git clone https://github.com/kibo-saurabhgadkari/TradeMaster.git
cd TradeMaster
- Install dependencies:
pip install -r requirements.txt
- Create
.env
file in the root directory with your Zerodha API credentials:
API_KEY=your_api_key
API_SECRET=your_api_secret
ZERODHA_USER_ID=your_user_id
ZERODHA_PASSWORD=your_password
ZERODHA_PIN=your_pin
EMAIL_SENDER=your_email@example.com
EMAIL_PASSWORD=your_email_password
EMAIL_RECIPIENT=recipient@example.com
TELEGRAM_BOT_TOKEN=your_telegram_bot_token
TELEGRAM_CHAT_ID=your_telegram_chat_id
auth_manager.py
- Handles Zerodha API authentication and token managementorder_manager.py
- Manages trading orders with ESM stock compliancenotification_manager.py
- Sends alerts and notificationsmain.py
- Main execution script with schedulingconfig/stocks.csv
- Configuration file for stocks to tradelogs/
- Directory containing application logs (daily format: YYYY-MM-DD.log)
- Configure your stock list in
config/stocks.csv
with desired parameters:
trading_symbol,quantity,order_type,limit_price,is_active
NSE:INFY,1,LIMIT,1000,True
NSE:TCS,1,MARKET,,True
- Run the application in scheduled mode:
python main.py --mode scheduled
- Or run immediately for testing:
python main.py --mode immediate
TradeMaster supports three authentication methods:
-
Using Existing Token:
- Uses a valid access token stored in .env
-
Manual Request Token:
- Generates access token from manually obtained request token
-
Automated Login:
- Uses Selenium to automate the login process
- Requires Zerodha credentials in .env
Example code for authentication:
from auth_manager import ZerodhaAuthManager
# Initialize the authentication manager
auth_manager = ZerodhaAuthManager('.env')
# Check if token is valid
if not auth_manager.is_token_valid():
# Perform automated login
auth_manager.automated_login()
The system ensures compliance with ESM stock trading rules:
- Uses CNC as product type (ESM stocks allow only CNC)
- Defaults to LIMIT orders when market orders aren't allowed
- Validates sufficient funds and proper lot sizes
- Places orders precisely at 9:30 AM IST using scheduling
The system supports various order types with specific handling for ESM stocks:
- Market orders
- Limit orders
- CNC product type
Example for placing an order:
from order_manager import OrderManager
order_mgr = OrderManager(auth_manager.kite)
order_mgr.place_order(
symbol="NSE:INFY",
quantity=1,
order_type="LIMIT",
price=1000,
product_type="CNC"
)
The notification system alerts you through multiple channels:
- Telegram
- Console logs
Notifications are sent for:
- Successful order placement
- Order failures
- Authentication/token failures
Stock configuration is managed through config/stocks.csv
with the following format:
trading_symbol,quantity,order_type,limit_price,is_active
NSE:INFY,1,LIMIT,1000,True
NSE:TCS,1,MARKET,,True
The application generates detailed logs in the logs/
directory with a daily naming format (YYYY-MM-DD.log), which include:
- Authentication events
- Order placements and executions
- System errors and warnings
- Timestamps for all actions
The system can be deployed on:
- Local machine (Windows/Linux/Mac)
- Cloud server (AWS EC2, GCP, DigitalOcean)
- Raspberry Pi
For persistent operation:
- Linux/Mac: Use screen or tmux
- Windows: Create a scheduled task
- Linux Server: Set up as a systemd service
Common issues and their solutions:
-
Authentication Failures:
- Verify your Zerodha credentials
- Check if Chrome is properly installed
- Ensure ChromeDriver is compatible with your Chrome version
-
Order Placement Issues:
- Verify you have sufficient funds
- Check for market hours (orders scheduled for 9:30 AM)
- Confirm stock configuration is correct
-
ESM Stock Restrictions:
- Ensure you're using CNC as product type
- Use LIMIT orders when required by ESM restrictions
- API keys and access tokens are stored securely in
.env
- Sensitive information is never logged or displayed
- Credentials can be encrypted for additional security
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
This software is for educational and informational purposes only. Use at your own risk. The creators are not responsible for any financial losses incurred through the use of this software.