PaperTrade-India 0.1.0.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- papertrade_india/__init__.py +250 -0
- papertrade_india/_context.py +144 -0
- papertrade_india/broker.py +546 -0
- papertrade_india/cli.py +474 -0
- papertrade_india/clock.py +29 -0
- papertrade_india/corporate_actions/__init__.py +10 -0
- papertrade_india/corporate_actions/bonus.py +105 -0
- papertrade_india/corporate_actions/dividends.py +99 -0
- papertrade_india/corporate_actions/rights.py +189 -0
- papertrade_india/corporate_actions/splits.py +111 -0
- papertrade_india/corporate_actions/store.py +247 -0
- papertrade_india/data/nse_holidays_2026.json +22 -0
- papertrade_india/data/nse_holidays_2027.json +9 -0
- papertrade_india/data/nse_universe_sample.csv +31 -0
- papertrade_india/domain/__init__.py +6 -0
- papertrade_india/domain/exceptions.py +183 -0
- papertrade_india/domain/models.py +213 -0
- papertrade_india/domain/rules/__init__.py +5 -0
- papertrade_india/domain/rules/risk.py +165 -0
- papertrade_india/domain/rules/tick_lot_band.py +139 -0
- papertrade_india/events.py +29 -0
- papertrade_india/exceptions.py +29 -0
- papertrade_india/execution/__init__.py +5 -0
- papertrade_india/execution/book.py +385 -0
- papertrade_india/execution/fees.py +213 -0
- papertrade_india/execution/settlement.py +360 -0
- papertrade_india/execution/simulation.py +176 -0
- papertrade_india/execution/slippage.py +120 -0
- papertrade_india/fees.py +29 -0
- papertrade_india/idempotency.py +29 -0
- papertrade_india/infrastructure/__init__.py +6 -0
- papertrade_india/infrastructure/clock.py +123 -0
- papertrade_india/infrastructure/events.py +137 -0
- papertrade_india/infrastructure/holidays.py +138 -0
- papertrade_india/infrastructure/idempotency.py +133 -0
- papertrade_india/infrastructure/ledger.py +141 -0
- papertrade_india/infrastructure/market_hours.py +200 -0
- papertrade_india/infrastructure/migrations.py +622 -0
- papertrade_india/infrastructure/observability.py +195 -0
- papertrade_india/infrastructure/persistence.py +130 -0
- papertrade_india/infrastructure/symbols.py +279 -0
- papertrade_india/infrastructure/watchlist.py +78 -0
- papertrade_india/interface.py +92 -0
- papertrade_india/ledger.py +29 -0
- papertrade_india/limit_orders.py +29 -0
- papertrade_india/market_hours.py +29 -0
- papertrade_india/microstructure.py +57 -0
- papertrade_india/migrations.py +29 -0
- papertrade_india/models.py +29 -0
- papertrade_india/observability.py +29 -0
- papertrade_india/orders/__init__.py +5 -0
- papertrade_india/orders/amo.py +98 -0
- papertrade_india/orders/book_helpers.py +111 -0
- papertrade_india/orders/bracket.py +115 -0
- papertrade_india/orders/limit.py +210 -0
- papertrade_india/orders/market.py +264 -0
- papertrade_india/orders/partial_fills.py +92 -0
- papertrade_india/orders/preopen.py +274 -0
- papertrade_india/orders/state.py +640 -0
- papertrade_india/orders/stop.py +122 -0
- papertrade_india/orders/submit.py +385 -0
- papertrade_india/partial_fills.py +29 -0
- papertrade_india/persistence.py +29 -0
- papertrade_india/preopen.py +29 -0
- papertrade_india/presets.py +120 -0
- papertrade_india/price_feed.py +316 -0
- papertrade_india/providers/__init__.py +123 -0
- papertrade_india/providers/alphavantage.py +180 -0
- papertrade_india/providers/base.py +256 -0
- papertrade_india/providers/cache.py +147 -0
- papertrade_india/providers/composite.py +244 -0
- papertrade_india/providers/dhan.py +266 -0
- papertrade_india/providers/finnhub.py +122 -0
- papertrade_india/providers/health.py +233 -0
- papertrade_india/providers/jugaad.py +142 -0
- papertrade_india/providers/kite.py +222 -0
- papertrade_india/providers/nse_bhavcopy.py +210 -0
- papertrade_india/providers/nsepython.py +98 -0
- papertrade_india/providers/registry.py +226 -0
- papertrade_india/providers/stooq.py +173 -0
- papertrade_india/providers/twelvedata.py +198 -0
- papertrade_india/providers/upstox.py +318 -0
- papertrade_india/providers/upstox_instruments.py +171 -0
- papertrade_india/providers/yfinance.py +205 -0
- papertrade_india/quickstart.py +111 -0
- papertrade_india/reads/__init__.py +6 -0
- papertrade_india/reads/account.py +140 -0
- papertrade_india/reads/orders.py +54 -0
- papertrade_india/reads/positions.py +188 -0
- papertrade_india/risk.py +29 -0
- papertrade_india/settlement.py +29 -0
- papertrade_india/simulation.py +29 -0
- papertrade_india/slippage.py +29 -0
- papertrade_india/symbols.py +29 -0
- papertrade_india/workers/__init__.py +6 -0
- papertrade_india/workers/limit_orders.py +390 -0
- papertrade_india-0.1.0.dev0.dist-info/METADATA +261 -0
- papertrade_india-0.1.0.dev0.dist-info/RECORD +101 -0
- papertrade_india-0.1.0.dev0.dist-info/WHEEL +4 -0
- papertrade_india-0.1.0.dev0.dist-info/entry_points.txt +2 -0
- papertrade_india-0.1.0.dev0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"""
|
|
2
|
+
papertrade_india
|
|
3
|
+
================
|
|
4
|
+
|
|
5
|
+
Production-grade simulated broker for NSE/BSE paper trading.
|
|
6
|
+
|
|
7
|
+
Exposes an Alpaca-style interface (same method signatures and
|
|
8
|
+
dataclass-shaped return values), so callers can swap brokers without
|
|
9
|
+
reshaping their code.
|
|
10
|
+
|
|
11
|
+
Public API
|
|
12
|
+
----------
|
|
13
|
+
- :class:`IndiaPaperBroker` — the broker
|
|
14
|
+
- :class:`BrokerInterface` — ABC that this broker and other adapters implement
|
|
15
|
+
- Models: :class:`Position`, :class:`Order`, :class:`Account`, :class:`Trade`
|
|
16
|
+
- Enums: :class:`OrderType`, :class:`OrderSide`, :class:`OrderStatus`, :class:`Exchange`
|
|
17
|
+
- Fee config: :class:`FeeConfig`, :class:`IndianFeeEngine`, :class:`FeeBreakdown`
|
|
18
|
+
- Calendar: :class:`NSECalendar`
|
|
19
|
+
- Price feed: :class:`PriceFeed`, :class:`YFinanceProvider`, :class:`JugaadDataProvider`
|
|
20
|
+
- Provider system (new): :class:`MarketDataProvider`, :class:`MarketQuote`, :class:`OHLCV`,
|
|
21
|
+
:class:`StooqProvider`, :class:`NSEBhavcopyProvider`, :class:`CompositeProvider`,
|
|
22
|
+
:class:`CircuitBreakerProvider`, :class:`ProviderRegistry`, :data:`default_registry`
|
|
23
|
+
- Limit-order watcher: :class:`LimitOrderWatcher`
|
|
24
|
+
- Exceptions: :class:`IndiaPaperBrokerError` and subclasses
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
from .broker import IndiaPaperBroker
|
|
28
|
+
from .corporate_actions import CorporateAction
|
|
29
|
+
from .domain.exceptions import (
|
|
30
|
+
AccountNotFoundError,
|
|
31
|
+
AMOWindowClosedError,
|
|
32
|
+
IdempotencyConflict,
|
|
33
|
+
IndiaPaperBrokerError,
|
|
34
|
+
InsufficientFundsError,
|
|
35
|
+
InsufficientSharesError,
|
|
36
|
+
InvalidOrderError,
|
|
37
|
+
KillSwitchActive,
|
|
38
|
+
LotSizeViolation,
|
|
39
|
+
MarginNotSupported,
|
|
40
|
+
MarketClosedError,
|
|
41
|
+
OrderNoLongerPending,
|
|
42
|
+
PriceBandViolation,
|
|
43
|
+
PriceUnavailableError,
|
|
44
|
+
RandomBrokerRejection,
|
|
45
|
+
RiskViolation,
|
|
46
|
+
SettlementError,
|
|
47
|
+
StalePriceRejected,
|
|
48
|
+
SymbolDelisted,
|
|
49
|
+
SymbolNotFound,
|
|
50
|
+
TickSizeViolation,
|
|
51
|
+
)
|
|
52
|
+
from .domain.models import (
|
|
53
|
+
Account,
|
|
54
|
+
Exchange,
|
|
55
|
+
Order,
|
|
56
|
+
OrderSide,
|
|
57
|
+
OrderStatus,
|
|
58
|
+
OrderType,
|
|
59
|
+
Position,
|
|
60
|
+
ProductType,
|
|
61
|
+
TimeInForce,
|
|
62
|
+
Trade,
|
|
63
|
+
)
|
|
64
|
+
from .domain.rules.risk import RiskConfig, RiskContext, RiskEngine
|
|
65
|
+
from .domain.rules.tick_lot_band import (
|
|
66
|
+
MicrostructureConfig,
|
|
67
|
+
round_to_tick,
|
|
68
|
+
)
|
|
69
|
+
from .execution.book import (
|
|
70
|
+
OrderBook,
|
|
71
|
+
OrderBookConfig,
|
|
72
|
+
OrderBookSimulator,
|
|
73
|
+
)
|
|
74
|
+
from .execution.fees import FeeBreakdown, FeeConfig, FeeSchedule, IndianFeeEngine
|
|
75
|
+
from .execution.settlement import (
|
|
76
|
+
PendingSettlement,
|
|
77
|
+
SettlementConfig,
|
|
78
|
+
SettlementEngine,
|
|
79
|
+
SettlementMode,
|
|
80
|
+
)
|
|
81
|
+
from .execution.simulation import (
|
|
82
|
+
LatencyConfig,
|
|
83
|
+
LatencySimulator,
|
|
84
|
+
RejectionConfig,
|
|
85
|
+
RejectionSimulator,
|
|
86
|
+
RejectScenario,
|
|
87
|
+
)
|
|
88
|
+
from .execution.slippage import SlippageConfig, apply_slippage
|
|
89
|
+
from .infrastructure.clock import Clock, ReplayClock, WallClock
|
|
90
|
+
from .infrastructure.events import Event
|
|
91
|
+
from .infrastructure.holidays import UpstoxHolidayProvider
|
|
92
|
+
from .infrastructure.ledger import CashMovement
|
|
93
|
+
from .infrastructure.market_hours import IST, NSECalendar, SessionPhase
|
|
94
|
+
from .infrastructure.observability import BrokerEvent, EventBus, stdlib_log_subscriber
|
|
95
|
+
from .infrastructure.symbols import SymbolEntry, SymbolMaster
|
|
96
|
+
from .infrastructure.watchlist import WatchlistStore
|
|
97
|
+
from .interface import BrokerInterface
|
|
98
|
+
from .orders.partial_fills import PartialFillConfig
|
|
99
|
+
from .orders.preopen import AuctionMatch, compute_equilibrium
|
|
100
|
+
from .price_feed import (
|
|
101
|
+
CachedLastKnownProvider,
|
|
102
|
+
JugaadDataProvider,
|
|
103
|
+
PriceFeed,
|
|
104
|
+
PriceProvider,
|
|
105
|
+
Quote,
|
|
106
|
+
YFinanceProvider,
|
|
107
|
+
resilient_feed,
|
|
108
|
+
)
|
|
109
|
+
from .providers import (
|
|
110
|
+
OHLCV,
|
|
111
|
+
CircuitBreakerProvider,
|
|
112
|
+
CompositeProvider,
|
|
113
|
+
DhanProvider,
|
|
114
|
+
KiteProvider,
|
|
115
|
+
MarketDataProvider,
|
|
116
|
+
MarketQuote,
|
|
117
|
+
MedianAggregation,
|
|
118
|
+
NSEBhavcopyProvider,
|
|
119
|
+
ProviderCapability,
|
|
120
|
+
ProviderError,
|
|
121
|
+
ProviderHealth,
|
|
122
|
+
ProviderInfo,
|
|
123
|
+
ProviderRegistry,
|
|
124
|
+
StooqProvider,
|
|
125
|
+
UpstoxProvider,
|
|
126
|
+
default_registry,
|
|
127
|
+
)
|
|
128
|
+
from .quickstart import quickstart
|
|
129
|
+
from .workers.limit_orders import LimitOrderWatcher
|
|
130
|
+
|
|
131
|
+
__all__ = [
|
|
132
|
+
# Core
|
|
133
|
+
"IndiaPaperBroker",
|
|
134
|
+
"BrokerInterface",
|
|
135
|
+
# Models
|
|
136
|
+
"Account",
|
|
137
|
+
"Order",
|
|
138
|
+
"Position",
|
|
139
|
+
"Trade",
|
|
140
|
+
# Enums
|
|
141
|
+
"Exchange",
|
|
142
|
+
"OrderSide",
|
|
143
|
+
"OrderStatus",
|
|
144
|
+
"OrderType",
|
|
145
|
+
"ProductType",
|
|
146
|
+
"SessionPhase",
|
|
147
|
+
"TimeInForce",
|
|
148
|
+
# Fees
|
|
149
|
+
"FeeBreakdown",
|
|
150
|
+
"FeeConfig",
|
|
151
|
+
"FeeSchedule",
|
|
152
|
+
"IndianFeeEngine",
|
|
153
|
+
# Calendar / time
|
|
154
|
+
"IST",
|
|
155
|
+
"NSECalendar",
|
|
156
|
+
"UpstoxHolidayProvider",
|
|
157
|
+
# Price feed
|
|
158
|
+
"CachedLastKnownProvider",
|
|
159
|
+
"JugaadDataProvider",
|
|
160
|
+
"PriceFeed",
|
|
161
|
+
"PriceProvider",
|
|
162
|
+
"Quote",
|
|
163
|
+
"YFinanceProvider",
|
|
164
|
+
"resilient_feed",
|
|
165
|
+
# New provider system
|
|
166
|
+
"MarketDataProvider",
|
|
167
|
+
"MarketQuote",
|
|
168
|
+
"OHLCV",
|
|
169
|
+
"ProviderCapability",
|
|
170
|
+
"ProviderError",
|
|
171
|
+
"ProviderInfo",
|
|
172
|
+
"ProviderHealth",
|
|
173
|
+
"CircuitBreakerProvider",
|
|
174
|
+
"CompositeProvider",
|
|
175
|
+
"MedianAggregation",
|
|
176
|
+
"ProviderRegistry",
|
|
177
|
+
"default_registry",
|
|
178
|
+
"StooqProvider",
|
|
179
|
+
"NSEBhavcopyProvider",
|
|
180
|
+
"KiteProvider",
|
|
181
|
+
"DhanProvider",
|
|
182
|
+
"UpstoxProvider",
|
|
183
|
+
# Background workers
|
|
184
|
+
"LimitOrderWatcher",
|
|
185
|
+
# Slippage / risk / symbols
|
|
186
|
+
"SlippageConfig",
|
|
187
|
+
"apply_slippage",
|
|
188
|
+
"RiskConfig",
|
|
189
|
+
"RiskContext",
|
|
190
|
+
"RiskEngine",
|
|
191
|
+
"SymbolMaster",
|
|
192
|
+
"SymbolEntry",
|
|
193
|
+
"WatchlistStore",
|
|
194
|
+
# Ledger / corporate actions
|
|
195
|
+
"CashMovement",
|
|
196
|
+
"CorporateAction",
|
|
197
|
+
# Partial fills / events / observability
|
|
198
|
+
"PartialFillConfig",
|
|
199
|
+
"Event",
|
|
200
|
+
"BrokerEvent",
|
|
201
|
+
"EventBus",
|
|
202
|
+
"stdlib_log_subscriber",
|
|
203
|
+
# Clocks / quickstart
|
|
204
|
+
"Clock",
|
|
205
|
+
"WallClock",
|
|
206
|
+
"ReplayClock",
|
|
207
|
+
"quickstart",
|
|
208
|
+
# Realism extensions
|
|
209
|
+
"MicrostructureConfig",
|
|
210
|
+
"OrderBookConfig",
|
|
211
|
+
"OrderBookSimulator",
|
|
212
|
+
"OrderBook",
|
|
213
|
+
"round_to_tick",
|
|
214
|
+
"SettlementConfig",
|
|
215
|
+
"SettlementEngine",
|
|
216
|
+
"SettlementMode",
|
|
217
|
+
"PendingSettlement",
|
|
218
|
+
"LatencyConfig",
|
|
219
|
+
"LatencySimulator",
|
|
220
|
+
"RejectionConfig",
|
|
221
|
+
"RejectionSimulator",
|
|
222
|
+
"RejectScenario",
|
|
223
|
+
# Pre-open auction
|
|
224
|
+
"AuctionMatch",
|
|
225
|
+
"compute_equilibrium",
|
|
226
|
+
# Exceptions
|
|
227
|
+
"IndiaPaperBrokerError",
|
|
228
|
+
"InsufficientFundsError",
|
|
229
|
+
"InsufficientSharesError",
|
|
230
|
+
"InvalidOrderError",
|
|
231
|
+
"MarketClosedError",
|
|
232
|
+
"OrderNoLongerPending",
|
|
233
|
+
"AccountNotFoundError",
|
|
234
|
+
"PriceUnavailableError",
|
|
235
|
+
"StalePriceRejected",
|
|
236
|
+
"RiskViolation",
|
|
237
|
+
"KillSwitchActive",
|
|
238
|
+
"IdempotencyConflict",
|
|
239
|
+
"SymbolNotFound",
|
|
240
|
+
"SymbolDelisted",
|
|
241
|
+
"TickSizeViolation",
|
|
242
|
+
"LotSizeViolation",
|
|
243
|
+
"PriceBandViolation",
|
|
244
|
+
"SettlementError",
|
|
245
|
+
"RandomBrokerRejection",
|
|
246
|
+
"MarginNotSupported",
|
|
247
|
+
"AMOWindowClosedError",
|
|
248
|
+
]
|
|
249
|
+
|
|
250
|
+
__version__ = "0.1.0.dev0"
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"""Shared collaborator bundle handed to every subsystem function.
|
|
2
|
+
|
|
3
|
+
Subsystems take this as their first argument rather than a back-reference
|
|
4
|
+
to ``IndiaPaperBroker``. This breaks circular imports (subsystems can
|
|
5
|
+
import the context dataclass without importing the broker module) and
|
|
6
|
+
keeps subsystem-level tests cheap (build a fake context, no full broker
|
|
7
|
+
boot).
|
|
8
|
+
|
|
9
|
+
The context is mostly immutable: every field except ``pending_events``
|
|
10
|
+
is set once at broker construction and read by subsystems. Event
|
|
11
|
+
emission appends to ``pending_events`` inside a transaction; the broker
|
|
12
|
+
drains it post-commit via :meth:`drain_pending_events`.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import sqlite3
|
|
18
|
+
from dataclasses import dataclass, field
|
|
19
|
+
from datetime import datetime
|
|
20
|
+
from typing import TYPE_CHECKING
|
|
21
|
+
|
|
22
|
+
if TYPE_CHECKING: # pragma: no cover - import cycle guard
|
|
23
|
+
from .domain.models import Exchange
|
|
24
|
+
from .domain.rules.risk import RiskEngine
|
|
25
|
+
from .domain.rules.tick_lot_band import MicrostructureConfig
|
|
26
|
+
from .execution.book import OrderBookSimulator
|
|
27
|
+
from .execution.fees import FeeSchedule, IndianFeeEngine
|
|
28
|
+
from .execution.settlement import SettlementEngine
|
|
29
|
+
from .execution.simulation import LatencySimulator, RejectionSimulator
|
|
30
|
+
from .execution.slippage import SlippageConfig
|
|
31
|
+
from .infrastructure.clock import Clock
|
|
32
|
+
from .infrastructure.market_hours import NSECalendar
|
|
33
|
+
from .infrastructure.observability import BrokerEvent, EventBus
|
|
34
|
+
from .infrastructure.persistence import Persistence
|
|
35
|
+
from .infrastructure.symbols import SymbolMaster
|
|
36
|
+
from .orders.partial_fills import PartialFillConfig
|
|
37
|
+
from .price_feed import PriceFeed
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass
|
|
41
|
+
class BrokerContext:
|
|
42
|
+
"""Bundle of collaborators passed to subsystem functions.
|
|
43
|
+
|
|
44
|
+
Mutability rule
|
|
45
|
+
---------------
|
|
46
|
+
Treat every field except ``pending_events`` as read-only. The list is
|
|
47
|
+
appended to during a transaction (via :meth:`emit`) and drained after
|
|
48
|
+
the transaction commits (via :meth:`drain_pending_events`).
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
account_id: str
|
|
52
|
+
default_exchange: Exchange
|
|
53
|
+
|
|
54
|
+
persistence: Persistence
|
|
55
|
+
price_feed: PriceFeed
|
|
56
|
+
calendar: NSECalendar
|
|
57
|
+
|
|
58
|
+
fee_schedule: FeeSchedule
|
|
59
|
+
slippage_config: SlippageConfig
|
|
60
|
+
risk_engine: RiskEngine
|
|
61
|
+
symbol_master: SymbolMaster
|
|
62
|
+
|
|
63
|
+
microstructure_config: MicrostructureConfig
|
|
64
|
+
book_sim: OrderBookSimulator
|
|
65
|
+
settlement: SettlementEngine
|
|
66
|
+
latency_sim: LatencySimulator
|
|
67
|
+
reject_sim: RejectionSimulator
|
|
68
|
+
partial_fill_config: PartialFillConfig
|
|
69
|
+
|
|
70
|
+
events: EventBus
|
|
71
|
+
clock: Clock
|
|
72
|
+
|
|
73
|
+
enforce_market_hours: bool
|
|
74
|
+
enforce_fresh_prices: bool
|
|
75
|
+
mark_to_bid: bool
|
|
76
|
+
|
|
77
|
+
# Opt-in: reject fills on delayed feeds (provider is_real_time=False).
|
|
78
|
+
# Defaults False so public delayed providers (yfinance) keep working.
|
|
79
|
+
enforce_real_time: bool = False
|
|
80
|
+
|
|
81
|
+
pending_events: list[BrokerEvent] = field(default_factory=list)
|
|
82
|
+
|
|
83
|
+
# ── Helpers used by subsystems ────────────────────────────────────
|
|
84
|
+
|
|
85
|
+
def now_iso(self) -> str:
|
|
86
|
+
"""ISO timestamp of the broker's current 'now' (clock-aware)."""
|
|
87
|
+
return self.clock.now().isoformat()
|
|
88
|
+
|
|
89
|
+
def fee_engine_for(self, when_iso: str) -> IndianFeeEngine:
|
|
90
|
+
"""Build the fee engine for an order's trade date."""
|
|
91
|
+
from .execution.fees import IndianFeeEngine
|
|
92
|
+
|
|
93
|
+
d = datetime.fromisoformat(when_iso).date()
|
|
94
|
+
return IndianFeeEngine(self.fee_schedule.config_on(d))
|
|
95
|
+
|
|
96
|
+
def emit(
|
|
97
|
+
self,
|
|
98
|
+
conn: sqlite3.Connection,
|
|
99
|
+
event_type: str,
|
|
100
|
+
order_id: str | None = None,
|
|
101
|
+
payload: dict | None = None,
|
|
102
|
+
) -> None:
|
|
103
|
+
"""Persist an event row AND queue it for post-commit publication.
|
|
104
|
+
|
|
105
|
+
The persistence write is part of the caller's transaction, so
|
|
106
|
+
the event log can never disagree with what was committed.
|
|
107
|
+
Subscribers see the event only after the caller invokes
|
|
108
|
+
:meth:`drain_pending_events` — typically right after
|
|
109
|
+
``with persistence.transaction()`` exits.
|
|
110
|
+
"""
|
|
111
|
+
from .infrastructure import events as _events
|
|
112
|
+
from .infrastructure.observability import BrokerEvent
|
|
113
|
+
|
|
114
|
+
recorded_at = self.now_iso()
|
|
115
|
+
_events.emit(
|
|
116
|
+
conn,
|
|
117
|
+
event_type=event_type,
|
|
118
|
+
account_id=self.account_id,
|
|
119
|
+
order_id=order_id,
|
|
120
|
+
payload=payload or {},
|
|
121
|
+
recorded_at_iso=recorded_at,
|
|
122
|
+
)
|
|
123
|
+
self.pending_events.append(
|
|
124
|
+
BrokerEvent(
|
|
125
|
+
event_type=event_type,
|
|
126
|
+
account_id=self.account_id,
|
|
127
|
+
order_id=order_id,
|
|
128
|
+
payload=dict(payload or {}),
|
|
129
|
+
recorded_at=datetime.fromisoformat(recorded_at),
|
|
130
|
+
)
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
def drain_pending_events(self) -> None:
|
|
134
|
+
"""Publish queued events to the bus. Call after each transaction
|
|
135
|
+
commits, so subscribers don't see uncommitted state."""
|
|
136
|
+
if not self.pending_events:
|
|
137
|
+
return
|
|
138
|
+
events_to_send = self.pending_events
|
|
139
|
+
self.pending_events = []
|
|
140
|
+
for ev in events_to_send:
|
|
141
|
+
self.events.publish(ev)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
__all__ = ["BrokerContext"]
|