backtrader.comminfo module

Refactored CommInfo system (Day 44)

Migrated CommInfo system from MetaParams to new ParameterizedBase system. Maintains fully backward compatible API interface.

class backtrader.comminfo.CommInfoBase[源代码]

基类:ParameterizedBase

Base Class for the Commission Schemes.

Migrated from MetaParams to ParameterizedBase system for better parameter management and validation.

Params:
  • commission (def: 0.0): base commission value in percentage or monetary units

  • mult (def 1.0): multiplier applied to the asset for value/profit

  • margin (def: None): amount of monetary units needed to open/hold an operation

  • automargin (def: False): Used by get_margin to automatically calculate margin

  • commtype (def: None): Commission type (COMM_PERC/COMM_FIXED)

  • stocklike (def: False): Indicates if the instrument is Stock-like or Futures-like

  • percabs (def: False): whether commission is XX% or 0.XX when commtype is COMM_PERC

  • interest (def: 0.0): yearly interest charged for holding short selling position

  • interest_long (def: False): whether to charge interest on long positions

  • leverage (def: 1.0): amount of leverage for the asset

COMM_PERC = 0
COMM_FIXED = 1
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

maker_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

taker_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

open_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

close_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

close_today_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

close_yesterday_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

mult

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

margin

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

stocklike
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

interest

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

interest_long

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

leverage

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

automargin

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)[源代码]

Initialize CommInfo object

set_param(name, value, validate=True)[源代码]

Set parameter value with optional validation.

参数:
  • name -- Parameter name

  • value -- Parameter value

  • validate -- Whether to perform validation

抛出:
get_margin(price)[源代码]

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

get_leverage()[源代码]

Returns the level of leverage allowed for this commission scheme

getsize(price, cash)[源代码]

Returns the needed size to meet a cash operation at a given price

getoperationcost(size, price)[源代码]

Returns the needed amount of cash an operation would cost

getvaluesize(size, price)[源代码]

Returns the value of size for given a price. For future-like objects it is fixed at size * margin

getvalue(position, price)[源代码]

Returns the value of a position given a price. For future-like objects it is fixed at size * margin

getcommission(size, price, role=None)[源代码]

Calculates the commission of an operation at a given price.

confirmexec(size, price, role=None)[源代码]

Confirms execution and returns commission.

profitandloss(size, price, newprice)[源代码]

Return actual profit and loss a position has

cashadjust(size, price, newprice)[源代码]

Calculates cash adjustment for a given price difference

get_credit_interest(data, pos, dt)[源代码]

Calculates the credit due for short selling or product specific

class backtrader.comminfo.CommissionInfo[源代码]

基类:CommInfoBase

Base Class for the actual Commission Schemes.

CommInfoBase was created to keep support for the original, incomplete, support provided by backtrader. New commission schemes derive from this class which subclasses CommInfoBase.

The default value of percabs is also changed to True

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

class backtrader.comminfo.ComminfoDC[源代码]

基类:CommInfoBase

Digital currency commission class

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

interest

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

get_margin(price)[源代码]

Calculate the margin required for digital currency trading.

参数:

price -- Current price of the asset.

返回:

Margin calculated as price * mult * margin parameter.

返回类型:

float

get_credit_interest(data, pos, dt)[源代码]

Simplified implementation for digital currency interest calculation

class backtrader.comminfo.ComminfoFuturesPercent[源代码]

基类:CommInfoBase

Futures percentage commission class

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

mult

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

margin

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

margin_amount

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

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

get_margin(price)[源代码]

Calculate the margin required for futures percentage commission.

参数:

price -- Current price of the asset.

返回:

Margin calculated as price * mult * margin parameter.

返回类型:

float

class backtrader.comminfo.ComminfoFuturesMixed[源代码]

基类:ComminfoFuturesPercent

Futures commission with both percentage and fixed per-lot components.

commission_amount

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

open_commission_amount

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

close_commission_amount

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

close_today_commission_amount

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

close_yesterday_commission_amount

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

class backtrader.comminfo.ComminfoFuturesInverse[源代码]

基类:ComminfoFuturesMixed

Inverse futures commission and PnL using fixed contract notional.

Crypto inverse contracts quote PnL and fees from a fixed contract value, for example BTC-USD-SWAP with a 100 USD contract value. Their quote-value PnL changes with the price ratio rather than a linear price * mult notional.

get_margin(price)[源代码]

Return per-contract margin from fixed contract notional.

profitandloss(size, price, newprice)[源代码]

Return quote-equivalent inverse-contract PnL.

cashadjust(size, price, newprice)[源代码]

Mark inverse futures with the same quote-equivalent PnL rule.

class backtrader.comminfo.ComminfoFuturesFixed[源代码]

基类:CommInfoBase

Futures fixed commission class

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

mult

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

margin

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

margin_amount

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

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

get_margin(price)[源代码]

Calculate the margin required for futures fixed commission.

参数:

price -- Current price of the asset.

返回:

Margin calculated as price * mult * margin parameter.

返回类型:

float

class backtrader.comminfo.ComminfoFundingRate[源代码]

基类:CommInfoBase

Funding rate class

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

mult

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

margin

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

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

get_margin(price)[源代码]

Calculate the margin required for funding rate trading.

参数:

price -- Current price of the asset.

返回:

Margin calculated as price * mult * margin parameter.

返回类型:

float

get_credit_interest(data, pos, dt)[源代码]

Calculate funding rate for Binance futures