This repository was archived by the owner on May 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrategy_runtime.py
More file actions
75 lines (64 loc) · 2.8 KB
/
strategy_runtime.py
File metadata and controls
75 lines (64 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
from __future__ import annotations
from dataclasses import dataclass
from typing import Callable
from typing import Any, Mapping
from quant_platform_kit.strategy_contracts import StrategyEntrypoint, StrategyRuntimeAdapter
from runtime_config_support import PlatformRuntimeSettings
from strategy_loader import (
load_strategy_entrypoint_for_profile,
load_strategy_runtime_adapter_for_profile,
)
@dataclass(frozen=True)
class LoadedStrategyRuntime:
entrypoint: StrategyEntrypoint
runtime_settings: PlatformRuntimeSettings
runtime_adapter: StrategyRuntimeAdapter
runtime_config: Mapping[str, Any] = None
merged_runtime_config: Mapping[str, Any] = None
@property
def profile(self) -> str:
return self.entrypoint.manifest.profile
@property
def required_inputs(self) -> frozenset[str]:
return frozenset(self.entrypoint.manifest.required_inputs)
def describe(self) -> Mapping[str, Any]:
return {
"strategy_profile": self.profile,
"strategy_domain": self.entrypoint.manifest.domain,
"strategy_target_mode": self.entrypoint.manifest.target_mode,
"required_inputs": sorted(self.required_inputs),
"available_inputs": sorted(self.runtime_adapter.available_inputs or ()),
"available_capabilities": sorted(self.runtime_adapter.available_capabilities or ()),
"paper_account_group": self.runtime_settings.paper_account_group,
"service_name": self.runtime_settings.service_name,
"mode": "paper_only",
}
def load_runtime_parameters(self, *, logger: Callable[[str], None] | None = None) -> Mapping[str, Any]:
runtime_loader = self.runtime_adapter.runtime_parameter_loader
if not callable(runtime_loader):
return {}
return dict(
runtime_loader(
config_path=self.runtime_settings.strategy_config_path,
logger=logger or (lambda _message: None),
)
or {}
)
def load_strategy_runtime(settings: PlatformRuntimeSettings) -> LoadedStrategyRuntime:
entrypoint = load_strategy_entrypoint_for_profile(settings.strategy_profile)
runtime_adapter = load_strategy_runtime_adapter_for_profile(settings.strategy_profile)
runtime = LoadedStrategyRuntime(
entrypoint=entrypoint,
runtime_settings=settings,
runtime_adapter=runtime_adapter,
)
runtime_config = runtime.load_runtime_parameters()
merged_runtime_config = dict(entrypoint.manifest.default_config)
merged_runtime_config.update(runtime_config)
return LoadedStrategyRuntime(
entrypoint=entrypoint,
runtime_settings=settings,
runtime_adapter=runtime_adapter,
runtime_config=runtime_config,
merged_runtime_config=merged_runtime_config,
)