| 728 | self.__set_contingent('tp', price) |
| 729 | |
| 730 | def __set_contingent(self, type, price): |
| 731 | assert type in ('sl', 'tp') |
| 732 | assert price is None or 0 < price < np.inf, f'Make sure 0 < price < inf! price: {price}' |
| 733 | attr = f'_{self.__class__.__qualname__}__{type}_order' |
| 734 | order: Order = getattr(self, attr) |
| 735 | if order: |
| 736 | order.cancel() |
| 737 | if price: |
| 738 | kwargs = {'stop': price} if type == 'sl' else {'limit': price} |
| 739 | order = self.__broker.new_order(-self.size, trade=self, tag=self.tag, **kwargs) |
| 740 | setattr(self, attr, order) |
| 741 | |
| 742 | |
| 743 | class _Broker: |