Skip to content

SecurityManager

QuantConnect.Securities.SecurityManager

SecurityManager(time_keeper: ITimeKeeper)

Bases: ExtendedDictionary[Symbol, Security], IDictionary[Symbol, Security], INotifyCollectionChanged, Iterable[KeyValuePair[Symbol, Security]]

Enumerable security management class for grouping security objects into an array and providing any common properties.

Initialise the algorithm security manager with two empty dictionaries

Parameters:

Name Type Description Default
time_keeper ITimeKeeper
required

collection_changed

collection_changed: _EventContainer[
    Callable[
        [Object, NotifyCollectionChangedEventArgs], Any
    ],
    Any,
]

Event fired when a security is added or removed from this collection

utc_time

utc_time: datetime

Gets the most recent time this manager was updated

count

count: int

Count of the number of securities in the collection.

is_read_only

is_read_only: bool

Flag indicating if the internal array is read only.

get_keys

get_keys: Iterable[Symbol]

Gets an System.Collections.Generic.ICollection{T} containing the Symbol objects of the System.Collections.Generic.IDictionary{TKey, TValue}.

This codeEntityType is protected.

get_values

get_values: Iterable[Security]

Gets an System.Collections.Generic.ICollection{T} containing the values in the System.Collections.Generic.IDictionary{TKey, TValue}.

This codeEntityType is protected.

total

total: ICollection[Security]

Get a list of the complete security objects for this collection, including non active or delisted securities

__contains__

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

Signature descriptions:

  • Check if this collection contains this symbol.

  • Checks if the dictionary contains the specified key.

Parameters:

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

Symbol we're checking for.

None
key Optional[Union[Symbol, str, BaseContract]]

The key to locate in the dictionary

None

Returns:

Type Description
bool

Depends on the signature used. Case 1: [Bool true if contains this symbol pair.]; Case 2: [true if the dictionary contains an element with the specified key; otherwise, false.]

__getitem__

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

Signature descriptions:

  • Indexer method for the security manager to access the securities objects by their symbol.

  • Indexer method for the base dictioanry to access the objects by their symbol.

Parameters:

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

Symbol object indexer

None
key Optional[Union[Symbol, str, BaseContract]]

Key object indexer

None

Returns:

Type Description
Security

Depends on the signature used. Case 1: [Security.]; Case 2: [Object of t_value.]

__setitem__

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

Signature descriptions:

  • Indexer method for the security manager to access the securities objects by their symbol.

  • Indexer method for the base dictioanry to access the objects by their symbol.

Parameters:

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

Symbol object indexer

None
key Optional[Union[Symbol, str, BaseContract]]

Key object indexer

None

Returns:

Type Description
Optional[None]

Depends on the signature used. Case 1: [Security.]; Case 2: [Object of t_value.]

add

add(
    symbol: Union[Symbol, str, BaseContract],
    security: Security,
) -> None
add(security: Security) -> None
add(pair: KeyValuePair[Symbol, Security]) -> None

Signature descriptions:

  • Add a new security with this symbol to the collection.

  • Add a symbol-security by its key value pair.

Parameters:

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

symbol for security we're trading

None
security Optional[Security]

security object

None
pair Optional[KeyValuePair[Symbol, Security]]
None

contains_key

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

Signature descriptions:

  • Check if this collection contains this symbol.

  • Checks if the dictionary contains the specified key.

Parameters:

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

Symbol we're checking for.

None
key Optional[Union[Symbol, str, BaseContract]]

The key to locate in the dictionary

None

Returns:

Type Description
bool

Depends on the signature used. Case 1: [Bool true if contains this symbol pair.]; Case 2: [true if the dictionary contains an element with the specified key; otherwise, false.]

create_security

create_security(
    symbol: Union[Symbol, str, BaseContract],
    subscription_data_config_list: List[
        SubscriptionDataConfig
    ],
    leverage: float = 0,
    add_to_symbol_cache: bool = True,
    underlying: Security = None,
) -> Security
create_security(
    symbol: Union[Symbol, str, BaseContract],
    subscription_data_config: SubscriptionDataConfig,
    leverage: float = 0,
    add_to_symbol_cache: bool = True,
    underlying: Security = None,
) -> Security

Creates a new security

get

get(key: Union[Symbol, str, BaseContract]) -> Security
get(
    key: Union[Symbol, str, BaseContract], value: Security
) -> Security

Returns the value for the specified key if key is in dictionary.

Parameters:

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

key to be searched in the dictionary

required
value Optional[Security]

Value to be returned if the key is not found. The default value is null.

None

Returns:

Type Description
Security

The value for the specified key if key is in dictionary.

pop

pop(key: Union[Symbol, str, BaseContract]) -> Security
pop(
    key: Union[Symbol, str, BaseContract],
    default_value: Security,
) -> Security

Removes and returns an element from a dictionary having the given key.

Parameters:

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

Key which is to be searched for removal

required
default_value Optional[Security]

Value which is to be returned when the key is not in the dictionary

None

Returns:

Type Description
Security

If key is found - removed/popped element from the dictionary

remove

remove(pair: KeyValuePair[Symbol, Security]) -> bool
remove(symbol: Union[Symbol, str, BaseContract]) -> bool
remove(key: Union[Symbol, str, BaseContract]) -> bool

Signature descriptions:

  • Remove a key value of of symbol-securities from the collections.

  • Remove this symbol security: Dictionary interface implementation.

  • Removes the value with the specified key

Parameters:

Name Type Description Default
pair Optional[KeyValuePair[Symbol, Security]]

Key Value pair of symbol-security to remove

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

Symbol we're searching for

None
key Optional[Union[Symbol, str, BaseContract]]

The key object of the element to remove.

None

Returns:

Type Description
bool

Depends on the signature used. Case 1: [Boolean true on success.]; Case 2: [true success.]; Case 3: [true if the element is successfully found and removed; otherwise, false.]

__iter__

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

__len__

__len__() -> int

setdefault

setdefault(
    key: Union[Symbol, str, BaseContract],
) -> Security
setdefault(
    key: Union[Symbol, str, BaseContract],
    default_value: Security,
) -> Security

Returns the value of a key (if the key is in dictionary). If not, it inserts key with a value to the dictionary.

Parameters:

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

Key with null/None value is inserted to the dictionary if key is not in the dictionary.

required
default_value Optional[Security]

Default value

None

Returns:

Type Description
Security

The value of the key if it is in the dictionary

try_get_value

try_get_value(
    symbol: Union[Symbol, str, BaseContract],
    security: Optional[Security],
) -> Tuple[bool, Security]
try_get_value(
    key: Union[Symbol, str, BaseContract],
    value: Optional[Security],
) -> Tuple[bool, Security]

Signature descriptions:

  • Try and get this security object with matching symbol and return true on success.

  • Gets the value associated with the specified key.

Parameters:

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

String search symbol

None
security Optional[Optional[Security]]

Output Security object

None
key Optional[Union[Symbol, str, BaseContract]]

The key whose value to get.

None
value Optional[Optional[Security]]

When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.

None

Returns:

Type Description
Tuple[bool, Security]

Depends on the signature used. Case 1: [True on successfully locating the security object.]; Case 2: [true if the object that implements System.Collections.Generic.IDictionary`2 contains an element with the specified key; otherwise, false.]

clear

clear() -> None

Clear the securities array to delete all the portfolio and asset information.

contains

contains(pair: KeyValuePair[Symbol, Security]) -> bool

Check if this collection contains this key value pair.

Parameters:

Name Type Description Default
pair KeyValuePair[Symbol, Security]

Search key-value pair

required

Returns:

Type Description
bool

Bool true if contains this key-value pair.

copy_to

copy_to(
    array: List[KeyValuePair[Symbol, Security]], number: int
) -> None

Copy from the internal array to an external array.

Parameters:

Name Type Description Default
array List[KeyValuePair[Symbol, Security]]

Array we're outputting to

required
number int

Starting index of array

required

create_benchmark_security

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

Creates a new benchmark security

get_items

get_items() -> Iterable[KeyValuePair[Symbol, Security]]

Gets all the items in the dictionary

Returns:

Type Description
Iterable[KeyValuePair[Symbol, Security]]

All the items in the dictionary.

on_collection_changed

on_collection_changed(
    changed_event_args: NotifyCollectionChangedEventArgs,
) -> None

Event invocator for the collection_changed event

This codeEntityType is protected.

Parameters:

Name Type Description Default
changed_event_args NotifyCollectionChangedEventArgs

Event arguments for the collection_changed event

required

set_live_mode

set_live_mode(is_live_mode: bool) -> None

Set live mode state of the algorithm

Parameters:

Name Type Description Default
is_live_mode bool

True, live mode is enabled

required

set_security_service

set_security_service(
    security_service: SecurityService,
) -> None

Sets the Security Service to be used

fromkeys

fromkeys(
    sequence: List[QuantConnect_ExtendedDictionary_TKey],
) -> Dict[Any, Any]
fromkeys(
    sequence: List[QuantConnect_ExtendedDictionary_TKey],
    value: QuantConnect_ExtendedDictionary_TValue,
) -> Dict[Any, Any]

Signature descriptions:

  • Creates a new dictionary from the given sequence of elements.

  • Creates a new dictionary from the given sequence of elements with a value provided by the user.

Parameters:

Name Type Description Default
sequence List[QuantConnect_ExtendedDictionary_TKey]

Sequence of elements which is to be used as keys for the new dictionary

required
value Optional[QuantConnect_ExtendedDictionary_TValue]

Value which is set to each each element of the dictionary

None

Returns:

Type Description
Dict[Any, Any]

Returns a new dictionary with the given sequence of elements as the keys of the dictionary.

copy

copy() -> Dict[Any, Any]

Creates a shallow copy of the IExtendedDictionary{TKey, TValue}.

Returns:

Type Description
Dict[Any, Any]

Returns a shallow copy of the dictionary. It doesn't modify the original dictionary.

items

items() -> List[Any]

Returns a view object that displays a list of dictionary's (key, value) tuple pairs.

Returns:

Type Description
List[Any]

Returns a view object that displays a list of a given dictionary's (key, value) tuple pair.

keys

keys() -> List[Any]

Returns a view object that displays a list of all the key objects in the dictionary

Returns:

Type Description
List[Any]

Returns a view object that displays a list of all the key objects. When the dictionary is changed, the view object also reflect these changes.

popitem

popitem() -> Any

Returns and removes an arbitrary element (key, value) pair from the dictionary.

Returns:

Type Description
Any

Returns an arbitrary element (key, value) pair from the dictionary removes an arbitrary element(the same element which is returned) from the dictionary. Note: Arbitrary elements and random elements are not same.The popitem() doesn't return a random element.

update

update(other: Any) -> None

Updates the dictionary with the elements from the another dictionary object or from an iterable of key/value pairs. The update() method adds element(s) to the dictionary if the key is not in the dictionary.If the key is in the dictionary, it updates the key with the new value.

Parameters:

Name Type Description Default
other Any

Takes either a dictionary or an iterable object of key/value pairs (generally tuples).

required

values

values() -> List[Any]

Returns a view object that displays a list of all the values in the dictionary.

Returns:

Type Description
List[Any]

Returns a view object that displays a list of all values in a given dictionary.

check_for_implicitly_created_symbol

check_for_implicitly_created_symbol(
    symbol: Union[Symbol, str, BaseContract],
) -> None

Checks if the symbol is implicitly created from a string, in which case it is not in the symbol cache, and throws a KeyNotFoundException.

This codeEntityType is protected.