Infrastructure

The infrastructure subpackage provides technology specific infrastructure implementations.

Out of the box a Redis backed cache is implemented.

Cache

class RedisCacheDatabase ( TraderId trader_id , Logger logger , Serializer serializer , config: Optional[CacheDatabaseConfig] = None )

Bases: CacheDatabase

Provides a cache database backed by Redis.

Parameters :
  • trader_id ( TraderId ) – The trader ID for the database.

  • logger ( Logger ) – The logger for the database.

  • serializer ( Serializer ) – The serializer for database operations.

  • config ( CacheDatabaseConfig , optional ) – The configuration for the instance.

Raises :

TypeError – If config is not of type CacheDatabaseConfig .

Warning

Redis can only accurately store int64 types to 17 digits of precision. Therefore nanosecond timestamp int64’s with 19 digits will lose 2 digits of precision when persisted. One way to solve this is to ensure the serializer converts timestamp int64’s to strings on the way into Redis, and converts timestamp strings back to int64’s on the way out. One way to achieve this is to set the timestamps_as_str flag to true for the MsgPackSerializer , as per the default implementations for both TradingNode and BacktestEngine .

add_account ( self , Account account ) void

Add the given account to the database.

Parameters :

account ( Account ) – The account to add.

add_currency ( self , Currency currency ) void

Add the given currency to the database.

Parameters :

currency ( Currency ) – The currency to add.

add_instrument ( self , Instrument instrument ) void

Add the given instrument to the database.

Parameters :

instrument ( Instrument ) – The instrument to add.

add_order ( self , Order order ) void

Add the given order to the database.

Parameters :

order ( Order ) – The order to add.

add_position ( self , Position position ) void

Add the given position associated with the given strategy ID.

Parameters :

position ( Position ) – The position to add.

add_submit_order_command ( self , SubmitOrder command ) void

Add the given submit order command to the database.

Parameters :

command ( SubmitOrder ) – The command to add.

add_submit_order_list_command ( self , SubmitOrderList command ) void

Add the given submit order list command to the database.

Parameters :

command ( SubmitOrderList ) – The command to add.

delete_strategy ( self , StrategyId strategy_id ) void

Delete the given strategy from the database.

Parameters :

strategy_id ( StrategyId ) – The ID of the strategy state dictionary to delete.

flush ( self ) void

Flush the database which clears all data.

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_accounts ( self ) dict

Load all accounts from the database.

Returns :

dict[AccountId, Account]

load_currencies ( self ) dict

Load all currencies from the database.

Returns :

dict[str, Currency]

load_currency ( self , unicode code ) Currency

Load the currency associated with the given currency code (if found).

Parameters :

code ( str ) – The currency code to load.

Returns :

Currency or None

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 :

Instrument or None

load_instruments ( self ) dict

Load all instruments from the database.

Returns :

dict[InstrumentId, Instrument]

load_order ( self , ClientOrderId client_order_id ) Order

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

Parameters :

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

Returns :

Order or None

load_orders ( self ) dict

Load all orders from the database.

Returns :

dict[ClientOrderId, Order]

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_positions ( self ) dict

Load all positions from the database.

Returns :

dict[PositionId, Position]

load_strategy ( self , StrategyId strategy_id ) dict

Load the state for the given strategy.

Parameters :

strategy_id ( StrategyId ) – The ID of the strategy state dictionary to load.

Returns :

dict[str, bytes]

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_commands ( self ) dict

Load all submit order commands from the database.

Returns :

dict[ClientOrderId, SubmitOrder]

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

load_submit_order_list_commands ( self ) dict

Load all submit order list commands from the database.

Returns :

dict[OrderListId, SubmitOrderList]

update_account ( self , Account account ) void

Update the given account in the database.

Parameters :

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

update_order ( self , Order order ) void

Update the given order in the database.

Parameters :

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

update_position ( self , Position position ) void

Update the given position in the database.

Parameters :

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

update_strategy ( self , Strategy strategy ) void

Update the given strategy state in the database.

Parameters :

strategy ( Strategy ) – The strategy to update.