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/instruments_service.py
7826 views
1
import datetime
2
import logging
3
4
from tinkoff.invest import Client, TradingSchedule, InstrumentIdType, InstrumentStatus
5
6
from configuration.settings import ShareSettings
7
from invest_api.invest_error_decorators import invest_error_logging, invest_api_retry
8
from invest_api.utils import moex_exchange_name
9
10
__all__ = ("InstrumentService")
11
12
logger = logging.getLogger(__name__)
13
14
15
class InstrumentService:
16
"""
17
The class encapsulate tinkoff instruments api
18
"""
19
def __init__(self, token: str, app_name: str) -> None:
20
self.__token = token
21
self.__app_name = app_name
22
23
def moex_today_trading_schedule(self) -> (bool, datetime, datetime):
24
"""
25
:return: Information about trading day status, datetime trading day start, datetime trading day end
26
(both on today)
27
"""
28
for schedule in self.__trading_schedules(
29
exchange=moex_exchange_name(),
30
_from=datetime.datetime.utcnow(),
31
_to=datetime.datetime.utcnow() + datetime.timedelta(days=1)
32
):
33
for day in schedule.days:
34
if day.date.date() == datetime.date.today():
35
logger.info(f"MOEX today schedule: {day}")
36
return day.is_trading_day, day.start_time, day.end_time
37
38
return False, datetime.datetime.utcnow(), datetime.datetime.utcnow()
39
40
@invest_api_retry()
41
@invest_error_logging
42
def __trading_schedules(
43
self,
44
exchange: str,
45
_from: datetime,
46
_to: datetime
47
) -> list[TradingSchedule]:
48
result = []
49
50
with Client(self.__token, app_name=self.__app_name) as client:
51
logger.debug(f"Trading Schedules for exchange: {exchange}, from: {_from}, to: {_to}")
52
53
for schedule in client.instruments.trading_schedules(
54
exchange=exchange,
55
from_=_from,
56
to=_to
57
).exchanges:
58
logger.debug(f"{schedule}")
59
result.append(schedule)
60
61
return result
62
63
@invest_api_retry()
64
@invest_error_logging
65
def share_by_figi(self, figi: str) -> ShareSettings:
66
"""
67
:return: Information about share settings by it figi
68
"""
69
with Client(self.__token, app_name=self.__app_name) as client:
70
logger.debug(f"ShareBy figi: {figi}:")
71
72
share = client.instruments.share_by(
73
id_type=InstrumentIdType.INSTRUMENT_ID_TYPE_FIGI,
74
id=figi
75
).instrument
76
logger.debug(f"{share}")
77
78
return ShareSettings(
79
ticker=share.ticker,
80
lot=share.lot,
81
short_enabled_flag=share.short_enabled_flag,
82
otc_flag=share.otc_flag,
83
buy_available_flag=share.buy_available_flag,
84
sell_available_flag=share.sell_available_flag,
85
api_trade_available_flag=share.api_trade_available_flag
86
)
87
88
@invest_api_retry()
89
@invest_error_logging
90
def __currencies(self) -> None:
91
with Client(self.__token, app_name=self.__app_name) as client:
92
for cur in client.instruments.currencies(
93
instrument_status=InstrumentStatus.INSTRUMENT_STATUS_BASE
94
).instruments:
95
logger.debug(f"{cur}")
96
97
@invest_api_retry()
98
@invest_error_logging
99
def __instrument_by_figi(self, figi: str) -> None:
100
with Client(self.__token, app_name=self.__app_name) as client:
101
logger.debug(f"InstrumentBy figi: {figi}:")
102
103
instrument = client.instruments.get_instrument_by(id_type=InstrumentIdType.INSTRUMENT_ID_TYPE_FIGI,
104
id=figi).instrument
105
106
logger.debug(f"{instrument}")
107
108