A comprehensive Python application for calculating and tracking SPX (S&P 500 Index) 0DTE (zero days to expiration) straddle costs using real market data from Polygon.io, with Discord webhook notifications and automated scheduling.
Status: โ Working with Real Market Data
- โ SPX Index Data: Real-time 9:30 AM market open prices
- โ SPX Options Data: Real 0DTE option prices (15-minute delayed)
- โ Accurate Calculations: Verified with live market data
- โ Discord Integration: Notifications working
- โ Web Dashboard: Live at http://localhost:8000/api/spx-straddle/dashboard
Latest Test Results (June 13, 2025):
- SPX Price at 9:30 AM: $6,000.56
- ATM Strike: $6,000.00
- Call Price: $18.60 | Put Price: $17.30
- Total Straddle Cost: $35.90
- Real-time SPX Data: Fetches SPX index prices at 9:30 AM ET using Polygon.io
- Real SPX Options Data: Retrieves SPXW (SPX 0DTE) call and put option prices at 9:31 AM ET
- Straddle Calculation: Automatically calculates ATM (at-the-money) straddle costs
- Historical Analysis: Stores and analyzes historical straddle cost data
- Statistical Insights: Provides trend analysis, volatility metrics, and pattern recognition
- Discord Webhooks: Sends automated notifications to Discord channels via webhooks
- REST API: Full-featured API with web dashboard
- Automated Scheduling: Daily calculations and weekly data cleanup
- Docker Support: Complete containerized deployment
- Data Persistence: Redis-based storage with configurable retention
- Python 3.11+
- Polygon.io API key with Options subscription (minimum Options Starter $29/month)
- Redis server (or use Docker Compose)
- Discord webhook URL (optional, for notifications)
-
Clone and setup:
git clone <repository-url> cd spxstraddle cp env.example .env
-
Configure environment variables in
.env:POLYGON_API_KEY=your_polygon_api_key_here DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/YOUR_WEBHOOK_ID/YOUR_WEBHOOK_TOKEN # Optional DISCORD_ENABLED=true # Set to false to disable Discord
-
Start the application:
# Start API server and Redis docker-compose up # Or start with scheduler for automated calculations docker-compose --profile scheduler up # Or run example usage docker-compose --profile example up spx-example
-
Access the dashboard: http://localhost:8000/api/spx-straddle/dashboard
-
Install dependencies:
pip install -r requirements.txt
-
Start Redis (if not using Docker):
redis-server
-
Configure environment:
cp env.example .env # Edit .env with your API keys -
Run the application:
# Run example usage python example_usage.py # Start API server python api_server.py # Start scheduler python scheduler.py
import asyncio
from spx_calculator import SPXStraddleCalculator
async def calculate_straddle():
calculator = SPXStraddleCalculator("your_polygon_api_key")
await calculator.initialize()
result = await calculator.calculate_spx_straddle_cost()
if 'error' not in result:
print(f"Straddle Cost: ${result['straddle_cost']:.2f}")
print(f"SPX Price: ${result['spx_price_930am']:.2f}")
print(f"ATM Strike: {result['atm_strike']}")
await calculator.close()
asyncio.run(calculate_straddle())from discord_notifier import DiscordNotifier
async def send_notification():
notifier = DiscordNotifier("https://discord.com/api/webhooks/YOUR_WEBHOOK_ID/YOUR_WEBHOOK_TOKEN")
await notifier.initialize()
if notifier.is_enabled():
await notifier.notify_straddle_result(result)
await notifier.close()The REST API provides comprehensive access to all functionality:
GET /api/spx-straddle/today- Get today's straddle dataPOST /api/spx-straddle/calculate- Trigger new calculationGET /api/spx-straddle/history?days=30- Get historical dataGET /api/spx-straddle/statistics?days=30- Get statistical analysisGET /api/spx-straddle/export/csv?days=30- Export data as CSVGET /api/spx-straddle/status- System health check
POST /api/discord/test- Test Discord integrationPOST /api/discord/notify/today- Send today's data to Discord
GET /api/spx-straddle/dashboard- Web dashboardGET /docs- API documentation (Swagger UI)
The scheduler runs daily calculations and sends Discord notifications:
Set these environment variables:
ENABLE_SCHEDULER=true
CALCULATION_TIME=09:32 # Time in ET for daily calculation
CLEANUP_DAY=sunday # Day for weekly cleanup
CLEANUP_TIME=02:00 # Time for cleanup
KEEP_DAYS=90 # Days of data to retain- Daily: Calculates straddle cost at 9:32 AM ET (weekdays only)
- Weekly: Cleans up old data (configurable day/time)
- Notifications: Sends results to Discord automatically
| Variable | Description | Default | Required |
|---|---|---|---|
POLYGON_API_KEY |
Polygon.io API key with Options subscription | - | โ |
REDIS_URL |
Redis connection URL | redis://localhost:6379 |
โ |
DISCORD_WEBHOOK_URL |
Discord webhook URL | - | โ |
DISCORD_ENABLED |
Enable Discord notifications | false |
โ |
LOG_LEVEL |
Logging level | INFO |
โ |
CALCULATION_TIME |
Daily calculation time (ET) | 09:32 |
โ |
CLEANUP_DAY |
Weekly cleanup day | sunday |
โ |
KEEP_DAYS |
Data retention days | 90 |
โ |
For SPX Options Data, you need:
- Minimum: Options Starter ($29/month)
- Includes: 15-minute delayed SPX options data
- Data Available: 9:47 AM ET onwards (for 9:31 AM option prices)
-
Create a Discord Webhook:
- Go to your Discord server
- Right-click on the channel where you want notifications
- Select "Edit Channel" โ "Integrations" โ "Webhooks"
- Click "New Webhook"
- Copy the webhook URL
-
Configure the Application:
DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/YOUR_WEBHOOK_ID/YOUR_WEBHOOK_TOKEN DISCORD_ENABLED=true
- Index Data: Uses
I:SPXticker for S&P 500 index prices - Options Data: Uses
O:SPXW{YYMMDD}{C/P}{strike*1000}format for 0DTE options - Timing:
- SPX price fetched at 9:30 AM ET (market open)
- Option prices fetched at 9:31 AM ET (1 minute after open)
- Strike Calculation: Rounds SPX price to nearest $5 increment
- 9:30 AM: Fetch SPX opening price
- Calculate: ATM strike (nearest $5)
- 9:31 AM: Fetch call and put option prices
- Calculate: Straddle cost = call price + put price
- Store: Save results to Redis
- Notify: Send Discord notification (if enabled)
- Trend Analysis: Linear regression on historical straddle costs
- Volatility: Coefficient of variation (std dev / mean)
- Data Source: Only uses collected data (not historical backfill)
- Meaningful Results: Requires 7+ days of collected data
The Docker Compose setup includes:
- spx-api: REST API server
- spx-scheduler: Automated scheduler (optional)
- redis: Data storage
- spx-example: Example usage (for testing)
# Start API only
docker-compose up spx-api redis
# Start with scheduler
docker-compose --profile scheduler up
# Run example
docker-compose --profile example up spx-example
# View logs
docker-compose logs -f spx-api
# Scale services
docker-compose up --scale spx-api=2- API:
GET /health - Docker: Built-in health checks
- Redis: Connection monitoring
Structured logging with configurable levels:
LOG_LEVEL=DEBUG # DEBUG, INFO, WARNING, ERROR- Calculation success/failure rates
- API response times
- Discord notification status
- Data storage metrics
The application includes comprehensive error handling:
- API Errors: Graceful HTTP error responses
- Data Errors: Validation and retry logic
- Discord Errors: Fallback notification strategies
- Network Errors: Automatic retry with exponential backoff
Full API documentation is available at /docs when running the server. The API follows REST conventions and includes:
- Request/response schemas
- Error codes and messages
- Example requests and responses
- Authentication requirements (if any)
- API Keys: Store in environment variables, never in code
- Docker: Runs as non-root user
- Redis: Configure authentication in production
- Discord: Use bot tokens, not user tokens
- Network: Consider firewall rules for production
Run the example script to test all functionality:
python example_usage.pyThis will:
- Test calculator initialization
- Perform a sample calculation
- Test Discord integration
- Demonstrate all API features
- Show system status
- Free Tier: 5 requests/minute
- Paid Tiers: Higher limits available
- Optimization: Application caches results to minimize API calls
- Memory: ~50MB base + Redis storage
- CPU: Minimal (calculation bursts)
- Storage: ~1MB per month of daily data
- Network: ~1KB per calculation
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- "No SPX data received": Check if markets are open and Polygon.io API key is valid
- Discord not working: Verify bot token and channel ID
- Redis connection failed: Ensure Redis is running and accessible
- Check the logs for detailed error messages
- Verify environment variables are set correctly
- Test with the example script first
- Check Polygon.io API status
# Check system status
curl http://localhost:8000/api/spx-straddle/status
# Test Discord
curl -X POST http://localhost:8000/api/discord/test
# View logs
docker-compose logs spx-api
# Check Redis
redis-cli ping- Support for other indices (NDX, RUT)
- Multiple expiration dates
- Advanced pattern recognition
- Slack integration
- Grafana dashboards
- Machine learning predictions
- Mobile app
- Real-time WebSocket updates
Built with โค๏ธ for options traders and quantitative analysts