Path: blob/master/ invest-robot-contest_tinkoffSDK-master/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,7MarginalTradeIsNotActive,8)9from tinkoff.invest.strategies.base.strategy_settings_base import StrategySettings10from tinkoff.invest.utils import quotation_to_decimal1112logger = logging.getLogger(__name__)131415class AccountManager:16def __init__(self, services: Services, strategy_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 quotation_to_decimal(Quotation(units=balance.units, nano=balance.nano)), quotation_to_decimal(Quotation(units=shares.units, nano=balance.nano))2829def ensure_marginal_trade(self) -> None:30account_id = self._strategy_settings.account_id31try:32response = self._services.users.get_margin_attributes(account_id=account_id)33except Exception as e:34raise MarginalTradeIsNotActive() from e35value = quotation_to_decimal(response.funds_sufficiency_level)36if value <= 1:37raise InsufficientMarginalTradeFunds()38logger.info("Marginal trade is active")394041