Execution API Reference¶
financelib.trading.execution.Order(symbol: str, side: OrderSide, order_type: OrderType = OrderType.MARKET, price: float = 0.0, amount: float = 0.0, status: OrderStatus = OrderStatus.PENDING, filled_price: float = 0.0, filled_at: str = '', commission: float = 0.0, slippage: float = 0.0, order_id: str = '', created_at: str = (lambda: datetime.now().isoformat())())
dataclass
¶
Represents a trading order.
Attributes:
| Name | Type | Description |
|---|---|---|
symbol |
str
|
Trading symbol. |
side |
OrderSide
|
BUY or SELL. |
order_type |
OrderType
|
Order type (market, limit, stop, stop_limit). |
price |
float
|
Limit/stop price (0 for market orders). |
amount |
float
|
Order quantity. |
status |
OrderStatus
|
Current order status. |
filled_price |
float
|
Actual fill price (after slippage). |
filled_at |
str
|
Fill timestamp. |
commission |
float
|
Commission paid. |
slippage |
float
|
Slippage incurred. |
order_id |
str
|
Unique order identifier. |
created_at |
str
|
Order creation timestamp. |
financelib.trading.execution.OrderManager(slippage_model: Optional[SlippageModel] = None, commission_model: Optional[CommissionModel] = None)
¶
Manages order lifecycle — creation, tracking, and history.
Example
om = OrderManager() order = om.create_order("THYAO.IS", OrderSide.BUY, amount=100, price=172.5) om.fill_order(order.order_id, filled_price=172.55)
Source code in financelib/trading/execution.py
Attributes¶
total_commission: float
property
¶
Total commissions paid across all filled orders.
Functions¶
cancel_order(order_id: str) -> bool
¶
Cancel a pending order.
create_order(symbol: str, side: OrderSide, amount: float, price: float = 0.0, order_type: OrderType = OrderType.MARKET) -> Order
¶
Create a new order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Trading symbol. |
required |
side
|
OrderSide
|
BUY or SELL. |
required |
amount
|
float
|
Order quantity. |
required |
price
|
float
|
Limit/stop price. |
0.0
|
order_type
|
OrderType
|
Order type. |
MARKET
|
Returns:
| Type | Description |
|---|---|
Order
|
Created Order object. |
Source code in financelib/trading/execution.py
fill_order(order_id: str, filled_price: float) -> Optional[Order]
¶
Fill an order at the given price (with slippage and commission).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
order_id
|
str
|
Order ID to fill. |
required |
filled_price
|
float
|
Market price before slippage. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Order]
|
Updated Order object, or None if not found. |
Source code in financelib/trading/execution.py
get_all_orders() -> List[Order]
¶
get_filled_orders() -> List[Order]
¶
get_open_orders() -> List[Order]
¶
financelib.trading.execution.SlippageModel(fixed_bps: float = 5.0, volume_impact: float = 0.0)
¶
Slippage model for realistic backtesting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fixed_bps
|
float
|
Fixed slippage in basis points (1 bp = 0.01%). |
5.0
|
volume_impact
|
float
|
Price impact as a function of order size / volume. |
0.0
|
Example
model = SlippageModel(fixed_bps=5) actual_price = model.apply(100.0, "buy") 100.05
Source code in financelib/trading/execution.py
Functions¶
apply(price: float, side: str, order_size: float = 0, volume: float = 0) -> float
¶
Apply slippage to a price.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
price
|
float
|
Original price. |
required |
side
|
str
|
'buy' or 'sell'. |
required |
order_size
|
float
|
Order quantity. |
0
|
volume
|
float
|
Market volume. |
0
|
Returns:
| Type | Description |
|---|---|
float
|
Adjusted price after slippage. |
Source code in financelib/trading/execution.py
financelib.trading.execution.CommissionModel(rate: float = 0.001, minimum: float = 0.0)
¶
Commission model for trade cost calculation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
float
|
Commission rate as a decimal (e.g., 0.001 for 0.1%). |
0.001
|
minimum
|
float
|
Minimum commission per trade. |
0.0
|
Example
model = CommissionModel(rate=0.001, minimum=1.0) model.calculate(10000) 10.0
Source code in financelib/trading/execution.py
financelib.trading.execution.OrderStatus
¶
Bases: Enum
financelib.trading.execution.OrderSide
¶
Bases: Enum
financelib.trading.execution.OrderType
¶
Bases: Enum