Skip to content

CashBook

QuantConnect.Securities.CashBook

CashBook()

Bases: ExtendedDictionary[str, Cash], IDictionary[str, Cash], ICurrencyConverter, Iterable[KeyValuePair[str, Cash]]

Provides a means of keeping track of the different cash holdings of an algorithm

Initializes a new instance of the CashBook class.

updated

updated: _EventContainer[
    Callable[[Object, CashBookUpdatedEventArgs], Any], Any
]

Event fired when a Cash instance is added or removed, and when the Cash.updated is triggered for the currently hold instances

account_currency

account_currency: str

Gets the base currency used

total_value_in_account_currency

total_value_in_account_currency: float

Gets the total value of the cash book in units of the base currency

count

count: int

Gets the count of Cash items in this CashBook.

is_read_only

is_read_only: bool

Gets a value indicating whether this instance is read only.

get_keys

get_keys: Iterable[str]

Gets the keys.

This codeEntityType is protected.

get_values

get_values: Iterable[Cash]

Gets the values.

This codeEntityType is protected.

add

add(
    symbol: str, quantity: float, conversion_rate: float
) -> Cash
add(item: KeyValuePair[str, Cash]) -> None
add(symbol: str, value: Cash) -> None

Signature descriptions:

  • Adds a new cash of the specified symbol and quantity

  • Add the specified item to this CashBook.

  • Add the specified key and value.

Parameters:

Name Type Description Default
symbol Optional[str]

The symbol used to reference the new cash

None
quantity Optional[float]

The amount of new cash to start

None
conversion_rate Optional[float]

The conversion rate used to determine the initial

None
item Optional[KeyValuePair[str, Cash]]

KeyValuePair of symbol -> Cash item

None
value Optional[Cash]

Value.

None

Returns:

Type Description
Optional[Cash]

The added cash instance.

convert_to_account_currency

convert_to_account_currency(
    source_quantity: float, source_currency: str
) -> float
convert_to_account_currency(
    cash_amount: CashAmount,
) -> CashAmount

Signature descriptions:

  • Converts a quantity of source currency units into the account currency

  • Converts a cash amount to the account currency

Parameters:

Name Type Description Default
source_quantity Optional[float]

The quantity of source currency to be converted

None
source_currency Optional[str]

The source currency symbol

None
cash_amount Optional[CashAmount]

The CashAmount instance to convert

None

Returns:

Type Description
float | CashAmount

Depends on the signature used. Case 1: [The converted value.]; Case 2: [A new CashAmount instance denominated in the account currency.]

remove

remove(symbol: str) -> bool
remove(item: KeyValuePair[str, Cash]) -> bool

Signature descriptions:

  • Remove the Cash item corresponding to the specified symbol

  • Remove the specified item.

Parameters:

Name Type Description Default
symbol Optional[str]

The symbolto be removed

None
item Optional[KeyValuePair[str, Cash]]

Item.

None

__contains__

__contains__(symbol: str) -> bool

Determines whether the current instance contains an entry with the specified symbol.

Parameters:

Name Type Description Default
symbol str

Key.

required

Returns:

Type Description
bool

true, if key was contained, false otherwise.

__getitem__

__getitem__(symbol: str) -> Cash

Gets or sets the QuantConnect.Securities.Cash with the specified symbol.

Parameters:

Name Type Description Default
symbol str

Symbol.

required

__iter__

__iter__() -> Iterator[KeyValuePair[str, Cash]]

__len__

__len__() -> int

__setitem__

__setitem__(symbol: str, value: Cash) -> None

Gets or sets the QuantConnect.Securities.Cash with the specified symbol.

Parameters:

Name Type Description Default
symbol str

Symbol.

required

clear

clear() -> None

Clear this instance of all Cash entries.

contains

contains(item: KeyValuePair[str, Cash]) -> bool

Determines whether the current collection contains the specified value.

Parameters:

Name Type Description Default
item KeyValuePair[str, Cash]

Item.

required

contains_key

contains_key(symbol: str) -> bool

Determines whether the current instance contains an entry with the specified symbol.

Parameters:

Name Type Description Default
symbol str

Key.

required

Returns:

Type Description
bool

true, if key was contained, false otherwise.

convert

convert(
    source_quantity: float,
    source_currency: str,
    destination_currency: str,
) -> float

Converts a quantity of source currency units into the specified destination currency

Parameters:

Name Type Description Default
source_quantity float

The quantity of source currency to be converted

required
source_currency str

The source currency symbol

required
destination_currency str

The destination currency symbol

required

Returns:

Type Description
float

The converted value.

copy_to

copy_to(
    array: List[KeyValuePair[str, Cash]], array_index: int
) -> None

Copies to the specified array.

Parameters:

Name Type Description Default
array List[KeyValuePair[str, Cash]]

Array.

required
array_index int

Array index.

required

ensure_currency_data_feeds

ensure_currency_data_feeds(
    securities: SecurityManager,
    subscriptions: SubscriptionManager,
    market_map: IReadOnlyDictionary[SecurityType, str],
    changes: SecurityChanges,
    security_service: ISecurityService,
    default_resolution: Resolution = ...,
) -> List[SubscriptionDataConfig]

Checks the current subscriptions and adds necessary currency pair feeds to provide real time conversion data

Parameters:

Name Type Description Default
securities SecurityManager

The SecurityManager for the algorithm

required
subscriptions SubscriptionManager

The SubscriptionManager for the algorithm

required
market_map IReadOnlyDictionary[SecurityType, str]

The market map that decides which market the new security should be in

required
changes SecurityChanges

Will be used to consume SecurityChanges.added_securities

required
security_service ISecurityService

Will be used to create required new Security

required
default_resolution Resolution

The default resolution to use for the internal subscriptions

...

Returns:

Type Description
List[SubscriptionDataConfig]

Returns a list of added currency SubscriptionDataConfig.

get_enumerator

get_enumerator() -> IEnumerator[KeyValuePair[str, Cash]]

Gets the enumerator.

Returns:

Type Description
IEnumerator[KeyValuePair[str, Cash]]

The enumerator.

get_items

get_items() -> Iterable[KeyValuePair[str, Cash]]

Gets all the items in the dictionary

Returns:

Type Description
Iterable[KeyValuePair[str, Cash]]

All the items in the dictionary.

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_value

try_get_value(
    symbol: str, value: Optional[Cash]
) -> Tuple[bool, Cash]

Try to get the value.

Parameters:

Name Type Description Default
symbol str

The symbol.

required
value Optional[Cash]

Value.

required

Returns:

Type Description
Tuple[bool, Cash]

true, if get value was tryed, false otherwise.

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.

get

get(
    key: QuantConnect_ExtendedDictionary_TKey,
) -> QuantConnect_ExtendedDictionary_TValue
get(
    key: QuantConnect_ExtendedDictionary_TKey,
    value: QuantConnect_ExtendedDictionary_TValue,
) -> QuantConnect_ExtendedDictionary_TValue

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

Parameters:

Name Type Description Default
key QuantConnect_ExtendedDictionary_TKey

key to be searched in the dictionary

required
value Optional[QuantConnect_ExtendedDictionary_TValue]

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

None

Returns:

Type Description
QuantConnect_ExtendedDictionary_TValue

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

pop

pop(
    key: QuantConnect_ExtendedDictionary_TKey,
) -> QuantConnect_ExtendedDictionary_TValue
pop(
    key: QuantConnect_ExtendedDictionary_TKey,
    default_value: QuantConnect_ExtendedDictionary_TValue,
) -> QuantConnect_ExtendedDictionary_TValue

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

Parameters:

Name Type Description Default
key QuantConnect_ExtendedDictionary_TKey

Key which is to be searched for removal

required
default_value Optional[QuantConnect_ExtendedDictionary_TValue]

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

None

Returns:

Type Description
QuantConnect_ExtendedDictionary_TValue

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

setdefault

setdefault(
    key: QuantConnect_ExtendedDictionary_TKey,
) -> QuantConnect_ExtendedDictionary_TValue
setdefault(
    key: QuantConnect_ExtendedDictionary_TKey,
    default_value: QuantConnect_ExtendedDictionary_TValue,
) -> QuantConnect_ExtendedDictionary_TValue

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 QuantConnect_ExtendedDictionary_TKey

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

required
default_value Optional[QuantConnect_ExtendedDictionary_TValue]

Default value

None

Returns:

Type Description
QuantConnect_ExtendedDictionary_TValue

The value of the key if it is in 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.