Events API¶
Domain events: fills, funding, liquidation, and settlement.
Fill¶
Fill
dataclass
¶
from_dict
classmethod
¶
Construct a Fill from a mapping (e.g., exchange fill JSON).
create
classmethod
¶
create(
*,
order_id: str,
instrument_id: str,
side: OrderSide | str,
quantity: Quantity | Decimal | str,
price: Price | Decimal | str,
timestamp_ms: TimestampMs | int,
fee: Money | Decimal | str = Decimal(0)
) -> Fill
Create Fill with keyword-only arguments for safety.
All parameters must be passed by name, preventing positional argument order mistakes that are common with the regular constructor.
Example
fill = Fill.create( ... order_id="o1", ... instrument_id="BTC-USD", ... side=OrderSide.BUY, ... quantity=Decimal("1"), ... price=Decimal("10000"), ... timestamp_ms=1234567890, ... )
from_exchange_fill
classmethod
¶
Construct a Fill from a raw exchange payload.
Known keys (fallbacks are used if a key is missing): - id | fill_id | trade_id | order_id -> order_id - symbol | instrument | instrument_id -> instrument_id - side (BUY/SELL) -> side - qty | size | quantity -> quantity - price -> price - fee | commission -> fee (defaults to 0) - ts | timestamp | time | timestamp_ms -> timestamp_ms
FillBuilder¶
FillBuilder ¶
Fluent builder for creating fills.
Example
fill = ( ... FillBuilder() ... .order("order-123") ... .instrument("BTC-PERP") ... .buy("1.5") ... .at_price("10000") ... .with_fee("10") ... .at_time(1234567890000) ... .build() ... )
FundingEvent¶
FundingEvent
dataclass
¶
LiquidationEvent¶
LiquidationEvent
dataclass
¶
SettlementEvent¶
SettlementEvent
dataclass
¶
FeeEvent¶
FeeEvent
dataclass
¶
Explicit fee charged to an account.
amount
Positive fee size (e.g. Decimal("1.5") for a 1.5 USDT fee). From the account's point of view this is a cash outflow.
signed_amount
Signed representation from the account's perspective (negative = cash paid out).
signed_amount
property
¶
Return fee as a signed cash-flow (negative = outflow).
Event Hooks¶
EventBus¶
EventBus ¶
Simple event bus for pub/sub pattern.
Example
bus = EventBus() @bus.on("fill") ... def handle_fill(fill): ... print(f"Received fill: {fill}") bus.emit("fill", fill=my_fill)
on¶
on ¶
Register a global event handler.
Example
@on("fill_applied") def log_fill(fill, portfolio): print(f"Applied {fill}")
off¶
off ¶
Remove global event handler(s).