Skip to content

contango.strategy.rule_based.position_sizers.allocation_position_sizer

contango.strategy.rule_based.position_sizers.allocation_position_sizer

AllocationPositionSizer

AllocationPositionSizer(allocation: float)

Bases: PositionSizer[TradingContext]

A position sizer with an allocation of the portfolio cash.

Initializes `FixedPositionSizer.

Parameters:

Name Type Description Default
allocation float

The allocation of the portfolio cash to trade.

required
Source code in src\contango\strategy\rule_based\position_sizers\allocation_position_sizer.py
11
12
13
14
15
16
17
18
def __init__(self, allocation: float) -> None:
    """
    Initializes `FixedPositionSizer.

    Args:
        allocation: The allocation of the portfolio cash to trade.
    """
    self._allocation = allocation

size

size(context: TradingContext) -> int

Returns the allocation percent of the portfolio cash (converted to units).

Source code in src\contango\strategy\rule_based\position_sizers\allocation_position_sizer.py
20
21
22
23
24
25
26
27
28
29
30
def size(self, context: TradingContext) -> int:
    """
    Returns the allocation percent of the portfolio cash (converted to units).
    """
    cash = context.portfolio.cash * self._allocation

    if context.event.close == 0.0:
        return 0

    units = int(cash / context.event.close)
    return units