Skip to content

mst-rajatmishra/Trading-with-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Trading with Python - Options Chain Analyzer

A comprehensive Python-based GUI application for real-time options trading analysis, specifically designed for Indian stock market indices (NIFTY and BANKNIFTY). This application provides live options chain data with advanced Open Interest (OI) analysis using Zerodha's KiteConnect API.

πŸš€ Features

Core Functionality

  • Real-time Options Chain Display: Live options data for NIFTY 50 and BANKNIFTY indices
  • Open Interest Analysis: Advanced OI tracking with change indicators
  • Multi-timeframe Support: 5min, 10min, 15min, 30min, 60min, and daily charts
  • Visual OI Indicators: Color-coded display based on OI changes
  • Historical Data Integration: 7-day historical data for trend analysis
  • Auto-expiry Detection: Automatic calculation of option expiry dates

Visual Interface

  • Interactive GUI: Built with tkinter for cross-platform compatibility
  • Scrollable Options Chain: Handle large number of strike prices
  • Color-coded Analysis:
    • 🟒 Green: High OI increase (>500,000)
    • πŸ”΄ Red: OI decrease
    • 🟠 Orange: Moderate changes
  • Real-time Updates: Live market data refresh

πŸ“‹ Prerequisites

Required Dependencies

# Install from requirements file
pip install -r requirements.txt

# Or install manually:
pip install kiteconnect>=4.0.0
pip install python-dateutil>=2.8.0

# Note: tkinter and sqlite3 are usually included with Python

API Requirements

  • Zerodha Kite API Account: You need an active Zerodha trading account
  • API Key & Secret: Obtain from Zerodha Developer Console
  • Request Token: Generated during authentication process

πŸ”§ Setup Instructions

1. API Configuration

  1. Login to Kite Connect Developer Console
  2. Create a new app and get your api_key and api_secret
  3. Update the following lines in API.PY:
    kite=KiteConnect(api_key="YOUR_API_KEY_HERE")
    data=kite.generate_session(request_token,api_secret="YOUR_API_SECRET_HERE")

2. Install Dependencies

# Install required packages
pip install -r requirements.txt

3. Running the Application

cd "TRADING list"
python API.PY

πŸ–ΌοΈ Application Interface

Main Window Components

The application interface consists of several key areas:

  1. Top Control Panel:

    • Connection Status: Red "NOT CONNECTED" button (changes to green "CONNECTED")
    • Index Selector: Dropdown to choose between NIFTY and BANKNIFTY
    • Expiry Selector: Dropdown with available expiry dates
    • Timeframe Selector: Choose from 5min to daily intervals
    • Option Chain Button: Generate the options chain display
  2. Options Chain Display:

    [CE_OI] [LTP] [CHANGE] [STRIKE] [LTP] [CHANGE] [PE_OI]
    
    • Left Side (CE): Call options data
    • Center (STRIKE): Strike prices with current price highlighted
    • Right Side (PE): Put options data
  3. Color Coding System:

    • Spring Green: Current market price strike
    • Pale Green: High positive OI change (>500K)
    • Red: Negative OI change
    • Orange/Tomato: Moderate OI changes

Authentication Flow

  1. Click "NOT CONNECTED" button
  2. Popup window appears requesting Kite token
  3. Enter your request token and click "SUBMIT"
  4. Button updates to show "CONNECTED" status
  5. Expiry dates populate automatically

πŸ“Š How It Works

Application Workflow

  1. Authentication Process

    • Click "NOT CONNECTED" button
    • Enter your Kite request token
    • Application authenticates and fetches available expiries
    • Button changes to "CONNECTED" (green)
  2. Options Chain Setup

    • Select index: NIFTY or BANKNIFTY
    • Choose expiry date from dropdown
    • Select timeframe for analysis
    • Click "OPTION CHAIN" to generate data
  3. Data Display Structure

    CE_OI | LTP | CHANGE | STRIKE | LTP | CHANGE | PE_OI
    
    • CE: Call Options (Left side)
    • PE: Put Options (Right side)
    • OI: Open Interest values
    • LTP: Last Traded Price
    • CHANGE: OI change from previous period

Technical Implementation

Key Components

  1. Main GUI Window (root)

    • 700x550 pixel window with black background
    • Uses winnative theme for professional appearance
  2. Authentication System (connectZerodha())

    • Popup window for token entry
    • Session generation with Kite API
    • Access token management
  3. Expiry Calculation (expiry())

    • Fetches current market price
    • Rounds to nearest 100 (BANKNIFTY) or 50 (NIFTY)
    • Calculates available expiry dates (30-day scan)
  4. Options Chain Generation (optionChain())

    • Creates scrollable frame with strike prices
    • Fetches 20 strikes around current market price
    • Displays Call and Put options symmetrically
  5. Open Interest Analysis (oi_data())

    • Fetches 7-day historical data
    • Calculates OI changes
    • Applies color coding based on significance

Performance Optimization

  • Multi-threading: Separate threads for OI data fetching
  • Batch API Calls: Multiple instruments fetched in single request
  • Efficient UI Updates: Direct widget manipulation

Color Coding Logic

if (oi_change > 500000):
    color = "palegreen"      # Significant bullish/bearish activity
elif (oi_change < 0):
    color = "red"           # Decreasing interest
else:
    color = "tomato1"       # Moderate activity

🎯 Usage Guide

Step-by-Step Operation

  1. Initial Setup

    • Launch the application
    • Click "NOT CONNECTED" button
    • Enter your Kite request token
    • Wait for "CONNECTED" status
  2. Market Analysis

    • Select NIFTY or BANKNIFTY from dropdown
    • Choose desired expiry date
    • Select analysis timeframe
    • Click "OPTION CHAIN"
  3. Reading the Data

    • Green Strikes: High activity zones (>500K OI change)
    • Red Strikes: Reducing interest
    • Current Price: Highlighted in springgreen
    • Strike Price: Center column shows exercise prices
  4. Interpretation Tips

    • High Call OI: Potential resistance level
    • High Put OI: Potential support level
    • OI changes: Direction of market sentiment

πŸ” Advanced Features

Multi-threading Implementation

The application uses threading for:

  • Non-blocking UI updates
  • Parallel data fetching
  • Improved response times

Error Handling

  • API connection failures
  • Invalid token management
  • Market data unavailability
  • Date calculation edge cases

πŸ”§ Troubleshooting

Common Issues

  1. "Module not found" errors

    # Install all dependencies
    pip install -r requirements.txt
    
    # For tkinter on Ubuntu/Debian
    sudo apt-get install python3-tk
  2. Authentication failures

    • Verify API key and secret are correct
    • Check if request token is fresh (expires quickly)
    • Ensure Zerodha account has API access enabled
  3. No data showing

    • Check market hours (9:15 AM - 3:30 PM IST)
    • Verify network connectivity
    • Check Zerodha API status
  4. GUI not responsive

    • Close and restart application
    • Check system resources
    • Verify threading is working properly

Development Notes

Run the validation script to check setup:

python validate.py

⚠️ Important Notes

Market Hours

  • Application works during NSE trading hours (9:15 AM - 3:30 PM IST)
  • After-hours data may be limited or delayed

Rate Limits

  • Zerodha API has rate limits (3 requests/second)
  • Application includes appropriate delays

Data Accuracy

  • Real-time data subject to exchange delays
  • OI data updates every few minutes during market hours

πŸ› οΈ Technical Architecture

Dependencies

  • GUI Framework: tkinter (cross-platform)
  • API Client: kiteconnect (Zerodha official)
  • Date Handling: datetime, dateutil
  • Threading: Python threading module
  • Data Storage: sqlite3 (for future enhancements)

File Structure

Trading-with-python/
β”œβ”€β”€ README.md
└── TRADING list/
    └── API.PY          # Main application file

Key Classes and Functions

  • connectZerodha(): API authentication
  • expiry(): Expiry date calculation
  • optionChain(): Main options chain display
  • oi_data(): Open Interest analysis
  • startThread(): Threading management

🚨 Disclaimer

This application is for educational and analysis purposes only. Always:

  • Verify data accuracy before trading decisions
  • Understand market risks
  • Consult financial advisors for investment decisions
  • Test thoroughly in paper trading before live use

πŸ“ License

This project is provided as-is for educational purposes. Users are responsible for their own trading decisions and API usage compliance.

🀝 Contributing

Feel free to submit issues, fork the repository, and create pull requests for improvements.


Note: Ensure you have proper API credentials and understand the associated costs before using this application for live trading analysis.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages