-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime_config_support.py
More file actions
335 lines (288 loc) · 10.8 KB
/
runtime_config_support.py
File metadata and controls
335 lines (288 loc) · 10.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
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
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
from __future__ import annotations
import json
import os
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Callable
from strategy_registry import (
DEFAULT_STRATEGY_PROFILE as PLATFORM_DEFAULT_STRATEGY_PROFILE,
IBKR_PLATFORM,
resolve_strategy_definition,
)
DEFAULT_ACCOUNT_GROUP = "default"
DEFAULT_STRATEGY_PROFILE = PLATFORM_DEFAULT_STRATEGY_PROFILE
@dataclass(frozen=True)
class AccountGroupConfig:
ib_gateway_instance_name: str | None = None
ib_gateway_zone: str | None = None
ib_gateway_mode: str | None = None
ib_gateway_ip_mode: str | None = None
ib_client_id: int | None = None
service_name: str | None = None
account_ids: tuple[str, ...] = ()
@dataclass(frozen=True)
class PlatformRuntimeSettings:
project_id: str | None
ib_gateway_instance_name: str
ib_gateway_zone: str
ib_gateway_mode: str
ib_gateway_ip_mode: str
ib_client_id: int
strategy_profile: str
strategy_domain: str
feature_snapshot_path: str | None
feature_snapshot_manifest_path: str | None
strategy_config_path: str | None
strategy_config_source: str | None
reconciliation_output_path: str | None
dry_run_only: bool
account_group: str
service_name: str | None
account_ids: tuple[str, ...]
tg_token: str | None
tg_chat_id: str | None
notify_lang: str
def load_platform_runtime_settings(
*,
project_id_resolver: Callable[[], str | None],
logger: Callable[[str], None] = print,
secret_client_factory: Callable[[], Any] | None = None,
) -> PlatformRuntimeSettings:
project_id = project_id_resolver()
strategy_definition = resolve_strategy_definition(
os.getenv("STRATEGY_PROFILE"),
platform_id=IBKR_PLATFORM,
)
strategy_config_path, strategy_config_source = resolve_strategy_config_path(
strategy_definition.profile,
explicit_path=first_non_empty(
os.getenv("IBKR_STRATEGY_CONFIG_PATH"),
os.getenv("STRATEGY_CONFIG_PATH"),
),
)
account_group = resolve_account_group(os.getenv("ACCOUNT_GROUP"))
group_config = load_account_group_config(
project_id=project_id,
account_group=account_group,
raw_json=os.getenv("IB_ACCOUNT_GROUP_CONFIG_JSON"),
secret_name=os.getenv("IB_ACCOUNT_GROUP_CONFIG_SECRET_NAME"),
secret_client_factory=secret_client_factory,
)
instance_name = require_group_string(
group_config.ib_gateway_instance_name,
field_name="ib_gateway_instance_name",
account_group=account_group,
)
return PlatformRuntimeSettings(
project_id=project_id,
ib_gateway_instance_name=instance_name,
ib_gateway_zone=first_non_empty(
group_config.ib_gateway_zone,
os.getenv("IB_GATEWAY_ZONE", "").strip(),
)
or "",
ib_gateway_mode=resolve_ib_gateway_mode(
require_group_string(
group_config.ib_gateway_mode,
field_name="ib_gateway_mode",
account_group=account_group,
)
),
ib_gateway_ip_mode=resolve_ib_gateway_ip_mode(
first_non_empty(group_config.ib_gateway_ip_mode, os.getenv("IB_GATEWAY_IP_MODE")),
logger=logger,
),
ib_client_id=require_group_int(
group_config.ib_client_id,
field_name="ib_client_id",
account_group=account_group,
),
strategy_profile=strategy_definition.profile,
strategy_domain=strategy_definition.domain,
feature_snapshot_path=first_non_empty(
os.getenv("IBKR_FEATURE_SNAPSHOT_PATH"),
os.getenv("FEATURE_SNAPSHOT_PATH"),
),
feature_snapshot_manifest_path=first_non_empty(
os.getenv("IBKR_FEATURE_SNAPSHOT_MANIFEST_PATH"),
os.getenv("FEATURE_SNAPSHOT_MANIFEST_PATH"),
),
strategy_config_path=strategy_config_path,
strategy_config_source=strategy_config_source,
reconciliation_output_path=first_non_empty(
os.getenv("IBKR_RECONCILIATION_OUTPUT_PATH"),
os.getenv("RECONCILIATION_OUTPUT_PATH"),
),
dry_run_only=resolve_bool_env(os.getenv("IBKR_DRY_RUN_ONLY")),
account_group=account_group,
service_name=group_config.service_name,
account_ids=group_config.account_ids,
tg_token=os.getenv("TELEGRAM_TOKEN"),
tg_chat_id=os.getenv("GLOBAL_TELEGRAM_CHAT_ID"),
notify_lang=os.getenv("NOTIFY_LANG", "en"),
)
def resolve_strategy_profile(raw_value: str | None) -> str:
return resolve_strategy_definition(
raw_value,
platform_id=IBKR_PLATFORM,
).profile
def resolve_account_group(raw_value: str | None) -> str:
value = (raw_value or "").strip()
if not value:
raise EnvironmentError("ACCOUNT_GROUP is required")
return value
def resolve_strategy_config_path(
strategy_profile: str,
*,
explicit_path: str | None,
) -> tuple[str | None, str | None]:
path = first_non_empty(explicit_path)
if path is not None:
return path, "env"
if str(strategy_profile).strip().lower() == "cash_buffer_branch_default":
bundled = (
Path(__file__).resolve().parent
/ "research"
/ "configs"
/ "growth_pullback_cash_buffer_branch_default.json"
)
if bundled.exists():
return str(bundled), "bundled_canonical_default"
return None, None
def load_account_group_config(
*,
project_id: str | None,
account_group: str,
raw_json: str | None,
secret_name: str | None,
secret_client_factory: Callable[[], Any] | None = None,
) -> AccountGroupConfig:
payload = None
if secret_name:
if not project_id:
raise EnvironmentError(
"GOOGLE_CLOUD_PROJECT is required when IB_ACCOUNT_GROUP_CONFIG_SECRET_NAME is set"
)
payload = load_secret_payload(
project_id,
secret_name,
secret_client_factory=secret_client_factory,
)
elif raw_json:
payload = raw_json
if not payload:
raise EnvironmentError(
"IB_ACCOUNT_GROUP_CONFIG_SECRET_NAME or IB_ACCOUNT_GROUP_CONFIG_JSON is required"
)
configs = parse_account_group_configs(payload)
if account_group not in configs:
available = ", ".join(sorted(configs))
raise ValueError(
f"ACCOUNT_GROUP={account_group!r} not found in account-group config; available groups: {available}"
)
return configs[account_group]
def parse_account_group_configs(payload: str) -> dict[str, AccountGroupConfig]:
raw_data = json.loads(payload)
groups = raw_data.get("groups", raw_data) if isinstance(raw_data, dict) else None
if not isinstance(groups, dict):
raise ValueError("IB account-group config must be a JSON object or {\"groups\": {...}}")
parsed: dict[str, AccountGroupConfig] = {}
for group_name, group_payload in groups.items():
if not isinstance(group_payload, dict):
raise ValueError(f"Account group {group_name!r} must be a JSON object")
parsed[str(group_name)] = AccountGroupConfig(
ib_gateway_instance_name=normalize_optional_string(group_payload.get("ib_gateway_instance_name")),
ib_gateway_zone=normalize_optional_string(group_payload.get("ib_gateway_zone")),
ib_gateway_mode=normalize_optional_string(group_payload.get("ib_gateway_mode")),
ib_gateway_ip_mode=normalize_optional_string(group_payload.get("ib_gateway_ip_mode")),
ib_client_id=parse_optional_int(group_payload.get("ib_client_id")),
service_name=normalize_optional_string(group_payload.get("service_name")),
account_ids=parse_account_ids(group_payload.get("account_ids")),
)
return parsed
def load_secret_payload(
project_id: str,
secret_name: str,
*,
secret_client_factory: Callable[[], Any] | None = None,
) -> str:
if secret_client_factory is None:
try:
import google.cloud.secretmanager_v1 as secret_manager
except ImportError:
from google.cloud import secret_manager
secret_client_factory = secret_manager.SecretManagerServiceClient
client = secret_client_factory()
resource_name = f"projects/{project_id}/secrets/{secret_name}/versions/latest"
response = client.access_secret_version(request={"name": resource_name})
return response.payload.data.decode("UTF-8")
def parse_account_ids(raw_value: Any) -> tuple[str, ...]:
if raw_value is None:
return ()
if not isinstance(raw_value, (list, tuple)):
raise ValueError("account_ids must be a JSON array of strings")
parsed = []
for item in raw_value:
value = normalize_optional_string(item)
if value is None:
continue
parsed.append(value)
return tuple(parsed)
def parse_optional_int(raw_value: Any) -> int | None:
if raw_value is None or raw_value == "":
return None
return int(raw_value)
def normalize_optional_string(raw_value: Any) -> str | None:
if raw_value is None:
return None
value = str(raw_value).strip()
return value or None
def first_non_empty(*values: str | None) -> str | None:
for value in values:
normalized = normalize_optional_string(value)
if normalized is not None:
return normalized
return None
def resolve_bool_env(raw_value: str | None) -> bool:
value = str(raw_value or "").strip().lower()
return value in {"1", "true", "yes", "y", "on"}
def require_group_string(
raw_value: str | None,
*,
field_name: str,
account_group: str,
) -> str:
value = normalize_optional_string(raw_value)
if value is None:
raise EnvironmentError(
f"Account group {account_group!r} requires {field_name}"
)
return value
def require_group_int(
raw_value: int | None,
*,
field_name: str,
account_group: str,
) -> int:
if raw_value is None:
raise EnvironmentError(
f"Account group {account_group!r} requires {field_name}"
)
return int(raw_value)
def resolve_ib_gateway_mode(raw_value: str | None) -> str:
mode = (raw_value or "").strip().lower()
if not mode:
raise EnvironmentError("IB_GATEWAY_MODE is required and must be either 'live' or 'paper'")
if mode in {"live", "paper"}:
return mode
raise EnvironmentError("IB_GATEWAY_MODE must be either 'live' or 'paper'")
def resolve_ib_gateway_ip_mode(
raw_value: str | None,
*,
logger: Callable[[str], None] = print,
) -> str:
mode = (raw_value or "internal").strip().lower()
if mode in {"internal", "external"}:
return mode
logger(f"Invalid IB_GATEWAY_IP_MODE={mode!r}, defaulting to internal")
return "internal"