Skip to content

trade ¤

LiveExecution ¤

LiveExecution(
    id: str,
    exchange: LiveExchange,
    data: LiveDataFeed,
    size: float,
    price: float,
    at: float,
    order_id: str | None = None,
    order: Order | None = None,
    position_id: str | None = None,
    position: Position | None = None,
    api: LiveAPI | None = None,
    raw: object | None = None,
    **kwargs
)

Bases: _LiveTrade, Execution

Execution for Live

Source code in lettrade/exchange/live/trade.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def __init__(
    self,
    id: str,
    exchange: "LiveExchange",
    data: "LiveDataFeed",
    size: float,
    price: float,
    at: float,
    order_id: str | None = None,
    order: "Order | None" = None,
    position_id: str | None = None,
    position: "Position | None" = None,
    # tag: str | None = None,
    api: LiveAPI | None = None,
    raw: object | None = None,
    **kwargs,
):
    super().__init__(
        id=id,
        exchange=exchange,
        data=data,
        size=size,
        price=price,
        at=at,
        order_id=order_id,
        order=order,
        position_id=position_id,
        position=position,
        api=api,
        raw=raw,
        **kwargs,
    )

is_long property ¤

is_long: bool

True if side is long (size is positive).

Returns:

  • bool ( bool ) –

    True/False

is_short property ¤

is_short: bool

True if side is short (size is negative).

Returns:

  • bool ( bool ) –

    description

side property ¤

side: TradeSide

True if side is short (size is negative).

Returns:

from_raw abstractmethod classmethod ¤

from_raw(raw, exchange: LiveExchange) -> LiveExecution

Building new LiveExecution from live api raw object

Parameters:

  • raw (_type_) –

    description

  • exchange (LiveExchange) –

    description

Returns:

Source code in lettrade/exchange/live/trade.py
77
78
79
80
81
82
83
84
85
86
87
88
89
@classmethod
@abstractmethod
def from_raw(cls, raw, exchange: "LiveExchange") -> "LiveExecution":
    """Building new LiveExecution from live api raw object

    Args:
        raw (_type_): _description_
        exchange (LiveExchange): _description_

    Returns:
        LiveExecution: _description_
    """
    raise NotImplementedError(cls)

merge ¤

merge(other: Execution)

Merge to keep object handler but not overwrite for Strategy using when Strategy want to store object and object will be automatic update directly

Source code in lettrade/exchange/execution.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def merge(self, other: "Execution"):
    """
    Merge to keep object handler but not overwrite for Strategy using when Strategy want to store object and object will be automatic update directly
    """
    if other is self:
        return

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

    self.price = other.price
    self.size = other.size

    if other.at:
        self.at = other.at
    if other.order_id:
        self.order_id = other.order_id
    if other.order:
        self.order = other.order
    if other.position_id:
        self.position_id = other.position_id
    if other.position:
        self.position = other.position

LiveOrder ¤

LiveOrder(
    id: str,
    exchange: LiveExchange,
    data: LiveDataFeed,
    size: float,
    state: OrderState = OrderState.Pending,
    type: OrderType = OrderType.Market,
    limit_price: float | None = None,
    stop_price: float | None = None,
    sl_price: float | None = None,
    tp_price: float | None = None,
    parent: Position | None = None,
    tag: str | None = None,
    api: LiveAPI | None = None,
    raw: object | None = None,
    **kwargs
)

Bases: _LiveTrade, Order

Source code in lettrade/exchange/live/trade.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
def __init__(
    self,
    id: str,
    exchange: "LiveExchange",
    data: "LiveDataFeed",
    size: float,
    state: OrderState = OrderState.Pending,
    type: OrderType = OrderType.Market,
    limit_price: float | None = None,
    stop_price: float | None = None,
    sl_price: float | None = None,
    tp_price: float | None = None,
    parent: "Position | None" = None,
    tag: str | None = None,
    api: LiveAPI | None = None,
    raw: object | None = None,
    **kwargs,
):
    super().__init__(
        id=id,
        exchange=exchange,
        data=data,
        size=size,
        state=state,
        type=type,
        limit_price=limit_price,
        stop_price=stop_price,
        sl_price=sl_price,
        tp_price=tp_price,
        parent=parent,
        tag=tag,
        api=api,
        raw=raw,
        **kwargs,
    )

is_closed property ¤

is_closed: bool

Flag to check Order closed

Returns:

  • bool ( bool ) –

    True if state in [OrderState.Filled, OrderState.Canceled]

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 Order still alive

Returns:

  • bool ( bool ) –

    True if state in [OrderState.Pending, OrderState.Placed, OrderState.Partial]

is_short property ¤

is_short: bool

True if side is short (size is negative).

Returns:

  • bool ( bool ) –

    description

is_sl_order property ¤

is_sl_order: bool

Order is stop-loss order of a Position

Returns:

  • bool ( bool ) –

    description

is_tp_order property ¤

is_tp_order: bool

Order is take-profit order of a Position

Returns:

  • bool ( bool ) –

    description

limit property ¤

limit: float | None

Getter of limit_price

Returns:

  • float | None

    float | None: float or None

place_price property ¤

place_price: float | None

Getter of place_price

Returns:

  • float | None

    float | None: float or None

side property ¤

side: TradeSide

True if side is short (size is negative).

Returns:

sl property ¤

sl: float | None

Getter of sl_price

Returns:

  • float | None

    float | None: float or None

stop property ¤

stop: float | None

Getter of stop_price

Returns:

  • float | None

    float | None: float or None

tp property ¤

tp: float | None

Getter of tp_price

Returns:

  • float | None

    float | None: float or None

cancel ¤

cancel(**kwargs) -> OrderResult

Cancel order

Returns:

Source code in lettrade/exchange/live/trade.py
183
184
185
186
187
188
189
190
def cancel(self, **kwargs) -> OrderResult:
    """Cancel order

    Returns:
        OrderResult: _description_
    """
    result = self._api.order_close(order=self, **kwargs)
    return super().cancel(raw=result)

fill ¤

fill(
    price: float, at: Timestamp, raw: object | None = None
) -> OrderResult

Fill Order. Set status to OrderState.Executed. Send event to Exchange

Parameters:

  • price (float) –

    Executed price

  • at (Timestamp) –

    Executed bar

Raises:

Returns:

Source code in lettrade/exchange/order.py
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
def fill(
    self,
    price: float,
    at: pd.Timestamp,
    raw: object | None = None,
) -> "OrderResult":
    """Fill `Order`.
    Set `status` to `OrderState.Executed`.
    Send event to `Exchange`

    Args:
        price (float): Executed price
        at (pd.Timestamp): Executed bar

    Raises:
        RuntimeError: _description_

    Returns:
        OrderResult: result of `Order`
    """
    if self.state != OrderState.Placed:
        raise RuntimeError(f"Order {self.id} state {self.state} is not Placed")

    self.filled_at = at
    self.filled_price = price
    self.state = OrderState.Filled
    self.exchange.on_order(self)
    return OrderResultOk(order=self, raw=raw)

from_position abstractmethod classmethod ¤

from_position(
    position: LivePosition, sl=None, tp=None
) -> LiveOrder

summary

Parameters:

  • position (LivePosition) –

    description

  • sl (_type_, default: None ) –

    description. Defaults to None.

  • tp (_type_, default: None ) –

    description. Defaults to None.

Returns:

Source code in lettrade/exchange/live/trade.py
206
207
208
209
210
211
212
213
214
215
216
217
218
219
@classmethod
@abstractmethod
def from_position(cls, position: "LivePosition", sl=None, tp=None) -> "LiveOrder":
    """_summary_

    Args:
        position (LivePosition): _description_
        sl (_type_, optional): _description_. Defaults to None.
        tp (_type_, optional): _description_. Defaults to None.

    Returns:
        LiveOrder: _description_
    """
    raise NotImplementedError(cls)

from_raw abstractmethod classmethod ¤

from_raw(raw, exchange: LiveExchange) -> LiveOrder

summary

Parameters:

  • raw (_type_) –

    description

  • exchange (LiveExchange) –

    description

Returns:

Source code in lettrade/exchange/live/trade.py
192
193
194
195
196
197
198
199
200
201
202
203
204
@classmethod
@abstractmethod
def from_raw(cls, raw, exchange: "LiveExchange") -> "LiveOrder":
    """_summary_

    Args:
        raw (_type_): _description_
        exchange (LiveExchange): _description_

    Returns:
        LiveOrder: _description_
    """
    raise NotImplementedError(cls)

merge ¤

merge(other: Order)

Update current Order variables by other Order

Parameters:

  • other (Order) –

    Merge source Order

Raises:

Source code in lettrade/exchange/order.py
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
def merge(self, other: "Order"):
    """Update current `Order` variables by other `Order`

    Args:
        other (Order): Merge source `Order`

    Raises:
        RuntimeError: Validate same id
    """
    if other is self:
        return

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

    self.size = other.size
    self.sl_price = other.sl_price
    self.tp_price = other.tp_price

    if other.limit_price:
        self.limit_price = other.limit_price
    if other.stop_price:
        self.stop_price = other.stop_price
    if other.placed_at:
        self.placed_at = other.placed_at

    if other.filled_price:
        self.filled_price = other.filled_price
    if other.filled_at:
        self.filled_at = other.filled_at

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

LivePosition ¤

LivePosition(
    id: str,
    exchange: LiveExchange,
    data: LiveDataFeed,
    size: float,
    parent: Order,
    tag: str | None = None,
    state: PositionState = PositionState.Open,
    entry_price: float | None = None,
    entry_fee: float = 0.0,
    entry_at: int | None = None,
    sl_order: Order | None = None,
    tp_order: Order | None = None,
    api: LiveAPI | None = None,
    raw: object | None = None,
    **kwargs
)

Bases: _LiveTrade, Position

Source code in lettrade/exchange/live/trade.py
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
def __init__(
    self,
    id: str,
    exchange: "LiveExchange",
    data: "LiveDataFeed",
    size: float,
    parent: Order,
    tag: str | None = None,
    state: PositionState = PositionState.Open,
    entry_price: float | None = None,
    entry_fee: float = 0.0,
    entry_at: int | None = None,
    sl_order: Order | None = None,
    tp_order: Order | None = None,
    api: LiveAPI | None = None,
    raw: object | None = None,
    **kwargs,
):
    super().__init__(
        id=id,
        exchange=exchange,
        data=data,
        size=size,
        parent=parent,
        tag=tag,
        state=state,
        entry_price=entry_price,
        entry_fee=entry_fee,
        entry_at=entry_at,
        sl_order=sl_order,
        tp_order=tp_order,
        api=api,
        raw=raw,
        **kwargs,
    )

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:

exit abstractmethod ¤

exit() -> PositionResult

summary

Returns:

Source code in lettrade/exchange/live/trade.py
279
280
281
282
283
284
285
286
@abstractmethod
def exit(self) -> PositionResult:
    """_summary_

    Returns:
        bool: _description_
    """
    raise NotImplementedError(type(self))

from_raw abstractmethod classmethod ¤

from_raw(
    raw,
    exchange: LiveExchange,
    state: PositionState = PositionState.Open,
    **kwargs
) -> LivePosition

summary

Parameters:

  • raw (_type_) –

    description

  • exchange (LiveExchange) –

    description

Returns:

Source code in lettrade/exchange/live/trade.py
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
@classmethod
@abstractmethod
def from_raw(
    cls,
    raw,
    exchange: "LiveExchange",
    state: PositionState = PositionState.Open,
    **kwargs,
) -> "LivePosition":
    """_summary_

    Args:
        raw (_type_): _description_
        exchange (LiveExchange): _description_

    Returns:
        LivePosition: _description_
    """
    raise NotImplementedError(cls)

merge ¤

merge(other: LivePosition) -> bool

Merge LivePosition from another

Parameters:

Returns:

  • bool ( bool ) –

    description

Source code in lettrade/exchange/live/trade.py
288
289
290
291
292
293
294
295
296
297
298
299
300
def merge(self, other: "LivePosition") -> bool:
    """Merge LivePosition from another

    Args:
        other (LivePosition): _description_

    Returns:
        bool: _description_
    """
    if not super().merge(other):
        return False
    self.raw = other.raw
    return True

update abstractmethod ¤

update(
    sl: float | None = None,
    tp: float | None = None,
    caller: float | None = None,
    **kwargs
) -> PositionResult

summary

Parameters:

  • sl (float | None, default: None ) –

    description. Defaults to None.

  • tp (float | None, default: None ) –

    description. Defaults to None.

  • caller (float | None, default: None ) –

    description. Defaults to None.

Raises:

Source code in lettrade/exchange/live/trade.py
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
@abstractmethod
def update(
    self,
    sl: float | None = None,
    tp: float | None = None,
    caller: float | None = None,
    **kwargs,
) -> PositionResult:
    """_summary_

    Args:
        sl (float | None, optional): _description_. Defaults to None.
        tp (float | None, optional): _description_. Defaults to None.
        caller (float | None, optional): _description_. Defaults to None.

    Raises:
        NotImplementedError: _description_
    """
    raise NotImplementedError(type(self))