Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_tinkoff_invest_robot-main/src/algotrading/_sandbox_accounts.py
7831 views
1
from tinkoff.invest import MoneyValue, OrderDirection, OrderType
2
3
from loguru import logger
4
from tinkoff.invest.utils import now
5
6
from src.algotrading.instruments_service import get_instrument_by
7
from src.algotrading.utils import to_float
8
from src.algotrading import utils
9
10
11
def get_sandbox_accounts(client):
12
sandbox_accounts = {}
13
accounts = client.sandbox.get_sandbox_accounts()
14
for account in accounts.accounts:
15
sandbox_accounts[account.id] = {}
16
sandbox_accounts[account.id]['info'] = {}
17
sandbox_accounts[account.id]['info']['name'] = account.name
18
sandbox_accounts[account.id]['info']['status'] = " ".join(account.status._name_.split('_')[2:])
19
sandbox_accounts[account.id]['info']['opened_date'] = account.opened_date.strftime("%Y-%m-%d %H:%M")
20
sandbox_accounts[account.id]['info']['access_level'] = " ".join(account.access_level._name_.split('_')[3:])
21
22
return sandbox_accounts
23
24
25
def delete(uuid):
26
api_client = utils.api_client_configure()
27
with api_client as client:
28
client.sandbox.close_sandbox_account(account_id=uuid)
29
30
31
def payin(uuid, amount, cur='rub'):
32
if cur == 'rub':
33
pay_in_cur(uuid, amount, cur)
34
elif cur == 'usd':
35
pay_in_cur(uuid, amount, cur)
36
37
38
def pay_in_cur(uuid, amount, cur):
39
money = MoneyValue(cur, amount, 0)
40
api_client = utils.api_client_configure()
41
with api_client as client:
42
logger.info(client.sandbox.sandbox_pay_in(account_id=uuid, amount=money))
43
44
45
def open():
46
api_client = utils.api_client_configure()
47
with api_client as client:
48
client.sandbox.open_sandbox_account()
49
50
51
def get_sandbox_positions(client, account_id):
52
positions = {}
53
sandbox_positions = client.sandbox.get_sandbox_positions(account_id=account_id)
54
for val in ['money', 'blocked']:
55
positions[val] = []
56
for money in sandbox_positions.__getattribute__(val):
57
positions[val].append({"name": money.currency,
58
"balance": to_float(money)
59
})
60
61
positions['securities'] = []
62
for position in sandbox_positions.securities:
63
positions['securities'].append({"name": get_instrument_by(position.figi)['name'],
64
"balance": position.balance,
65
"blocked": position.blocked
66
})
67
68
return positions
69
70
71
def get():
72
api_client = utils.api_client_configure()
73
with api_client as client:
74
sandbox_accounts = get_sandbox_accounts(client)
75
for account_id in sandbox_accounts:
76
positions = get_sandbox_positions(client, account_id)
77
78
sandbox_accounts[account_id]['positions'] = positions
79
80
return sandbox_accounts
81
82
83
def post_sandbox_order(client, account_id: str, figi: str, direct: str, count_lot: int = 1):
84
if direct == 'buy':
85
direction = OrderDirection.ORDER_DIRECTION_BUY
86
elif direct == 'sell':
87
direction = OrderDirection.ORDER_DIRECTION_SELL
88
else:
89
logger.warning('')
90
raise ValueError(f'Неправельный парамерт {direct=}')
91
92
order = client.sandbox.post_sandbox_order(
93
account_id=account_id,
94
figi=figi,
95
quantity=count_lot,
96
order_id=str(now().timestamp()),
97
direction=direction,
98
order_type=OrderType.ORDER_TYPE_MARKET
99
)
100
101
return order
102
103
104
if __name__ == "__main__":
105
print(get())
106
107