forked from deynekoaa/tinkoff-analytics
/
tinkoffapi.py
71 lines (53 loc) · 2.59 KB
/
tinkoffapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from datetime import datetime
from decimal import Decimal
from typing import List
import json
from pprint import pprint
from utils import localize, get_now
import tinvest
OPERATIONS_FROM='2000-01-01T00:00:00.000000+03:00'
#from utils import localize, get_now
class TinkoffApi:
"""Обёртка для работы с API Тинькова на основе библиотеки tinvest"""
Currency = tinvest.Currency
InstrumentType = tinvest.InstrumentType
OperationTypeWithCommission = tinvest.OperationTypeWithCommission
def __init__(self, api_token: str, account_type: str):
self._client = tinvest.SyncClient(api_token)
self._account_type = account_type
self._account_id = None
accounts = self._client.get_accounts().payload.accounts
#pprint(accounts)
for account in accounts:
if account.broker_account_type == self._account_type:
self._account_id = account.broker_account_id
#pprint(account)
def get_usd_course(self) -> Decimal:
"""Отдаёт текущий курс доллара в брокере"""
return self._client.get_market_orderbook(figi="BBG0013HGFT4", depth=1)\
.payload.last_price
def get_eur_course(self) -> Decimal:
"""Отдаёт текущий курс евро в брокере"""
return self._client.get_market_orderbook(figi="BBG0013HJJ31", depth=1)\
.payload.last_price
def get_portfolio_positions(self) \
-> List[tinvest.schemas.PortfolioPosition]:
"""Возвращает все позиции в портфеле"""
positions = self._client.get_portfolio(broker_account_id=self._account_id)\
.payload.positions
return positions
def get_portfolio_currencies(self) \
-> List[tinvest.schemas.Currencies]:
"""Возвращает денежные позиции в портфеле"""
positions = self._client.get_portfolio_currencies(broker_account_id=self._account_id)\
.payload.currencies
return positions
def get_all_operations(self, from_in: datetime = OPERATIONS_FROM) \
-> List[tinvest.schemas.Operation]:
"""Возвращает все операции в портфеле 'с' и 'по' указанные даты"""
#from_ = localize(from_in)
from_ = from_in
now = get_now()
operations = self._client.get_operations(broker_account_id=self._account_id, from_=from_, to=now)\
.payload.operations
return operations