Cache

The cache subpackage provides common caching infrastructure.

A running Nautilus system generally uses a single centralized cache which can be accessed by many components.

class Cache ( Logger logger , CacheDatabase database: Optional[CacheDatabase] = None , config: Optional[CacheConfig] = None )

Bases: CacheFacade

Provides a common object cache for market and execution related data.

Parameters :
  • logger ( Logger ) – The logger for the cache.

  • database ( CacheDatabase , optional ) – The database for the cache. If None then will bypass persistence.

  • config ( CacheConfig , optional ) – The cache configuration.

Raises :

TypeError – If config is not of type CacheConfig .

account ( self , AccountId account_id ) Account

Return the account matching the given ID (if found).

Parameters :

account_id ( AccountId ) – The account ID.

Returns :

Account or None

account_for_venue ( self , Venue venue ) Account

Return the account matching the given client ID (if found).

Parameters :

venue ( Venue ) – The venue for the account.

Returns :

Account or None

account_id ( self , Venue venue ) AccountId

Return the account ID for the given venue (if found).

Parameters :

venue ( Venue ) – The venue for the account ID.

Returns :

AccountId or None

accounts ( self ) list

Return all accounts in the cache.

Returns :

list[Account]

actor_ids ( self ) set

Return all actor IDs.

Returns :

set[ComponentId]

add ( self , unicode key , bytes value ) void

Add the given general object to the cache.

The cache is agnostic to what the object actually is (and how it may be serialized), offering maximum flexibility.

Parameters :
  • key ( str ) – The cache key for the object.

  • value ( bytes ) – The object value to write.

add_account ( self , Account account ) void

Add the given account to the cache.

Parameters :

account ( Account ) – The account to add.

Raises :

ValueError – If account_id is already contained in the cache.

add_bar ( self , Bar bar ) void

Add the given bar to the cache.

Parameters :

bar ( Bar ) – The bar to add.

add_bars ( self , list bars ) void

Add the given bars to the cache.

Parameters :

bars ( list [ Bar ] ) – The bars to add.

add_currency ( self , Currency currency ) void

Add the given currency to the cache.

Parameters :

currency ( Currency ) – The currency to add.

add_instrument ( self , Instrument instrument ) void

Add the given instrument to the cache.

Parameters :

instrument ( Instrument ) – The instrument to add.

add_order ( self , Order order , PositionId position_id , bool override=False ) void

Add the given order to the cache indexed with the given position ID.

Parameters :
  • order ( Order ) – The order to add.

  • position_id ( PositionId ) – The position ID to index for the order.

  • override ( bool , default False ) – If the added order should ‘override’ any existing order and replace it in the cache. This is currently used for emulated orders which are being released and transformed into another type.

Raises :

ValueError – If order.client_order_id is already contained in the cache.

add_order_book ( self , OrderBook order_book ) void

Add the given order book to the cache.

Parameters :

order_book ( OrderBook ) – The order book to add.

add_order_list ( self , OrderList order_list ) void

Add the given order list to the cache.

Parameters :

order_list ( OrderList ) – The order_list to add.

Raises :

ValueError – If order_list.id is already contained in the cache.

add_position ( self , Position position , OmsType oms_type ) void

Add the given position to the cache.

Parameters :
  • position ( Position ) – The position to add.

  • oms_type ( OmsType ) – The order management system type for the position.

Raises :

ValueError – If oms_type is HEDGING and a virtual position.id is already contained in the cache.

add_position_id ( self , PositionId position_id , Venue venue , ClientOrderId client_order_id , StrategyId strategy_id ) void

Index the given position ID with the other given IDs.

Parameters :
  • position_id ( PositionId ) – The position ID to index.

  • venue ( Venue ) – The venue ID to index with the position ID.

  • client_order_id ( ClientOrderId ) – The client order ID to index with the position ID.

  • strategy_id ( StrategyId ) – The strategy ID to index with the position ID.

add_quote_tick ( self , QuoteTick tick ) void

Add the given quote tick to the cache.

Parameters :

tick ( QuoteTick ) – The tick to add.

add_quote_ticks ( self , list ticks ) void

Add the given quote ticks to the cache.

Parameters :

ticks ( list [ QuoteTick ] ) – The ticks to add.

add_submit_order_command ( self , SubmitOrder command ) void

Add the given command to the cache.

Parameters :

command ( SubmitOrder ) – The command to add to the cache.

add_submit_order_list_command ( self , SubmitOrderList command ) void

Add the given command to the cache.

Parameters :

command ( SubmitOrderList ) – The command to add to the cache.

add_ticker ( self , Ticker ticker ) void

Add the given ticker to the cache.

Parameters :

ticker ( Ticker ) – The ticker to add.

add_trade_tick ( self , TradeTick tick ) void

Add the given trade tick to the cache.

Parameters :

tick ( TradeTick ) – The tick to add.

add_trade_ticks ( self , list ticks ) void

Add the given trade ticks to the cache.

Parameters :

ticks ( list [ TradeTick ] ) – The ticks to add.

bar ( self , BarType bar_type , int index=0 ) Bar

Return the bar for the given bar type at the given index.

Last bar if no index specified.

Parameters :
  • bar_type ( BarType ) – The bar type to get.

  • index ( int , optional ) – The index for the bar to get.

Returns :

Bar or None – If no bars or no bar at index then returns None .

Notes

Reverse indexed (most recent bar at index 0).

bar_capacity

The caches bar capacity.

Returns :

int

bar_count ( self , BarType bar_type ) int

The count of bars for the given bar type.

Parameters :

bar_type ( BarType ) – The bar type to count.

Returns :

int

bars ( self , BarType bar_type ) list

Return bars for the given bar type.

Parameters :

bar_type ( BarType ) – The bar type for bars to get.

Returns :

list[Bar]

book_update_count ( self , InstrumentId instrument_id ) int

The count of order book updates for the given instrument ID.

Will return zero if there is no book for the instrument ID.

Parameters :

instrument_id ( InstrumentId ) – The instrument ID for the book.

Returns :

int

build_index ( self ) void

Clear the current cache index and re-build.

cache_accounts ( self ) void

Clear the current accounts cache and load accounts from the cache database.

cache_commands ( self ) void

Clear the current submit order commands cache and load commands from the cache database.

cache_currencies ( self ) void

Clear the current currencies cache and load currencies from the cache database.

cache_general ( self ) void

Clear the current general cache and load the general objects from the cache database.

cache_instruments ( self ) void

Clear the current instruments cache and load instruments from the cache database.

cache_order_lists ( self ) void

Clear the current order lists cache and load order lists using cached orders.

cache_orders ( self ) void

Clear the current orders cache and load orders from the cache database.

cache_positions ( self ) void

Clear the current positions cache and load positions from the cache database.

check_integrity ( self ) bool

Check integrity of data within the cache.

All data should be loaded from the database prior to this call. If an error is found then a log error message will also be produced.

Returns :

bool – True if checks pass, else False.

check_residuals ( self ) bool

Check for any residual open state and log warnings if any are found.

‘Open state’ is considered to be open orders and open positions.

Returns :

bool – True if residuals exist, else False.

clear_index ( self ) void
client_order_id ( self , VenueOrderId venue_order_id ) ClientOrderId

Return the client order ID matching the given venue order ID (if found).

Parameters :

venue_order_id ( VenueOrderId ) – The venue assigned order ID.

Returns :

ClientOrderId or None

client_order_ids ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) set

Return all client order IDs with the given query filters.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

Returns :

set[ClientOrderId]

client_order_ids_closed ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) set

Return all closed client order IDs with the given query filters.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

Returns :

set[ClientOrderId]

client_order_ids_emulated ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) set

Return all emulated client order IDs with the given query filters.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

Returns :

set[ClientOrderId]

client_order_ids_inflight ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) set

Return all in-flight client order IDs with the given query filters.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

Returns :

set[ClientOrderId]

client_order_ids_open ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) set

Return all open client order IDs with the given query filters.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

Returns :

set[ClientOrderId]

delete_actor ( self , Actor actor ) void

Delete the given actor from the cache.

Parameters :

actor ( Actor ) – The actor to deregister.

Raises :

ValueError – If actor is not contained in the actors index.

delete_strategy ( self , Strategy strategy ) void

Delete the given strategy from the cache.

Parameters :

strategy ( Strategy ) – The strategy to deregister.

Raises :

ValueError – If strategy is not contained in the strategies index.

exec_algorithm_ids ( self ) set

Return all execution algorithm IDs.

Returns :

set[ExecAlgorithmId]

flush_db ( self ) void

Flush the caches database which permanently removes all persisted data.

Warning

Permanent data loss.

get ( self , unicode key ) bytes

Add the given general object to the cache.

The cache is agnostic to what the object actually is (and how it may be serialized), offering maximum flexibility.

Parameters :

key ( str ) – The cache key for the object.

Returns :

bytes or None

get_xrate ( self , Venue venue , Currency from_currency , Currency to_currency , PriceType price_type=PriceType.MID ) double

Return the calculated exchange rate.

Parameters :
  • venue ( Venue ) – The venue for the exchange rate.

  • from_currency ( Currency ) – The currency to convert from.

  • to_currency ( Currency ) – The currency to convert to.

  • price_type ( PriceType ) – The price type for the exchange rate.

Returns :

double

Raises :

ValueError – If price_type is LAST .

has_bars ( self , BarType bar_type ) bool

Return a value indicating whether the cache has bars for the given bar type.

Parameters :

bar_type ( BarType ) – The bar type for the bars.

Returns :

bool

has_order_book ( self , InstrumentId instrument_id ) bool

Return a value indicating whether the cache has an order book snapshot for the given instrument ID.

Parameters :

instrument_id ( InstrumentId ) – The instrument ID for the order book snapshot.

Returns :

bool

has_quote_ticks ( self , InstrumentId instrument_id ) bool

Return a value indicating whether the cache has quote ticks for the given instrument ID.

Parameters :

instrument_id ( InstrumentId ) – The instrument ID for the ticks.

Returns :

bool

has_tickers ( self , InstrumentId instrument_id ) bool

Return a value indicating whether the cache has tickers for the given instrument ID.

Parameters :

instrument_id ( InstrumentId ) – The instrument ID for the ticks.

Returns :

bool

has_trade_ticks ( self , InstrumentId instrument_id ) bool

Return a value indicating whether the cache has trade ticks for the given instrument ID.

Parameters :

instrument_id ( InstrumentId ) – The instrument ID for the ticks.

Returns :

bool

instrument ( self , InstrumentId instrument_id ) Instrument

Return the instrument corresponding to the given instrument ID.

Parameters :

instrument_id ( InstrumentId ) – The instrument ID of the instrument to return.

Returns :

Instrument or None

instrument_ids ( self , Venue venue=None ) list

Return all instrument IDs held by the cache.

Parameters :

venue ( Venue , optional ) – The venue filter for the query.

Returns :

list[InstrumentId]

instruments ( self , Venue venue=None ) list

Return all instruments held by the cache.

Parameters :

venue ( Venue , optional ) – The venue filter for the query.

Returns :

list[Instrument]

is_order_closed ( self , ClientOrderId client_order_id ) bool

Return a value indicating whether an order with the given ID is closed.

Parameters :

client_order_id ( ClientOrderId ) – The client order ID to check.

Returns :

bool

is_order_emulated ( self , ClientOrderId client_order_id ) bool

Return a value indicating whether an order with the given ID is emulated.

Parameters :

client_order_id ( ClientOrderId ) – The client order ID to check.

Returns :

bool

is_order_inflight ( self , ClientOrderId client_order_id ) bool

Return a value indicating whether an order with the given ID is in-flight.

Parameters :

client_order_id ( ClientOrderId ) – The client order ID to check.

Returns :

bool

is_order_open ( self , ClientOrderId client_order_id ) bool

Return a value indicating whether an order with the given ID is open.

Parameters :

client_order_id ( ClientOrderId ) – The client order ID to check.

Returns :

bool

is_position_closed ( self , PositionId position_id ) bool

Return a value indicating whether a position with the given ID exists and is closed.

Parameters :

position_id ( PositionId ) – The position ID.

Returns :

bool

is_position_open ( self , PositionId position_id ) bool

Return a value indicating whether a position with the given ID exists and is open.

Parameters :

position_id ( PositionId ) – The position ID.

Returns :

bool

load_account ( self , AccountId account_id ) Account

Load the account associated with the given account_id (if found).

Parameters :

account_id ( AccountId ) – The account ID to load.

Returns :

Account or None

load_actor ( self , Actor actor ) void

Load the state dictionary into the given actor.

Parameters :

actor ( Actor ) – The actor to load.

load_instrument ( self , InstrumentId instrument_id ) Instrument

Load the instrument associated with the given instrument_id (if found).

Parameters :

instrument_id ( InstrumentId ) – The instrument ID to load.

Returns :

Account or None

load_order ( self , ClientOrderId client_order_id ) Order

Load the order associated with the given ID (if found).

Parameters :

client_order_id ( ClientOrderId ) – The client order ID to load.

Returns :

Order or None

load_position ( self , PositionId position_id ) Position

Load the position associated with the given ID (if found).

Parameters :

position_id ( PositionId ) – The position ID to load.

Returns :

Position or None

load_strategy ( self , Strategy strategy ) void

Load the state dictionary into the given strategy.

Parameters :

strategy ( Strategy ) – The strategy to load.

load_submit_order_command ( self , ClientOrderId client_order_id ) SubmitOrder

Load the command associated with the given client order ID (if found).

Parameters :

client_order_id ( ClientOrderId ) – The client order ID for the command to load.

Returns :

SubmitOrder or None

load_submit_order_list_command ( self , OrderListId order_list_id ) SubmitOrderList

Load the command associated with the given order list ID (if found).

Parameters :

order_list_id ( OrderListId ) – The order list ID for the command to load.

Returns :

SubmitOrderList or None

order ( self , ClientOrderId client_order_id ) Order

Return the order matching the given client order ID (if found).

Returns :

Order or None

order_book ( self , InstrumentId instrument_id ) OrderBook

Return the order book for the given instrument ID.

Parameters :

instrument_id ( InstrumentId ) –

Returns :

OrderBook or None

order_exists ( self , ClientOrderId client_order_id ) bool

Return a value indicating whether an order with the given ID exists.

Parameters :

client_order_id ( ClientOrderId ) – The client order ID to check.

Returns :

bool

order_list ( self , OrderListId order_list_id ) OrderList

Return the order list matching the given order list ID (if found).

Returns :

OrderList or None

order_list_exists ( self , OrderListId order_list_id ) bool

Return a value indicating whether an order list with the given ID exists.

Parameters :

order_list_id ( OrderListId ) – The order list ID to check.

Returns :

bool

order_list_ids ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) set

Return all order list IDs.

Returns :

set[OrderListId]

order_lists ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) list

Return all order lists matching the given query filters.

No particular order of list elements is guaranteed.

Returns :

list[OrderList]

orders ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , OrderSide side=OrderSide.NO_ORDER_SIDE ) list

Return all orders matching the given query filters.

No particular order of list elements is guaranteed.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

  • side (OrderSide, default NO_ORDER_SIDE (no filter)) – The order side query filter.

Returns :

list[Order]

orders_closed ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , OrderSide side=OrderSide.NO_ORDER_SIDE ) list

Return all closed orders with the given query filters.

No particular order of list elements is guaranteed.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

  • side (OrderSide, default NO_ORDER_SIDE (no filter)) – The order side query filter.

Returns :

list[Order]

orders_closed_count ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , OrderSide side=OrderSide.NO_ORDER_SIDE ) int

Return the count of closed orders with the given query filters.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

  • side (OrderSide, default NO_ORDER_SIDE (no filter)) – The order side query filter.

Returns :

int

orders_emulated ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , OrderSide side=OrderSide.NO_ORDER_SIDE ) list

Return all emulated orders with the given query filters.

No particular order of list elements is guaranteed.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

  • side (OrderSide, default NO_ORDER_SIDE (no filter)) – The order side query filter.

Returns :

list[Order]

orders_emulated_count ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , OrderSide side=OrderSide.NO_ORDER_SIDE ) int

Return the count of emulated orders with the given query filters.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

  • side (OrderSide, default NO_ORDER_SIDE (no filter)) – The order side query filter.

Returns :

int

orders_for_exec_algorithm ( self , ExecAlgorithmId exec_algorithm_id , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , OrderSide side=OrderSide.NO_ORDER_SIDE ) list

Return all execution algorithm orders for the given query filters.

Parameters :
  • exec_algorithm_id ( ExecAlgorithmId ) – The execution algorithm ID.

  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

  • side (OrderSide, default NO_ORDER_SIDE (no filter)) – The order side query filter.

Returns :

list[Order]

orders_for_exec_spawn ( self , ClientOrderId client_order_id ) list

Return all orders for the given execution spawn ID (if found).

Will also include the primary (original) order.

Parameters :

client_order_id ( ClientOrderId ) – The execution algorithm spawning primary (original) client order ID.

Returns :

list[Order]

orders_for_position ( self , PositionId position_id ) list

Return all orders for the given position ID.

Parameters :

position_id ( PositionId ) – The position ID for the orders.

Returns :

list[Order]

orders_inflight ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , OrderSide side=OrderSide.NO_ORDER_SIDE ) list

Return all in-flight orders with the given query filters.

No particular order of list elements is guaranteed.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

  • side (OrderSide, default NO_ORDER_SIDE (no filter)) – The order side query filter.

Returns :

list[Order]

orders_inflight_count ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , OrderSide side=OrderSide.NO_ORDER_SIDE ) int

Return the count of in-flight orders with the given query filters.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

  • side (OrderSide, default NO_ORDER_SIDE (no filter)) – The order side query filter.

Returns :

int

orders_open ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , OrderSide side=OrderSide.NO_ORDER_SIDE ) list

Return all open orders with the given query filters.

No particular order of list elements is guaranteed.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

  • side (OrderSide, default NO_ORDER_SIDE (no filter)) – The order side query filter.

Returns :

list[Order]

orders_open_count ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , OrderSide side=OrderSide.NO_ORDER_SIDE ) int

Return the count of open orders with the given query filters.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

  • side (OrderSide, default NO_ORDER_SIDE (no filter)) – The order side query filter.

Returns :

int

orders_total_count ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , OrderSide side=OrderSide.NO_ORDER_SIDE ) int

Return the total count of orders with the given query filters.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

  • side (OrderSide, default NO_ORDER_SIDE (no filter)) – The order side query filter.

Returns :

int

position ( self , PositionId position_id ) Position

Return the position associated with the given ID (if found).

Parameters :

position_id ( PositionId ) – The position ID.

Returns :

Position or None

position_closed_ids ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) set

Return all closed position IDs with the given query filters.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

Returns :

set[PositionId]

position_exists ( self , PositionId position_id ) bool

Return a value indicating whether a position with the given ID exists.

Parameters :

position_id ( PositionId ) – The position ID.

Returns :

int

position_for_order ( self , ClientOrderId client_order_id ) Position

Return the position associated with the given client order ID (if found).

Parameters :

client_order_id ( ClientOrderId ) – The client order ID.

Returns :

Position or None

position_id ( self , ClientOrderId client_order_id ) PositionId

Return the position ID associated with the given client order ID (if found).

Parameters :

client_order_id ( ClientOrderId ) – The client order ID associated with the position.

Returns :

PositionId or None

position_ids ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) set

Return all position IDs with the given query filters.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

Returns :

set[PositionId]

position_open_ids ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) set

Return all open position IDs with the given query filters.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

Returns :

set[PositionId]

position_snapshots ( self , PositionId position_id=None ) list

Return all position snapshots with the given optional identifier filter.

Parameters :

position_id ( PositionId , optional ) – The position ID query filter.

Returns :

list[Position]

positions ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , PositionSide side=PositionSide.NO_POSITION_SIDE ) list

Return all positions with the given query filters.

No particular order of list elements is guaranteed.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

  • side (PositionSide, default NO_POSITION_SIDE (no filter)) – The position side query filter.

Returns :

list[Position]

positions_closed ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) list

Return all closed positions with the given query filters.

No particular order of list elements is guaranteed.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

Returns :

list[Position]

positions_closed_count ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) int

Return the count of closed positions with the given query filters.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

Returns :

int

positions_open ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , PositionSide side=PositionSide.NO_POSITION_SIDE ) list

Return all open positions with the given query filters.

No particular order of list elements is guaranteed.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

  • side (PositionSide, default NO_POSITION_SIDE (no filter)) – The position side query filter.

Returns :

list[Position]

positions_open_count ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , PositionSide side=PositionSide.NO_POSITION_SIDE ) int

Return the count of open positions with the given query filters.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

  • side (PositionSide, default NO_POSITION_SIDE (no filter)) – The position side query filter.

Returns :

int

positions_total_count ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , PositionSide side=PositionSide.NO_POSITION_SIDE ) int

Return the total count of positions with the given query filters.

Parameters :
  • venue ( Venue , optional ) – The venue ID query filter.

  • instrument_id ( InstrumentId , optional ) – The instrument ID query filter.

  • strategy_id ( StrategyId , optional ) – The strategy ID query filter.

  • side (PositionSide, default NO_POSITION_SIDE (no filter)) – The position side query filter.

Returns :

int

price ( self , InstrumentId instrument_id , PriceType price_type ) Price

Return the price for the given instrument ID and price type.

Parameters :
  • instrument_id ( InstrumentId ) – The instrument ID for the price.

  • price_type ( PriceType ) – The price type for the query.

Returns :

Price or None

quote_tick ( self , InstrumentId instrument_id , int index=0 ) QuoteTick

Return the quote tick for the given instrument ID at the given index.

Last quote tick if no index specified.

Parameters :
  • instrument_id ( InstrumentId ) – The instrument ID for the tick to get.

  • index ( int , optional ) – The index for the tick to get.

Returns :

QuoteTick or None – If no ticks or no tick at index then returns None .

Notes

Reverse indexed (most recent tick at index 0).

quote_tick_count ( self , InstrumentId instrument_id ) int

The count of quote ticks for the given instrument ID.

Parameters :

instrument_id ( InstrumentId ) – The instrument ID for the ticks.

Returns :

int

quote_ticks ( self , InstrumentId instrument_id ) list

Return the quote ticks for the given instrument ID.

Parameters :

instrument_id ( InstrumentId ) – The instrument ID for the ticks to get.

Returns :

list[QuoteTick]

reset ( self ) void

Reset the cache.

All stateful fields are reset to their initial value.

snapshot_position ( self , Position position ) void

Snapshot the given position in its current state.

The position ID will be appended with a UUID v4 string.

Parameters :

position ( Position ) – The position to snapshot.

strategy_id_for_order ( self , ClientOrderId client_order_id ) StrategyId

Return the strategy ID associated with the given ID (if found).

Parameters :

client_order_id ( ClientOrderId ) – The client order ID associated with the strategy.

Returns :

StrategyId or None

strategy_id_for_position ( self , PositionId position_id ) StrategyId

Return the strategy ID associated with the given ID (if found).

Parameters :

position_id ( PositionId ) – The position ID associated with the strategy.

Returns :

StrategyId or None

strategy_ids ( self ) set

Return all strategy IDs.

Returns :

set[StrategyId]

tick_capacity

The caches tick capacity.

Returns :

int

ticker ( self , InstrumentId instrument_id , int index=0 ) Ticker

Return the ticker for the given instrument ID at the given index.

Last ticker if no index specified.

Parameters :
  • instrument_id ( InstrumentId ) – The instrument ID for the ticker to get.

  • index ( int , optional ) – The index for the ticker to get.

Returns :

Ticker or None – If no tickers or no ticker at index then returns None .

Notes

Reverse indexed (most recent ticker at index 0).

ticker_count ( self , InstrumentId instrument_id ) int

The count of tickers for the given instrument ID.

Parameters :

instrument_id ( InstrumentId ) – The instrument ID for the tickers.

Returns :

int

tickers ( self , InstrumentId instrument_id ) list

Return the tickers for the given instrument ID.

Parameters :

instrument_id ( InstrumentId ) – The instrument ID for the ticks to get.

Returns :

list[QuoteTick]

trade_tick ( self , InstrumentId instrument_id , int index=0 ) TradeTick

Return the trade tick for the given instrument ID at the given index

Last trade tick if no index specified.

Parameters :
  • instrument_id ( InstrumentId ) – The instrument ID for the tick to get.

  • index ( int , optional ) – The index for the tick to get.

Returns :

TradeTick or None – If no ticks or no tick at index then returns None .

Notes

Reverse indexed (most recent tick at index 0).

trade_tick_count ( self , InstrumentId instrument_id ) int

The count of trade ticks for the given instrument ID.

Parameters :

instrument_id ( InstrumentId ) – The instrument ID for the ticks.

Returns :

int

trade_ticks ( self , InstrumentId instrument_id ) list

Return trade ticks for the given instrument ID.

Parameters :

instrument_id ( InstrumentId ) – The instrument ID for the ticks to get.

Returns :

list[TradeTick]

update_account ( self , Account account ) void

Update the given account in the cache.

Parameters :

account ( The account to update ( from last event ) . ) –

update_actor ( self , Actor actor ) void

Update the given actor state in the cache.

Parameters :

actor ( Actor ) – The actor to update.

update_order ( self , Order order ) void

Update the given order in the cache.

Parameters :

order ( Order ) – The order to update (from last event).

update_position ( self , Position position ) void

Update the given position in the cache.

Parameters :

position ( Position ) – The position to update (from last event).

update_strategy ( self , Strategy strategy ) void

Update the given strategy state in the cache.

Parameters :

strategy ( Strategy ) – The strategy to update.

venue_order_id ( self , ClientOrderId client_order_id ) VenueOrderId

Return the order ID matching the given client order ID (if found).

Returns :

VenueOrderId or None

class CacheDatabase ( Logger logger )

Bases: object

The base class for all cache databases.

Parameters :

logger ( Logger ) – The logger for the database.

Warning

This class should not be used directly, but through a concrete subclass.

add ( self , unicode key , bytes value ) void

Abstract method (implement in subclass).

add_account ( self , Account account ) void

Abstract method (implement in subclass).

add_currency ( self , Currency currency ) void

Abstract method (implement in subclass).

add_instrument ( self , Instrument instrument ) void

Abstract method (implement in subclass).

add_order ( self , Order order ) void

Abstract method (implement in subclass).

add_position ( self , Position position ) void

Abstract method (implement in subclass).

add_submit_order_command ( self , SubmitOrder command ) void

Abstract method (implement in subclass).

add_submit_order_list_command ( self , SubmitOrderList command ) void

Abstract method (implement in subclass).

delete_actor ( self , ComponentId component_id ) void

Abstract method (implement in subclass).

delete_strategy ( self , StrategyId strategy_id ) void

Abstract method (implement in subclass).

flush ( self ) void

Abstract method (implement in subclass).

load ( self ) dict

Abstract method (implement in subclass).

load_account ( self , AccountId account_id ) Account

Abstract method (implement in subclass).

load_accounts ( self ) dict

Abstract method (implement in subclass).

load_actor ( self , ComponentId component_id ) dict

Abstract method (implement in subclass).

load_currencies ( self ) dict

Abstract method (implement in subclass).

load_currency ( self , unicode code ) Currency

Abstract method (implement in subclass).

load_instrument ( self , InstrumentId instrument_id ) Instrument

Abstract method (implement in subclass).

load_instruments ( self ) dict

Abstract method (implement in subclass).

load_order ( self , ClientOrderId client_order_id ) Order

Abstract method (implement in subclass).

load_orders ( self ) dict

Abstract method (implement in subclass).

load_position ( self , PositionId position_id ) Position

Abstract method (implement in subclass).

load_positions ( self ) dict

Abstract method (implement in subclass).

load_strategy ( self , StrategyId strategy_id ) dict

Abstract method (implement in subclass).

load_submit_order_command ( self , ClientOrderId client_order_id ) SubmitOrder

Abstract method (implement in subclass).

load_submit_order_commands ( self ) dict

Abstract method (implement in subclass).

load_submit_order_list_command ( self , OrderListId order_list_id ) SubmitOrderList

Abstract method (implement in subclass).

load_submit_order_list_commands ( self ) dict

Abstract method (implement in subclass).

update_account ( self , Account event ) void

Abstract method (implement in subclass).

update_actor ( self , Actor actor ) void

Abstract method (implement in subclass).

update_order ( self , Order order ) void

Abstract method (implement in subclass).

update_position ( self , Position position ) void

Abstract method (implement in subclass).

update_strategy ( self , Strategy strategy ) void

Abstract method (implement in subclass).

class CacheFacade

Bases: object

Provides a read-only facade for the common Cache .

account ( self , AccountId account_id ) Account

Abstract method (implement in subclass).

account_for_venue ( self , Venue venue ) Account

Abstract method (implement in subclass).

account_id ( self , Venue venue ) AccountId

Abstract method (implement in subclass).

accounts ( self ) list

Abstract method (implement in subclass).

actor_ids ( self ) set

Abstract method (implement in subclass).

add ( self , unicode key , bytes value ) void

Abstract method (implement in subclass).

bar ( self , BarType bar_type , int index=0 ) Bar

Abstract method (implement in subclass).

bar_count ( self , BarType bar_type ) int

Abstract method (implement in subclass).

bars ( self , BarType bar_type ) list

Abstract method (implement in subclass).

book_update_count ( self , InstrumentId instrument_id ) int

Abstract method (implement in subclass).

client_order_id ( self , VenueOrderId venue_order_id ) ClientOrderId

Abstract method (implement in subclass).

client_order_ids ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) set

Abstract method (implement in subclass).

client_order_ids_closed ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) set

Abstract method (implement in subclass).

client_order_ids_emulated ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) set

Abstract method (implement in subclass).

client_order_ids_inflight ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) set

Abstract method (implement in subclass).

client_order_ids_open ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) set

Abstract method (implement in subclass).

exec_algorithm_ids ( self ) set

Abstract method (implement in subclass).

get ( self , unicode key ) bytes

Abstract method (implement in subclass).

get_xrate ( self , Venue venue , Currency from_currency , Currency to_currency , PriceType price_type=PriceType.MID ) double

Abstract method (implement in subclass).

has_bars ( self , BarType bar_type ) bool

Abstract method (implement in subclass).

has_order_book ( self , InstrumentId instrument_id ) bool

Abstract method (implement in subclass).

has_quote_ticks ( self , InstrumentId instrument_id ) bool

Abstract method (implement in subclass).

has_tickers ( self , InstrumentId instrument_id ) bool

Abstract method (implement in subclass).

has_trade_ticks ( self , InstrumentId instrument_id ) bool

Abstract method (implement in subclass).

instrument ( self , InstrumentId instrument_id ) Instrument

Abstract method (implement in subclass).

instrument_ids ( self , Venue venue=None ) list

Abstract method (implement in subclass).

instruments ( self , Venue venue=None ) list

Abstract method (implement in subclass).

is_order_closed ( self , ClientOrderId client_order_id ) bool

Abstract method (implement in subclass).

is_order_emulated ( self , ClientOrderId client_order_id ) bool

Abstract method (implement in subclass).

is_order_inflight ( self , ClientOrderId client_order_id ) bool

Abstract method (implement in subclass).

is_order_open ( self , ClientOrderId client_order_id ) bool

Abstract method (implement in subclass).

is_position_closed ( self , PositionId position_id ) bool

Abstract method (implement in subclass).

is_position_open ( self , PositionId position_id ) bool

Abstract method (implement in subclass).

order ( self , ClientOrderId client_order_id ) Order

Abstract method (implement in subclass).

order_book ( self , InstrumentId instrument_id ) OrderBook

Abstract method (implement in subclass).

order_exists ( self , ClientOrderId client_order_id ) bool

Abstract method (implement in subclass).

order_list ( self , OrderListId order_list_id ) OrderList

Abstract method (implement in subclass).

order_list_exists ( self , OrderListId order_list_id ) bool

Abstract method (implement in subclass).

order_list_ids ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) set

Abstract method (implement in subclass).

order_lists ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) list

Abstract method (implement in subclass).

orders ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , OrderSide side=OrderSide.NO_ORDER_SIDE ) list

Abstract method (implement in subclass).

orders_closed ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , OrderSide side=OrderSide.NO_ORDER_SIDE ) list

Abstract method (implement in subclass).

orders_closed_count ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , OrderSide side=OrderSide.NO_ORDER_SIDE ) int

Abstract method (implement in subclass).

orders_emulated ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , OrderSide side=OrderSide.NO_ORDER_SIDE ) list

Abstract method (implement in subclass).

orders_emulated_count ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , OrderSide side=OrderSide.NO_ORDER_SIDE ) int

Abstract method (implement in subclass).

orders_for_exec_algorithm ( self , ExecAlgorithmId exec_algorithm_id , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , OrderSide side=OrderSide.NO_ORDER_SIDE ) list

Abstract method (implement in subclass).

orders_for_exec_spawn ( self , ClientOrderId client_order_id ) list

Abstract method (implement in subclass).

orders_for_position ( self , PositionId position_id ) list

Abstract method (implement in subclass).

orders_inflight ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , OrderSide side=OrderSide.NO_ORDER_SIDE ) list

Abstract method (implement in subclass).

orders_inflight_count ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , OrderSide side=OrderSide.NO_ORDER_SIDE ) int

Abstract method (implement in subclass).

orders_open ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , OrderSide side=OrderSide.NO_ORDER_SIDE ) list

Abstract method (implement in subclass).

orders_open_count ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , OrderSide side=OrderSide.NO_ORDER_SIDE ) int

Abstract method (implement in subclass).

orders_total_count ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , OrderSide side=OrderSide.NO_ORDER_SIDE ) int

Abstract method (implement in subclass).

position ( self , PositionId position_id ) Position

Abstract method (implement in subclass).

position_closed_ids ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) set

Abstract method (implement in subclass).

position_exists ( self , PositionId position_id ) bool

Abstract method (implement in subclass).

position_for_order ( self , ClientOrderId client_order_id ) Position

Abstract method (implement in subclass).

position_id ( self , ClientOrderId client_order_id ) PositionId

Abstract method (implement in subclass).

position_ids ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) set

Abstract method (implement in subclass).

position_open_ids ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) set

Abstract method (implement in subclass).

position_snapshots ( self , PositionId position_id=None ) list

Abstract method (implement in subclass).

positions ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , PositionSide side=PositionSide.NO_POSITION_SIDE ) list

Abstract method (implement in subclass).

positions_closed ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) list

Abstract method (implement in subclass).

positions_closed_count ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None ) int

Abstract method (implement in subclass).

positions_open ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , PositionSide side=PositionSide.NO_POSITION_SIDE ) list

Abstract method (implement in subclass).

positions_open_count ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , PositionSide side=PositionSide.NO_POSITION_SIDE ) int

Abstract method (implement in subclass).

positions_total_count ( self , Venue venue=None , InstrumentId instrument_id=None , StrategyId strategy_id=None , PositionSide side=PositionSide.NO_POSITION_SIDE ) int

Abstract method (implement in subclass).

price ( self , InstrumentId instrument_id , PriceType price_type ) Price

Abstract method (implement in subclass).

quote_tick ( self , InstrumentId instrument_id , int index=0 ) QuoteTick

Abstract method (implement in subclass).

quote_tick_count ( self , InstrumentId instrument_id ) int

Abstract method (implement in subclass).

quote_ticks ( self , InstrumentId instrument_id ) list

Abstract method (implement in subclass).

strategy_id_for_order ( self , ClientOrderId client_order_id ) StrategyId

Abstract method (implement in subclass).

strategy_id_for_position ( self , PositionId position_id ) StrategyId

Abstract method (implement in subclass).

strategy_ids ( self ) set

Abstract method (implement in subclass).

ticker ( self , InstrumentId instrument_id , int index=0 ) Ticker

Abstract method (implement in subclass).

ticker_count ( self , InstrumentId instrument_id ) int

Abstract method (implement in subclass).

tickers ( self , InstrumentId instrument_id ) list

Abstract method (implement in subclass).

trade_tick ( self , InstrumentId instrument_id , int index=0 ) TradeTick

Abstract method (implement in subclass).

trade_tick_count ( self , InstrumentId instrument_id ) int

Abstract method (implement in subclass).

trade_ticks ( self , InstrumentId instrument_id ) list

Abstract method (implement in subclass).

venue_order_id ( self , ClientOrderId client_order_id ) VenueOrderId

Abstract method (implement in subclass).