Skip to content

PositionGroup

QuantConnect.Securities.Positions.PositionGroup

PositionGroup(
    buying_power_model: IPositionGroupBuyingPowerModel,
    quantity: float,
    *positions: Union[IPosition, Iterable[IPosition]]
)
PositionGroup(
    key: PositionGroupKey,
    quantity: float,
    *positions: Union[IPosition, Iterable[IPosition]]
)
PositionGroup(
    key: PositionGroupKey,
    quantity: float,
    positions: Dictionary[Symbol, IPosition],
)

Bases: Object, IPositionGroup, Iterable[IPosition]

Provides a default implementation of IPositionGroup

Initializes a new instance of the PositionGroup class

Parameters:

Name Type Description Default
buying_power_model Optional[IPositionGroupBuyingPowerModel]

The buying power model to use for this group

None
quantity float

The group quantity, which must be the ratio of quantity to unit quantity of each position

required
positions Union[IPosition, Iterable[IPosition]]

The positions comprising this group

()
key Optional[PositionGroupKey]

The deterministic key for this group

None

count

count: int

Gets the number of positions in the group

key

Gets the key identifying this group

quantity

quantity: float

Gets the whole number of units in this position group

positions

positions: Iterable[IPosition]

Gets the positions in this group

buying_power_model

buying_power_model: IPositionGroupBuyingPowerModel

Gets the buying power model defining how margin works in this group

__iter__

__iter__() -> Iterator[IPosition]

__len__

__len__() -> int

empty

empty(
    buying_power_model: IPositionGroupBuyingPowerModel,
) -> PositionGroup

Instantiates a default empty position group instance

Parameters:

Name Type Description Default
buying_power_model IPositionGroupBuyingPowerModel

The buying power model to use for this group

required

get_enumerator

get_enumerator() -> IEnumerator[IPosition]

Returns an enumerator that iterates through the collection.

Returns:

Type Description
IEnumerator[IPosition]

An enumerator that can be used to iterate through the collection.

to_string

to_string() -> str

Returns a string that represents the current object.

Returns:

Type Description
str

A string that represents the current object.

try_get_position

try_get_position(
    symbol: Union[Symbol, str, BaseContract, Security],
    position: Optional[IPosition],
) -> Tuple[bool, IPosition]

Attempts to retrieve the position with the specified symbol

Parameters:

Name Type Description Default
symbol Union[Symbol, str, BaseContract, Security]

The symbol

required
position Optional[IPosition]

The position, if found

required

Returns:

Type Description
Tuple[bool, IPosition]

True if the position was found, otherwise false.

closes

closes(initial_group: IPositionGroup) -> bool

Checks whether the provided groups are closing/reducing each other, that is, each of their positions are in opposite sides.

Parameters:

Name Type Description Default
initial_group IPositionGroup

The initial position group before a trade

required

Returns:

Type Description
bool

Whether final resulting position group is a reduction of the initial one.

create_unit_group

create_unit_group(
    position_mananger: SecurityPositionGroupModel,
) -> IPositionGroup

Creates a new IPositionGroup with each position's quantity equaling it's unit quantity

Returns:

Type Description
IPositionGroup

A position group with the same position ratios as the template but with the specified group quantity.

get_position

get_position(
    symbol: Union[Symbol, str, BaseContract, Security],
) -> IPosition

Gets the position in the group matching the provided

Parameters:

Name Type Description Default
symbol Union[Symbol, str, BaseContract, Security]
required

get_user_friendly_name

get_user_friendly_name() -> str

Gets a user friendly name for the provided group

is_empty

is_empty() -> bool

Determines whether the position group is empty

Returns:

Type Description
bool

True if the position group is empty, that is, it has no positions, false otherwise.

is_inverted_of

is_inverted_of(other: IPositionGroup) -> bool

Checks whether the provided groups are in opposite sides, that is, each of their positions are in opposite sides.

Parameters:

Name Type Description Default
other IPositionGroup

The group to check against

required

Returns:

Type Description
bool

Whether the position groups are the inverted version of each other, that is, contain the same positions each on the opposite side.

with_quantity

with_quantity(
    group_quantity: float,
    position_mananger: SecurityPositionGroupModel,
) -> IPositionGroup

Creates a new IPositionGroup with the specified group_quantity. If the quantity provided equals the template's quantity then the template is returned.

Parameters:

Name Type Description Default
group_quantity float

The quantity of the new group

required
position_mananger SecurityPositionGroupModel

The position manager to use to resolve positions

required

Returns:

Type Description
IPositionGroup

A position group with the same position ratios as the template but with the specified group quantity.