Skip to main content
Version: latest

Position

class Position

Bases: object

Position(Instrument instrument, OrderFilled fill) -> None

Represents a position in a market.

The position ID may be assigned at the trading venue, or can be system generated depending on a strategies OMS (Order Management System) settings.

  • Parameters:
    • instrument (Instrument) – The trading instrument for the position.
    • fill (OrderFilled) – The order fill event which opened the position.
  • Raises:
    • ValueError – If instrument.id is not equal to fill.instrument_id.
    • ValueError – If event.position_id is None.

account_id

The account ID associated with the position.

  • Returns: AccountId

apply(self, OrderFilled fill)

Applies the given order fill event to the position.

  • Parameters: fill (OrderFilled) – The order fill event to apply.
  • Raises: KeyError – If fill.trade_id already applied to the position.

avg_px_close

The average close price.

  • Returns: double

avg_px_open

The average open price.

  • Returns: double

base_currency

The position base currency (if applicable).

  • Returns: Currency or None

calculate_pnl(self, double avg_px_open, double avg_px_close, Quantity quantity)

Return a calculated PnL.

Result will be in quote currency for standard instruments, or base currency for inverse instruments.

  • Parameters:
    • avg_px_open (double) – The average open price.
    • avg_px_close (double) – The average close price.
    • quantity (Quantity) – The quantity for the calculation.
  • Returns: In settlement currency.
  • Return type: Money

client_order_ids

Return the client order IDs associated with the position.

closing_order_id

The client order ID for the order which closed the position.

  • Returns: ClientOrderId or None

commissions(self)

Return the total commissions generated by the position.

  • Return type: list[Money]

duration_ns

The total open duration (nanoseconds).

  • Returns: uint64_t

entry

The position entry order side.

  • Returns: OrderSide

event_count

Return the count of order fill events applied to the position.

  • Return type: int

events

Return the order fill events for the position.

  • Return type: list[Event]

id

The position ID.

  • Returns: PositionId

info(self)

Return a summary description of the position.

  • Return type: str

instrument_id

The position instrument ID.

  • Returns: InstrumentId

is_closed

Return whether the position side is FLAT.

  • Return type: bool

is_inverse

If the quantity is expressed in quote currency.

  • Returns: bool

is_long

Return whether the position side is LONG.

  • Return type: bool

is_open

Return whether the position side is not FLAT.

  • Return type: bool

is_opposite_side(self, OrderSide side)

Return a value indicating whether the given order side is opposite to the current position side.

  • Parameters: side (OrderSide {BUY, SELL})
  • Returns: True if side is opposite, else False.
  • Return type: bool

is_short

Return whether the position side is SHORT.

  • Return type: bool

last_event

Return the last order fill event.

last_trade_id

Return the last trade match ID for the position.

multiplier

The multiplier for the positions instrument.

  • Returns: Quantity

notional_value(self, Price last)

Return the current notional value of the position.

  • Parameters: last (Price) – The last close price for the position.
  • Returns: In quote currency.
  • Return type: Money

opening_order_id

The client order ID for the order which opened the position.

  • Returns: ClientOrderId

peak_qty

The peak directional quantity reached by the position.

  • Returns: Quantity

price_precision

The price precision for the position.

  • Returns: uint8

quantity

The current open quantity.

  • Returns: Quantity

quote_currency

The position quote currency.

  • Returns: Currency

realized_pnl

The current realized PnL for the position (including commissions).

  • Returns: Money or None

realized_return

The current realized return for the position.

  • Returns: double

settlement_currency

The position settlement currency (for PnL).

  • Returns: Currency

side

The current position side.

  • Returns: PositionSide

static side_from_order_side(OrderSide side)

Return the position side resulting from the given order side (from FLAT).

  • Parameters: side (OrderSide {BUY, SELL}) – The order side
  • Return type: PositionSide

signed_decimal_qty(self)

Return a signed decimal representation of the position quantity.

  • If the position is LONG, the value is positive (e.g. Decimal(‘10.25’))
  • If the position is SHORT, the value is negative (e.g. Decimal(‘-10.25’))
  • If the position is FLAT, the value is zero (e.g. Decimal(‘0’))
  • Return type: Decimal

signed_qty

The current signed quantity (positive for position side LONG, negative for SHORT).

  • Returns: double

size_precision

The size precision for the position.

  • Returns: uint8

strategy_id

The strategy ID associated with the position.

  • Returns: StrategyId

symbol

Return the positions ticker symbol.

to_dict(self)

Return a dictionary representation of this object.

  • Return type: dict[str, object]

total_pnl(self, Price last)

Return the total PnL from the given last quote tick.

Result will be in quote currency for standard instruments, or base currency for inverse instruments.

  • Parameters: last (Price) – The last price for the calculation.
  • Return type: Money

trade_ids

Return the trade match IDs associated with the position.

trader_id

The trader ID associated with the position.

  • Returns: TraderId

ts_closed

UNIX timestamp (nanoseconds) when the position was closed.

  • Returns: uint64_t

ts_init

UNIX timestamp (nanoseconds) when the object was initialized.

  • Returns: uint64_t

ts_last

UNIX timestamp (nanoseconds) when the last event occurred.

  • Returns: uint64_t

ts_opened

UNIX timestamp (nanoseconds) when the position was opened.

  • Returns: uint64_t

unrealized_pnl(self, Price last)

Return the unrealized PnL from the given last quote tick.

Result will be in quote currency for standard instruments, or base currency for inverse instruments.

  • Parameters: last (Price) – The last price for the calculation.
  • Return type: Money

venue

Return the positions trading venue.

venue_order_ids

Return the venue order IDs associated with the position.