Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_invest-bot-main/invest_api/services/stop_orders_service.py
7813 views
1
import datetime
2
import logging
3
4
from tinkoff.invest import Client, Quotation, StopOrderDirection, StopOrderExpirationType, StopOrderType, StopOrder
5
6
from invest_api.invest_error_decorators import invest_error_logging, invest_api_retry
7
8
__all__ = ("StopOrderService")
9
10
logger = logging.getLogger(__name__)
11
12
13
class StopOrderService:
14
"""
15
The class encapsulate tinkoff stop order service api
16
"""
17
def __init__(self, token: str, app_name: str) -> None:
18
self.__token = token
19
self.__app_name = app_name
20
21
@invest_api_retry()
22
@invest_error_logging
23
def __post_stop_order(
24
self,
25
account_id: str,
26
figi: str,
27
count_lots: int,
28
price: Quotation,
29
stop_price: Quotation,
30
direction: StopOrderDirection,
31
expiration_type: StopOrderExpirationType,
32
stop_order_type: StopOrderType,
33
expire_date: datetime
34
) -> str:
35
with Client(self.__token, app_name=self.__app_name) as client:
36
logger.debug(f"Post stop order for: {account_id}")
37
38
return client.stop_orders.post_stop_order(
39
figi=figi,
40
quantity=count_lots,
41
price=price,
42
stop_price=stop_price,
43
direction=direction,
44
account_id=account_id,
45
expiration_type=expiration_type,
46
stop_order_type=stop_order_type,
47
expire_date=expire_date
48
).stop_order_id
49
50
@invest_api_retry()
51
@invest_error_logging
52
def get_stop_orders(self, account_id: str) -> list[StopOrder]:
53
with Client(self.__token, app_name=self.__app_name) as client:
54
return client.stop_orders.get_stop_orders(account_id=account_id).stop_orders
55
56
@invest_api_retry()
57
@invest_error_logging
58
def cancel_stop_order(self, account_id: str, stop_order_id: str) -> None:
59
with Client(self.__token, app_name=self.__app_name) as client:
60
client.stop_orders.cancel_stop_order(account_id=account_id, stop_order_id=stop_order_id)
61
62