Path: blob/master/ invest-robot-contest_tinkoffSDK-master/my_account_manager.py
5925 views
import logging1from decimal import Decimal23from tinkoff.invest import Quotation4from tinkoff.invest.services import Services5from tinkoff.invest.strategies.base.errors import (6InsufficientMarginalTradeFunds, MarginalTradeIsNotActive)7from tinkoff.invest.strategies.base.strategy_settings_base import \8StrategySettings9from tinkoff.invest.utils import quotation_to_decimal1011logger = logging.getLogger(__name__)121314class AccountManager:15def __init__(self, services: Services,16strategy_settings: StrategySettings):17self._services = services18self._strategy_settings = strategy_settings1920def get_current_balance(self) -> Decimal:21account_id = self._strategy_settings.account_id22portfolio_response = self._services.operations.get_portfolio(23account_id=account_id24)25balance = portfolio_response.total_amount_currencies26shares = portfolio_response.total_amount_shares27return (28quotation_to_decimal(Quotation(units=balance.units, nano=balance.nano)),29quotation_to_decimal(Quotation(units=shares.units, nano=balance.nano))30)3132def ensure_marginal_trade(self) -> None:33account_id = self._strategy_settings.account_id34try:35response = self._services.users.get_margin_attributes(account_id=account_id)36except Exception as e:37raise MarginalTradeIsNotActive() from e38value = quotation_to_decimal(response.funds_sufficiency_level)39if value <= 1:40raise InsufficientMarginalTradeFunds()41logger.info("Marginal trade is active")424344