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.

Example

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[source]

Bases: 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)[source]

Get the position size for an order.

_getsizing(comminfo, cash, data, isbuy)[source]

Override to implement sizing logic.

set(strategy, broker)[source]

Set the strategy and broker references.

Example

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

Initialize the Sizer with any provided parameters.

getsizing(data, isbuy)[source]

Get the position size for an order.

Parameters:
  • data – The target data for the order.

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

Returns:

The position size to use for the order, as determined

by the _getsizing method.

Return type:

int

set(strategy, broker)[source]

Set the strategy and broker references for this sizer.

Parameters:
  • strategy – The strategy instance using this sizer.

  • broker – The broker instance for portfolio information.

backtrader.sizer.SizerBase

alias of Sizer