Skip to main content
Version: latest

Portfolio

The portfolio subpackage provides portfolio management functionality.

class Portfolio

Bases: PortfolioFacade

Portfolio(MessageBus msgbus, CacheFacade cache, Clock clock)

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.

account(self, Venue venue)

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

  • Parameters: venue (Venue) – The venue for the account.
  • Return type: Account or None

balances_locked(self, Venue venue)

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

  • Parameters: venue (Venue) – The venue for the margin.
  • Return type: dict[Currency, Money] or None

dispose(self)

Dispose of the portfolio.

All stateful fields are reset to their initial value.

initialize_orders(self)

Initialize the portfolios orders.

Performs all account calculations for the current orders state.

initialize_positions(self)

Initialize the portfolios positions.

Performs all account calculations for the current position state.

is_completely_flat(self)

Return a value indicating whether the portfolio is completely flat.

  • Returns: True if net flat across all instruments, else False.
  • Return type: bool

is_flat(self, InstrumentId instrument_id)

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

  • Parameters: instrument_id (InstrumentId) – The instrument query filter.
  • Returns: True if net flat, else False.
  • Return type: bool

is_net_long(self, InstrumentId instrument_id)

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

  • Parameters: instrument_id (InstrumentId) – The instrument for the query.
  • Returns: True if net long, else False.
  • Return type: bool

is_net_short(self, InstrumentId instrument_id)

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

  • Parameters: instrument_id (InstrumentId) – The instrument for the query.
  • Returns: True if net short, else False.
  • Return type: bool

margins_init(self, Venue venue)

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

  • Parameters: venue (Venue) – The venue for the margin.
  • Return type: dict[Currency, Money] or None

margins_maint(self, Venue venue)

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

  • Parameters: venue (Venue) – The venue for the margin.
  • Return type: dict[Currency, Money] or None

net_exposure(self, InstrumentId instrument_id)

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

  • Parameters: instrument_id (InstrumentId) – The instrument for the calculation.
  • Return type: Money or None

net_exposures(self, Venue venue)

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

  • Parameters: venue (Venue) – The venue for the market value.
  • Return type: 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.
  • Return type: Decimal

reset(self)

Reset the portfolio.

All stateful fields are reset to their initial value.

set_specific_venue(self, Venue venue)

Set a specific venue for the portfolio.

  • Parameters: venue (Venue) – The specific venue to set.

unrealized_pnl(self, InstrumentId instrument_id)

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

  • Parameters: instrument_id (InstrumentId) – The instrument for the unrealized PnL.
  • Return type: Money or None

unrealized_pnls(self, Venue venue)

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

  • Parameters: venue (Venue) – The venue for the unrealized pnl.
  • Return type: dict[Currency, Money] or None

update_account(self, AccountState event)

Apply the given account state.

  • Parameters: event (AccountState) – The account state to apply.

update_order(self, OrderEvent event)

Update the portfolio with the given order.

  • Parameters: event (OrderEvent) – The event to update with.

update_position(self, PositionEvent event)

Update the portfolio with the given position event.

  • Parameters: event (PositionEvent) – The event to update with.

update_quote_tick(self, QuoteTick tick)

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.

class PortfolioFacade

Bases: object

Provides a read-only facade for a Portfolio.

account(self, Venue venue)

Abstract method (implement in subclass).

analyzer

The portfolios analyzer.

  • Returns: PortfolioAnalyzer

balances_locked(self, Venue venue)

Abstract method (implement in subclass).

initialized

If the portfolio is initialized.

  • Returns: bool

is_completely_flat(self)

Abstract method (implement in subclass).

is_flat(self, InstrumentId instrument_id)

Abstract method (implement in subclass).

is_net_long(self, InstrumentId instrument_id)

Abstract method (implement in subclass).

is_net_short(self, InstrumentId instrument_id)

Abstract method (implement in subclass).

margins_init(self, Venue venue)

Abstract method (implement in subclass).

margins_maint(self, Venue venue)

Abstract method (implement in subclass).

net_exposure(self, InstrumentId instrument_id)

Abstract method (implement in subclass).

net_exposures(self, Venue venue)

Abstract method (implement in subclass).

net_position(self, InstrumentId instrument_id)

Abstract method (implement in subclass).

unrealized_pnl(self, InstrumentId instrument_id)

Abstract method (implement in subclass).

unrealized_pnls(self, Venue venue)

Abstract method (implement in subclass).

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

Bases: PortfolioFacade

Portfolio(MessageBus msgbus, CacheFacade cache, Clock clock)

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.

account(self, Venue venue)

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

  • Parameters: venue (Venue) – The venue for the account.
  • Return type: Account or None

analyzer

The portfolios analyzer.

  • Returns: PortfolioAnalyzer

balances_locked(self, Venue venue)

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

  • Parameters: venue (Venue) – The venue for the margin.
  • Return type: dict[Currency, Money] or None

dispose(self)

Dispose of the portfolio.

All stateful fields are reset to their initial value.

initialize_orders(self)

Initialize the portfolios orders.

Performs all account calculations for the current orders state.

initialize_positions(self)

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)

Return a value indicating whether the portfolio is completely flat.

  • Returns: True if net flat across all instruments, else False.
  • Return type: bool

is_flat(self, InstrumentId instrument_id)

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

  • Parameters: instrument_id (InstrumentId) – The instrument query filter.
  • Returns: True if net flat, else False.
  • Return type: bool

is_net_long(self, InstrumentId instrument_id)

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

  • Parameters: instrument_id (InstrumentId) – The instrument for the query.
  • Returns: True if net long, else False.
  • Return type: bool

is_net_short(self, InstrumentId instrument_id)

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

  • Parameters: instrument_id (InstrumentId) – The instrument for the query.
  • Returns: True if net short, else False.
  • Return type: bool

margins_init(self, Venue venue)

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

  • Parameters: venue (Venue) – The venue for the margin.
  • Return type: dict[Currency, Money] or None

margins_maint(self, Venue venue)

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

  • Parameters: venue (Venue) – The venue for the margin.
  • Return type: dict[Currency, Money] or None

net_exposure(self, InstrumentId instrument_id)

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

  • Parameters: instrument_id (InstrumentId) – The instrument for the calculation.
  • Return type: Money or None

net_exposures(self, Venue venue)

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

  • Parameters: venue (Venue) – The venue for the market value.
  • Return type: 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.
  • Return type: Decimal

reset(self)

Reset the portfolio.

All stateful fields are reset to their initial value.

set_specific_venue(self, Venue venue)

Set a specific venue for the portfolio.

  • Parameters: venue (Venue) – The specific venue to set.

unrealized_pnl(self, InstrumentId instrument_id)

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

  • Parameters: instrument_id (InstrumentId) – The instrument for the unrealized PnL.
  • Return type: Money or None

unrealized_pnls(self, Venue venue)

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

  • Parameters: venue (Venue) – The venue for the unrealized pnl.
  • Return type: dict[Currency, Money] or None

update_account(self, AccountState event)

Apply the given account state.

  • Parameters: event (AccountState) – The account state to apply.

update_order(self, OrderEvent event)

Update the portfolio with the given order.

  • Parameters: event (OrderEvent) – The event to update with.

update_position(self, PositionEvent event)

Update the portfolio with the given position event.

  • Parameters: event (PositionEvent) – The event to update with.

update_quote_tick(self, QuoteTick tick)

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.

class PortfolioFacade

Bases: object

Provides a read-only facade for a Portfolio.

account(self, Venue venue)

Abstract method (implement in subclass).

analyzer

The portfolios analyzer.

  • Returns: PortfolioAnalyzer

balances_locked(self, Venue venue)

Abstract method (implement in subclass).

initialized

If the portfolio is initialized.

  • Returns: bool

is_completely_flat(self)

Abstract method (implement in subclass).

is_flat(self, InstrumentId instrument_id)

Abstract method (implement in subclass).

is_net_long(self, InstrumentId instrument_id)

Abstract method (implement in subclass).

is_net_short(self, InstrumentId instrument_id)

Abstract method (implement in subclass).

margins_init(self, Venue venue)

Abstract method (implement in subclass).

margins_maint(self, Venue venue)

Abstract method (implement in subclass).

net_exposure(self, InstrumentId instrument_id)

Abstract method (implement in subclass).

net_exposures(self, Venue venue)

Abstract method (implement in subclass).

net_position(self, InstrumentId instrument_id)

Abstract method (implement in subclass).

unrealized_pnl(self, InstrumentId instrument_id)

Abstract method (implement in subclass).

unrealized_pnls(self, Venue venue)

Abstract method (implement in subclass).