Path: blob/master/ invest-robot-contest_invest-bot-main/invest_api/services/client_service.py
7813 views
import logging1from datetime import timedelta23from tinkoff.invest import CandleInterval, Client, HistoricCandle4from tinkoff.invest.utils import now56from invest_api.invest_error_decorators import invest_error_logging, invest_api_retry78__all__ = ("ClientService")910logger = logging.getLogger(__name__)111213class ClientService:14"""15The class encapsulate tinkoff client api16"""17def __init__(self, token: str, app_name: str) -> None:18self.__token = token19self.__app_name = app_name2021@invest_api_retry()22@invest_error_logging23def download_historic_candle(24self,25figi: str,26from_days: int,27interval: CandleInterval28) -> list[HistoricCandle]:29"""Download and return all requested historical candles"""30result: list[HistoricCandle] = []3132from_ = now() - timedelta(days=from_days)33logger.info(f"Start download recent candles. Figi: {figi}, from days: {from_}, interval: {interval.name}")3435with Client(self.__token, app_name=self.__app_name) as client:36for candle in client.get_all_candles(37figi=figi,38from_=from_,39interval=interval40):41logger.debug(candle)4243result.append(candle)4445logger.info(f"Download complete: candles count {len(result)}")4647return result4849@invest_api_retry()50@invest_error_logging51def cancel_all_orders(self, account_id: str) -> None:52""" Cancel all open orders. """53logger.info(f"Cancel all orders for account id: {account_id}")5455with Client(self.__token, app_name=self.__app_name) as client:56client.cancel_all_orders(account_id=account_id)5758logger.info(f"Cancellation all orders complete.")596061