backtrader.commissions package

Commission Schemes Module - Predefined commission configurations.

This module provides pre-configured commission schemes for common trading instruments like stocks and futures. These schemes extend the base CommInfoBase with default parameters.

Classes:

CommInfo: Base commission scheme with percentage-based commission. CommInfoFutures: Futures commission scheme. CommInfoFuturesPerc: Futures with percentage commission. CommInfoFuturesFixed: Futures with fixed commission. CommInfoStocks: Stock commission scheme. CommInfoStocksPerc: Stocks with percentage commission. CommInfoStocksFixed: Stocks with fixed commission.

Example

Setting commission scheme: >>> cerebro = bt.Cerebro() >>> comminfo = bt.commissions.CommInfoStocks(commission=0.001) >>> cerebro.broker.addcommissioninfo(cominfo)

class backtrader.commissions.CommInfo[source]

Bases: CommInfoBase

Base commission scheme with percentage-based commission.

class backtrader.commissions.CommInfoFutures[source]

Bases: CommInfoBase

Futures commission scheme.

params = (('stocklike', False),)
class backtrader.commissions.CommInfoFuturesPerc[source]

Bases: CommInfoFutures

Futures commission scheme with percentage-based commission.

Commission is calculated as a percentage of the trading volume.

params = (('commtype', 0),)
class backtrader.commissions.CommInfoFuturesFixed[source]

Bases: CommInfoFutures

Futures commission scheme with fixed per-contract commission.

Commission is a fixed amount per contract traded.

params = (('commtype', 1),)
class backtrader.commissions.CommInfoStocks[source]

Bases: CommInfoBase

Stock commission scheme with stock-like asset behavior.

Uses stock-like margin and position handling.

params = (('stocklike', True),)
class backtrader.commissions.CommInfoStocksPerc[source]

Bases: CommInfoStocks

Stock commission scheme with percentage-based commission.

Commission is calculated as a percentage of the trading volume.

params = (('commtype', 0),)
class backtrader.commissions.CommInfoStocksFixed[source]

Bases: CommInfoStocks

Stock commission scheme with fixed per-share commission.

Commission is a fixed amount per share traded.

params = (('commtype', 1),)
class backtrader.commissions.OptionPremiumCommInfo[source]

Bases: CommInfoBase

Premium option accounting over neutral, source-bound SDK rule facts.

This class retains Backtrader’s ordinary premium, value, PnL and fee math. Contract and fee facts arrive through an SDK-issued InstrumentExecutionRulesSnapshot scoped to an explicit neutral hedge class and must pass its route-bound currentness check before use. Fee cash calculations additionally require verified currency provenance. The snapshot is accounting evidence, not a write capability. Seller margin remains unavailable until a neutral pre-fee margin fact can be consumed without double counting.

stocklike

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

commtype

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

percabs

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

premium_style

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

option_type

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

instrument_key

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

hedge_class

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

execution_rules_snapshot_port

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 CommInfo object

property fee_evidence_status: str
seller_margin_status()[source]
Return type:

str

refresh_execution_rules_snapshot()[source]

Explicitly replace the pinned snapshot through its selected SDK port.

Return type:

None

getoperationcost(size, price, is_buy=None, *, side=None, role='open')[source]

Return premium cost or an approved opening seller margin.

getpremiumvalue(size, price)[source]

Return the premium transaction value for any execution side.

Opening short risk uses a separately sourced margin value in getoperationcost(). A broker fill’s executed value is always the traded premium, regardless of whether that fill opens or closes a long or short position.

getsize(price, cash)[source]

Return buyer quantity using premium plus the complete open fee.

getvaluesize(size, price)[source]

Return signed option position value at a mark price.

getvalue(position, price)[source]

Return signed position value; shorts remain negative.

profitandloss(size, price, newprice)[source]

Return linear signed option PnL.

cashadjust(size, price, newprice)[source]

Premium-style options settle through execution; no mark cash flow.

get_margin(price)[source]

Returns the actual margin/guarantees needed for a single item of the asset at the given price. The default implementation has this policy:

  • Use param margin if param automargin evaluates to False

  • Use param mult * price if automargin < 0

  • Use param automargin * price if automargin > 0

accounting_projection(size, price, *, is_buy=True, role='open')[source]

Return a reviewable single-leg projection without changing cash.

Return type:

dict[str, Any]

exception backtrader.commissions.OptionAccountingError[source]

Bases: ValueError

Raised when option accounting facts are missing or inconsistent.

__init__(code, message=None)[source]
Parameters:
  • code (str)

  • message (str | None)

Submodules