-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecision_mapper.py
More file actions
442 lines (414 loc) · 16.5 KB
/
decision_mapper.py
File metadata and controls
442 lines (414 loc) · 16.5 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
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
from __future__ import annotations
from collections.abc import Mapping
from typing import Any
from us_equity_strategies.catalog import resolve_canonical_profile
from quant_platform_kit.strategy_contracts import (
PositionTarget,
StrategyDecision,
ValueTargetExecutionAnnotations,
build_value_target_execution_annotations,
build_value_target_portfolio_inputs_from_account_state,
build_value_target_portfolio_inputs_from_snapshot,
build_value_target_runtime_plan,
resolve_decision_target_mode,
translate_decision_to_target_mode,
)
_SAFE_HAVEN_SYMBOLS = frozenset({"BOXX", "BIL"})
_INCOME_SYMBOLS = frozenset({"QQQI", "SPYI"})
_DEFAULT_MIN_TRADE_FLOOR = 100.0
_DEFAULT_REBALANCE_THRESHOLD_RATIO = 0.01
def _build_portfolio_inputs(
*,
account_state: Mapping[str, Any] | None,
snapshot: Any | None,
):
if account_state is not None:
return build_value_target_portfolio_inputs_from_account_state(account_state)
if snapshot is not None:
return build_value_target_portfolio_inputs_from_snapshot(
snapshot,
include_sellable_quantities=True,
liquid_cash=float(snapshot.buying_power or snapshot.cash_balance or 0.0),
)
raise ValueError("LongBridge plan mapping requires account_state or snapshot")
def _cash_by_currency_from_account_state(
account_state: Mapping[str, Any] | None,
) -> dict[str, float]:
if account_state is None:
return {}
raw_cash = account_state.get("cash_by_currency")
if not isinstance(raw_cash, Mapping):
return {}
cash_by_currency: dict[str, float] = {}
for currency, amount in raw_cash.items():
normalized_currency = str(currency or "").strip().upper()
if not normalized_currency:
continue
cash_by_currency[normalized_currency] = float(amount)
return cash_by_currency
def _cash_by_currency_from_snapshot(snapshot: Any | None) -> dict[str, float]:
metadata = getattr(snapshot, "metadata", {}) or {}
if not isinstance(metadata, Mapping):
return {}
raw_cash = metadata.get("cash_by_currency")
if not isinstance(raw_cash, Mapping):
return {}
cash_by_currency: dict[str, float] = {}
for currency, amount in raw_cash.items():
normalized_currency = str(currency or "").strip().upper()
if not normalized_currency:
continue
cash_by_currency[normalized_currency] = float(amount)
return cash_by_currency
def _symbol_role(symbol: str) -> str | None:
normalized = str(symbol or "").strip().upper()
if normalized in _SAFE_HAVEN_SYMBOLS:
return "safe_haven"
if normalized in _INCOME_SYMBOLS:
return "income"
return None
def _default_threshold_value(total_equity: float) -> float:
return max(_DEFAULT_MIN_TRADE_FLOOR, float(total_equity) * _DEFAULT_REBALANCE_THRESHOLD_RATIO)
def _build_weight_translation_annotations(
decision: StrategyDecision,
*,
total_equity: float,
liquid_cash: float,
runtime_metadata: Mapping[str, Any] | None = None,
) -> ValueTargetExecutionAnnotations:
diagnostics = {**dict(runtime_metadata or {}), **dict(decision.diagnostics)}
execution_annotations: dict[str, Any] = {}
raw_runtime_annotations = runtime_metadata.get("execution_annotations") if isinstance(runtime_metadata, Mapping) else None
if isinstance(raw_runtime_annotations, Mapping):
execution_annotations.update(raw_runtime_annotations)
raw_annotations = diagnostics.get("execution_annotations")
if isinstance(raw_annotations, Mapping):
execution_annotations.update(raw_annotations)
threshold_value = _default_threshold_value(total_equity)
signal_display = str(
diagnostics.get("signal_description")
or diagnostics.get("signal_display")
or diagnostics.get("signal_message")
or ""
).strip() or None
status_display = str(
diagnostics.get("status_description")
or diagnostics.get("market_status")
or diagnostics.get("canary_status")
or ""
).strip() or None
dashboard_text = str(
execution_annotations.get("dashboard_text")
or diagnostics.get("dashboard")
or ""
).strip() or None
benchmark_symbol = str(diagnostics.get("benchmark_symbol") or "").strip().upper() or None
return ValueTargetExecutionAnnotations(
trade_threshold_value=threshold_value,
reserved_cash=0.0,
signal_display=signal_display,
status_display=status_display,
dashboard_text=dashboard_text,
benchmark_symbol=benchmark_symbol,
benchmark_price=(
float(diagnostics["benchmark_price"])
if diagnostics.get("benchmark_price") is not None
else None
),
long_trend_value=(
float(diagnostics["long_trend_value"])
if diagnostics.get("long_trend_value") is not None
else None
),
exit_line=(
float(diagnostics["exit_line"])
if diagnostics.get("exit_line") is not None
else None
),
signal_date=(
str(execution_annotations.get("signal_date") or diagnostics.get("signal_date") or "").strip() or None
),
effective_date=(
str(execution_annotations.get("effective_date") or diagnostics.get("effective_date") or "").strip()
or None
),
execution_timing_contract=(
str(
execution_annotations.get("execution_timing_contract")
or diagnostics.get("execution_timing_contract")
or ""
).strip()
or None
),
execution_calendar_source=(
str(
execution_annotations.get("execution_calendar_source")
or diagnostics.get("execution_calendar_source")
or ""
).strip()
or None
),
signal_effective_after_trading_days=(
int(signal_delay)
if (
signal_delay := execution_annotations.get(
"signal_effective_after_trading_days",
diagnostics.get("signal_effective_after_trading_days"),
)
)
is not None
else None
),
current_min_trade=threshold_value,
investable_cash=max(0.0, float(liquid_cash)),
)
def _build_hold_current_value_decision(portfolio_inputs) -> StrategyDecision:
positions: list[PositionTarget] = []
for symbol, market_value in sorted(portfolio_inputs.market_values.items()):
positions.append(
PositionTarget(
symbol=str(symbol),
target_value=float(market_value),
role=_symbol_role(str(symbol)),
)
)
return StrategyDecision(positions=tuple(positions))
def _normalize_to_value_target_decision(
decision: StrategyDecision,
*,
portfolio_inputs,
runtime_metadata: Mapping[str, Any] | None = None,
) -> tuple[StrategyDecision, ValueTargetExecutionAnnotations | None]:
target_mode = resolve_decision_target_mode(decision)
no_execute = "no_execute" in set(decision.risk_flags)
if target_mode == "value" and not no_execute:
return decision, None
if target_mode == "weight" and not no_execute:
translated = translate_decision_to_target_mode(
decision,
target_mode="value",
total_equity=float(portfolio_inputs.total_equity),
)
return translated, _build_weight_translation_annotations(
decision,
total_equity=float(portfolio_inputs.total_equity),
liquid_cash=float(portfolio_inputs.liquid_cash),
runtime_metadata=runtime_metadata,
)
synthetic = _build_hold_current_value_decision(portfolio_inputs)
synthetic_annotations = _build_weight_translation_annotations(
decision,
total_equity=float(portfolio_inputs.total_equity),
liquid_cash=float(portfolio_inputs.liquid_cash),
runtime_metadata=runtime_metadata,
)
return synthetic, synthetic_annotations
def _resolve_layout(strategy_profile: str) -> tuple[str, tuple[str, ...], tuple[str, ...], dict[str, Any]]:
strategy_profile = resolve_canonical_profile(strategy_profile)
if strategy_profile == "tqqq_growth_income":
return (
"risk_safe_income",
("risk_safe", "income"),
(
"trade_threshold_value",
"reserved_cash",
"signal_display",
"status_display",
"dashboard_text",
"signal_date",
"effective_date",
"execution_timing_contract",
"execution_calendar_source",
"signal_effective_after_trading_days",
"benchmark_symbol",
"benchmark_price",
"long_trend_value",
"exit_line",
"current_min_trade",
"investable_cash",
),
{
"reserved_cash": 0.0,
"signal_display": "",
"status_display": "",
"dashboard_text": "",
"signal_date": "",
"effective_date": "",
"execution_timing_contract": "",
"execution_calendar_source": "",
"signal_effective_after_trading_days": None,
"benchmark_symbol": "QQQ",
"benchmark_price": 0.0,
"long_trend_value": 0.0,
"exit_line": 0.0,
"current_min_trade": 0.0,
"investable_cash": 0.0,
},
)
if strategy_profile in {"tech_communication_pullback_enhancement", "qqq_tech_enhancement"}:
return (
"risk_safe_income",
("risk_safe",),
(
"trade_threshold_value",
"reserved_cash",
"signal_display",
"status_display",
"dashboard_text",
"signal_date",
"effective_date",
"execution_timing_contract",
"execution_calendar_source",
"signal_effective_after_trading_days",
"benchmark_symbol",
"benchmark_price",
"long_trend_value",
"exit_line",
"current_min_trade",
"investable_cash",
),
{
"reserved_cash": 0.0,
"signal_display": "",
"status_display": "",
"dashboard_text": "",
"signal_date": "",
"effective_date": "",
"execution_timing_contract": "",
"execution_calendar_source": "",
"signal_effective_after_trading_days": None,
"benchmark_symbol": "QQQ",
"benchmark_price": 0.0,
"long_trend_value": 0.0,
"exit_line": 0.0,
"current_min_trade": 0.0,
"investable_cash": 0.0,
},
)
return (
"risk_safe_income",
("risk", "income", "safe"),
(
"trade_threshold_value",
"signal_display",
"status_display",
"dashboard_text",
"signal_date",
"effective_date",
"execution_timing_contract",
"execution_calendar_source",
"signal_effective_after_trading_days",
"benchmark_symbol",
"benchmark_price",
"long_trend_value",
"exit_line",
"deploy_ratio_text",
"income_ratio_text",
"income_locked_ratio_text",
"active_risk_asset",
"investable_cash",
"current_min_trade",
),
{
"signal_display": "",
"status_display": "",
"dashboard_text": "",
"signal_date": "",
"effective_date": "",
"execution_timing_contract": "",
"execution_calendar_source": "",
"signal_effective_after_trading_days": None,
"deploy_ratio_text": "",
"income_ratio_text": "",
"income_locked_ratio_text": "",
"current_min_trade": 0.0,
"investable_cash": 0.0,
},
)
def map_strategy_decision_to_plan(
decision: StrategyDecision,
*,
account_state: Mapping[str, Any] | None = None,
snapshot: Any | None = None,
strategy_profile: str,
runtime_metadata: Mapping[str, Any] | None = None,
) -> dict[str, Any]:
canonical_profile = resolve_canonical_profile(strategy_profile)
portfolio_inputs = _build_portfolio_inputs(account_state=account_state, snapshot=snapshot)
normalized_decision, normalized_annotations = _normalize_to_value_target_decision(
decision,
portfolio_inputs=portfolio_inputs,
runtime_metadata=runtime_metadata,
)
annotations = normalized_annotations
if annotations is None:
merged_diagnostics = {**dict(runtime_metadata or {}), **dict(normalized_decision.diagnostics)}
merged_execution_annotations: dict[str, Any] = {}
raw_runtime_annotations = runtime_metadata.get("execution_annotations") if isinstance(runtime_metadata, Mapping) else None
if isinstance(raw_runtime_annotations, Mapping):
merged_execution_annotations.update(raw_runtime_annotations)
raw_decision_annotations = merged_diagnostics.get("execution_annotations")
if isinstance(raw_decision_annotations, Mapping):
merged_execution_annotations.update(raw_decision_annotations)
merged_decision = StrategyDecision(
positions=normalized_decision.positions,
budgets=normalized_decision.budgets,
risk_flags=normalized_decision.risk_flags,
diagnostics={
**merged_diagnostics,
"execution_annotations": merged_execution_annotations,
},
)
annotations = build_value_target_execution_annotations(merged_decision)
investable_cash = annotations.investable_cash
if investable_cash is None:
investable_cash = max(
0.0,
portfolio_inputs.liquid_cash - annotations.reserved_cash,
)
current_min_trade = annotations.current_min_trade
if current_min_trade is None:
current_min_trade = annotations.trade_threshold_value
annotations = ValueTargetExecutionAnnotations(
trade_threshold_value=annotations.trade_threshold_value,
reserved_cash=annotations.reserved_cash,
signal_display=annotations.signal_display,
status_display=annotations.status_display,
dashboard_text=annotations.dashboard_text,
separator=annotations.separator,
benchmark_symbol=annotations.benchmark_symbol,
benchmark_price=annotations.benchmark_price,
long_trend_value=annotations.long_trend_value,
exit_line=annotations.exit_line,
signal_date=annotations.signal_date,
effective_date=annotations.effective_date,
execution_timing_contract=annotations.execution_timing_contract,
execution_calendar_source=annotations.execution_calendar_source,
signal_effective_after_trading_days=annotations.signal_effective_after_trading_days,
deploy_ratio_text=annotations.deploy_ratio_text,
income_ratio_text=annotations.income_ratio_text,
income_locked_ratio_text=annotations.income_locked_ratio_text,
active_risk_asset=annotations.active_risk_asset,
current_min_trade=current_min_trade,
investable_cash=investable_cash,
)
strategy_symbols_order, portfolio_rows_layout, execution_fields, execution_defaults = _resolve_layout(
canonical_profile
)
plan = build_value_target_runtime_plan(
normalized_decision,
strategy_profile=canonical_profile,
portfolio_inputs=portfolio_inputs,
annotations=annotations,
strategy_symbols_order=strategy_symbols_order,
portfolio_rows_layout=portfolio_rows_layout,
include_sellable_quantities=True,
execution_fields=execution_fields,
execution_defaults=execution_defaults,
)
cash_by_currency = _cash_by_currency_from_account_state(account_state)
if not cash_by_currency:
cash_by_currency = _cash_by_currency_from_snapshot(snapshot)
if cash_by_currency:
plan["portfolio"]["cash_by_currency"] = cash_by_currency
return plan