backtrader.broker module¶
Backtrader Broker Module.
This module provides the broker system for order execution and portfolio management. It handles order creation, position tracking, cash management, and commission calculation.
- Key Classes:
BrokerBase: Base class for broker implementations. BrokerAliasMixin: Mixin providing method aliases.
- The broker system supports:
Order execution (buy, sell, cancel)
Position management
Cash and value tracking
Commission schemes
Order history
- class backtrader.broker.BrokerAliasMixin[source]¶
Bases:
objectMixin to provide method aliases without using metaclasses.
This mixin creates method aliases for compatibility with different naming conventions (e.g., get_cash/getcash, get_value/getvalue).
- class backtrader.broker.BrokerBase[source]¶
Bases:
BrokerAliasMixin,ParameterizedBaseBase class for broker implementations.
The broker handles order execution, position tracking, and cash management. It supports commission schemes, margin requirements, and order history.
- commission¶
Default commission scheme for all assets.
- comminfo¶
Dictionary mapping asset names to commission info objects.
- Params:
commission: Default commission scheme (CommInfoBase instance).
- commission¶
Advanced parameter descriptor with type checking and validation.
This descriptor replaces the metaclass-based parameter system with a more modern and maintainable approach. It provides:
Automatic type checking and conversion
Value validation
Default value handling
Documentation support
Python 3.6+ __set_name__ support
- __init__(**kwargs)[source]¶
Initialize the broker instance.
- Parameters:
**kwargs – Keyword arguments passed to parent class.
- init()[source]¶
Initialize the commission info dictionary.
Sets up the default commission scheme if not already present. Called from both __init__ and start methods.
- add_order_history(orders, notify=False)[source]¶
Add order history to the broker.
- Parameters:
orders – Orders to add to history.
notify – Whether to notify about these orders.
- Raises:
NotImplementedError – Must be implemented by subclasses.
- set_fund_history(fund)[source]¶
Set fund history for the broker.
- Parameters:
fund – Fund history data.
- Raises:
NotImplementedError – Must be implemented by subclasses.
- getcommissioninfo(data)[source]¶
Get the commission info for a given data.
- Parameters:
data – Data feed to get commission info for.
- Returns:
The commission info for the data, or the default.
- Return type:
- setcommission(commission=0.0, margin=None, mult=1.0, commtype=None, percabs=True, stocklike=False, interest=0.0, interest_long=False, leverage=1.0, automargin=False, name=None)[source]¶
This method sets a `` CommissionInfo`` object for assets managed in the broker with the parameters. Consult the reference for
CommInfoBaseIf name is None, this will be the default for assets for which no other
CommissionInfoscheme can be found
- addcommissioninfo(comminfo, name=None)[source]¶
Add a CommissionInfo object for an asset.
- Parameters:
comminfo – The CommissionInfo object to add.
name – Asset name. If None, sets as default for all assets.
- getcash()[source]¶
Get the current available cash.
- Returns:
Current cash amount.
- Return type:
- Raises:
NotImplementedError – Must be implemented by subclasses.
- getvalue(datas=None)[source]¶
Get the current portfolio value.
- Parameters:
datas – Data feeds to calculate value for (optional).
- Returns:
Current portfolio value.
- Return type:
- Raises:
NotImplementedError – Must be implemented by subclasses.
Get the current number of shares in fund-like mode.
- Returns:
Number of shares (1.0 for abstract mode).
- Return type:
Get the current number of shares in fund-like mode.
- Returns:
Number of shares (1.0 for abstract mode).
- Return type:
- set_fundmode(fundmode, fundstartval=None)[source]¶
Set the fund mode for the broker.
- Parameters:
fundmode – True to enable fund mode, False otherwise.
fundstartval – Initial fund value (optional).
Note
Not all brokers support fund mode.
- get_fundmode()[source]¶
Get the current fund mode status.
- Returns:
True if fund mode is enabled, False otherwise.
- Return type:
- property fundmode¶
Get the current fund mode status.
- Returns:
True if fund mode is enabled, False otherwise.
- Return type:
- getposition(data)[source]¶
Get the current position for a data feed.
- Parameters:
data – Data feed to get position for.
- Returns:
Current position for the data feed.
- Return type:
- Raises:
NotImplementedError – Must be implemented by subclasses.
- submit(order)[source]¶
Submit an order to the broker.
- Parameters:
order – Order object to submit.
- Raises:
NotImplementedError – Must be implemented by subclasses.
- cancel(order)[source]¶
Cancel a pending order.
- Parameters:
order – Order object to cancel.
- Raises:
NotImplementedError – Must be implemented by subclasses.
- buy(owner, data, size, price=None, plimit=None, exectype=None, valid=None, tradeid=0, oco=None, trailamount=None, trailpercent=None, **kwargs)[source]¶
Create a buy order.
- Parameters:
owner – Strategy/owner creating the order.
data – Data feed for the order.
size – Size of the order (positive for buy).
price – Limit price (optional).
plimit – Profit limit price (optional).
exectype – Execution type (Market, Limit, Stop, etc.).
valid – Validity period for the order.
tradeid – Trade identifier.
oco – One-cancels-other order reference.
trailamount – Trailing amount for stop orders.
trailpercent – Trailing percent for stop orders.
**kwargs – Additional keyword arguments.
- Returns:
The created order object.
- Return type:
- Raises:
NotImplementedError – Must be implemented by subclasses.
- sell(owner, data, size, price=None, plimit=None, exectype=None, valid=None, tradeid=0, oco=None, trailamount=None, trailpercent=None, **kwargs)[source]¶
Create a sell order.
- Parameters:
owner – Strategy/owner creating the order.
data – Data feed for the order.
size – Size of the order (positive for sell).
price – Limit price (optional).
plimit – Profit limit price (optional).
exectype – Execution type (Market, Limit, Stop, etc.).
valid – Validity period for the order.
tradeid – Trade identifier.
oco – One-cancels-other order reference.
trailamount – Trailing amount for stop orders.
trailpercent – Trailing percent for stop orders.
**kwargs – Additional keyword arguments.
- Returns:
The created order object.
- Return type:
- Raises:
NotImplementedError – Must be implemented by subclasses.