-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrategy_runtime.py
More file actions
198 lines (175 loc) · 7.16 KB
/
strategy_runtime.py
File metadata and controls
198 lines (175 loc) · 7.16 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
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
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import Any, Callable, Mapping
from quant_platform_kit.common.feature_snapshot import load_feature_snapshot_guarded
from quant_platform_kit.common.feature_snapshot_runtime import (
FeatureSnapshotRuntimeSettings,
evaluate_feature_snapshot_strategy,
)
from quant_platform_kit.strategy_contracts import (
StrategyDecision,
StrategyEntrypoint,
StrategyRuntimeAdapter,
build_strategy_context_from_available_inputs,
)
from runtime_config_support import PlatformRuntimeSettings
from strategy_loader import (
load_strategy_entrypoint_for_profile,
load_strategy_runtime_adapter_for_profile,
)
_FEATURE_SNAPSHOT_INPUT = "feature_snapshot"
@dataclass(frozen=True)
class StrategyEvaluationResult:
decision: StrategyDecision
metadata: Mapping[str, Any] = field(default_factory=dict)
@dataclass(frozen=True)
class LoadedStrategyRuntime:
entrypoint: StrategyEntrypoint
runtime_adapter: StrategyRuntimeAdapter
runtime_settings: PlatformRuntimeSettings
runtime_overrides: Mapping[str, Any] = field(default_factory=dict)
runtime_config: Mapping[str, Any] = field(default_factory=dict)
merged_runtime_config: Mapping[str, Any] = field(default_factory=dict)
logger: Callable[[str], None] = print
@property
def profile(self) -> str:
return self.entrypoint.manifest.profile
@property
def display_name(self) -> str:
return str(self.entrypoint.manifest.display_name)
@property
def managed_symbols(self) -> tuple[str, ...]:
configured = self.merged_runtime_config.get("managed_symbols", ())
return tuple(str(symbol) for symbol in configured)
def evaluate(
self,
*,
translator: Callable[[str], str],
signal_text_fn: Callable[[str], str] | None = None,
**available_inputs,
) -> StrategyEvaluationResult:
runtime_config = dict(self.runtime_overrides)
runtime_config.setdefault("translator", translator)
if signal_text_fn is not None:
runtime_config.setdefault("signal_text_fn", signal_text_fn)
if _FEATURE_SNAPSHOT_INPUT in frozenset(self.entrypoint.manifest.required_inputs):
return self._evaluate_feature_snapshot_strategy(
runtime_config=runtime_config,
available_inputs=available_inputs,
)
ctx = build_strategy_context_from_available_inputs(
entrypoint=self.entrypoint,
runtime_adapter=self.runtime_adapter,
as_of=datetime.now(timezone.utc),
available_inputs=available_inputs,
runtime_config=runtime_config,
)
decision = self.entrypoint.evaluate(ctx)
return StrategyEvaluationResult(
decision=decision,
metadata={
"strategy_profile": self.profile,
"strategy_display_name": self.display_name,
},
)
def _evaluate_feature_snapshot_strategy(
self,
*,
runtime_config: Mapping[str, Any],
available_inputs: Mapping[str, Any],
) -> StrategyEvaluationResult:
result = evaluate_feature_snapshot_strategy(
entrypoint=self.entrypoint,
runtime_adapter=self.runtime_adapter,
runtime_settings=FeatureSnapshotRuntimeSettings(
feature_snapshot_path=self.runtime_settings.feature_snapshot_path,
feature_snapshot_manifest_path=self.runtime_settings.feature_snapshot_manifest_path,
strategy_config_path=self.runtime_settings.strategy_config_path,
strategy_config_source=self.runtime_settings.strategy_config_source,
dry_run_only=self.runtime_settings.dry_run_only,
),
runtime_config=runtime_config,
merged_runtime_config=self.merged_runtime_config,
available_inputs=available_inputs,
base_managed_symbols=self.managed_symbols,
include_strategy_display_name=True,
set_run_as_of=True,
snapshot_loader=load_feature_snapshot_guarded,
)
return StrategyEvaluationResult(
decision=result.decision,
metadata=result.metadata,
)
def load_runtime_parameters(self) -> dict[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=self.logger,
)
or {}
)
def _default_runtime_settings(profile: str, display_name: str) -> PlatformRuntimeSettings:
return PlatformRuntimeSettings(
project_id=None,
secret_name="",
account_prefix="DEFAULT",
strategy_profile=profile,
strategy_display_name=display_name,
strategy_domain="us_equity",
account_region="DEFAULT",
notify_lang="en",
tg_token=None,
tg_chat_id=None,
dry_run_only=False,
feature_snapshot_path=None,
feature_snapshot_manifest_path=None,
strategy_config_path=None,
strategy_config_source=None,
)
def _build_runtime_overrides(profile: str, runtime_settings: PlatformRuntimeSettings) -> dict[str, Any]:
overrides: dict[str, Any] = {}
if profile == "tqqq_growth_income":
if runtime_settings.income_threshold_usd is not None:
overrides["income_threshold_usd"] = runtime_settings.income_threshold_usd
if runtime_settings.qqqi_income_ratio is not None:
overrides["qqqi_income_ratio"] = runtime_settings.qqqi_income_ratio
return overrides
def load_strategy_runtime(
raw_profile: str | None,
*,
runtime_settings: PlatformRuntimeSettings | None = None,
runtime_overrides: Mapping[str, Any] | None = None,
logger: Callable[[str], None] = print,
) -> LoadedStrategyRuntime:
entrypoint = load_strategy_entrypoint_for_profile(raw_profile)
runtime_adapter = load_strategy_runtime_adapter_for_profile(raw_profile)
resolved_runtime_settings = runtime_settings or _default_runtime_settings(
entrypoint.manifest.profile,
entrypoint.manifest.display_name,
)
overrides = _build_runtime_overrides(entrypoint.manifest.profile, resolved_runtime_settings)
overrides.update(runtime_overrides or {})
runtime = LoadedStrategyRuntime(
entrypoint=entrypoint,
runtime_adapter=runtime_adapter,
runtime_settings=resolved_runtime_settings,
runtime_overrides=overrides,
logger=logger,
)
runtime_config = runtime.load_runtime_parameters()
merged_runtime_config = dict(entrypoint.manifest.default_config)
merged_runtime_config.update(runtime_config)
merged_runtime_config.update(overrides)
return LoadedStrategyRuntime(
entrypoint=entrypoint,
runtime_adapter=runtime_adapter,
runtime_settings=resolved_runtime_settings,
runtime_overrides=overrides,
runtime_config=runtime_config,
merged_runtime_config=merged_runtime_config,
logger=logger,
)