Skip to content

PositionGroupCollection

QuantConnect.Securities.Positions.PositionGroupCollection

PositionGroupCollection(
    groups: Dictionary[PositionGroupKey, IPositionGroup],
    groups_by_symbol: Dictionary[
        Symbol, HashSet[IPositionGroup]
    ],
)
PositionGroupCollection(groups: Sequence[IPositionGroup])

Bases: Object, Sequence[IPositionGroup], Iterable[IPositionGroup]

Provides a collection type for IPositionGroup

Initializes a new instance of the PositionGroupCollection class

Parameters:

Name Type Description Default
groups Dictionary[PositionGroupKey, IPositionGroup] | Sequence[IPositionGroup]

The position groups keyed by their group key

required
groups_by_symbol Optional[Dictionary[Symbol, HashSet[IPositionGroup]]]

The position groups keyed by the symbol of each position

None

EMPTY

Gets an empty instance of the PositionGroupCollection class

count

count: int

Gets the number of positions in this group

is_only_default_groups

is_only_default_groups: bool

Gets whether or not this collection contains only default position groups

keys

keys: Sequence[PositionGroupKey]

Gets the position groups keys in this collection

values

values: Sequence[IPositionGroup]

Gets the position groups in this collection

__getitem__

__getitem__(key: PositionGroupKey) -> IPositionGroup

Gets the IPositionGroup matching the specified key. If one does not exist, then an empty group is returned matching the unit quantities defined in the key

Parameters:

Name Type Description Default
key PositionGroupKey

The position group key to search for

required

Returns:

Type Description
IPositionGroup

The position group matching the specified key, or a new empty group if no matching group is found.

__iter__

__iter__() -> Iterator[IPositionGroup]

add

Creates a new PositionGroupCollection that contains all of the position groups in this collection in addition to the specified group. If a group with the same key already exists then it is overwritten.

combine_with

Merges this position group collection with the provided other collection.

contains

contains(key: PositionGroupKey) -> bool

Determines whether or not a group with the specified key exists in this collection

Parameters:

Name Type Description Default
key PositionGroupKey

The group key to search for

required

Returns:

Type Description
bool

True if a group with the specified key was found, false otherwise.

get_enumerator

get_enumerator() -> IEnumerator[IPositionGroup]

Returns an enumerator that iterates through the collection.

Returns:

Type Description
IEnumerator[IPositionGroup]

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

try_get_group

try_get_group(
    key: PositionGroupKey, group: Optional[IPositionGroup]
) -> Tuple[bool, IPositionGroup]

Attempts to retrieve the group with the specified key

Parameters:

Name Type Description Default
key PositionGroupKey

The group key to search for

required
group Optional[IPositionGroup]

The position group

required

Returns:

Type Description
Tuple[bool, IPositionGroup]

True if group with key found, otherwise false.

try_get_groups

try_get_groups(
    symbol: Union[Symbol, str, BaseContract],
    groups: Optional[Sequence[IPositionGroup]],
) -> Tuple[bool, Sequence[IPositionGroup]]

Attempts to retrieve all groups that contain the provided symbol

Parameters:

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

The symbol

required
groups Optional[Sequence[IPositionGroup]]

The groups if any were found, otherwise null

required

Returns:

Type Description
Tuple[bool, Sequence[IPositionGroup]]

True if groups were found for the specified symbol, otherwise false.