Broker Architecture
The Pulse framework uses an abstract base classBroker that defines the core interface, with specific implementations like SimulationBroker for backtesting scenarios.
Key responsibilities include:
- Order Management: Creating and tracking orders
- Trade Execution: Converting orders into executed trades
- Order Lifecycle: Managing open orders and their execution timing
- Market Simulation: Providing realistic trade execution for backtesting
Broker class is very simple, it only supports creating market orders and does not simulate any slippage or other market conditions. We are working on making the Broker more sophisticated, allowing for different order types and slippage simulation. Also, the idea of Broker is to be easily switched to a live trading environment, so that the same strategy can be used for both backtesting and live trading.
Creating a Simulation Broker
TheSimulationBroker is the default implementation used for backtesting. It simulates market order execution by immediately filling all orders at the current market price during each pulse:
Output
Creating Orders
Orders are created using the broker’screate_order() method. This method requires:
- pulse_index: The current timing context
- instrument: The financial instrument to trade
- quantity: Number of contracts (positive for buy, negative for sell)
Output
Order Execution and Trade Generation
When the broker is pulsed, it executes all open orders and converts them into trades. TheSimulationBroker treats all orders as market orders, filling them immediately at the current market price:
Output
Strategy class you can access the broker instance via self.broker. You can create orders using self.broker.create_order(...) and pulse the broker with self.broker.pulse(pulse_index), or you can call self.create_order(...) which is a shorthand for self.broker.create_order(...). Another way is to call self.buy(...) or self.sell(...) methods, which are also shorthand for creating buy and sell orders respectively.
