33from collections .abc import Mapping
44from typing import Any
55
6- from quant_platform_kit .strategy_contracts import StrategyDecision
7-
8-
9- def _target_values (decision : StrategyDecision ) -> dict [str , float ]:
10- target_values : dict [str , float ] = {}
11- for position in decision .positions :
12- if position .target_value is None :
13- raise ValueError (
14- "LongBridge decision mapper requires target_value positions; "
15- f"position { position .symbol !r} is missing target_value"
16- )
17- target_values [position .symbol ] = float (position .target_value )
18- return target_values
19-
20-
21- def _symbols_by_role (decision : StrategyDecision ) -> tuple [list [str ], list [str ], list [str ]]:
22- risk_symbols : list [str ] = []
23- income_symbols : list [str ] = []
24- safe_haven_symbols : list [str ] = []
25- target_values = _target_values (decision )
26- for position in decision .positions :
27- if position .role == "safe_haven" :
28- safe_haven_symbols .append (position .symbol )
29- elif position .role == "income" :
30- income_symbols .append (position .symbol )
31- else :
32- risk_symbols .append (position .symbol )
33- risk_symbols = sorted (dict .fromkeys (risk_symbols ))
34- income_symbols = sorted (
35- dict .fromkeys (income_symbols ),
36- key = lambda symbol : (- target_values .get (symbol , 0.0 ), symbol ),
37- )
38- safe_haven_symbols = sorted (dict .fromkeys (safe_haven_symbols ))
39- return risk_symbols , income_symbols , safe_haven_symbols
6+ from quant_platform_kit .strategy_contracts import (
7+ StrategyDecision ,
8+ build_value_target_execution_annotations ,
9+ build_value_target_execution_plan ,
10+ build_value_target_plan_payload ,
11+ build_value_target_portfolio_plan ,
12+ )
4013
4114
4215def map_strategy_decision_to_plan (
@@ -45,38 +18,68 @@ def map_strategy_decision_to_plan(
4518 account_state : Mapping [str , Any ],
4619 strategy_profile : str ,
4720) -> dict [str , Any ]:
48- diagnostics = dict (decision .diagnostics )
49- target_values = _target_values (decision )
50- risk_symbols , income_symbols , safe_haven_symbols = _symbols_by_role (decision )
51- strategy_assets = tuple (risk_symbols + safe_haven_symbols + income_symbols )
52- portfolio_rows = tuple (
53- row
54- for row in (
55- tuple (risk_symbols ),
56- tuple (income_symbols ),
57- tuple (safe_haven_symbols ),
58- )
59- if row
21+ execution_plan = build_value_target_execution_plan (
22+ decision ,
23+ strategy_profile = strategy_profile ,
24+ )
25+ annotations = build_value_target_execution_annotations (decision )
26+ portfolio_plan = build_value_target_portfolio_plan (
27+ execution_plan ,
28+ market_values = dict (account_state ["market_values" ]),
29+ quantities = dict (account_state ["quantities" ]),
30+ sellable_quantities = dict (account_state ["sellable_quantities" ]),
31+ total_equity = float (account_state ["total_strategy_equity" ]),
32+ liquid_cash = float (account_state ["available_cash" ]),
33+ strategy_symbols_order = "risk_safe_income" ,
34+ portfolio_rows_layout = ("risk" , "income" , "safe" ),
35+ )
36+ plan = build_value_target_plan_payload (
37+ strategy_profile = strategy_profile ,
38+ portfolio_plan = portfolio_plan ,
39+ annotations = annotations ,
40+ include_sellable_quantities = True ,
41+ execution_fields = (
42+ "trade_threshold_value" ,
43+ "signal_display" ,
44+ "status_display" ,
45+ "deploy_ratio_text" ,
46+ "income_ratio_text" ,
47+ "income_locked_ratio_text" ,
48+ "active_risk_asset" ,
49+ "investable_cash" ,
50+ "current_min_trade" ,
51+ ),
52+ execution_defaults = {
53+ "signal_display" : "" ,
54+ "status_display" : "" ,
55+ "deploy_ratio_text" : "" ,
56+ "income_ratio_text" : "" ,
57+ "income_locked_ratio_text" : "" ,
58+ "current_min_trade" : 0.0 ,
59+ "investable_cash" : portfolio_plan .liquid_cash ,
60+ },
6061 )
62+ risk_symbols = list (portfolio_plan .risk_symbols )
63+ income_symbols = list (portfolio_plan .income_symbols )
6164
62- return {
63- "strategy_profile" : strategy_profile ,
64- "strategy_assets" : strategy_assets ,
65+ plan .update ({
66+ "strategy_assets" : portfolio_plan .strategy_symbols ,
6567 "limit_order_symbols" : tuple (risk_symbols + income_symbols ),
66- "portfolio_rows" : portfolio_rows ,
67- "available_cash" : float (account_state ["available_cash" ]),
68- "market_values" : dict (account_state ["market_values" ]),
69- "quantities" : dict (account_state ["quantities" ]),
70- "sellable_quantities" : dict (account_state ["sellable_quantities" ]),
71- "total_strategy_equity" : float (account_state ["total_strategy_equity" ]),
72- "current_min_trade" : float (diagnostics ["current_min_trade" ]),
73- "targets" : target_values ,
74- "market_status" : diagnostics ["market_status" ],
75- "signal_message" : diagnostics ["signal_message" ],
76- "deploy_ratio_text" : diagnostics ["deploy_ratio_text" ],
77- "income_ratio_text" : diagnostics ["income_ratio_text" ],
78- "income_locked_ratio_text" : diagnostics ["income_locked_ratio_text" ],
79- "active_risk_asset" : diagnostics .get ("active_risk_asset" ),
80- "investable_cash" : float (diagnostics ["investable_cash" ]),
81- "threshold_value" : float (diagnostics ["threshold_value" ]),
82- }
68+ "portfolio_rows" : portfolio_plan .portfolio_rows ,
69+ "available_cash" : portfolio_plan .liquid_cash ,
70+ "market_values" : dict (portfolio_plan .market_values ),
71+ "quantities" : dict (portfolio_plan .quantities ),
72+ "sellable_quantities" : dict (portfolio_plan .sellable_quantities or {}),
73+ "total_strategy_equity" : portfolio_plan .total_equity ,
74+ "current_min_trade" : float (annotations .current_min_trade or 0.0 ),
75+ "targets" : dict (portfolio_plan .target_values ),
76+ "market_status" : annotations .status_display or "" ,
77+ "signal_message" : annotations .signal_display or "" ,
78+ "deploy_ratio_text" : annotations .deploy_ratio_text or "" ,
79+ "income_ratio_text" : annotations .income_ratio_text or "" ,
80+ "income_locked_ratio_text" : annotations .income_locked_ratio_text or "" ,
81+ "active_risk_asset" : annotations .active_risk_asset ,
82+ "investable_cash" : float (annotations .investable_cash or portfolio_plan .liquid_cash ),
83+ "threshold_value" : float (annotations .trade_threshold_value ),
84+ })
85+ return plan
0 commit comments