-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.py
More file actions
304 lines (284 loc) · 13.4 KB
/
Copy pathrunner.py
File metadata and controls
304 lines (284 loc) · 13.4 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
from __future__ import annotations
import hashlib
import json
from typing import Any
from tradearena.core.domain import (
AgentProtocolTrace,
ExperimentConfig,
PortfolioState,
ReproducibilityState,
RiskCheck,
RiskPhase,
RiskReport,
ToolCallRecord,
)
from tradearena.core.interfaces import (
AnalystAgent,
Evaluator,
ExecutionAgent,
MarketDataProvider,
MemoryStore,
OrderSimulator,
RiskManagerAgent,
StrategyAgent,
)
from tradearena.core.serialization import to_jsonable
from tradearena.core.trajectory import StepRecord, Trajectory
class TradeArena:
"""Composable experiment runner for one financial-agent stack."""
def __init__(
self,
config: ExperimentConfig,
data_provider: MarketDataProvider,
analysts: list[AnalystAgent],
strategy: StrategyAgent,
risk_manager: RiskManagerAgent,
execution_agent: ExecutionAgent,
order_simulator: OrderSimulator,
memory: MemoryStore,
evaluators: list[Evaluator],
) -> None:
self.config = config
self.data_provider = data_provider
self.analysts = analysts
self.strategy = strategy
self.risk_manager = risk_manager
self.execution_agent = execution_agent
self.order_simulator = order_simulator
self.memory = memory
self.evaluators = evaluators
def run(self) -> tuple[Trajectory, dict[str, float | int | str]]:
portfolio = PortfolioState(cash=self.config.initial_cash)
trajectory = Trajectory(
experiment_name=self.config.name,
seed=self.config.seed,
metadata={
"data_provider": self.data_provider.name,
"analysts": [agent.name for agent in self.analysts],
"strategy": self.strategy.name,
"risk_manager": self.risk_manager.name,
"execution_agent": self.execution_agent.name,
"order_simulator": self.order_simulator.name,
},
)
warm_start_pending = dict(getattr(self.config, "initial_holdings_weights", {}) or {})
for snapshot in self.data_provider.stream():
portfolio.last_prices.update({symbol: bar.close for symbol, bar in snapshot.bars.items()})
if warm_start_pending:
self._seed_warm_start(portfolio, snapshot, warm_start_pending)
warm_start_pending = {}
before_memory = len(getattr(self.memory, "events", []))
memory_digest_before = self._memory_digest()
signals = []
for analyst in self.analysts:
signals.extend(analyst.analyze(snapshot, portfolio.copy(), self.memory))
tool_outputs = (
ToolCallRecord(
tool_name="analyst_stack",
inputs={"analysts": [agent.name for agent in self.analysts]},
outputs={"signals": [to_jsonable(signal) for signal in signals]},
timestamp=snapshot.timestamp,
),
)
decisions = self.strategy.decide(snapshot, signals, portfolio.copy(), self.memory)
approved = self.risk_manager.approve(snapshot, decisions, portfolio.copy(), self.memory)
risk_report = getattr(self.risk_manager, "last_report", {})
orders = self.execution_agent.create_orders(snapshot, approved, portfolio.copy())
fills = self.order_simulator.execute(snapshot, orders, portfolio)
execution_report = getattr(self.order_simulator, "last_report", {})
in_trade_report = self.risk_manager.monitor(snapshot, orders, fills, portfolio.copy(), self.memory)
attribution = self.risk_manager.attribute(snapshot, fills, portfolio.copy(), self.memory)
post_trade_report = RiskReport(
timestamp=snapshot.timestamp,
checks=(RiskCheck(name="post_trade_attribution", passed=True, severity="info", message="post-trade attribution recorded"),),
approved_count=len(approved),
blocked_count=0,
clipped_count=0,
phase=RiskPhase.POST_TRADE,
budget=getattr(self.risk_manager, "budget", lambda: None)(),
attribution=attribution,
)
reproducibility_state = self._reproducibility_state(
snapshot=snapshot,
portfolio=portfolio.copy(),
memory_digest=memory_digest_before,
tool_outputs=tool_outputs,
)
agent_trace = self._agent_trace(
snapshot=snapshot,
signals=signals,
decisions=decisions,
approved=approved,
orders=orders,
fills=fills,
risk_report=risk_report,
in_trade_report=in_trade_report,
post_trade_report=post_trade_report,
execution_report=execution_report,
reproducibility_state=reproducibility_state,
)
risk_violations: list[Any] = []
for report in (risk_report, in_trade_report, post_trade_report):
risk_violations.extend(getattr(report, "violations", []) or [])
self.memory.record(
"step",
{
"timestamp": snapshot.timestamp,
"reproducibility_state": reproducibility_state,
"agent_trace": agent_trace,
"signals": signals,
"decisions": decisions,
"approved_decisions": approved,
"risk_report": risk_report,
"in_trade_report": in_trade_report,
"post_trade_report": post_trade_report,
"risk_violations": risk_violations,
"orders": orders,
"fills": fills,
"execution_report": execution_report,
"equity": portfolio.equity(),
},
)
memory_events = getattr(self.memory, "events", [])[before_memory:]
trajectory.append(
StepRecord(
timestamp=snapshot.timestamp,
observation={
"prices": {symbol: bar.close for symbol, bar in snapshot.bars.items()},
"news_count": len(snapshot.news),
"macro_count": len(snapshot.macro),
"filings_count": len(getattr(snapshot, "filings", ())),
"alt_data_count": len(snapshot.alt_data),
},
signals=[to_jsonable(signal) for signal in signals],
decisions=[to_jsonable(decision) for decision in decisions],
approved_decisions=[to_jsonable(decision) for decision in approved],
orders=[to_jsonable(order) for order in orders],
fills=[to_jsonable(fill) for fill in fills],
portfolio={
"cash": portfolio.cash,
"positions": dict(portfolio.positions),
"last_prices": dict(portfolio.last_prices),
"equity": portfolio.equity(),
},
reproducibility_state=to_jsonable(reproducibility_state),
agent_trace=to_jsonable(agent_trace),
risk_report=to_jsonable(risk_report),
in_trade_report=to_jsonable(in_trade_report),
post_trade_report=to_jsonable(post_trade_report),
execution_report=to_jsonable(execution_report),
risk_violations=[to_jsonable(violation) for violation in risk_violations],
memory_events=[to_jsonable(event) for event in memory_events],
)
)
metrics: dict[str, float | int | str] = {}
for evaluator in self.evaluators:
metrics.update(evaluator.evaluate(trajectory))
return trajectory, metrics
def _seed_warm_start(self, portfolio, snapshot, weights: dict[str, float]) -> None:
"""Seed the portfolio with target-weight holdings at first-bar prices,
free of execution cost, so initial-construction friction is not charged
to the agent (anchor robustness check)."""
equity = portfolio.cash
for symbol, weight in weights.items():
price = snapshot.bars[symbol].close if symbol in snapshot.bars else 0.0
if price <= 0.0:
continue
notional = equity * float(weight)
qty = notional / price
portfolio.positions[symbol] = portfolio.positions.get(symbol, 0.0) + qty
portfolio.cash -= notional
def _memory_digest(self) -> str:
events = getattr(self.memory, "events", [])
payload = to_jsonable(events[-10:])
encoded = json.dumps(payload, sort_keys=True, default=str).encode("utf-8")
return hashlib.sha256(encoded).hexdigest()
def _risk_budget(self):
budget_fn = getattr(self.risk_manager, "budget", None)
return budget_fn() if callable(budget_fn) else None
def _simulator_state(self) -> dict[str, object]:
return {
"name": self.order_simulator.name,
"pending_orders": len(getattr(self.order_simulator, "_pending", [])),
"step": getattr(self.order_simulator, "_step", None),
"sequence": getattr(self.order_simulator, "_sequence", None),
}
def _reproducibility_state(self, snapshot, portfolio, memory_digest: str, tool_outputs: tuple[ToolCallRecord, ...]) -> ReproducibilityState:
return ReproducibilityState(
prompt_version=str(self.config.metadata.get("prompt_version", "baseline-v0")),
model_version=str(self.config.metadata.get("model_version", "deterministic-baseline")),
retrieved_documents=tuple(self.config.metadata.get("retrieved_documents", ())),
market_data_timestamp=snapshot.timestamp,
tool_outputs=tool_outputs,
memory_digest=memory_digest,
risk_constraints=self._risk_budget(),
portfolio_state={
"cash": portfolio.cash,
"positions": dict(portfolio.positions),
"last_prices": dict(portfolio.last_prices),
"equity": portfolio.equity(),
},
agent_discussion_history=tuple(self.config.metadata.get("agent_discussion_history", ())),
execution_simulator_state=self._simulator_state(),
random_seed=self.config.seed,
)
def _agent_trace(
self,
*,
snapshot,
signals,
decisions,
approved,
orders,
fills,
risk_report,
in_trade_report,
post_trade_report,
execution_report,
reproducibility_state,
) -> AgentProtocolTrace:
schemas = {
"observation": {
"timestamp": "datetime",
"bars": "dict[str, Bar]",
"news": "tuple[NewsItem]",
"macro": "tuple[MacroPoint]",
"filings": "tuple[FilingItem]",
"alt_data": "dict[str, Any]",
},
"memory": {"digest": "sha256", "events": "append-only journal"},
"tool": {"tool_name": "str", "inputs": "dict", "outputs": "dict", "status": "str"},
"action": {"decision": "target_weight", "order": "side/quantity/type/limit"},
"risk": {"budget": "RiskBudget", "checks": "RiskCheck[]", "violations": "RiskViolation[]"},
"trajectory": {"observe": "dict", "plan": "dict", "act": "fills", "reflect": "post-trade attribution"},
"evaluation": {"performance": "returns/risk", "execution": "costs/fills", "audit": "coverage/violations"},
}
return AgentProtocolTrace(
observation_schema=schemas["observation"],
memory_schema=schemas["memory"],
tool_schema=schemas["tool"],
action_schema=schemas["action"],
risk_schema=schemas["risk"],
trajectory_schema=schemas["trajectory"],
evaluation_schema=schemas["evaluation"],
observe={
"timestamp": snapshot.timestamp,
"symbols": tuple(snapshot.bars),
"news_count": len(snapshot.news),
"macro_count": len(snapshot.macro),
"filings_count": len(getattr(snapshot, "filings", ())),
"alt_data_count": len(snapshot.alt_data),
"memory_digest": reproducibility_state.memory_digest,
},
plan={
"analysts": [agent.name for agent in self.analysts],
"strategy": self.strategy.name,
"signals": signals,
"decisions": decisions,
},
propose_order={"execution_agent": self.execution_agent.name, "orders": orders},
risk_report={"pre_trade": risk_report, "in_trade": in_trade_report, "post_trade": post_trade_report},
revise={"approved_decisions": approved, "revisions": [decision.metadata for decision in approved]},
act={"simulator": self.order_simulator.name, "fills": fills, "execution_report": execution_report},
reflect={"post_trade_attribution": post_trade_report.attribution, "portfolio_equity": reproducibility_state.portfolio_state["equity"]},
)