Skip to main content

A Python CLI application for fetching real-time options trading data from Upstox API with Black-Scholes Greeks calculations

Project description

Option Chain Data Fetcher

A clean, interactive CLI app for fetching option chain data from Upstox API with Black-Scholes Greeks calculation.

Features

  • โœจ Interactive symbol selection with smart recognition
  • ๐Ÿ“Š Support for 180+ F&O enabled stocks
  • ๐Ÿ“ˆ Support for all major indices (NIFTY, BANKNIFTY, etc.)
  • ๐Ÿ“… Automatic expiry date fetching
  • ๐ŸŽฏ Strike price specific data filtering
  • ๐ŸŽจ Beautiful terminal UI with no clutter
  • ๐Ÿ“‰ Displays both Call (CE) and Put (PE) options
  • ๐Ÿ“ Shows comprehensive data: LTP, Volume, OI, Bid/Ask
  • ๐Ÿ”ฌ Option Greeks: Delta, Gamma, Theta, Vega, IV
  • ๐Ÿ” Built-in symbol search (type 'LIST' to see all supported symbols)

Project Structure

bsholes_up/
โ”œโ”€โ”€ src/                        # Main application code
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ option_chain_app.py    # Main application logic
โ”‚   โ”œโ”€โ”€ black_scholes.py       # Black-Scholes calculator
โ”‚   โ”œโ”€โ”€ stock_mappings.py      # Stock ISIN mappings
โ”‚   โ”œโ”€โ”€ instrument_manager.py  # F&O instrument management
โ”‚   โ””โ”€โ”€ token_manager.py       # OAuth & token management
โ”œโ”€โ”€ web_app/                    # Web application
โ”‚   โ”œโ”€โ”€ app.py                  # FastAPI backend with OAuth
โ”‚   โ”œโ”€โ”€ templates/
โ”‚   โ”‚   โ””โ”€โ”€ index.html          # Frontend UI
โ”‚   โ””โ”€โ”€ static/
โ”‚       โ”œโ”€โ”€ app.js              # Frontend logic
โ”‚       โ””โ”€โ”€ styles.css          # UI styles
โ”œโ”€โ”€ tests/                      # Test files
โ”‚   โ”œโ”€โ”€ test_api_connection.py
โ”‚   โ”œโ”€โ”€ test_stock_validation.py
โ”‚   โ”œโ”€โ”€ test_option_chain_flow.py
โ”‚   โ”œโ”€โ”€ test_integration.py
โ”‚   โ”œโ”€โ”€ test_black_scholes.py
โ”‚   โ””โ”€โ”€ test_integration_bs.py
โ”œโ”€โ”€ scripts/                    # Debug and utility scripts
โ”‚   โ”œโ”€โ”€ debug_stock_options.py
โ”‚   โ”œโ”€โ”€ check_underlying.py
โ”‚   โ””โ”€โ”€ get_isin.py
โ”œโ”€โ”€ run.py                      # CLI entry point
โ”œโ”€โ”€ run_web.py                  # Web app entry point
โ”œโ”€โ”€ requirements.txt            # Python dependencies
โ”œโ”€โ”€ .env.example                # Environment variable template
โ””โ”€โ”€ README.md                   # This file

Setup

  1. Install dependencies:
pip install -r requirements.txt
  1. Configure Upstox authentication:

    Option A: OAuth Login (Recommended for Web App)

    • Copy .env.example to .env
    • Add your Upstox API credentials:
      UPSTOX_CLIENT_ID=your_api_key
      UPSTOX_CLIENT_SECRET=your_api_secret
      UPSTOX_REDIRECT_URI=http://localhost:8000/auth/callback
      
    • Get these from your Upstox Developer Console app settings

    Option B: Manual Token (CLI usage)

    • Copy .env.example to .env
    • Add your access token to .env:
      UPSTOX_ACCESS_TOKEN=your_access_token
      

Usage

CLI Application

Run the CLI app:

python run.py

Web Application (with OAuth)

Run the web app:

python run_web.py

Then open http://localhost:8000 in your browser.

OAuth Flow:

  1. Click "Login with Upstox" button
  2. You'll be redirected to Upstox login page
  3. After successful login, you'll be redirected back
  4. Token is automatically managed (expires at 3:30 AM IST daily)
  5. No manual token entry needed!

Follow the prompts:

  1. Enter asset symbol (e.g., TCS, SBIN, NIFTY, BANKNIFTY)
    • Type 'LIST' to see all supported F&O symbols
  2. Select expiry date from the list
  3. Enter strike price
  4. View the option chain data with Greeks

Testing

Run tests to verify everything works:

# Test API connection
python tests/test_api_connection.py

# Test stock validation
python tests/test_stock_validation.py

# Test option chain flow
python tests/test_option_chain_flow.py

# Integration tests
python tests/test_integration.py

# Test Black-Scholes calculations
python tests/test_black_scholes.py

# Test Black-Scholes integration
python tests/test_integration_bs.py

Debug Scripts

# Debug stock options
python scripts/debug_stock_options.py

# Check underlying assets
python scripts/check_underlying.py

# Get ISIN for symbols
python scripts/get_isin.py

Supported Symbols

Indices

  • NIFTY, BANKNIFTY, FINNIFTY, MIDCPNIFTY

Stocks (180+ F&O enabled)

Banking: SBIN, HDFCBANK, ICICIBANK, AXISBANK, KOTAKBANK, and more
IT: TCS, INFY, WIPRO, HCLTECH, TECHM, and more
Oil & Gas: RELIANCE, ONGC, IOC, BPCL, and more
Auto: TATAMOTORS, MARUTI, M&M, BAJAJ-AUTO, and more
Pharma: SUNPHARMA, DRREDDY, CIPLA, DIVISLAB, and more
FMCG: HINDUNILVR, ITC, NESTLEIND, BRITANNIA, and more

Type 'LIST' in the app to see all supported symbols.

Requirements

  • Python 3.7+
  • Upstox API access token
  • Terminal with Unicode support for best display

How It Works

The app uses ISIN (International Securities Identification Number) mappings to correctly identify F&O enabled stocks. For indices, it uses the standard index identifiers. This ensures accurate option chain data retrieval for all supported instruments.

Deploying to Railway

  1. Ensure your repository contains:

    • Procfile with: web: uvicorn web_app.app:app --host 0.0.0.0 --port ${PORT:-8000}
    • .env.example with UPSTOX_ACCESS_TOKEN=
    • requirements.txt including fastapi, uvicorn, and Jinja2
  2. On Railway:

    • Create a new project and connect this repo.
    • In Settings โ†’ Variables, add UPSTOX_ACCESS_TOKEN with your token.
    • Deploy. The service will listen on $PORT automatically.
  3. Health check: GET /healthz should return { "status": "ok" }.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

theoprice-0.1.1.tar.gz (24.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

theoprice-0.1.1-py3-none-any.whl (22.5 kB view details)

Uploaded Python 3

File details

Details for the file theoprice-0.1.1.tar.gz.

File metadata

  • Download URL: theoprice-0.1.1.tar.gz
  • Upload date:
  • Size: 24.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for theoprice-0.1.1.tar.gz
Algorithm Hash digest
SHA256 803f5fc3095e472c0864d62c257a2a77c5e7e8d76c649aebe38b4849b63835a7
MD5 44503923aeb81adedd4bfbbb2407357c
BLAKE2b-256 88ef006817d7c72067a38f8820e467578fcbb3ee573c18d03bc7bdb1a206ba3f

See more details on using hashes here.

File details

Details for the file theoprice-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: theoprice-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 22.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for theoprice-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f7294c2f9dc2602feea2c7e9a34c9c588b4bd9bdeb78ace7574e40d3ae3b058b
MD5 195b5a13c0a6a561022ad74dbd4402dd
BLAKE2b-256 2746ee5728064667e0d5ecd8b09c2b1182b90be40b12e99d15b7ec35c48d511f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page