Infrastructure

The infrastructure subpackage provides technology specific infrastructure implementations.

An out of the box Redis <https://redis.io/> backed cache is implemented.

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 ( self , unicode key , bytes value ) void

Add the given general object value to the database.

Parameters :
  • key ( str ) – The key to write to.

  • value ( bytes ) – The object value.

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 , PositionId position_id=None , ClientId client_id=None ) void

Add the given order to the database.

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

  • position_id ( PositionId , optional ) – The position ID to associate with this order.

  • client_id ( ClientId , optional ) – The execution client ID to associate with this order.

add_position ( self , Position position ) void

Add the given position to the database.

Parameters :

position ( Position ) – The position to add.

add_synthetic ( self , SyntheticInstrument synthetic ) void

Add the given synthetic instrument to the database.

Parameters :

synthetic ( SyntheticInstrument ) – The synthetic instrument to add.

delete_actor ( self , ComponentId component_id ) void

Delete the given actor from the database.

Parameters :

component_id ( ComponentId ) – The ID of the actor state dictionary to delete.

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.

heartbeat ( self , datetime timestamp ) void

Add a heartbeat at the given timestamp .

Parameters :

timestamp ( datetime ) – The timestamp for the heartbeat.

index_order_position ( self , ClientOrderId client_order_id , PositionId position_id ) void

Add an index entry for the given client_order_id to position_id .

Parameters :
index_venue_order_id ( self , ClientOrderId client_order_id , VenueOrderId venue_order_id ) void

Add an index entry for the given venue_order_id to client_order_id .

Parameters :
load ( self ) dict

Load all general objects from the database.

Returns :

dict[str, bytes]

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_actor ( self , ComponentId component_id ) dict

Load the state for the given actor.

Parameters :

component_id ( ComponentId ) – The ID of the actor state dictionary to load.

Returns :

dict[str, bytes]

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

Load the order to execution client index from the database.

Returns :

dict[ClientOrderId, ClientId]

load_index_order_position ( self ) dict

Load the order to position index from the database.

Returns :

dict[ClientOrderId, PositionId]

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_synthetic ( self , InstrumentId instrument_id ) SyntheticInstrument

Load the synthetic instrument associated with the given synthetic instrument ID (if found).

Parameters :

instrument_id ( InstrumentId ) – The synthetic instrument ID to load.

Returns :

SyntheticInstrument or None

Raises :

ValueError – If instrument_id is not for a synthetic instrument.

load_synthetics ( self ) dict

Load all synthetic instruments from the database.

Returns :

dict[InstrumentId, SyntheticInstrument]

snapshot_order_state ( self , Order order ) void

Snapshot the state of the given order .

Parameters :

order ( Order ) – The order for the state snapshot.

snapshot_position_state ( self , Position position , uint64_t ts_snapshot , Money unrealized_pnl=None ) void

Snapshot the state of the given position .

Parameters :
  • position ( Position ) – The position for the state snapshot.

  • ts_snapshot ( uint64_t ) – The UNIX timestamp (nanoseconds) when the snapshot was taken.

  • unrealized_pnl ( Money , optional ) – The unrealized PnL for the state snapshot.

update_account ( self , Account account ) void

Update the given account in the database.

Parameters :

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

update_actor ( self , Actor actor ) void

Update the given actor state in the database.

Parameters :

actor ( Actor ) – The actor to update.

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.