Position

class lumibot.entities.position.Position(strategy, asset, quantity, orders=None, hold=0, available=0, avg_fill_price=None)

Bases: object

This is a Position object. It is used to keep track of the quantity of an asset owned in a strategy. Position objects are retreived from the broker using the get_positions() or get_position() methods.

strategy

The strategy that owns this position.

Type:

str

asset

The asset that this position is for.

Type:

Asset

symbol

The symbol of the asset. e.g. AAPL for Apple stock.

Type:

str

quantity

The quantity of the asset owned.

Type:

float

orders

The orders that have been executed for this position.

Type:

list of Order

hold

The assets that are not free in the portfolio. (Crypto: only)

Type:

float

available

The assets that are free in the portfolio. (Crypto: only)

Type:

float

avg_fill_price

The average fill price of the position.

Type:

float

current_price

The current price of the asset.

Type:

float

market_value

The market value of the position.

Type:

float

pnl

The profit and loss of the position.

Type:

float

pnl_percent

The profit and loss of the position as a percentage of the average fill price.

Type:

float

asset_type

The type of the asset.

Type:

str

exchange

The exchange that the position is on.

Type:

str

currency

The currency that the position is denominated in.

Type:

str

multiplier

The multiplier of the asset.

Type:

float

expiration

The expiration of the asset. (Options and futures: only). Probably better to use on position.asset

Type:

datetime.date

strike

The strike price of the asset. (Options: only). Probably better to use on position.asset

Type:

float

option_type

The type of the option. (Options: only). Probably better to use on position.asset

Type:

str

side

The side of the position (LONG or SHORT)

Type:

PositionSide

class PositionSide(value)

Bases: StrEnum

An enumeration.

LONG = 'LONG'
SHORT = 'SHORT'
add_order(order: Order, quantity: Decimal = Decimal('0'))
property available
classmethod from_dict(data)
get_selling_order(quote_asset=None)

Returns an order that can be used to sell this position.

Parameters:

None

Returns:

order – An order that can be used to sell this position.

Return type:

Order

property hold
property quantity
to_dict()

Convert position to dictionary for serialization.

NOTE: We explicitly exclude internal Python fields and large data fields that can cause DynamoDB 400KB limit errors: - _bars: Historical bar data (can be 1.8MB+) - _raw: Raw broker response data (can be 22KB+) - _asset: Duplicate asset data (5KB+) - Any field starting with underscore (Python internals)

We ONLY return the essential fields needed for portfolio tracking.

to_minimal_dict() dict

Return a minimal dictionary representation of the position for progress logging.

This creates a lightweight representation suitable for real-time progress updates, containing only the essential fields needed to display the position.

Returns:

A minimal dictionary with keys: - asset: Minimal asset dict (from asset.to_minimal_dict()) - qty: Position quantity - val: Market value (rounded to 2 decimal places) - pnl: Unrealized P&L (rounded to 2 decimal places)

Return type:

dict

Example

>>> position = Position(strategy="MyStrategy", asset=Asset("AAPL"), quantity=100)
>>> position.to_minimal_dict()
{'asset': {'symbol': 'AAPL', 'type': 'stock'}, 'qty': 100, 'val': 15000.00, 'pnl': 500.00}
value_type(value)