A comprehensive Python validation utility using Pydantic for loading, validating, and exposing trading platform settings as strongly-typed objects. This module integrates seamlessly with OpenBB Platform and other financial data providers, brokers, and trading systems.
- Comprehensive Validation: Uses Pydantic v2 for strong typing and validation
- OpenBB Integration: Native support for OpenBB Platform configuration
- Multi-Broker Support: Supports Alpaca, Interactive Brokers, and more
- Crypto Exchange Support: Built-in configurations for Binance, Coinbase, Bybit
- Risk Management: Comprehensive risk limits and monitoring configuration
- Environment Variables: Secure credential management with environment variable support
- Production Ready: Environment-specific validation and security checks
-
Install Pydantic (if not already installed):
pip install pydantic
-
Copy the configuration loader:
cp config_loader.py /path/to/your/project/
from config_loader import ConfigurationLoader, load_config
# Method 1: Using the loader class
loader = ConfigurationLoader('trading_platform.example.json')
config = loader.load_config()
# Method 2: Using the convenience function
config = load_config('trading_platform.example.json')
print(f"Environment: {config.metadata.environment}")
print(f"Configured sections: {len([s for s in [config.data, config.brokers, config.models, config.risk, config.execution, config.backtest, config.system] if s])}")from config_loader import ConfigurationLoader
loader = ConfigurationLoader('trading_platform.example.json')
config = loader.load_config()
# Get OpenBB-specific configuration
openbb_config = loader.get_openbb_config()
if openbb_config:
print(f"OpenBB WebSocket URL: {openbb_config.websocket_url}")
print(f"Rate limits: {openbb_config.rate_limits.requests_per_second} req/s")
# In real implementation:
# from openbb import obb
# obb.account.login(pat=openbb_config.credentials.api_key)# Get broker-specific configurations
alpaca_config = loader.get_broker_config('alpaca')
if alpaca_config:
print(f"Alpaca Base URL: {alpaca_config.base_url}")
print(f"Supported assets: {alpaca_config.supported_assets}")
ib_config = loader.get_broker_config('interactive_brokers')
if ib_config:
print(f"IB Account: {ib_config.account_id}")
print(f"Gateway: {ib_config.gateway_host}:{ib_config.gateway_port}")# Get risk limits for different asset classes
portfolio_limits = loader.get_risk_limits() # Portfolio-level limits
crypto_limits = loader.get_risk_limits('crypto') # Crypto-specific limits
equity_limits = loader.get_risk_limits('equities') # Equity-specific limits
if portfolio_limits:
print(f"Max position size: {portfolio_limits.max_position_size * 100:.1f}%")
print(f"Stop loss: {portfolio_limits.stop_loss * 100:.1f}%")The configuration supports the following main sections:
metadata: Version, environment, and creation informationdata: Data providers (OpenBB, Polygon, crypto exchanges) and storagebrokers: Broker connections and authenticationmodels: ML models, pricing models, and analyticsrisk: Risk limits, monitoring, and complianceexecution: Order management and trading algorithmsbacktest: Backtesting framework configurationsystem: Infrastructure, logging, monitoring, and security
The configuration loader supports secure credential management through environment variables:
# Set required environment variables
export OPENBB_API_KEY="your_openbb_api_key"
export POLYGON_API_KEY="your_polygon_key"
export ALPACA_API_KEY="your_alpaca_key"
export DATABASE_PASSWORD="your_db_password"
# ... etcIn your configuration file:
{
"data": {
"providers": {
"openbb": {
"credentials": {
"environment_variable": "${OPENBB_API_KEY}"
}
}
}
}
}The configuration loader can be used as a standalone script:
# Validate a configuration file
python config_loader.py --config trading_platform.example.json --validate
# Export configuration (after environment variable resolution)
python config_loader.py --config trading_platform.example.json --export output.json
# Create default configuration
python config_loader.pyRun the comprehensive integration example:
# Set environment variables
export OPENBB_API_KEY=your_key
export ALPACA_API_KEY=your_key
# ... other required variables
# Run integration demo
python integration_example.py --config trading_platform.example.jsonThis will demonstrate:
- Configuration loading and validation
- OpenBB Platform integration setup
- Broker connection initialization
- Risk management system setup
- Data source configuration
- Execution engine initialization
{
"metadata": {
"version": "1.0.0",
"environment": "development"
},
"data": {
"providers": {
"openbb": {
"credentials": {
"environment_variable": "${OPENBB_API_KEY}"
},
"websocket_url": "wss://websocket.openbb.co",
"rate_limits": {
"requests_per_second": 10,
"requests_per_hour": 5000
},
"symbols": {
"equities": ["AAPL", "GOOGL", "MSFT"],
"futures": ["ES", "NQ", "YM"]
}
}
}
}
}{
"brokers": {
"connections": {
"alpaca": {
"credentials": {
"environment_variable": "${ALPACA_API_KEY}"
},
"base_url": "https://paper-api.alpaca.markets",
"supported_assets": ["stocks", "etfs", "options", "crypto"]
},
"interactive_brokers": {
"account_id": "DU123456",
"gateway_host": "localhost",
"gateway_port": 7497,
"client_id": 1
}
}
}
}{
"risk": {
"limits": {
"portfolio_level": {
"max_position_size": 0.05,
"max_daily_loss": 0.02,
"stop_loss": 0.015,
"take_profit": 0.03
},
"asset_class_level": {
"crypto": {
"max_position_size": 0.03,
"max_daily_loss": 0.05
}
}
},
"monitoring": {
"real_time": true,
"kill_switch": {
"enabled": true,
"threshold": 0.08
}
}
}
}The configuration loader provides comprehensive validation:
- Type Safety: All fields are strongly typed using Pydantic
- Range Validation: Numeric fields have min/max constraints
- Format Validation: URLs, patterns, and formats are validated
- Business Logic: Cross-field validation (e.g., production environment security requirements)
- Environment Variables: Validates environment variable patterns and resolution
The loader provides detailed error messages:
try:
config = load_config('invalid_config.json')
except ValueError as e:
print(f"Configuration error: {e}")
except FileNotFoundError as e:
print(f"File not found: {e}")
except json.JSONDecodeError as e:
print(f"Invalid JSON: {e}")if loader.is_production():
print("Production environment detected!")
# Additional production-specific logic# Export resolved configuration
loader.export_config('resolved_config.json')The configuration models can be extended for custom validation:
from config_loader import TradingPlatformConfig
from pydantic import field_validator
class CustomConfig(TradingPlatformConfig):
@field_validator('custom_field')
@classmethod
def validate_custom_field(cls, v):
# Custom validation logic
return vWhen the CI runs, it generates and uploads an exported configuration artifact at artifacts/exported_config.json. You can download it from any workflow run as follows:
- In GitHub, go to Actions > Trading Platform CI.
- Click the latest run for your branch (main/develop or your PR).
- In the Summary page, scroll to the Artifacts section.
- Download the artifact named "exported-config". It contains exported_config.json.
Notes:
- The artifact is produced by the Integration Tests job. It runs on both push and pull_request events.
- Exported artifacts are ignored by git and not committed to the repository.
- QUICK_SETUP_CHECKLIST.md - 20-minute setup guide with step-by-step instructions
- TRADING_PLATFORM_GUIDE.md - Comprehensive documentation covering all features, setup, usage, and troubleshooting
- FEATURES_OVERVIEW.md - Complete feature list and capabilities overview
- CONFIGURATION_PRECEDENCE.md - Environment variable precedence and override behavior
- PRE_COMMIT_CI_README.md - Pre-commit hooks and CI/CD pipeline documentation
- SETUP_COMPLETE.md - Summary of complete setup and validation
- docs/SCHEMA_USAGE_GUIDE.md - How to extend and modify the configuration schema
- docs/CONTRIBUTION_GUIDE.md - Contribution guidelines and development workflow
- docs/SCHEMA_REFERENCE.md - Complete schema reference documentation
- test_README.md - Testing documentation and test suite overview
- Test files:
test_config_loader.py,test_pre_commit_setup.py,ci_integration_test.py
config_loader.py- Main configuration loader with Pydantic modelstrading_platform.example.json- Comprehensive example configurationtrading_platform.schema.json- JSON schema for validation.env.example- Environment variables template
integration_example.py- Basic integration exampleenhanced_integration_example.py- Advanced example with .env supportexported_config.json- Example exported configuration
.pre-commit-config.yaml- Pre-commit hooks configuration.github/workflows/ci.yml- GitHub Actions CI/CD pipeline.flake8- Python linting configurationrequirements.txt- Python dependencies
scripts/regenerate_docs.sh- Documentation regenerationscripts/validate_examples.py- Configuration validationscripts/extract_schema_docs.py- Schema documentation extractor
# Follow the quick setup checklist
open QUICK_SETUP_CHECKLIST.md# Follow the complete guide
open TRADING_PLATFORM_GUIDE.md# Review all capabilities
open FEATURES_OVERVIEW.md- Python 3.9.6+ (tested with 3.9, 3.10, 3.11, 3.12)
- Pydantic 2.x for configuration validation
- Optional:
python-dotenvfor .env file support - Development:
pre-commit,pytest,black,flake8,isort
- π Type-Safe Configuration - Pydantic v2 validation
- π Multi-Asset Support - Stocks, Options, Crypto, Forex, Futures
- π Broker Integration - Alpaca, IB, Binance, Coinbase, Bybit
- π Data Providers - OpenBB, Polygon, crypto exchanges
- π€ ML Model Support - LSTM, RL, custom models
- β‘ Risk Management - Multi-level limits and monitoring
- π Security - Environment variables, secret masking
- β Production Ready - CI/CD, testing, documentation
- Read docs/CONTRIBUTION_GUIDE.md
- Setup pre-commit hooks:
pre-commit install - Follow the development workflow in the comprehensive guide
- Test your changes:
pytestandpre-commit run --all-files - Update documentation when adding features
- Troubleshooting: See the troubleshooting section in
TRADING_PLATFORM_GUIDE.md - Examples: Review integration examples and test files
- Schema Extension: Use
docs/SCHEMA_USAGE_GUIDE.md - Issues: Check environment variables and run validation tools
This system is production-ready with:
- β Comprehensive testing and validation
- β Security scanning and secret management
- β CI/CD pipeline with multi-Python support
- β Performance optimization and monitoring
- β Complete documentation and troubleshooting guides
Ready to use in your trading platform immediately!