-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloop.py
More file actions
309 lines (274 loc) · 12 KB
/
Copy pathloop.py
File metadata and controls
309 lines (274 loc) · 12 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
"""The deterministic runtime step, in Senpi's order:
1. **reconcile** — positions the venue no longer shows are ``closed_externally``;
2. **exits** — close requests from the source (if it makes any) first, then every tracked
position runs one DSL tick; a close reason closes it, otherwise a moved floor updates
the exchange stop;
3. **entries** — signals go through expiry → dedup → slots/held → account gates →
asset gate → sizing → order; every rejection is emitted with its reason code.
``Engine`` owns mutable bookkeeping; all decisions are made by the pure modules around it.
"""
from __future__ import annotations
from collections.abc import Mapping
from dataclasses import dataclass, field
from typing import Any
from hl_agent.data.models import AccountState
from hl_agent.engine.config import DslConfig, GuardRails, StrategyConfig
from hl_agent.engine.dedup import DedupState
from hl_agent.engine.dsl import CloseReason, DslState, roe_pct, stop_price, tick
from hl_agent.engine.guardrails import GateReason, GuardRailState, check_liquidity
from hl_agent.engine.ports import Broker, ExitSource, Fill, MarketView, SignalSource
from hl_agent.engine.signals import Signal
from hl_agent.engine.sizing import OrderPlan, plan_order
DEFAULT_DEDUP_WINDOW_MS = 4 * 3_600_000
@dataclass(frozen=True, slots=True)
class Event:
time_ms: int
kind: str # "opened" | "closed" | "rejected" | "stop_moved"
asset: str
reason: str
payload: Mapping[str, Any] = field(default_factory=dict)
@dataclass(frozen=True, slots=True)
class Tracked:
signal: Signal
dsl: DslState
size: float
stop_price: float
fees_usd: float
@dataclass(frozen=True, slots=True)
class EngineConfig:
strategy: StrategyConfig
dsl: DslConfig
rails: GuardRails = field(default_factory=GuardRails)
dedup_window_ms: int = DEFAULT_DEDUP_WINDOW_MS
banned_assets: frozenset[str] = frozenset()
class Engine:
def __init__(
self,
cfg: EngineConfig,
market: MarketView,
broker: Broker,
source: SignalSource,
*,
now_ms: int,
exits: ExitSource | None = None,
) -> None:
self._cfg = cfg
self._market = market
self._broker = broker
self._source = source
self._exits = exits
self._positions: dict[str, Tracked] = {}
self._rails = GuardRailState.start(now_ms, market.account().account_value)
self._dedup = DedupState()
# ---- read-only views -------------------------------------------------------
@property
def positions(self) -> Mapping[str, Tracked]:
return dict(self._positions)
@property
def rails(self) -> GuardRailState:
return self._rails
# ---- the step ----------------------------------------------------------------
def step(self, now_ms: int) -> list[Event]:
events: list[Event] = []
account = self._market.account()
self._rails = self._rails.observe(self._cfg.rails, now_ms, account.account_value)
self._reconcile(account, now_ms, events)
self._run_requested_exits(now_ms, events)
self._run_exits(now_ms, events)
for signal in self._source.signals(now_ms):
if self._try_enter(signal, account, now_ms, events):
account = self._market.account() # margin moved; size the next one honestly
return events
def close_all(self, now_ms: int, reason: CloseReason = CloseReason.MANUAL_CLOSE) -> list[Event]:
"""Flatten every tracked position (kill switch, end of a backtest)."""
events: list[Event] = []
for asset in list(self._positions):
fill = self._broker.close(asset, reason, now_ms)
self._forget(asset, reason, fill.price, fill.fee_usd, now_ms, events)
return events
# ---- 1. reconcile ------------------------------------------------------------
def _reconcile(self, account: AccountState, now_ms: int, events: list[Event]) -> None:
for asset in list(self._positions):
venue = account.position(asset)
if venue is not None and venue.size > 0:
continue
known = self._broker.external_close(asset, now_ms)
if known is not None:
reason, fill = known
self._forget(asset, reason, fill.price, fill.fee_usd, fill.time_ms, events)
else:
price = self._market.price(asset) or self._positions[asset].dsl.entry_price
self._forget(asset, CloseReason.CLOSED_EXTERNALLY, price, 0.0, now_ms, events)
# ---- 2. exits -----------------------------------------------------------------
def _run_requested_exits(self, now_ms: int, events: list[Event]) -> None:
if self._exits is None:
return
for asset in self._exits.close_requests(now_ms):
if asset in self._positions:
fill = self._broker.close(asset, CloseReason.SOURCE_CLOSED, now_ms)
self._forget(
asset, CloseReason.SOURCE_CLOSED, fill.price, fill.fee_usd, now_ms, events
)
def _run_exits(self, now_ms: int, events: list[Event]) -> None:
for asset, tracked in list(self._positions.items()):
price = self._market.price(asset)
if price is None:
continue
result = tick(self._cfg.dsl, tracked.dsl, price, now_ms)
if result.close_reason is not None:
fill = self._broker.close(asset, result.close_reason, now_ms)
self._forget(asset, result.close_reason, fill.price, fill.fee_usd, now_ms, events)
continue
stop = result.stop_price
if stop != tracked.stop_price:
self._broker.set_stop(asset, stop, now_ms)
events.append(
Event(
now_ms,
"stop_moved",
asset,
"tier_advanced" if result.tier_advanced else "floor_ratchet",
{"stop_price": stop, "roe": result.roe},
)
)
self._positions[asset] = Tracked(
tracked.signal, result.state, tracked.size, stop, tracked.fees_usd
)
def _forget(
self,
asset: str,
reason: CloseReason,
exit_price: float,
exit_fee: float,
now_ms: int,
events: list[Event],
) -> None:
tracked = self._positions.pop(asset)
d = tracked.dsl
gross = (exit_price - d.entry_price) * d.direction.sign * tracked.size
pnl = gross - tracked.fees_usd - exit_fee
self._rails = self._rails.record_close(self._cfg.rails, asset, pnl, now_ms)
events.append(
Event(
now_ms,
"closed",
asset,
reason.value,
{
"signal_id": tracked.signal.signal_id,
"scanner": tracked.signal.scanner,
"direction": d.direction.value,
"entry_price": d.entry_price,
"exit_price": exit_price,
"size": tracked.size,
"leverage": d.leverage,
"pnl_usd": pnl,
"roe_pct": roe_pct(d.entry_price, exit_price, d.direction, d.leverage),
"high_water_roe": d.high_water_roe,
"held_minutes": (now_ms - d.opened_at_ms) / 60_000.0,
},
)
)
# ---- 3. entries ----------------------------------------------------------------
def _gate(self, signal: Signal, account: AccountState, now_ms: int) -> GateReason | None:
cfg = self._cfg
if not signal.is_valid(now_ms):
return GateReason.SIGNAL_EXPIRED
if signal.asset in cfg.banned_assets:
return GateReason.ASSET_BANNED
if self._dedup.is_duplicate(signal, window_ms=cfg.dedup_window_ms):
return GateReason.DUPLICATE
held = signal.asset in self._positions or account.position(signal.asset) is not None
if held and not cfg.strategy.allow_pyramiding:
return GateReason.ASSET_HELD
if len(self._positions) >= cfg.strategy.slots:
return GateReason.NO_SLOTS
return self._rails.check_account(
cfg.rails, now_ms, account.account_value
) or self._rails.check_asset(cfg.rails, now_ms, signal.asset)
def _try_enter(
self, signal: Signal, account: AccountState, now_ms: int, events: list[Event]
) -> bool:
"""Returns True when an order was placed."""
rejected = self._gate(signal, account, now_ms)
if rejected is not None:
events.append(self._rejection(signal, rejected, now_ms))
return False
instrument = self._market.instrument(signal.asset)
price = self._market.price(signal.asset)
if instrument is None or instrument.delisted:
events.append(self._rejection(signal, GateReason.ASSET_BANNED, now_ms))
return False
if price is None:
events.append(self._rejection(signal, GateReason.SIGNAL_NOT_READY, now_ms))
return False
plan = plan_order(
self._cfg.strategy,
signal,
instrument,
price=price,
withdrawable=account.withdrawable,
free_margin=account.free_margin,
)
if isinstance(plan, GateReason):
events.append(self._rejection(signal, plan, now_ms))
return False
# Last check, against the live book: a thin market turns the stop into a slippage trap.
rails = self._cfg.rails
book = self._market.order_book(signal.asset) if rails.liquidity_enabled else None
blocked, liq = check_liquidity(rails, book, plan.direction, plan.notional_usd)
if blocked is not None:
extra = {"notional_usd": round(plan.notional_usd, 2)}
if liq is not None:
extra.update(liq.as_payload())
events.append(self._rejection(signal, blocked, now_ms, extra))
return False
self._open(signal, plan, now_ms, events)
return True
def _open(self, signal: Signal, plan: OrderPlan, now_ms: int, events: list[Event]) -> None:
provisional = DslState.open(plan.reference_price, plan.direction, plan.leverage, now_ms)
fill: Fill = self._broker.open(plan, stop_price(self._cfg.dsl, provisional), now_ms)
dsl = DslState.open(fill.price, plan.direction, plan.leverage, fill.time_ms)
stop = stop_price(self._cfg.dsl, dsl)
if fill.price != plan.reference_price:
self._broker.set_stop(plan.asset, stop, now_ms)
self._positions[plan.asset] = Tracked(signal, dsl, fill.size, stop, fill.fee_usd)
self._rails = self._rails.record_entry()
self._dedup = self._dedup.remember(signal)
events.append(
Event(
now_ms,
"opened",
plan.asset,
GateReason.SUBMITTED.value,
{
"signal_id": signal.signal_id,
"scanner": signal.scanner,
"direction": plan.direction.value,
"size": fill.size,
"entry_price": fill.price,
"leverage": plan.leverage,
"margin_usd": plan.margin_usd,
"notional_usd": plan.notional_usd,
"stop_price": stop,
"fee_usd": fill.fee_usd,
"data": dict(signal.data),
},
)
)
@staticmethod
def _rejection(
signal: Signal, reason: GateReason, now_ms: int, extra: Mapping[str, Any] | None = None
) -> Event:
return Event(
now_ms,
"rejected",
signal.asset,
reason.value,
{
"signal_id": signal.signal_id,
"scanner": signal.scanner,
"direction": signal.direction.value,
**(extra or {}),
},
)