-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
350 lines (303 loc) · 12.9 KB
/
run.py
File metadata and controls
350 lines (303 loc) · 12.9 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
"""
Orchestrator entrypoint: run regime → portfolio → allocation in one shot.
Usage (from QuantTradingOS repo root):
python -m orchestrator.run
python -m orchestrator.run --prices path/to/prices.csv --holdings path/to/holdings.csv
Expects sibling agent dirs: Market-Regime-Agent, Capital-Allocation-Agent, Portfolio-Analyst-Agent.
"""
from __future__ import annotations
import argparse
import dataclasses
import json
import sys
from pathlib import Path
from datetime import datetime
import pandas as pd
DISCIPLINE_CACHE_FILENAME = "discipline_last_score.json"
# Repo root = parent of orchestrator/
ROOT = Path(__file__).resolve().parent.parent
# Add agent roots to path so we can import them
def _setup_paths(extra: list[str] | None = None) -> None:
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
names = ["Capital-Allocation-Agent", "Market-Regime-Agent", "Portfolio-Analyst-Agent"]
if extra:
names = list(names) + list(extra)
for name in names:
agent_root = ROOT / name
if agent_root.exists() and str(agent_root) not in sys.path:
sys.path.insert(0, str(agent_root))
def load_prices(csv_path: Path) -> pd.DataFrame:
"""Load wide-format CSV with date column and ticker columns (e.g. SPY, QQQ, TLT)."""
df = pd.read_csv(csv_path)
if "date" not in df.columns and "Date" in df.columns:
df = df.rename(columns={"Date": "date"})
df["date"] = pd.to_datetime(df["date"]).dt.strftime("%Y-%m-%d")
return df
def prices_for_portfolio(prices_wide: pd.DataFrame) -> pd.DataFrame:
"""Convert regime-style prices (date column + ticker columns) to index-by-date for compute_portfolio."""
out = prices_wide.copy()
out["date"] = pd.to_datetime(out["date"])
out = out.set_index("date").sort_index()
return out
def run_portfolio(holdings_path: Path, prices_df: pd.DataFrame) -> dict:
"""
Run Portfolio-Analyst-Agent compute_portfolio(holdings_df, prices_df).
If holdings symbols are not all in prices_df, fetch portfolio prices via yfinance.
"""
_setup_paths()
from app import compute_portfolio, fetch_prices # Portfolio-Analyst-Agent
holdings_df = pd.read_csv(holdings_path)
if "symbol" not in holdings_df.columns or "shares" not in holdings_df.columns:
raise ValueError("Holdings CSV must have columns: symbol, shares (optionally avg_price)")
symbols = holdings_df["symbol"].tolist()
missing = [s for s in symbols if s not in prices_df.columns]
if missing:
# Fetch portfolio symbols' prices (e.g. when regime CSV has SPY/QQQ/TLT but portfolio has AAPL/NVDA)
prices_df = fetch_prices(symbols)
if prices_df.empty:
raise ValueError("Could not fetch prices for portfolio symbols")
else:
# Use subset of regime prices for portfolio symbols only
prices_df = prices_df[[c for c in symbols if c in prices_df.columns]].copy()
if prices_df.empty:
prices_df = fetch_prices(symbols)
result = compute_portfolio(holdings_df, prices_df)
# Build dict expected by adapters
holdings = result["holdings"]
total_value = result["total_value"]
vol_annual = result.get("vol_annual") or 0.0
ret_total = result.get("ret_total") or 0.0
return {
"total_value": total_value,
"vol_annual": vol_annual,
"ret_total": ret_total,
"holdings": holdings,
"open_positions": len(holdings),
"total_exposure": total_value,
"portfolio_heat": min(1.0, vol_annual * 0.6) if vol_annual and not pd.isna(vol_annual) else 0.0,
}
def run_regime(prices: pd.DataFrame, state_dir: Path, skill_context: str | None = None):
"""Run Market-Regime-Agent; return RegimeDecision as dict."""
_setup_paths()
from src.agent import MarketRegimeAgent # Market-Regime-Agent
memory_path = str(state_dir / "regime_memory.json")
state_dir.mkdir(parents=True, exist_ok=True)
agent = MarketRegimeAgent(memory_path=memory_path)
# skill_context is available for future agent use; not passed to avoid signature change
decision, action = agent.run(prices)
return dataclasses.asdict(decision)
def run_allocation(inputs: dict, config_path: Path, skill_context: str | None = None) -> dict:
"""Run Capital-Allocation-Agent; return decision dict."""
_setup_paths()
from agent import CapitalAllocationAgent # Capital-Allocation-Agent
agent = CapitalAllocationAgent(config_path=str(config_path))
# skill_context is available for future agent use; not passed to avoid signature change
return agent.decide(inputs)
def get_cached_discipline_score(state_dir: Path) -> float | None:
"""Return last cached compliance score (0..1) if present, else None."""
path = state_dir / DISCIPLINE_CACHE_FILENAME
if not path.exists():
return None
try:
data = json.loads(path.read_text())
return float(data.get("compliance_score"))
except (json.JSONDecodeError, TypeError, KeyError):
return None
def set_cached_discipline_score(state_dir: Path, score: float) -> None:
"""Persist compliance score so pipeline can use it when trades+plan not provided."""
state_dir.mkdir(parents=True, exist_ok=True)
path = state_dir / DISCIPLINE_CACHE_FILENAME
path.write_text(json.dumps({
"compliance_score": score,
"asof": datetime.utcnow().isoformat() + "Z",
}, indent=2))
def _run_execution_discipline(trades_path: Path, plan_path: Path, regime_label: str, state_dir: Path) -> float:
"""Run Execution-Discipline-Agent; return compliance_score (0..1)."""
_setup_paths(extra=["Execution-Discipline-Agent"])
import pandas as pd
import json
from src.agent import ExecutionDisciplineAgent # Execution-Discipline-Agent
trades = pd.read_csv(trades_path)
plan = json.loads(plan_path.read_text())
memory_path = str(state_dir / "discipline_memory.json")
state_dir.mkdir(parents=True, exist_ok=True)
agent = ExecutionDisciplineAgent(memory_path=memory_path)
report = agent.run(trades, plan, regime_label)
return report.compliance_score
def _run_guardian(
total_value: float,
drawdown_pct: float,
regime_label: str,
volatility: str = "normal",
trade_type: str = "swing",
recent_losses: int = 0,
price: float | None = None,
atr: float | None = None,
skill_context: str | None = None,
) -> dict:
"""Run Capital-Guardian-Agent; return decision + optional stop_distance and position_size."""
_setup_paths(extra=["Capital-Guardian-Agent"])
from schemas import TradeInput # Capital-Guardian-Agent
from risk_engine import CapitalGuardian
from stop_loss import calculate_stop_loss
from position_sizer import calculate_position_size
# Map regime label to guardian regime (trend, range, choppy)
r = regime_label.upper()
if "HIGH VOL" in r or "CHOP" in r or "RANGING" in r:
regime = "choppy"
elif "UPTREND" in r or "DOWNTREND" in r:
regime = "trend"
else:
regime = "range"
trade = TradeInput(
account_size=total_value,
drawdown_pct=drawdown_pct,
volatility=volatility,
regime=regime,
trade_type=trade_type,
recent_losses=recent_losses,
)
guardian = CapitalGuardian()
decision = guardian.evaluate(trade)
out = {"guardian_decision": decision}
if price is not None and atr is not None and decision.get("trade_allowed"):
stop = calculate_stop_loss(atr, regime, trade_type)
size = calculate_position_size(total_value, decision["allowed_risk_pct"], stop, price)
out["stop_distance"] = stop
out["position_size"] = size
return out
def run_pipeline(
prices_path: Path,
holdings_path: Path,
config_path: Path,
state_dir: Path,
peak_equity: float | None = None,
execution_score: float = 0.88,
execution_trades_path: Path | None = None,
execution_plan_path: Path | None = None,
include_guardian: bool = False,
guardian_price: float | None = None,
guardian_atr: float | None = None,
regime_skill_context: str | None = None,
allocation_skill_context: str | None = None,
guardian_skill_context: str | None = None,
) -> dict:
"""
Run full pipeline: regime → portfolio → [execution-discipline] → allocation → [guardian].
Returns dict with keys: regime, portfolio, decision, and optionally execution_discipline_score, guardian_guardrails.
"""
from orchestrator.adapters import build_capital_allocation_inputs
prices = load_prices(prices_path)
prices_df = prices_for_portfolio(prices)
regime_output = run_regime(prices, state_dir, skill_context=regime_skill_context)
portfolio_output = run_portfolio(holdings_path, prices_df)
regime_label = regime_output.get("label", "Unknown")
# Execution discipline: real score if trades+plan provided; else use cached score if any
if execution_trades_path and execution_plan_path and execution_trades_path.exists() and execution_plan_path.exists():
execution_score = _run_execution_discipline(
execution_trades_path, execution_plan_path, regime_label, state_dir
)
set_cached_discipline_score(state_dir, execution_score)
else:
cached = get_cached_discipline_score(state_dir)
if cached is not None:
execution_score = cached
peak = peak_equity or (portfolio_output["total_value"] * 1.1)
inputs = build_capital_allocation_inputs(
market_regime_output=regime_output,
portfolio_analyst_output=portfolio_output,
execution_discipline_score=execution_score,
peak_equity=peak,
)
decision = run_allocation(inputs, config_path, skill_context=allocation_skill_context)
result = {
"regime": regime_output,
"portfolio": {
"total_value": portfolio_output["total_value"],
"vol_annual": portfolio_output["vol_annual"],
"open_positions": portfolio_output["open_positions"],
"portfolio_heat": portfolio_output["portfolio_heat"],
},
"decision": decision,
"execution_discipline_score": execution_score,
}
if include_guardian:
drawdown_pct = (peak - portfolio_output["total_value"]) / peak * 100.0 if peak > 0 else 0.0
guardian_result = _run_guardian(
total_value=portfolio_output["total_value"],
drawdown_pct=drawdown_pct,
regime_label=regime_label,
price=guardian_price,
atr=guardian_atr,
skill_context=guardian_skill_context,
)
result["guardian_guardrails"] = guardian_result
return result
def main() -> int:
parser = argparse.ArgumentParser(description="Run regime → allocation pipeline")
parser.add_argument(
"--prices",
type=Path,
default=ROOT / "Market-Regime-Agent" / "data" / "sample_prices.csv",
help="Path to prices CSV (date + ticker columns)",
)
parser.add_argument(
"--state-dir",
type=Path,
default=ROOT / "orchestrator" / "state",
help="Directory for regime memory etc.",
)
parser.add_argument(
"--config",
type=Path,
default=ROOT / "Capital-Allocation-Agent" / "config.yaml",
help="Path to Capital-Allocation-Agent config",
)
parser.add_argument(
"--holdings",
type=Path,
default=ROOT / "Portfolio-Analyst-Agent" / "portfolio.csv",
help="Path to holdings CSV (symbol, shares [, avg_price])",
)
parser.add_argument(
"--peak-equity",
type=float,
default=None,
help="Peak equity for drawdown (default: 1.1 * total_value)",
)
args = parser.parse_args()
if not args.prices.exists():
print(f"Error: prices file not found: {args.prices}", file=sys.stderr)
return 1
if not args.config.exists():
print(f"Error: config not found: {args.config}", file=sys.stderr)
return 1
if not args.holdings.exists():
print(f"Error: holdings file not found: {args.holdings}", file=sys.stderr)
return 1
print("Running Market-Regime-Agent...")
result = run_pipeline(
prices_path=args.prices,
holdings_path=args.holdings,
config_path=args.config,
state_dir=args.state_dir,
peak_equity=args.peak_equity,
)
regime_output = result["regime"]
portfolio_output = result["portfolio"]
decision = result["decision"]
print(f" regime label: {regime_output.get('label', '?')}")
print(f" confidence: {regime_output.get('confidence', 0):.2f}")
print("\nRunning Portfolio-Analyst-Agent (compute_portfolio)...")
print(f" total_value: {portfolio_output['total_value']:,.0f}")
print(f" vol_annual: {portfolio_output['vol_annual']:.2f}")
print(f" open_positions: {portfolio_output['open_positions']}")
print("\nRunning Capital-Allocation-Agent...")
print("\n--- Capital Allocation Decision ---")
for k, v in decision.items():
print(f" {k}: {v}")
print()
return 0
if __name__ == "__main__":
sys.exit(main())