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/client_service.py
7813 views
1
import logging
2
from datetime import timedelta
3
4
from tinkoff.invest import CandleInterval, Client, HistoricCandle
5
from tinkoff.invest.utils import now
6
7
from invest_api.invest_error_decorators import invest_error_logging, invest_api_retry
8
9
__all__ = ("ClientService")
10
11
logger = logging.getLogger(__name__)
12
13
14
class ClientService:
15
"""
16
The class encapsulate tinkoff client api
17
"""
18
def __init__(self, token: str, app_name: str) -> None:
19
self.__token = token
20
self.__app_name = app_name
21
22
@invest_api_retry()
23
@invest_error_logging
24
def download_historic_candle(
25
self,
26
figi: str,
27
from_days: int,
28
interval: CandleInterval
29
) -> list[HistoricCandle]:
30
"""Download and return all requested historical candles"""
31
result: list[HistoricCandle] = []
32
33
from_ = now() - timedelta(days=from_days)
34
logger.info(f"Start download recent candles. Figi: {figi}, from days: {from_}, interval: {interval.name}")
35
36
with Client(self.__token, app_name=self.__app_name) as client:
37
for candle in client.get_all_candles(
38
figi=figi,
39
from_=from_,
40
interval=interval
41
):
42
logger.debug(candle)
43
44
result.append(candle)
45
46
logger.info(f"Download complete: candles count {len(result)}")
47
48
return result
49
50
@invest_api_retry()
51
@invest_error_logging
52
def cancel_all_orders(self, account_id: str) -> None:
53
""" Cancel all open orders. """
54
logger.info(f"Cancel all orders for account id: {account_id}")
55
56
with Client(self.__token, app_name=self.__app_name) as client:
57
client.cancel_all_orders(account_id=account_id)
58
59
logger.info(f"Cancellation all orders complete.")
60
61