Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_TradingCompetition2022-main/account/SandboxAccount.py
5931 views
1
from account.Account import Account
2
from tinkoff.invest.services import (OrderType, MoneyValue)
3
from logger.LoggerFactory import LoggerFactory
4
from logger.BusinessLogger import BusinessLogger
5
6
7
class SandBoxAccount(Account):
8
""" ToDo implement logic for sandbox """
9
10
def __init__(self, client, account_id=None):
11
super(SandBoxAccount, self).__init__(client=client, account_id=account_id)
12
13
def get_user_account_id(self):
14
"""
15
Get SandBox Account ID
16
"""
17
sandbox_account_id = self._client.sandbox.open_sandbox_account().account_id
18
self._client.sandbox.sandbox_pay_in(account_id=sandbox_account_id,
19
amount=MoneyValue(currency='rub', units=int(self.daily_limit), nano=0))
20
return sandbox_account_id, 'SandBox_Account'
21
22
def _post_order(self, stock, quantity, direction):
23
""" Post order into TF platform """
24
post_order = self._client.sandbox.post_sandbox_order(figi=stock.figi, quantity=quantity, price=None,
25
direction=direction, account_id=self._account_id,
26
order_type=OrderType.ORDER_TYPE_MARKET,
27
order_id=self.__generate_order_id(ticker=stock.ticker))
28
# Log data
29
LoggerFactory.get_business_logger_instance().add_event(event_type=BusinessLogger.ORDER_POSTED,
30
obj=stock, value=post_order)
31
return post_order
32
33
def get_order_from_platform(self, order):
34
""" Read detail of order from TF platform """
35
order_state = self._client.sandbox.get_sandbox_order_state(account_id=self._account_id,
36
order_id=order.order_id)
37
return order_state
38
39
def get_operations(self, from_, to):
40
return self.client.sandbox.get_sandbox_operations(account_id=self.account_id,
41
from_=from_,
42
to=to)
43
44