Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_tinkoff-trading-bot-develop/tools/get_accounts.py
5929 views
1
from grpc import StatusCode
2
from pydantic import BaseSettings
3
from tinkoff.invest import Client, RequestError, AccountType
4
5
6
class Settings(BaseSettings):
7
token: str
8
9
class Config:
10
env_file = ".env"
11
12
13
settings = Settings()
14
15
16
def get_sandbox_accounts():
17
with Client(settings.token) as client:
18
sb_accounts = client.sandbox.get_sandbox_accounts().accounts
19
if len(sb_accounts) == 0:
20
client.sandbox.open_sandbox_account()
21
return client.sandbox.get_sandbox_accounts().accounts
22
23
24
def get_accounts():
25
with Client(settings.token) as client:
26
try:
27
return client.users.get_accounts().accounts
28
except RequestError as e:
29
if e.code == StatusCode.UNAUTHENTICATED:
30
return get_sandbox_accounts()
31
32
33
if __name__ == "__main__":
34
accounts = get_accounts()
35
for account in accounts:
36
print(f"id: {account.id}, name: {account.name}, type: {str(AccountType(account.type))}")
37
38