Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions alloc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,21 @@ def print_results(result: "WorkflowResult") -> None:
if alloc:
logger.info("Recommended allocation: %s", alloc)

# Recommended trades
trades = best.recommended_trades
if trades:
logger.info("Recommended trades:")
for trade in trades:
ticker = trade.get("ticker", "?")
action = trade.get("action", "hold").upper()
alloc_w = trade.get("allocation", 0.0)
change = trade.get("change", 0.0)
sign = "+" if change >= 0 else ""
logger.info(
" %-8s %s alloc=%.4f change=%s%.4f",
ticker, action, alloc_w, sign, change,
)

# Concentration
conc = result.concentration
if conc:
Expand Down
70 changes: 68 additions & 2 deletions alloc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,8 +891,73 @@ def _trainer(

# Final allocation
allocation: list[float] = []
if results.get("allocation_history"):
allocation = results["allocation_history"][-1].tolist()
allocation_history = results.get("allocation_history", [])
if allocation_history:
last_alloc = allocation_history[-1]
if isinstance(last_alloc, dict):
allocation = (
[last_alloc.get(t, 0.0) for t in tickers]
+ [last_alloc.get("cash", 0.0)]
)
else:
if hasattr(last_alloc, "tolist"):
allocation = last_alloc.tolist()
else:
allocation = list(last_alloc)

# Derive recommended_trades from allocation_history
recommended_trades: list[dict] | None = None
if len(allocation_history) >= 2:
prev_alloc = allocation_history[-2]
curr_alloc = allocation_history[-1]
if isinstance(prev_alloc, dict) and isinstance(curr_alloc, dict):
recommended_trades = []
for t in tickers:
prev_w = prev_alloc.get(t, 0.0)
curr_w = curr_alloc.get(t, 0.0)
change = curr_w - prev_w
if abs(change) < 1e-6:
action = "hold"
elif change > 0:
action = "buy"
else:
action = "sell"
recommended_trades.append({
"ticker": t,
"action": action,
"allocation": round(curr_w, 6),
"change": round(change, 6),
})
# Include cash
prev_cash = prev_alloc.get("cash", 0.0)
curr_cash = curr_alloc.get("cash", 0.0)
cash_change = curr_cash - prev_cash
if abs(cash_change) < 1e-6:
cash_action = "hold"
elif cash_change > 0:
cash_action = "buy"
else:
cash_action = "sell"
recommended_trades.append({
"ticker": "cash",
"action": cash_action,
"allocation": round(curr_cash, 6),
"change": round(cash_change, 6),
})
elif len(allocation_history) == 1:
# Only one allocation — derive from final_holdings
final_holdings = results.get("final_holdings", {})
if final_holdings:
recommended_trades = []
for t in tickers:
shares = final_holdings.get(t, 0)
action = "buy" if shares > 0 else "hold"
recommended_trades.append({
"ticker": t,
"action": action,
"allocation": 0.0,
"change": 0.0,
})

return {
"sharpe_ratio": sharpe_ratio,
Expand All @@ -901,6 +966,7 @@ def _trainer(
"model_roi": model_roi,
"buyhold_roi": buyhold_roi,
"allocation": allocation,
"recommended_trades": recommended_trades,
"model_path": None,
"results_path": None,
"update": update_iterations,
Expand Down
7 changes: 7 additions & 0 deletions alloc/utils/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ class TrainingTrial:
Buy-and-hold return on investment.
allocation : list[float]
Final allocation weights (one per ticker, plus cash).
recommended_trades : list[dict] | None
List of recommended trade actions derived from the final
allocation step. Each dict has keys ``ticker``, ``action``
(``"buy"`` / ``"sell"`` / ``"hold"``), ``allocation`` (target
weight), and ``change`` (delta vs. previous allocation).
model_path : str | None
Path to the saved model file.
results_path : str | None
Expand All @@ -109,6 +114,7 @@ class TrainingTrial:
model_roi: float | None = None
buyhold_roi: float | None = None
allocation: list[float] = field(default_factory=list)
recommended_trades: list[dict] | None = None
model_path: str | None = None
results_path: str | None = None

Expand Down Expand Up @@ -267,6 +273,7 @@ def _run_trial(self, trial_num: int) -> TrainingTrial:
model_roi=result.get("model_roi"),
buyhold_roi=result.get("buyhold_roi"),
allocation=result.get("allocation", []),
recommended_trades=result.get("recommended_trades"),
model_path=result.get("model_path"),
results_path=result.get("results_path"),
)
Expand Down
Loading
Loading