@@ -271,6 +271,28 @@ def _build_hold_current_value_decision(portfolio_inputs) -> StrategyDecision:
271271 return StrategyDecision (positions = tuple (positions ))
272272
273273
274+ def _build_zero_equity_value_decision (decision : StrategyDecision ) -> StrategyDecision :
275+ positions : list [PositionTarget ] = []
276+ for position in decision .positions :
277+ positions .append (
278+ PositionTarget (
279+ symbol = position .symbol ,
280+ target_value = 0.0 ,
281+ role = position .role or _symbol_role (position .symbol ),
282+ order_preference = position .order_preference ,
283+ )
284+ )
285+ return StrategyDecision (
286+ positions = tuple (positions ),
287+ budgets = decision .budgets ,
288+ risk_flags = tuple (dict .fromkeys ((* decision .risk_flags , "no_execute" ))),
289+ diagnostics = {
290+ ** dict (decision .diagnostics ),
291+ "execution_blocked_reason" : "non_positive_total_equity" ,
292+ },
293+ )
294+
295+
274296def _normalize_to_value_target_decision (
275297 decision : StrategyDecision ,
276298 * ,
@@ -284,14 +306,24 @@ def _normalize_to_value_target_decision(
284306 return decision , None
285307
286308 if target_mode == "weight" and not no_execute :
309+ total_equity = float (portfolio_inputs .total_equity )
310+ if total_equity <= 0.0 :
311+ return _build_zero_equity_value_decision (
312+ decision ,
313+ ), _build_weight_translation_annotations (
314+ decision ,
315+ total_equity = total_equity ,
316+ liquid_cash = float (portfolio_inputs .liquid_cash ),
317+ runtime_metadata = runtime_metadata ,
318+ )
287319 translated = translate_decision_to_target_mode (
288320 decision ,
289321 target_mode = "value" ,
290- total_equity = float ( portfolio_inputs . total_equity ) ,
322+ total_equity = total_equity ,
291323 )
292324 return translated , _build_weight_translation_annotations (
293325 decision ,
294- total_equity = float ( portfolio_inputs . total_equity ) ,
326+ total_equity = total_equity ,
295327 liquid_cash = float (portfolio_inputs .liquid_cash ),
296328 runtime_metadata = runtime_metadata ,
297329 )
0 commit comments