Simulated
_PendingOrder
dataclass
Internal order state tracked by the simulated broker.
This structure represents broker-side pending order state and is distinct from order request events. It is used to evaluate trigger conditions against incoming market bars and to generate fills when conditions are met.
Source code in src/onesecondtrader/brokers/simulated.py
SimulatedBroker
Bases: BrokerBase
Event-driven simulated broker for backtesting.
The broker subscribes to order request events and market bar events. Order requests are validated and accepted or rejected immediately. Accepted orders are stored as pending broker-side state and evaluated against each incoming bar. When an order triggers, a fill event is published with a deterministic fill price model based on the bar's OHLC values.
The broker publishes response events using the event timestamp to preserve simulated time consistency.
__init__(event_bus)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_bus
|
EventBus
|
Event bus used to receive order requests and market bars, and to publish broker responses and fills. |
required |
Source code in src/onesecondtrader/brokers/simulated.py
connect()
Establish broker readiness.
The simulated broker has no external connectivity requirements. This method is a no-op and exists to satisfy the broker interface.
_on_event(event)
Dispatch incoming events.
Market bar events are routed to bar processing. All other events are delegated to the broker base class for order request handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
EventBase
|
Incoming event received from the event bus. |
required |
Source code in src/onesecondtrader/brokers/simulated.py
_on_bar(event)
Process an incoming market bar.
Pending orders are evaluated against the bar in a fixed sequence to provide deterministic behavior. Crucially, limit orders are processed after stop limit orders to ensure that limit orders created by stop limit orders are evaluated against the same bar.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
BarReceived
|
Market bar used to trigger and price simulated fills. |
required |
Source code in src/onesecondtrader/brokers/simulated.py
_process_market_orders(event)
Fill pending market orders for the bar symbol.
Market orders are filled at the bar open price on the next received bar for the matching symbol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
BarReceived
|
Market bar providing the simulated fill price and timestamps. |
required |
Source code in src/onesecondtrader/brokers/simulated.py
_process_stop_orders(event)
Evaluate and fill pending stop orders for the bar symbol.
Stop orders trigger when the bar crosses the stop level. The fill price is modeled as the worse of the stop price and the bar open in the direction of the trade.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
BarReceived
|
Market bar used to evaluate triggers and determine fill prices. |
required |
Source code in src/onesecondtrader/brokers/simulated.py
_process_stop_limit_orders(event)
Evaluate pending stop-limit orders for the bar symbol.
Stop-limit orders trigger on stop conditions. When triggered, they are converted into pending limit orders at the same identifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
BarReceived
|
Market bar used to evaluate stop triggers. |
required |
Source code in src/onesecondtrader/brokers/simulated.py
_process_limit_orders(event)
Evaluate and fill pending limit orders for the bar symbol.
Limit orders trigger when the bar crosses the limit level. The fill price is modeled as the better of the limit price and the bar open in the direction of the trade.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
BarReceived
|
Market bar used to evaluate triggers and determine fill prices. |
required |
Source code in src/onesecondtrader/brokers/simulated.py
_reject_if_invalid_submission(event)
Validate an order submission request.
Invalid submissions are rejected immediately by publishing an OrderRejected response event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
OrderSubmissionRequest
|
Order submission request event to validate. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the submission is invalid and was rejected, otherwise False. |
Source code in src/onesecondtrader/brokers/simulated.py
_on_submit_order(event)
Handle an order submission request.
Valid orders are stored as pending broker-side state and acknowledged via an OrderAccepted response event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
OrderSubmissionRequest
|
Order submission request event. |
required |
Source code in src/onesecondtrader/brokers/simulated.py
_on_cancel_order(event)
Handle an order cancellation request.
If the referenced order is pending, it is removed and acknowledged via CancellationAccepted.
Otherwise, CancellationRejected is published.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
OrderCancellationRequest
|
Order cancellation request event. |
required |
Source code in src/onesecondtrader/brokers/simulated.py
_reject_if_invalid_modification(event)
Validate an order modification request.
Invalid modifications are rejected immediately by publishing a ModificationRejected response event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
OrderModificationRequest
|
Order modification request event to validate. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the modification is invalid and was rejected, otherwise False. |
Source code in src/onesecondtrader/brokers/simulated.py
_on_modify_order(event)
Handle an order modification request.
If the referenced order is pending, its fields are updated and acknowledged via ModificationAccepted.
Otherwise, ModificationRejected is published.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
OrderModificationRequest
|
Order modification request event. |
required |