Skip to content

position ¤

Position ¤

Position(
    id: str,
    exchange: Exchange,
    data: DataFeed,
    size: float,
    parent: Order,
    state: PositionState = PositionState.Open,
    entry_price: float | None = None,
    entry_fee: float = 0.0,
    entry_at: Timestamp | None = None,
    sl_order: Order | None = None,
    tp_order: Order | None = None,
    tag: str | None = None,
    **kwargs
)

Bases: BaseTransaction

When an Order is filled, it results in an active Position. Find active positions in Strategy.positions and closed, settled positions in Strategy.closed_positions.

Source code in lettrade/exchange/position.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def __init__(
    self,
    id: str,
    exchange: "Exchange",
    data: "DataFeed",
    size: float,
    parent: "Order",
    state: PositionState = PositionState.Open,
    entry_price: float | None = None,
    entry_fee: float = 0.0,
    entry_at: pd.Timestamp | None = None,
    sl_order: Order | None = None,
    tp_order: Order | None = None,
    tag: str | None = None,
    **kwargs,
):
    super().__init__(
        id=id,
        exchange=exchange,
        data=data,
        size=size,
        **kwargs,
    )
    self._account: "Account" = self.exchange._account

    self.state: PositionState = state
    self.parent: "Order" = parent
    self.tag: str | None = tag

    self.entry_price: float | None = entry_price
    self.entry_fee: float = entry_fee
    self.entry_at: pd.Timestamp | None = entry_at

    self.exit_price: float | None = None
    self.exit_fee: float = 0.0
    self.exit_at: pd.Timestamp | None = None
    self.exit_pl: float | None = None

    self.sl_order: Order | None = sl_order
    self.tp_order: Order | None = tp_order

fee property ¤

fee: float

Fee/Estimate Fee for trade

Returns:

  • float ( float ) –

    Fee

is_exited property ¤

is_exited: bool

Flag to check Position state.

Returns:

  • bool ( bool ) –

    True if the trade exited

is_long property ¤

is_long: bool

True if side is long (size is positive).

Returns:

  • bool ( bool ) –

    True/False

is_opening property ¤

is_opening: bool

Flag to check Position state.

Returns:

  • bool ( bool ) –

    True if the trade opening

is_short property ¤

is_short: bool

True if side is short (size is negative).

Returns:

  • bool ( bool ) –

    description

pl property ¤

pl: float

Estimate Profit or Loss of Position

Returns:

  • float ( float ) –

    PnL

side property ¤

side: TradeSide

True if side is short (size is negative).

Returns:

merge ¤

merge(other: Position) -> bool

Merge position from another position has same id

Parameters:

Raises:

Source code in lettrade/exchange/position.py
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
def merge(self, other: "Position") -> bool:
    """Merge position from another position has same id

    Args:
        other (Position): _description_

    Raises:
        RuntimeError: _description_
    """
    if other is self:
        return False

    if self.id != other.id:
        raise RuntimeError(f"Merge difference id {self.id} != {other.id} order")

    self.size = other.size

    if other.sl_order is not None:
        self.sl_order = other.sl_order
        self.sl_order.parent = self
    elif self.sl_order is not None:
        self.sl_order.cancel()
        self.sl_order = None

    if other.tp_order is not None:
        self.tp_order = other.tp_order
        self.tp_order.parent = self
    elif self.tp_order is not None:
        self.tp_order.cancel()
        self.tp_order = None

    if other.entry_at:
        self.entry_at = other.entry_at
    if other.entry_price:
        self.entry_price = other.entry_price

    if other.exit_at:
        self.exit_at = other.exit_at
    if other.exit_price:
        self.exit_price = other.exit_price
    if other.exit_fee:
        self.exit_fee = other.exit_fee
    if other.exit_pl:
        self.exit_pl = other.exit_pl

    if other.parent:
        self.parent = other.parent

    return True

PositionResult ¤

PositionResult(
    ok: bool = True,
    position: Position | None = None,
    raw: object | None = None,
)

Result of Position

Parameters:

  • ok (bool | None, default: True ) –

    Flag to check Position is success or not. Defaults to True.

  • position (Position | None, default: None ) –

    Position own the result. Defaults to None.

  • raw (object | None, default: None ) –

    Raw object of Position. Defaults to None.

Source code in lettrade/exchange/position.py
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
def __init__(
    self,
    ok: bool = True,
    position: "Position | None" = None,
    raw: object | None = None,
) -> None:
    """_summary_

    Args:
        ok (bool | None, optional): Flag to check `Position` is success or not. Defaults to True.
        position (Position | None, optional): Position own the result. Defaults to None.
        raw (object | None, optional): Raw object of `Position`. Defaults to None.
    """
    self.ok: bool = ok
    self.position: "Position | None" = position
    self.raw: object | None = raw

PositionResultError ¤

PositionResultError(
    error: str,
    position: Position | None = None,
    raw: object | None = None,
)

Bases: PositionResult

Result of a error Position

Parameters:

  • error (str) –

    Error message

  • position (Position | None, default: None ) –

    Position own the result. Defaults to None.

  • raw (object | None, default: None ) –

    Raw object of Position. Defaults to None.

Source code in lettrade/exchange/position.py
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
def __init__(
    self,
    error: str,
    position: "Position | None" = None,
    raw: object | None = None,
) -> None:
    """_summary_

    Args:
        error (str): Error message
        position (Position | None, optional): Position own the result. Defaults to None.
        raw (object | None, optional): Raw object of `Position`. Defaults to None.
    """
    super().__init__(ok=False, position=position, raw=raw)
    self.error: str = error

PositionResultOk ¤

PositionResultOk(
    position: Position | None = None,
    raw: object | None = None,
)

Bases: PositionResult

Result of a success Position

Parameters:

  • position (Position | None, default: None ) –

    Position own the result. Defaults to None.

  • raw (object | None, default: None ) –

    Raw object of Position. Defaults to None.

Source code in lettrade/exchange/position.py
272
273
274
275
276
277
278
279
280
281
282
283
def __init__(
    self,
    position: "Position | None" = None,
    raw: object | None = None,
) -> None:
    """_summary_

    Args:
        position (Position | None, optional): Position own the result. Defaults to None.
        raw (object | None, optional): Raw object of `Position`. Defaults to None.
    """
    super().__init__(ok=True, position=position, raw=raw)