backtrader.sizer module

Position Sizer Module - Position size calculation.

This module provides the base class for position sizers, which determine the size of orders to place based on available cash, risk parameters, and other factors.

Classes:

Sizer: Base class for position sizers. FixedSize: Sizer that uses a fixed size. FixedReverser: Sizer that reverses positions with fixed size. PercentSizer: Sizer that uses a percentage of available cash. AllInSizer: Sizer that uses all available cash. RiskReturnSizer: Sizer that sizes based on risk/reward ratio.

示例

Creating a custom sizer: >>> class MySizer(bt.Sizer): ... params = (('perc', 0.1),) ... ... def _getsizing(self, comminfo, cash, data, isbuy): ... return int(cash * self.p.perc / data.close[0])

class backtrader.sizer.Sizer[源代码]

基类:ParameterizedBase

Base class for position sizers.

This is the base class for sizers. Any sizer should subclass this and override the _getsizing method to provide custom position sizing logic.

strategy

The strategy using this sizer.

broker

The broker instance for portfolio information.

getsizing(data, isbuy)[源代码]

Get the position size for an order.

_getsizing(comminfo, cash, data, isbuy)[源代码]

Override to implement sizing logic.

set(strategy, broker)[源代码]

Set the strategy and broker references.

示例

>>> cerebro.addsizer(bt.sizers.FixedSize, stake=100)
strategy = None
broker = None
__init__(**kwargs)[源代码]

Initialize the Sizer with any provided parameters.

getsizing(data, isbuy)[源代码]

Get the position size for an order.

参数:
  • data -- The target data for the order.

  • isbuy -- True for buy operations, False for sell operations.

返回:

The position size to use for the order, as determined

by the _getsizing method.

返回类型:

int

set(strategy, broker)[源代码]

Set the strategy and broker references for this sizer.

参数:
  • strategy -- The strategy instance using this sizer.

  • broker -- The broker instance for portfolio information.

backtrader.sizer.SizerBase

Sizer 的别名