Portfolio

The portfolio subpackage provides portfolio management functionality.

Portfolio

The Portfolio facilitates the management of trading operations.

The intended use case is for a single Portfolio instance per running system, a fleet of trading strategies will organize around a portfolio with the help of the Trader` class.

The portfolio can satisfy queries for account information, margin balances, total risk exposures and total net positions.

class Portfolio ( MessageBus msgbus , CacheFacade cache , Clock clock , Logger logger=None )

Bases: PortfolioFacade

Provides a trading portfolio.

Currently there is a limitation of one account per ExecutionClient instance.

Parameters :
  • msgbus ( MessageBus ) – The message bus for the engine.

  • cache ( CacheFacade ) – The read-only cache for the portfolio.

  • clock ( Clock ) – The clock for the portfolio.

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

account ( self , Venue venue ) Account

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

Parameters :

venue ( Venue ) – The venue for the account.

Returns :

Account or None

analyzer

The portfolios analyzer.

Returns :

PortfolioAnalyzer

balances_locked ( self , Venue venue ) dict

Return the balances locked for the given venue (if found).

Parameters :

venue ( Venue ) – The venue for the margin.

Returns :

dict[Currency, Money] or None

initialize_orders ( self ) void

Initialize the portfolios orders.

Performs all account calculations for the current orders state.

initialize_positions ( self ) void

Initialize the portfolios positions.

Performs all account calculations for the current position state.

initialized

If the portfolio is initialized.

Returns :

bool

is_completely_flat ( self ) bool

Return a value indicating whether the portfolio is completely flat.

Returns :

bool – True if net flat across all instruments, else False.

is_flat ( self , InstrumentId instrument_id ) bool

Return a value indicating whether the portfolio is flat for the given instrument ID.

Parameters :

instrument_id ( InstrumentId ) – The instrument query filter.

Returns :

bool – True if net flat, else False.

is_net_long ( self , InstrumentId instrument_id ) bool

Return a value indicating whether the portfolio is net long the given instrument ID.

Parameters :

instrument_id ( InstrumentId ) – The instrument for the query.

Returns :

bool – True if net long, else False.

is_net_short ( self , InstrumentId instrument_id ) bool

Return a value indicating whether the portfolio is net short the given instrument ID.

Parameters :

instrument_id ( InstrumentId ) – The instrument for the query.

Returns :

bool – True if net short, else False.

margins_init ( self , Venue venue ) dict

Return the initial (order) margins for the given venue (if found).

Parameters :

venue ( Venue ) – The venue for the margin.

Returns :

dict[Currency, Money] or None

margins_maint ( self , Venue venue ) dict

Return the maintenance (position) margins for the given venue (if found).

Parameters :

venue ( Venue ) – The venue for the margin.

Returns :

dict[Currency, Money] or None

net_exposure ( self , InstrumentId instrument_id ) Money

Return the net exposure for the given instrument (if found).

Parameters :

instrument_id ( InstrumentId ) – The instrument for the calculation.

Returns :

Money or None

net_exposures ( self , Venue venue ) dict

Return the net exposures for the given venue (if found).

Parameters :

venue ( Venue ) – The venue for the market value.

Returns :

dict[Currency, Money] or None

net_position ( self , InstrumentId instrument_id )

Return the total net position for the given instrument ID. If no positions for instrument_id then will return Decimal(‘0’) .

Parameters :

instrument_id ( InstrumentId ) – The instrument for the query.

Returns :

Decimal

reset ( self ) void

Reset the portfolio.

All stateful fields are reset to their initial value.

unrealized_pnl ( self , InstrumentId instrument_id ) Money

Return the unrealized PnL for the given instrument ID (if found).

Parameters :

instrument_id ( InstrumentId ) – The instrument for the unrealized PnL.

Returns :

Money or None

unrealized_pnls ( self , Venue venue ) dict

Return the unrealized pnls for the given venue (if found).

Parameters :

venue ( Venue ) – The venue for the unrealized pnl.

Returns :

dict[Currency, Money] or None

update_account ( self , AccountState event ) void

Apply the given account state.

Parameters :

event ( AccountState ) – The account state to apply.

update_order ( self , OrderEvent event ) void

Update the portfolio with the given order.

Parameters :

event ( OrderEvent ) – The event to update with.

update_position ( self , PositionEvent event ) void

Update the portfolio with the given position event.

Parameters :

event ( PositionEvent ) – The event to update with.

update_quote_tick ( self , QuoteTick tick ) void

Update the portfolio with the given tick.

Clears the unrealized PnL for the quote ticks instrument, and performs any initialization calculations which may have been pending a market quote update.

Parameters :

tick ( QuoteTick ) – The tick to update with.

Base Classes

class PortfolioFacade

Bases: object

Provides a read-only facade for a Portfolio .

account ( self , Venue venue ) Account

Abstract method (implement in subclass).

analyzer

The portfolios analyzer.

Returns :

PortfolioAnalyzer

balances_locked ( self , Venue venue ) dict

Abstract method (implement in subclass).

initialized

If the portfolio is initialized.

Returns :

bool

is_completely_flat ( self ) bool

Abstract method (implement in subclass).

is_flat ( self , InstrumentId instrument_id ) bool

Abstract method (implement in subclass).

is_net_long ( self , InstrumentId instrument_id ) bool

Abstract method (implement in subclass).

is_net_short ( self , InstrumentId instrument_id ) bool

Abstract method (implement in subclass).

margins_init ( self , Venue venue ) dict

Abstract method (implement in subclass).

margins_maint ( self , Venue venue ) dict

Abstract method (implement in subclass).

net_exposure ( self , InstrumentId instrument_id ) Money

Abstract method (implement in subclass).

net_exposures ( self , Venue venue ) dict

Abstract method (implement in subclass).

net_position ( self , InstrumentId instrument_id )

Abstract method (implement in subclass).

unrealized_pnl ( self , InstrumentId instrument_id ) Money

Abstract method (implement in subclass).

unrealized_pnls ( self , Venue venue ) dict

Abstract method (implement in subclass).