Path: blob/master/ invest-robot-contest_TradingCompetition2022-main/account/SandboxAccount.py
5931 views
from account.Account import Account1from tinkoff.invest.services import (OrderType, MoneyValue)2from logger.LoggerFactory import LoggerFactory3from logger.BusinessLogger import BusinessLogger456class SandBoxAccount(Account):7""" ToDo implement logic for sandbox """89def __init__(self, client, account_id=None):10super(SandBoxAccount, self).__init__(client=client, account_id=account_id)1112def get_user_account_id(self):13"""14Get SandBox Account ID15"""16sandbox_account_id = self._client.sandbox.open_sandbox_account().account_id17self._client.sandbox.sandbox_pay_in(account_id=sandbox_account_id,18amount=MoneyValue(currency='rub', units=int(self.daily_limit), nano=0))19return sandbox_account_id, 'SandBox_Account'2021def _post_order(self, stock, quantity, direction):22""" Post order into TF platform """23post_order = self._client.sandbox.post_sandbox_order(figi=stock.figi, quantity=quantity, price=None,24direction=direction, account_id=self._account_id,25order_type=OrderType.ORDER_TYPE_MARKET,26order_id=self.__generate_order_id(ticker=stock.ticker))27# Log data28LoggerFactory.get_business_logger_instance().add_event(event_type=BusinessLogger.ORDER_POSTED,29obj=stock, value=post_order)30return post_order3132def get_order_from_platform(self, order):33""" Read detail of order from TF platform """34order_state = self._client.sandbox.get_sandbox_order_state(account_id=self._account_id,35order_id=order.order_id)36return order_state3738def get_operations(self, from_, to):39return self.client.sandbox.get_sandbox_operations(account_id=self.account_id,40from_=from_,41to=to)424344