Skip to content

PortfolioTargetCollection

QuantConnect.Algorithm.Framework.Portfolio.PortfolioTargetCollection

Bases: Object, IDictionary[Symbol, IPortfolioTarget], Iterable[KeyValuePair[Symbol, IPortfolioTarget]]

Provides a collection for managing IPortfolioTargets for each symbol

count

count: int

Gets the number of targets in this collection

is_empty

is_empty: bool

True if there is no target in the collection

is_read_only

is_read_only: bool

Gets false. This collection is not read-only.

keys

keys: ICollection[Symbol]

Gets the symbol keys for this collection

values

values: ICollection[IPortfolioTarget]

Gets all portfolio targets in this collection Careful, will return targets for securities that might have no data yet.

add

add(target: IPortfolioTarget) -> None
add(target: KeyValuePair[Symbol, IPortfolioTarget]) -> None
add(
    symbol: Union[Symbol, str, BaseContract],
    target: IPortfolioTarget,
) -> None

Adds the specified target to the collection. If a target for the same symbol already exists it wil be overwritten.

Parameters:

Name Type Description Default
target IPortfolioTarget | KeyValuePair[Symbol, IPortfolioTarget]

The portfolio target to add

required
symbol Optional[Union[Symbol, str, BaseContract]]

The symbol key

None

contains

contains(target: IPortfolioTarget) -> bool
contains(
    target: KeyValuePair[Symbol, IPortfolioTarget],
) -> bool

Signature descriptions:

  • Determines whether or not the specified target exists in this collection. NOTE: This checks for the exact specified target, not by symbol. Use ContainsKey to check by symbol.

  • Determines whether the specified symbol/target pair exists in this collection

Parameters:

Name Type Description Default
target IPortfolioTarget | KeyValuePair[Symbol, IPortfolioTarget]

The portfolio target to check for existence.

required

Returns:

Type Description
bool

Depends on the signature used. Case 1: [True if the target exists, false otherwise.]; Case 2: [True if the pair exists, false otherwise.]

copy_to

copy_to(
    array: List[IPortfolioTarget], array_index: int
) -> None
copy_to(
    array: List[KeyValuePair[Symbol, IPortfolioTarget]],
    array_index: int,
) -> None

Copies the targets in this collection to the specified array

Parameters:

Name Type Description Default
array List[IPortfolioTarget] | List[KeyValuePair[Symbol, IPortfolioTarget]]

The destination array to copy to

required
array_index int

The index in the array to start copying to

required

remove

remove(symbol: Union[Symbol, str, BaseContract]) -> bool
remove(
    target: KeyValuePair[Symbol, IPortfolioTarget],
) -> bool
remove(target: IPortfolioTarget) -> bool

Signature descriptions:

  • Removes the target for the specified symbol if it exists in this collection.

  • Removes the target for the specified symbol/target pair if it exists in this collection.

  • Removes the target if it exists in this collection.

Parameters:

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

The symbol to remove

None
target Optional[KeyValuePair[Symbol, IPortfolioTarget] | IPortfolioTarget]

The symbol/target pair to remove

None

Returns:

Type Description
bool

Depends on the signature used. Case 1: [True if the symbol's target was removed, false if it doesn't exist in the collection.]; Case 2: [True if the target was removed, false if it doesn't exist in the collection.]

__contains__

__contains__(
    symbol: Union[Symbol, str, BaseContract],
) -> bool

Determines whether the specified symbol exists as a key in this collection

Parameters:

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

The symbol key

required

Returns:

Type Description
bool

True if the symbol exists in this collection, false otherwise.

__getitem__

__getitem__(
    symbol: Union[Symbol, str, BaseContract],
) -> IPortfolioTarget

Gets or sets the portfolio target for the specified symbol

Parameters:

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

The symbol

required

Returns:

Type Description
IPortfolioTarget

The symbol's portfolio target if it exists in this collection, if not a KeyNotFoundException will be thrown.

__iter__

__iter__() -> (
    Iterator[KeyValuePair[Symbol, IPortfolioTarget]]
)

__len__

__len__() -> int

__setitem__

__setitem__(
    symbol: Union[Symbol, str, BaseContract],
    value: IPortfolioTarget,
) -> None

Gets or sets the portfolio target for the specified symbol

Parameters:

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

The symbol

required

Returns:

Type Description
None

The symbol's portfolio target if it exists in this collection, if not a KeyNotFoundException will be thrown.

add_range

add_range(targets: List[IPortfolioTarget]) -> None

Adds the specified targets to the collection. If a target for the same symbol already exists it will be overwritten.

Parameters:

Name Type Description Default
targets List[IPortfolioTarget]

The portfolio targets to add

required

clear

clear() -> None

Removes all portfolio targets from this collection

clear_fulfilled

clear_fulfilled(algorithm: IAlgorithm) -> None

Removes fulfilled portfolio targets from this collection. Will only take into account actual holdings and ignore open orders.

contains_key

contains_key(
    symbol: Union[Symbol, str, BaseContract],
) -> bool

Determines whether the specified symbol exists as a key in this collection

Parameters:

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

The symbol key

required

Returns:

Type Description
bool

True if the symbol exists in this collection, false otherwise.

get_enumerator

get_enumerator() -> IEnumerator[IPortfolioTarget]

Gets an enumerator to iterator over all portfolio targets in this collection. This is the default enumerator for this collection.

Returns:

Type Description
IEnumerator[IPortfolioTarget]

Portfolio targets enumerator.

order_by_margin_impact

order_by_margin_impact(
    algorithm: IAlgorithm,
) -> Iterable[IPortfolioTarget]

Returned an ordered enumerable where position reducing orders are executed first and the remaining orders are executed in decreasing order value. Will NOT return targets for securities that have no data yet. Will NOT return targets for which current holdings + open orders quantity, sum up to the target quantity

Parameters:

Name Type Description Default
algorithm IAlgorithm

The algorithm instance

required

try_get_value

try_get_value(
    symbol: Union[Symbol, str, BaseContract],
    target: Optional[IPortfolioTarget],
) -> Tuple[bool, IPortfolioTarget]

Attempts to retrieve the target for the specified symbol

Parameters:

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

The symbol

required
target Optional[IPortfolioTarget]

The portfolio target for the symbol, or null if not found

required

Returns:

Type Description
Tuple[bool, IPortfolioTarget]

True if the symbol's target was found, false if it does not exist in this collection.