-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinkoffapi.py
40 lines (30 loc) · 1.47 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
from datetime import datetime
from decimal import Decimal
from typing import List
import tinvest
import tinvest.schemas
from utils import localize, get_now
class TinkoffApi:
"""Обёртка для работы с API Тинькова на основе библиотеки tinvest"""
def __init__(self, api_token: str, broker_account_id: int):
self._client = tinvest.SyncClient(api_token)
self._broker_account_id = broker_account_id
def get_usd_course(self) -> Decimal:
"""Отдаёт текущий курс доллара в брокере"""
return Decimal(str(self
._client.get_market_orderbook(figi="BBG0013HGFT4", depth=1)
.payload.last_price)
)
def get_portfolio_positions(self) \
-> List[tinvest.schemas.PortfolioPosition]:
"""Возвращает все позиции в портфеле"""
return (self._client.get_portfolio(self._broker_account_id).payload.positions)
def get_all_operations(self, broker_account_started_at: datetime) \
-> List[tinvest.schemas.Operation]:
"""Возвращает все операции в портфеле с указанной даты"""
from_ = localize(broker_account_started_at)
now = get_now()
operations = (self._client.\
get_operations(broker_account_id=self._broker_account_id, from_=from_, to=now)\
.payload.operations)
return operations