Adapters & Repositories¶
Auto-generated API reference for key adapters and service classes.
ArcticDB Adapter¶
tradai.data.infrastructure.adapters.arctic_adapter.ArcticAdapter ¶
Bases: LoggerMixin, DataAdapter
ArcticDB storage adapter for OHLCV market data.
Implements DataAdapter interface for persisting data to ArcticDB on S3. Uses dependency injection for testability - accepts optional library client.
Connection string format: s3s://endpoint:bucket?aws_auth=true
Attributes:
| Name | Type | Description |
|---|---|---|
SYMBOL_SEPARATOR | Separator used to normalize symbol names |
Example
Default: creates ArcticDB connection lazily¶
adapter = ArcticAdapter( ... bucket="my-bucket", ... endpoint="s3.eu-central-1.amazonaws.com", ... library_name="futures" ... )
For testing: inject mock library¶
mock_library = Mock() adapter = ArcticAdapter( ... bucket="my-bucket", ... library_name="futures", ... arctic_library=mock_library ... )
Source code in libs/tradai-data/src/tradai/data/infrastructure/adapters/arctic_adapter.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 | |
library: ArcticLibraryProtocol property ¶
Lazy-loaded ArcticDB library.
Creates connection and gets library on first access. Uses aws_auth=true for AWS S3, or explicit credentials for LocalStack/MinIO.
Returns:
| Type | Description |
|---|---|
ArcticLibraryProtocol | ArcticDB library instance |
Raises:
| Type | Description |
|---|---|
ImportError | If ArcticDB is not installed |
save(data: OHLCVData, symbols: SymbolList, latest_query_date: datetime, timeframe: str | None = None) -> None ¶
Save OHLCV data to ArcticDB.
Groups data by symbol and uses upsert semantics: - New symbols: uses write - Existing symbols: uses update (upserts rows by index)
Stores metadata_version, last_query_date, last_candle_date, and optional timeframe in metadata for incremental updates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | OHLCVData | Market data to save | required |
symbols | SymbolList | Symbols being saved (for metadata) | required |
latest_query_date | datetime | Timestamp of when data was queried | required |
timeframe | str | None | Optional CCXT timeframe string stored in metadata | None |
Raises:
| Type | Description |
|---|---|
StorageError | If saving fails |
Example
adapter.save( ... data=ohlcv_data, ... symbols=SymbolList.from_input(["BTC/USDT:USDT"]), ... latest_query_date=datetime.now(UTC), ... timeframe="1h", ... )
Source code in libs/tradai-data/src/tradai/data/infrastructure/adapters/arctic_adapter.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | |
save_batch(data: OHLCVData, symbols: SymbolList, latest_query_date: datetime, timeframe: str | None = None) -> int ¶
Save OHLCV data to ArcticDB using batch write for better performance.
Uses write_batch for 2-3x faster writes compared to sequential save(). Best for initial data loads or when updating multiple symbols.
Note: This method uses write (not update), so it replaces existing data. For incremental upserts, use save() instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | OHLCVData | Market data to save | required |
symbols | SymbolList | Symbols being saved (for metadata) | required |
latest_query_date | datetime | Timestamp of when data was queried | required |
timeframe | str | None | Optional CCXT timeframe string stored in metadata | None |
Returns:
| Type | Description |
|---|---|
int | Number of symbols successfully saved |
Raises:
| Type | Description |
|---|---|
StorageError | If batch save fails |
Example
Save 10 symbols in batch (2-3x faster than save())¶
count = adapter.save_batch( ... data=ohlcv_data, ... symbols=SymbolList.from_input(["BTC/USDT:USDT", ...]), ... latest_query_date=datetime.now(UTC), ... timeframe="1h", ... ) print(f"Saved {count} symbols")
Source code in libs/tradai-data/src/tradai/data/infrastructure/adapters/arctic_adapter.py
load(symbols: SymbolList, date_range: DateRange) -> OHLCVData ¶
Load OHLCV data from ArcticDB.
Uses read_batch for efficient multi-symbol loading. Reconstructs DataFrame with symbol column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbols | SymbolList | Symbols to load | required |
date_range | DateRange | Date range to load | required |
Returns:
| Type | Description |
|---|---|
OHLCVData | OHLCVData from storage |
Raises:
| Type | Description |
|---|---|
DataNotFoundError | If no data found for symbols |
StorageError | If loading fails |
Example
data = adapter.load( ... symbols=SymbolList.from_input(["BTC/USDT:USDT"]), ... date_range=DateRange.from_strings("2024-01-01", "2024-01-31") ... )
Source code in libs/tradai-data/src/tradai/data/infrastructure/adapters/arctic_adapter.py
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 | |
exists(symbols: SymbolList) -> dict[str, bool] ¶
Check if data exists for given symbols.
Uses has_symbol for efficient individual checks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbols | SymbolList | Symbols to check | required |
Returns:
| Type | Description |
|---|---|
dict[str, bool] | Dictionary mapping symbol -> exists (True/False) |
Example
result = adapter.exists( ... SymbolList.from_input(["BTC/USDT:USDT", "ETH/USDT:USDT"]) ... ) result
Source code in libs/tradai-data/src/tradai/data/infrastructure/adapters/arctic_adapter.py
get_latest_date(symbols: SymbolList) -> dict[str, datetime] ¶
Get latest available date for each symbol.
Returns the actual last candle date from stored data. Falls back to last_query_date for backwards compatibility with older data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbols | SymbolList | Symbols to check | required |
Returns:
| Type | Description |
|---|---|
dict[str, datetime] | Dictionary mapping symbol -> latest candle date (only for existing symbols) |
Example
result = adapter.get_latest_date( ... SymbolList.from_input(["BTC/USDT:USDT"]) ... ) result
Source code in libs/tradai-data/src/tradai/data/infrastructure/adapters/arctic_adapter.py
get_stored_timeframe(symbols: SymbolList) -> dict[str, str | None] ¶
Get stored timeframe for each symbol from ArcticDB metadata.
Returns None for legacy data without timeframe metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbols | SymbolList | Symbols to check | required |
Returns:
| Type | Description |
|---|---|
dict[str, str | None] | Dictionary mapping symbol -> timeframe string or None |
Source code in libs/tradai-data/src/tradai/data/infrastructure/adapters/arctic_adapter.py
list_symbols() -> list[str] ¶
List all symbols stored in ArcticDB.
Returns symbols in trading format (e.g., "BTC/USDT:USDT").
Returns:
| Type | Description |
|---|---|
list[str] | List of trading symbols stored in the library |
Example
symbols = adapter.list_symbols() symbols ['BTC/USDT:USDT', 'ETH/USDT:USDT']
Source code in libs/tradai-data/src/tradai/data/infrastructure/adapters/arctic_adapter.py
delete(symbols: SymbolList) -> dict[str, bool] ¶
Delete data for given symbols from ArcticDB.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbols | SymbolList | Symbols to delete | required |
Returns:
| Type | Description |
|---|---|
dict[str, bool] | Dictionary mapping symbol -> deleted (True if existed and deleted) |
Raises:
| Type | Description |
|---|---|
StorageError | If deletion fails |
Example
result = adapter.delete( ... SymbolList.from_input(["BTC/USDT:USDT"]) ... ) result
Source code in libs/tradai-data/src/tradai/data/infrastructure/adapters/arctic_adapter.py
MLflow Adapter¶
tradai.common.mlflow.adapter.MLflowAdapter ¶
Bases: MLflowClientMixin, ExperimentsMixin, RegistryMixin, ArtifactsMixin
MLflow REST API adapter with automatic base path discovery.
Compatible with MLflow 3.x API. Uses Basic Authentication. Follows DI pattern - accepts optional session for testing.
This class composes functionality from specialized mixins: - MLflowClientMixin: HTTP client infrastructure, session management - ExperimentsMixin: Experiment creation, run tracking, metrics - RegistryMixin: Model version management, stage transitions - ArtifactsMixin: Artifact upload and download
Attributes:
| Name | Type | Description |
|---|---|---|
base_url | str | Base URL of the MLflow server |
verify_ssl | bool | Whether to verify SSL certificates |
Example
adapter = MLflowAdapter( ... base_url="https://mlflow.tradai.smartml.me", ... username="admin", ... password="secret", ... ) version = adapter.get_model_version("PascalStrategy") print(version.version) '3'
Methods from MLflowClientMixin
- api_base: Property to get/discover API base URL
- mlflow_client: Property to get MLflow Python SDK client
- is_available(): Check if MLflow server is reachable
- _request(): Send HTTP request to MLflow API
Methods from ExperimentsMixin
- log_experiment(): Log metrics to an experiment
- get_experiment_results(): Get experiment with all runs
- create_run(): Create a new run
- log_metric(): Log a single metric
- end_run(): End a run
- log_parameter(): Log a parameter
- set_tag(): Set a tag on a run
- search_experiments(): Search all experiments
- search_runs(): Search runs within experiments
Methods from RegistryMixin
- get_model_version(): Get model version info
- search_registered_models(): Search models in registry
- get_registered_model(): Get registered model details
- get_production_version(): Get production version for a model
- create_model_version(): Create a new model version
- transition_model_version_stage(): Transition version stage
- delete_model_version(): Delete a model version
- delete_registered_model(): Delete a registered model
- load_model(): Load a model for inference
Methods from ArtifactsMixin
- log_artifact(): Upload a file as artifact
- upload_artifact(): Upload directly to S3
- download_artifacts(): Download artifacts from a run
Source code in libs/tradai-common/src/tradai/common/mlflow/adapter.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
Configuration Services¶
tradai.common.config.service.ConfigVersionService ¶
Bases: LoggerMixin
High-level service for config version management.
Provides content-addressable versioning with deduplication, lifecycle management (DRAFT → ACTIVE → DEPRECATED), and atomic activation (deprecates previous active).
Follows existing patterns: - LoggerMixin for logging - Constructor DI for repository - Protocol-based dependencies
Example
repo = ConfigVersionRepository() service = ConfigVersionService(repository=repo) version = service.create_version( ... strategy_name="PascalStrategy", ... config_data={"timeframe": "1h", "stoploss": -0.05}, ... ) print(f"Created: {version.config_id}") activated = service.activate("PascalStrategy", version.config_id)
Source code in libs/tradai-common/src/tradai/common/config/service.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
create_version(strategy_name: str, config_data: dict[str, Any], description: str = '', created_by: str = 'system') -> ConfigVersion ¶
Create new config version with content-addressable deduplication.
If a version with the same content hash already exists, returns the existing version (deduplication).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy_name | str | Strategy this config belongs to | required |
config_data | dict[str, Any] | Configuration dictionary | required |
description | str | Optional version description | '' |
created_by | str | User/system creating the version | 'system' |
Returns:
| Type | Description |
|---|---|
ConfigVersion | Created or existing ConfigVersion |
Raises:
| Type | Description |
|---|---|
ValidationError | If config_data is empty |
Source code in libs/tradai-common/src/tradai/common/config/service.py
activate(strategy_name: str, config_id: str) -> ConfigVersion ¶
Activate a config version (deprecates previous active).
Transitions: - Current ACTIVE → DEPRECATED (with superseded_by) - Target DRAFT → ACTIVE (sets deployed_at)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy_name | str | Strategy name | required |
config_id | str | Version to activate | required |
Returns:
| Type | Description |
|---|---|
ConfigVersion | Activated ConfigVersion |
Raises:
| Type | Description |
|---|---|
NotFoundError | If version doesn't exist |
ValidationError | If version is not in DRAFT status |
Source code in libs/tradai-common/src/tradai/common/config/service.py
get_active(strategy_name: str) -> ConfigVersion | None ¶
Get currently active config version for strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy_name | str | Strategy to get active config for | required |
Returns:
| Type | Description |
|---|---|
ConfigVersion | None | Active ConfigVersion if exists, None otherwise |
Source code in libs/tradai-common/src/tradai/common/config/service.py
get_version(strategy_name: str, config_id: str) -> ConfigVersion | None ¶
Get specific config version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy_name | str | Strategy name | required |
config_id | str | Version identifier | required |
Returns:
| Type | Description |
|---|---|
ConfigVersion | None | ConfigVersion if found, None otherwise |
Source code in libs/tradai-common/src/tradai/common/config/service.py
list_versions(strategy_name: str, limit: int = 100, status_filter: ConfigVersionStatus | None = None) -> list[ConfigVersion] ¶
List config versions for a strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy_name | str | Strategy to list versions for | required |
limit | int | Maximum versions to return | 100 |
status_filter | ConfigVersionStatus | None | Optional status filter | None |
Returns:
| Type | Description |
|---|---|
list[ConfigVersion] | List of ConfigVersion entities (newest first) |
Source code in libs/tradai-common/src/tradai/common/config/service.py
deprecate(strategy_name: str, config_id: str) -> ConfigVersion ¶
Manually deprecate a config version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy_name | str | Strategy name | required |
config_id | str | Version to deprecate | required |
Returns:
| Type | Description |
|---|---|
ConfigVersion | Deprecated ConfigVersion |
Raises:
| Type | Description |
|---|---|
NotFoundError | If version doesn't exist |
ValidationError | If version is already deprecated |
Source code in libs/tradai-common/src/tradai/common/config/service.py
tradai.common.config.merge.ConfigMergeService ¶
Bases: LoggerMixin
Generic config merging service using OmegaConf.
Provides deep merge capabilities for configuration dictionaries, with optional validation using required fields or Pydantic schemas.
Works with raw dicts for maximum flexibility. Domain-specific services can wrap this and convert to/from their entity types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_repository | S3ConfigRepository | None | S3ConfigRepository for loading/saving configs (DI) | None |
bucket | str | None | S3 bucket name (creates repository if not provided) | None |
prefix | str | S3 key prefix for configs | '' |
Example
service = ConfigMergeService(config_repository=my_repo) merged = service.merge_configs( ... base_config_name="base-config", ... overrides={"version": "2.0.0"}, ... ) merged["version"] '2.0.0'
Source code in libs/tradai-common/src/tradai/common/config/merge.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | |
config_repository: S3ConfigRepository property ¶
Get or create the config repository (lazy initialization).
Thread-safe via double-checked locking pattern.
Returns:
| Type | Description |
|---|---|
S3ConfigRepository | S3ConfigRepository instance |
Raises:
| Type | Description |
|---|---|
ExternalServiceError | If no bucket provided and no repository injected |
load_config(config_name: str) -> dict[str, Any] ¶
Load config from repository.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_name | str | Name of the config (without extension) | required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | Configuration as dictionary |
Raises:
| Type | Description |
|---|---|
ExternalServiceError | If config not found or download fails |
Source code in libs/tradai-common/src/tradai/common/config/merge.py
apply_overrides(base: dict[str, Any], overrides: dict[str, Any]) -> dict[str, Any] ¶
Apply overrides using OmegaConf deep merge.
Performs a deep merge where override values take precedence. Nested dictionaries are merged recursively.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base | dict[str, Any] | Base configuration dictionary | required |
overrides | dict[str, Any] | Override values to apply | required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | New dictionary with overrides applied (original unchanged) |
Example
base = {"a": 1, "nested": {"x": 10, "y": 20}} overrides = {"nested": {"y": 200, "z": 30}} result = service.apply_overrides(base, overrides) result["nested"]
Source code in libs/tradai-common/src/tradai/common/config/merge.py
validate_config(config: dict[str, Any], required_fields: list[str] | None = None, schema: type[BaseModel] | None = None) -> ValidationResult ¶
Validate configuration.
Supports two validation modes: 1. Required fields check - simple presence validation 2. Pydantic schema - full type and constraint validation
Both can be used together.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config | dict[str, Any] | Configuration to validate | required |
required_fields | list[str] | None | List of field names that must be present | None |
schema | type[BaseModel] | None | Optional Pydantic model class for schema validation | None |
Returns:
| Type | Description |
|---|---|
ValidationResult | ValidationResult with errors/warnings |
Example
result = service.validate_config( ... config={"name": "test"}, ... required_fields=["name", "version"], ... ) result.valid False result.errors ["Missing required field: version"]
Source code in libs/tradai-common/src/tradai/common/config/merge.py
merge_configs(base_config_name: str, overrides: dict[str, Any], validate: bool = True, required_fields: list[str] | None = None, schema: type[BaseModel] | None = None) -> dict[str, Any] ¶
Load base config, apply overrides, and optionally validate.
This is the main convenience method that combines all operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_config_name | str | Name of base config to load from repository | required |
overrides | dict[str, Any] | Override values to apply | required |
validate | bool | Whether to validate the merged config | True |
required_fields | list[str] | None | Fields required for validation | None |
schema | type[BaseModel] | None | Optional Pydantic schema for validation | None |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | Merged configuration dictionary |
Raises:
| Type | Description |
|---|---|
ExternalServiceError | If config not found or validation fails |
Source code in libs/tradai-common/src/tradai/common/config/merge.py
save_config(config_name: str, config: dict[str, Any]) -> str ¶
Save config to repository.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_name | str | Name to save as (without extension) | required |
config | dict[str, Any] | Configuration dictionary to save | required |
Returns:
| Type | Description |
|---|---|
str | S3 URI of saved config |
Source code in libs/tradai-common/src/tradai/common/config/merge.py
list_configs() -> list[str] ¶
List available config names from repository.
Returns:
| Type | Description |
|---|---|
list[str] | List of config names |
tradai.common.config.loader.StrategyConfigLoader ¶
Bases: LoggerMixin
High-level strategy config loader for live trading.
Wraps ConfigMergeService with strategy-specific validation and entity conversion. Thread-safe lazy initialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_service | ConfigMergeService | None | Optional ConfigMergeService for DI | None |
bucket | str | None | S3 bucket name (required, pass explicitly via DI) | None |
prefix | str | S3 key prefix for strategy configs | 'strategies/' |
Example
loader = StrategyConfigLoader(bucket="tradai-configs-dev") config = loader.load("pascal-strategy") config.name 'pascal-strategy'
With runtime overrides¶
config = loader.load_with_overrides( ... "pascal-strategy", ... {"timeframe": "4h", "parameters": {"rsi_period": 21}} ... )
Source code in libs/tradai-common/src/tradai/common/config/loader.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 | |
config_service: ConfigMergeService property ¶
Get or create ConfigMergeService (thread-safe lazy init).
Returns:
| Type | Description |
|---|---|
ConfigMergeService | ConfigMergeService instance |
Raises:
| Type | Description |
|---|---|
ValidationError | If no bucket configured |
load(strategy_name: str) -> StrategyConfig ¶
Load and validate strategy configuration from S3.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy_name | str | Strategy identifier (e.g., "pascal-strategy") | required |
Returns:
| Type | Description |
|---|---|
StrategyConfig | Validated StrategyConfig entity |
Raises:
| Type | Description |
|---|---|
NotFoundError | If strategy config doesn't exist |
ValidationError | If config fails validation |
Source code in libs/tradai-common/src/tradai/common/config/loader.py
load_with_overrides(strategy_name: str, overrides: dict[str, Any]) -> StrategyConfig ¶
Load strategy config and apply runtime overrides.
Useful for live trading containers that need to modify parameters at runtime (e.g., different timeframe, pairs).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy_name | str | Strategy identifier | required |
overrides | dict[str, Any] | Override values to deep-merge | required |
Returns:
| Type | Description |
|---|---|
StrategyConfig | Validated StrategyConfig with overrides applied |
Raises:
| Type | Description |
|---|---|
NotFoundError | If strategy config doesn't exist |
ValidationError | If merged config fails validation |
Source code in libs/tradai-common/src/tradai/common/config/loader.py
validate(config: dict[str, Any]) -> ValidationResult ¶
Validate config against StrategyConfig schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config | dict[str, Any] | Configuration dictionary to validate | required |
Returns:
| Type | Description |
|---|---|
ValidationResult | ValidationResult with errors/warnings |
Source code in libs/tradai-common/src/tradai/common/config/loader.py
list_strategies() -> list[str] ¶
List available strategy configurations.
Returns:
| Type | Description |
|---|---|
list[str] | List of strategy names (without .yaml extension) |
Source code in libs/tradai-common/src/tradai/common/config/loader.py
exists(strategy_name: str) -> bool ¶
Check if strategy configuration exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy_name | str | Strategy identifier | required |
Returns:
| Type | Description |
|---|---|
bool | True if config exists, False otherwise |
Raises:
| Type | Description |
|---|---|
ExternalServiceError | If S3 is unavailable or access denied |
Source code in libs/tradai-common/src/tradai/common/config/loader.py
load_from_mlflow(strategy_name: str, stage: str = ModelStage.PRODUCTION.value) -> StrategyConfig ¶
Load strategy config via MLflow Model Registry.
Flow: 1. Get model version from MLflow registry (by name + stage) 2. Extract 'strategy.configuration_file' tag for S3 path 3. Extract metadata tags (timeframe, pairs, warmup_days) 4. Load config from S3 using extracted path 5. Apply extracted metadata as overrides 6. Return validated StrategyConfig
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy_name | str | Registered model name in MLflow (e.g., "PascalStrategy") | required |
stage | str | Model stage to load from ("Production", "Staging", "None") | PRODUCTION.value |
Returns:
| Type | Description |
|---|---|
StrategyConfig | Validated StrategyConfig |
Raises:
| Type | Description |
|---|---|
ExternalServiceError | If MLflow unavailable or API error |
NotFoundError | If model or config not found |
ValidationError | If config is invalid |
Example
config = loader.load_from_mlflow("PascalStrategy", stage="Production") config.name 'PascalStrategy'
Source code in libs/tradai-common/src/tradai/common/config/loader.py
load_with_fallback(strategy_name: str, stage: str = ModelStage.PRODUCTION.value) -> StrategyConfig ¶
Try loading from MLflow first, fall back to S3 on failure.
Useful for environments where MLflow may be unavailable or when graceful degradation is preferred.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy_name | str | Strategy identifier | required |
stage | str | MLflow stage to try first ("Production", "Staging") | PRODUCTION.value |
Returns:
| Type | Description |
|---|---|
StrategyConfig | Validated StrategyConfig |
Example
Tries MLflow registry first, then falls back to S3¶
config = loader.load_with_fallback("pascal-strategy")