Skip to content

marketcalls/openalgo-portfoliogreeks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Portfolio Greeks Calculator

A Flask application for calculating and monitoring option Greeks in a portfolio, with support for position (buy/sell) and lot-based quantities to accurately measure portfolio risk.

Features

  • Accurate Greeks Calculation: Individual and portfolio-level option Greeks (Delta, Gamma, Theta, Vega, Rho)
  • Dynamic Lot Size Fetching: Real-time lot sizes from OpenAlgo API instead of hardcoded values
  • Position-Aware Calculations: Proper sign conventions for buy/sell positions following standard option theory
  • Lot-Based Display: Individual option Greeks multiplied by lots for realistic position sizing
  • Portfolio Aggregation: Portfolio Greeks calculated as sum of individual option display values
  • Real-Time Market Data: Live spot prices and option prices via OpenAlgo API
  • Implied Volatility Calculation: Dynamic IV calculation from market prices using Black-Scholes
  • Modern UI: Built with Tailwind CSS and Daisy UI for responsive design
  • Secure API Management: Protected API key storage with settings page
  • Caching System: Intelligent caching to prevent API overuse while maintaining data freshness

Recent Updates & Fixes

Greeks Calculation Improvements

  • Fixed sign conventions: Proper Greeks signs for all position types (BUY/SELL × CE/PE)
  • Corrected portfolio aggregation: Portfolio Greeks now correctly sum individual display values
  • Interest rate standardization: Set to 0% as per standard practice
  • Dynamic lot size fetching: Uses OpenAlgo symbol API instead of hardcoded values
  • Improved IV calculation: Dynamic implied volatility from market prices with fallbacks

Display Corrections

  • Individual Greeks: Now properly multiplied by lots (standard convention)
  • Portfolio Greeks: Sum of individual option display values (no double multiplication)
  • Consistent calculations: Same logic for both web interface and API endpoints

Installation

  1. Clone the repository
  2. Install the required packages:
pip install -r requirements.txt
  1. Run the application:
python app.py

The application will be available at http://localhost:5001

API Key Setup

You'll need to obtain an OpenAlgo API key to use this application:

  1. Set up and run your OpenAlgo application (typically on http://127.0.0.1:5000)
  2. Log in to your OpenAlgo application
  3. Navigate to your account settings or API section
  4. Generate a new API key or copy your existing one
  5. Enter this key in the Settings page of the Portfolio Greeks application

Option Symbol Format

The application expects option symbols in the following format: NIFTY31JUL2525200CE where:

  • NIFTY is the underlying symbol
  • 31JUL25 is the expiration date (DD-MMM-YY)
  • 25200 is the strike price
  • CE is the option type (CE for Call, PE for Put)

Greeks Calculation Details

Individual Option Display

Individual option Greeks are multiplied by the number of lots to show position-level risk:

  • Display Formula: Greek × Lots
  • Example: If delta = 0.5 per unit and you have 2 lots, display shows 1.0

Portfolio Greeks

Portfolio Greeks are calculated by summing all individual option display values:

  • Portfolio Formula: Sum of (Greek × Lots) for all options
  • Proper Risk Aggregation: Shows total portfolio exposure

Position-Aware Greeks Calculation

Portfolio Greeks follow standard option theory, accounting for position type:

Position Delta Gamma Theta Vega Rho
Long Call + + - + +
Short Call - - + - -
Long Put - + - + -
Short Put + - + - +

Sign Convention:

  • Long positions: Keep original Black-Scholes signs
  • Short positions: Flip all Greeks signs (consistent across all Greeks)

Black-Scholes Parameters

  • Interest Rate: 0% (standard for Indian options)
  • Implied Volatility: Calculated from market prices using mibian library
  • Lot Sizes: Fetched dynamically from OpenAlgo API

Key Components

Backend (app.py)

  • Greeks Calculation: Uses mibian library for Black-Scholes calculations
  • API Integration: OpenAlgo API for market data and lot sizes
  • Caching: Response caching with configurable expiry
  • Error Handling: Comprehensive error handling and fallbacks

Frontend

  • Template: templates/index.html with dynamic Greeks display
  • Styling: Tailwind CSS with DaisyUI components
  • Real-time Updates: JavaScript for dynamic data refresh

Configuration

  • Lot Size Fetching: get_lot_size() function uses OpenAlgo symbol API
  • API Client: get_api_client() with secure key management
  • Portfolio Management: JSON-based portfolio storage

Technologies Used

  • Flask: Web framework with CSRF protection
  • Mibian: Black-Scholes options pricing and Greeks calculation
  • OpenAlgo API: Market data provider and lot size information
  • Tailwind CSS & DaisyUI: Modern, responsive UI framework
  • JavaScript: Dynamic UI updates and real-time data refresh
  • JSON: Configuration and portfolio data storage

Troubleshooting

Common Issues

  1. Large Portfolio Greeks Values: Ensure you're using the latest version with corrected aggregation
  2. API Connection: Verify OpenAlgo is running on http://127.0.0.1:5000
  3. Symbol Format: Check option symbols follow the expected format
  4. Lot Size Issues: Verify API key has permissions to access symbol information

Debug Mode

Enable Flask debug mode for detailed error messages:

export FLASK_DEBUG=1
python app.py

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes with appropriate tests
  4. Submit a pull request

License

MIT