A comprehensive Python application for downloading OHLCV (Open, High, Low, Close, Volume) stock data from multiple free APIs with both GUI and command-line interfaces.
- Multiple Data Sources: Yahoo Finance, Alpha Vantage, and Polygon APIs
- Dual Interface: Both GUI (tkinter) and command-line versions
- Flexible Timeframes: Support for various intervals (1m, 5m, 15m, 30m, 1h, 1d, 1wk, 1mo, etc.)
- Organized Storage: Automatic folder structure by ticker and date range
- Data Validation: Built-in data cleaning and validation
- Comprehensive Logging: Detailed logs for debugging and monitoring
- Metadata Tracking: JSON metadata files with download information
- Error Handling: Robust error handling with user-friendly messages
- Batch Downloads: Download from all sources simultaneously
- Data Retention: Automatic cleanup of files older than a configurable period
- Python 3.7 or higher
- Internet connection for API access
- Optional: API keys for Alpha Vantage and Polygon (free tiers available)
Download the following files to your project directory:
secure_ohlcv_downloader.py(GUI version)secure_ohlcv_cli.py(Command-line version)requirements-secure.txt
pip install -r requirements-secure.txtInstall additional tools for code quality and security checks:
pip install -r requirements-dev.txtRun the validation suite to verify your environment:
python scripts/run_validation.pyOr install manually:
pip install pandas yfinance alpha-vantage requests python-dotenv matplotlib plotly- Visit Alpha Vantage
- Sign up for a free API key
- Note: Free tier allows 5 API requests per minute and 500 requests per day
- Visit Polygon.io
- Sign up for a free account
- Get your API key from the dashboard
- Note: Free tier has limitations on data access
Run the graphical interface:
python secure_ohlcv_downloader.pyGUI Features:
- User-friendly interface with input fields
- Real-time progress tracking
- Status logging window
- Error messages and success notifications
- Support for all data sources
# Download AAPL data from Yahoo Finance (default)
python secure_ohlcv_cli.py AAPL
# Download with specific timeframe
python secure_ohlcv_cli.py AAPL --timeframe 1h
# Download with custom date range
python secure_ohlcv_cli.py TSLA --start 2023-01-01 --end 2023-12-31# Download from all sources
python secure_ohlcv_cli.py MSFT --source all --timeframe 1d
# Use Alpha Vantage with API key
python secure_ohlcv_cli.py GOOGL --source alpha_vantage --alpha-key YOUR_API_KEY
# Use Polygon with API key for intraday data
python secure_ohlcv_cli.py NVDA --source polygon --polygon-key YOUR_API_KEY --timeframe 5m
# Verbose logging
python secure_ohlcv_cli.py AAPL --verbose
# Custom output directory
python secure_ohlcv_cli.py AAPL --output-dir /path/to/custom/directorypositional arguments:
ticker Stock ticker symbol (e.g., AAPL, TSLA)
optional arguments:
-h, --help show this help message and exit
--source {yahoo,alpha_vantage,polygon,all}
Data source to use (default: yahoo)
--timeframe {1m,2m,5m,15m,30m,60m,90m,1h,1d,5d,1wk,1mo,3mo}
Timeframe for data (default: 1d)
--start START Start date (YYYY-MM-DD, default: 1 year ago)
--end END End date (YYYY-MM-DD, default: today)
--alpha-key ALPHA_KEY Alpha Vantage API key
--polygon-key POLYGON_KEY
Polygon API key
--output-dir OUTPUT_DIR
Output directory (default: data)
--verbose, -v Enable verbose logging
--version show program's version number and exit
The application creates an organized folder structure:
data/
โโโ AAPL/
โ โโโ 2023-01-01_to_2023-12-31/
โ โโโ AAPL_yahoo_finance.csv
โ โโโ AAPL_yahoo_metadata.json
โ โโโ AAPL_alpha_vantage.csv
โ โโโ AAPL_alpha_vantage_metadata.json
โ โโโ AAPL_polygon.csv
โ โโโ AAPL_polygon_metadata.json
โโโ TSLA/
โ โโโ 2024-01-01_to_2024-01-31/
โ โโโ ...
โโโ secure_downloader.log
Each CSV file contains OHLCV data with the following columns:
Date(index): TimestampOpen: Opening priceHigh: Highest priceLow: Lowest priceClose: Closing priceVolume: Trading volume
JSON metadata files contain:
{
"source": "Yahoo Finance",
"ticker": "AAPL",
"timeframe": "1d",
"start_date": "2023-01-01",
"end_date": "2023-12-31",
"download_time": "2024-01-15T10:30:00",
"records_count": 252,
"filename": "data/AAPL/2023-01-01_to_2023-12-31/AAPL_yahoo_finance.csv",
"columns": ["Open", "High", "Low", "Close", "Volume"],
"date_range_actual": {
"start": "2023-01-03",
"end": "2023-12-29"
}
}Create a .env file in your project directory:
ALPHA_VANTAGE_API_KEY=your_alpha_vantage_key_here
POLYGON_API_KEY=your_polygon_key_hereThen use python-dotenv to load them:
from dotenv import load_dotenv
import os
load_dotenv()
alpha_key = os.getenv('ALPHA_VANTAGE_API_KEY')
polygon_key = os.getenv('POLYGON_API_KEY')| Feature | Yahoo Finance | Alpha Vantage | Polygon |
|---|---|---|---|
| API Key Required | No | Yes (Free) | Yes (Free tier) |
| Rate Limits | Reasonable | 5/min, 500/day | Varies by plan |
| Intraday Data | Yes | Yes | Yes |
| Historical Range | Extensive | Good | Good |
| Data Quality | High | High | High |
| Reliability | High | Medium | High |
The application handles various error scenarios:
- Network Issues: Timeout and connection errors
- API Limits: Rate limiting and quota exceeded
- Invalid Tickers: Non-existent stock symbols
- Date Range Issues: Invalid or future dates
- Data Validation: Missing or corrupted data
Logs are saved to secure_downloader.log and include:
- Download start/completion times
- Success/failure status for each source
- Error messages and stack traces
- Data validation results
- API response information
-
"No display name and no $DISPLAY environment variable"
- Use the CLI version:
python secure_ohlcv_cli.py - Or run on a system with GUI support
- Use the CLI version:
-
"API key required"
- Obtain free API keys from Alpha Vantage or Polygon
- Use Yahoo Finance (no key required) as alternative
-
"No data returned"
- Check if ticker symbol is valid
- Verify date range is not in the future
- Ensure market was open during selected dates
-
Rate limit exceeded
- Wait before making additional requests
- Consider using multiple API keys
- Use Yahoo Finance for unlimited requests
Enable verbose logging for detailed information:
python secure_ohlcv_cli.py AAPL --verboseRefer to OPERATIONS.md for detailed production deployment and maintenance procedures. The guide covers environment setup, monitoring strategies, routine maintenance, capacity planning, and disaster recovery steps.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is open source and available under the MIT License.
For issues and questions:
- Check the troubleshooting section
- Review the log files for error details
- Ensure all dependencies are installed correctly
- Verify API keys are valid and have remaining quota
- Additional data sources (IEX Cloud, Quandl)
- Real-time data streaming
- Data visualization dashboard
- Database storage options
- Automated scheduling
- Portfolio tracking features
- Technical indicators calculation
- Export to multiple formats (Excel, JSON, Parquet)
After downloading data, you can analyze it with pandas:
import pandas as pd
import matplotlib.pyplot as plt
# Load downloaded data
df = pd.read_csv('data/AAPL/2023-01-01_to_2023-12-31/AAPL_yahoo_finance.csv',
index_col='Date', parse_dates=True)
# Basic statistics
print(df.describe())
# Plot closing prices
df['Close'].plot(title='AAPL Closing Prices', figsize=(12, 6))
plt.show()
# Calculate daily returns
df['Returns'] = df['Close'].pct_change()
print(f"Average daily return: {df['Returns'].mean():.4f}")
print(f"Volatility: {df['Returns'].std():.4f}")Happy Trading! ๐