Place new orders through `Strategy.buy()` and `Strategy.sell()`. Query existing orders through `Strategy.orders`. When an order is executed or [filled], it results in a `Trade`. If you wish to modify aspects of a placed but not yet filled order, cancel it and place a new one i
| 401 | |
| 402 | |
| 403 | class Order: |
| 404 | """ |
| 405 | Place new orders through `Strategy.buy()` and `Strategy.sell()`. |
| 406 | Query existing orders through `Strategy.orders`. |
| 407 | |
| 408 | When an order is executed or [filled], it results in a `Trade`. |
| 409 | |
| 410 | If you wish to modify aspects of a placed but not yet filled order, |
| 411 | cancel it and place a new one instead. |
| 412 | |
| 413 | All placed orders are [Good 'Til Canceled]. |
| 414 | |
| 415 | [filled]: https://www.investopedia.com/terms/f/fill.asp |
| 416 | [Good 'Til Canceled]: https://www.investopedia.com/terms/g/gtc.asp |
| 417 | """ |
| 418 | def __init__(self, broker: '_Broker', |
| 419 | size: float, |
| 420 | limit_price: Optional[float] = None, |
| 421 | stop_price: Optional[float] = None, |
| 422 | sl_price: Optional[float] = None, |
| 423 | tp_price: Optional[float] = None, |
| 424 | parent_trade: Optional['Trade'] = None, |
| 425 | tag: object = None): |
| 426 | self.__broker = broker |
| 427 | assert size != 0 |
| 428 | self.__size = size |
| 429 | self.__limit_price = limit_price |
| 430 | self.__stop_price = stop_price |
| 431 | self.__sl_price = sl_price |
| 432 | self.__tp_price = tp_price |
| 433 | self.__parent_trade = parent_trade |
| 434 | self.__tag = tag |
| 435 | |
| 436 | def _replace(self, **kwargs): |
| 437 | for k, v in kwargs.items(): |
| 438 | setattr(self, f'_{self.__class__.__qualname__}__{k}', v) |
| 439 | return self |
| 440 | |
| 441 | def __repr__(self): |
| 442 | return '<Order {}>'.format(', '.join(f'{param}={try_(lambda: round(value, 5), value)!r}' |
| 443 | for param, value in ( |
| 444 | ('size', self.__size), |
| 445 | ('limit', self.__limit_price), |
| 446 | ('stop', self.__stop_price), |
| 447 | ('sl', self.__sl_price), |
| 448 | ('tp', self.__tp_price), |
| 449 | ('contingent', self.is_contingent), |
| 450 | ('tag', self.__tag), |
| 451 | ) if value is not None)) # noqa: E126 |
| 452 | |
| 453 | def cancel(self): |
| 454 | """Cancel the order.""" |
| 455 | self.__broker.orders.remove(self) |
| 456 | trade = self.__parent_trade |
| 457 | if trade: |
| 458 | if self is trade._sl_order: |
| 459 | trade._replace(sl_order=None) |
| 460 | elif self is trade._tp_order: |