38 lines
1.6 KiB
Python
38 lines
1.6 KiB
Python
import os
|
|
from datetime import datetime, timedelta, timezone
|
|
from dotenv import load_dotenv
|
|
from t_tech.invest import Client
|
|
|
|
os.environ['SSL_CERT_FILE'] = '/etc/ssl/certs/ca-certificates.crt'
|
|
os.environ['GRPC_DEFAULT_SSL_ROOTS_FILE_PATH'] = '/etc/ssl/certs/ca-certificates.crt'
|
|
|
|
load_dotenv("/root/sber_bot/tok.env")
|
|
TOKEN = os.getenv("TINKOFF_TOKEN")
|
|
|
|
def q_to_f(q): return q.units + q.nano/1e9 if q else 0
|
|
|
|
with Client(TOKEN, target="invest-public-api.tbank.ru:443") as client:
|
|
# 1. Загружаем справочник тикеров, чтобы не мучить API в цикле
|
|
instr_dict = {i.figi: i.ticker for i in client.instruments.shares().instruments}
|
|
accounts = client.users.get_accounts().accounts
|
|
|
|
print(f"{'Дата (MSK)':<15} | {'Тикер':<6} | {'Тип':<8} | {'Цена':<8} | {'Кол':<4} | {'Сумма':<10}")
|
|
print("-" * 65)
|
|
|
|
for acc in accounts:
|
|
ops = client.operations.get_operations(
|
|
account_id=acc.id,
|
|
from_=datetime.now(timezone.utc) - timedelta(days=7),
|
|
to=datetime.now(timezone.utc)
|
|
).operations
|
|
|
|
for o in ops:
|
|
if o.operation_type.name in ['OPERATION_TYPE_BUY', 'OPERATION_TYPE_SELL']:
|
|
ticker = instr_dict.get(o.figi, "N/A")
|
|
date = o.date.astimezone(timezone(timedelta(hours=3))).strftime("%d.%m %H:%M")
|
|
op_type = "BUY" if "BUY" in o.operation_type.name else "SELL"
|
|
price = q_to_f(o.price)
|
|
payment = q_to_f(o.payment)
|
|
|
|
print(f"{date:<15} | {ticker:<6} | {op_type:<8} | {price:<8.2f} | {int(o.quantity):<4} | {payment:<10.2f}")
|