Path: blob/master/ invest-robot-contest_invest-bot-main/invest_api/services/stop_orders_service.py
7813 views
import datetime1import logging23from tinkoff.invest import Client, Quotation, StopOrderDirection, StopOrderExpirationType, StopOrderType, StopOrder45from invest_api.invest_error_decorators import invest_error_logging, invest_api_retry67__all__ = ("StopOrderService")89logger = logging.getLogger(__name__)101112class StopOrderService:13"""14The class encapsulate tinkoff stop order service api15"""16def __init__(self, token: str, app_name: str) -> None:17self.__token = token18self.__app_name = app_name1920@invest_api_retry()21@invest_error_logging22def __post_stop_order(23self,24account_id: str,25figi: str,26count_lots: int,27price: Quotation,28stop_price: Quotation,29direction: StopOrderDirection,30expiration_type: StopOrderExpirationType,31stop_order_type: StopOrderType,32expire_date: datetime33) -> str:34with Client(self.__token, app_name=self.__app_name) as client:35logger.debug(f"Post stop order for: {account_id}")3637return client.stop_orders.post_stop_order(38figi=figi,39quantity=count_lots,40price=price,41stop_price=stop_price,42direction=direction,43account_id=account_id,44expiration_type=expiration_type,45stop_order_type=stop_order_type,46expire_date=expire_date47).stop_order_id4849@invest_api_retry()50@invest_error_logging51def get_stop_orders(self, account_id: str) -> list[StopOrder]:52with Client(self.__token, app_name=self.__app_name) as client:53return client.stop_orders.get_stop_orders(account_id=account_id).stop_orders5455@invest_api_retry()56@invest_error_logging57def cancel_stop_order(self, account_id: str, stop_order_id: str) -> None:58with Client(self.__token, app_name=self.__app_name) as client:59client.stop_orders.cancel_stop_order(account_id=account_id, stop_order_id=stop_order_id)606162