Skip to content

Advanced Multi-Asset Trading Platform Configuration System - Comprehensive Pydantic-based configuration management with schema validation, environment variable support, and automated testing

Notifications You must be signed in to change notification settings

AITactician/Trading-Platform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Trading Platform Configuration Loader

CI

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.

Features

  • 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

Installation

  1. Install Pydantic (if not already installed):

    pip install pydantic
  2. Copy the configuration loader:

    cp config_loader.py /path/to/your/project/

Quick Start

Basic Usage

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])}")

OpenBB Integration

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)

Broker Configuration

# 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}")

Risk Management

# 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}%")

Configuration Structure

The configuration supports the following main sections:

  • metadata: Version, environment, and creation information
  • data: Data providers (OpenBB, Polygon, crypto exchanges) and storage
  • brokers: Broker connections and authentication
  • models: ML models, pricing models, and analytics
  • risk: Risk limits, monitoring, and compliance
  • execution: Order management and trading algorithms
  • backtest: Backtesting framework configuration
  • system: Infrastructure, logging, monitoring, and security

Environment Variables

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"
# ... etc

In your configuration file:

{
  "data": {
    "providers": {
      "openbb": {
        "credentials": {
          "environment_variable": "${OPENBB_API_KEY}"
        }
      }
    }
  }
}

Command Line Usage

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.py

Integration Example

Run 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.json

This will demonstrate:

  • Configuration loading and validation
  • OpenBB Platform integration setup
  • Broker connection initialization
  • Risk management system setup
  • Data source configuration
  • Execution engine initialization

Configuration Examples

Basic OpenBB Configuration

{
  "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"]
        }
      }
    }
  }
}

Broker Configuration

{
  "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 Management Configuration

{
  "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
      }
    }
  }
}

Validation Features

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

Error Handling

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}")

Advanced Features

Production Environment Validation

if loader.is_production():
    print("Production environment detected!")
    # Additional production-specific logic

Configuration Export

# Export resolved configuration
loader.export_config('resolved_config.json')

Custom Validation

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 v

πŸ“š Complete Documentation

Downloading CI Artifacts (Exported Config)

When 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.

πŸš€ Getting Started

πŸ”§ Configuration & Setup

πŸ“– Developer Documentation

πŸ§ͺ Testing & Quality

  • test_README.md - Testing documentation and test suite overview
  • Test files: test_config_loader.py, test_pre_commit_setup.py, ci_integration_test.py

πŸ“ Key Files

Core System

  • config_loader.py - Main configuration loader with Pydantic models
  • trading_platform.example.json - Comprehensive example configuration
  • trading_platform.schema.json - JSON schema for validation
  • .env.example - Environment variables template

Integration Examples

  • integration_example.py - Basic integration example
  • enhanced_integration_example.py - Advanced example with .env support
  • exported_config.json - Example exported configuration

Development & CI/CD

  • .pre-commit-config.yaml - Pre-commit hooks configuration
  • .github/workflows/ci.yml - GitHub Actions CI/CD pipeline
  • .flake8 - Python linting configuration
  • requirements.txt - Python dependencies

Utilities & Scripts

  • scripts/regenerate_docs.sh - Documentation regeneration
  • scripts/validate_examples.py - Configuration validation
  • scripts/extract_schema_docs.py - Schema documentation extractor

🎯 Quick Start Options

Option 1: Immediate Setup (5 minutes)

# Follow the quick setup checklist
open QUICK_SETUP_CHECKLIST.md

Option 2: Comprehensive Setup (20 minutes)

# Follow the complete guide
open TRADING_PLATFORM_GUIDE.md

Option 3: Explore Features First

# Review all capabilities
open FEATURES_OVERVIEW.md

πŸ”§ System Requirements

  • Python 3.9.6+ (tested with 3.9, 3.10, 3.11, 3.12)
  • Pydantic 2.x for configuration validation
  • Optional: python-dotenv for .env file support
  • Development: pre-commit, pytest, black, flake8, isort

✨ Key Features Highlight

  • πŸ”’ 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

🀝 Contributing

  1. Read docs/CONTRIBUTION_GUIDE.md
  2. Setup pre-commit hooks: pre-commit install
  3. Follow the development workflow in the comprehensive guide
  4. Test your changes: pytest and pre-commit run --all-files
  5. Update documentation when adding features

πŸ“ž Support & Help

  • 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

πŸš€ Production Deployment

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!

About

Advanced Multi-Asset Trading Platform Configuration System - Comprehensive Pydantic-based configuration management with schema validation, environment variable support, and automated testing

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published