Position - Loren1166/NautilusTrader- GitHub Wiki
Position | 仓位
class Position | 仓位
class Position:
"""
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.
仓位 ID 可以由交易场所分配,也可以根据策略 OMS(订单管理系统)设置由系统生成。
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.
如果 instrument.id 不等于 fill.instrument_id。
ValueError – If event.position_id is None.
如果 event.position_id 为 None。
"""
def __init__(self, instrument: Instrument, fill: OrderFilled) -> None:
...
@property
def account_id(self) -> AccountId:
"""
The account ID associated with the position.
与仓位关联的账户 ID。
Returns: AccountId
"""
...
def apply(self, fill: OrderFilled) -> None:
"""
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.
如果 fill.trade_id 已经应用于仓位。
"""
...
@property
def avg_px_close(self) -> float:
"""
The average close price.
平均平仓价格。
Returns: double
"""
...
@property
def avg_px_open(self) -> float:
"""
The average open price.
平均开仓价格。
Returns: double
"""
...
@property
def base_currency(self) -> Currency:
"""
The position base currency (if applicable).
仓位基础货币(如果适用)。
Returns: Currency or None
"""
...
def calculate_pnl(self, avg_px_open: float, avg_px_close: float, quantity: Quantity) -> Money:
"""
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
"""
...
@property
def client_order_ids(self) -> list:
"""
Return the client order IDs associated with the position.
返回与仓位关联的客户订单 ID。
Return type: list[VenueOrderId]
"""
...
@property
def closing_order_id(self) -> ClientOrderId:
"""
The client order ID for the order which closed the position.
平仓订单的客户订单 ID。
Returns: ClientOrderId or None
"""
...
def commissions(self) -> list:
"""
Return the total commissions generated by the position.
返回仓位产生的总佣金。
Return type: list[Money]
"""
...
@property
def duration_ns(self) -> int:
"""
The total open duration (nanoseconds).
总开仓时间(纳秒)。
Returns: uint64_t
"""
...
@property
def entry(self) -> OrderSide:
"""
The position entry order side.
仓位入场订单方向。
Returns: OrderSide
"""
...
@property
def event_count(self) -> int:
"""
Return the count of order fill events applied to the position.
返回应用于仓位的订单成交事件计数。
Return type: int
"""
...
@property
def events(self) -> list:
"""
Return the order fill events for the position.
返回仓位的订单成交事件。
Return type: list[Event]
"""
...
@property
def id(self) -> PositionId:
"""
The position ID.
仓位 ID。
Returns: PositionId
"""
...
def info(self) -> str:
"""
Return a summary description of the position.
返回仓位的摘要描述。
Return type: str
"""
...
@property
def instrument_id(self) -> InstrumentId:
"""
The position instrument ID.
仓位工具 ID。
Returns: InstrumentId
"""
...
@property
def is_closed(self) -> bool:
"""
Return whether the position side is FLAT.
返回仓位方向是否为 FLAT。
Return type: bool
"""
...
@property
def is_inverse(self) -> bool:
"""
If the quantity is expressed in quote currency.
如果数量以报价货币表示。
Returns: bool
"""
...
@property
def is_long(self) -> bool:
"""
Return whether the position side is LONG.
返回仓位方向是否为 LONG。
Return type: bool
"""
...
@property
def is_open(self) -> bool:
"""
Return whether the position side is not FLAT.
返回仓位方向是否不为 FLAT。
Return type: bool
"""
...
def is_opposite_side(self, side: OrderSide) -> bool:
"""
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. 如果方向相反,则返回 True,否则返回 False。
Return type: bool
"""
...
@property
def is_short(self) -> bool:
"""
Return whether the position side is SHORT.
返回仓位方向是否为 SHORT。
Return type: bool
"""
...
@property
def last_event(self) -> OrderFilled:
"""
Return the last order fill event.
返回最后一个订单成交事件。
Return type: OrderFilled
"""
...
@property
def last_trade_id(self) -> TradeId:
"""
Return the last trade match ID for the position.
返回仓位的最后一个交易匹配 ID。
Return type: TradeId
"""
...
@property
def multiplier(self) -> Quantity:
"""
The multiplier for the positions instrument.
仓位工具的乘数。
Returns: Quantity
"""
...
def notional_value(self, last: Price) -> Money:
"""
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
"""
...
@property
def opening_order_id(self) -> ClientOrderId:
"""
The client order ID for the order which opened the position.
开仓订单的客户订单 ID。
Returns: ClientOrderId
"""
...
@property
def peak_qty(self) -> Quantity:
"""
The peak directional quantity reached by the position.
仓位达到的峰值方向数量。
Returns: Quantity
"""
...
@property
def price_precision(self) -> int:
"""
The price precision for the position.
仓位的价格精度。
Returns: uint8
"""
...
@property
def quantity(self) -> Quantity:
"""
The current open quantity.
当前未平仓数量。
Returns: Quantity
"""
...
@property
def quote_currency(self) -> Currency:
"""
The position quote currency.
仓位报价货币。
Returns: Currency
"""
...
@property
def realized_pnl(self) -> Money:
"""
The current realized PnL for the position (including commissions).
仓位的当前已实现盈亏(包括佣金)。
Returns: Money or None
"""
...
@property
def realized_return(self) -> float:
"""
The current realized return for the position.
仓位的当前已实现回报。
Returns: double
"""
...
@property
def settlement_currency(self) -> Currency:
"""
The position settlement currency (for PnL).
仓位结算货币(用于盈亏)。
Returns: Currency
"""
...
@property
def side(self) -> PositionSide:
"""
The current position side.
当前仓位方向。
Returns: PositionSide
"""
...
@staticmethod
def side_from_order_side(side: OrderSide) -> PositionSide:
"""
Return the position side resulting from the given order side (from FLAT).
返回由给定订单方向产生的仓位方向(从 FLAT 开始)。
Parameters:
参数:
side (OrderSide {BUY, SELL}) – The order side
订单方向
Return type: PositionSide
"""
...
def signed_decimal_qty(self) -> Decimal:
"""
Return a signed decimal representation of the position quantity.
返回仓位数量的带符号十进制表示形式。
If the position is LONG, the value is positive (e.g. Decimal('10.25'))
如果仓位为 LONG,则值为正数(例如 Decimal('10.25'))
If the position is SHORT, the value is negative (e.g. Decimal('-10.25'))
如果仓位为 SHORT,则值为负数(例如 Decimal('-10.25'))
If the position is FLAT, the value is zero (e.g. Decimal('0'))
如果仓位为 FLAT,则值为零(例如 Decimal('0'))
Return type: Decimal
"""
...
@property
def signed_qty(self) -> float:
"""
The current signed quantity (positive for position side LONG, negative for SHORT).
当前带符号的数量(仓位方向为 LONG 时为正,SHORT 时为负)。
Returns: double
"""
...
@property
def size_precision(self) -> int:
"""
The size precision for the position.
仓位的大小精度。
Returns: uint8
"""
...
@property
def strategy_id(self) -> StrategyId:
"""
The strategy ID associated with the position.
与仓位关联的策略 ID。
Returns: StrategyId
"""
...
@property
def symbol(self) -> Symbol:
"""
Return the positions ticker symbol.
返回仓位的股票代码。
Return type: Symbol
"""
...
def to_dict(self) -> dict:
"""
Return a dictionary representation of this object.
返回此对象的字典表示形式。
Return type: dict[str, object]
"""
...
def total_pnl(self, last: Price) -> Money:
"""
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
"""
...
@property
def trade_ids(self) -> list:
"""
Return the trade match IDs associated with the position.
返回与仓位关联的交易匹配 ID。
Return type: list[TradeId]
"""
...
@property
def trader_id(self) -> TraderId:
"""
The trader ID associated with the position.
与仓位关联的交易员 ID。
Returns: TraderId
"""
...
@property
def ts_closed(self) -> int:
"""
UNIX timestamp (nanoseconds) when the position was closed.
仓位平仓时的 UNIX 时间戳(纳秒)。
Returns: uint64_t
"""
...
@property
def ts_init(self) -> int:
"""
UNIX timestamp (nanoseconds) when the object was initialized.
对象初始化时的 UNIX 时间戳(纳秒)。
Returns: uint64_t
"""
...
@property
def ts_last(self) -> int:
"""
UNIX timestamp (nanoseconds) when the last event occurred.
上次事件发生时的 UNIX 时间戳(纳秒)。
Returns: uint64_t
"""
...
@property
def ts_opened(self) -> int:
"""
UNIX timestamp (nanoseconds) when the position was opened.
仓位开仓时的 UNIX 时间戳(纳秒)。
Returns: uint64_t
"""
...
def unrealized_pnl(self, last: Price) -> Money:
"""
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
"""
...
@property
def venue(self) -> Venue:
"""
Return the positions trading venue.
返回仓位的交易场所。
Return type: Venue
"""
...
@property
def venue_order_ids(self) -> list:
"""
Return the venue order IDs associated with the position.
返回与仓位关联的交易场所订单 ID。
Return type: list[VenueOrderId]
"""
...