54 lines
2.4 KiB
Python
54 lines
2.4 KiB
Python
import os
|
||
from datetime import datetime, timedelta, timezone
|
||
from dotenv import load_dotenv
|
||
from t_tech.invest import Client
|
||
|
||
# Настройка SSL для вашего сервера
|
||
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):
|
||
if not q: return 0.0
|
||
return q.units + q.nano/1e9
|
||
|
||
with Client(TOKEN, target="invest-public-api.tbank.ru:443") as client:
|
||
# 1. Загружаем справочник акций, чтобы знать тикеры
|
||
print("⏳ Загрузка справочника инструментов...")
|
||
shares = client.instruments.shares().instruments
|
||
ticker_map = {s.uid: s.ticker for s in shares}
|
||
figi_map = {s.figi: s.ticker for s in shares}
|
||
|
||
# 2. Получаем все счета
|
||
accounts = client.users.get_accounts().accounts
|
||
|
||
print(f"\n{'ДАТА (MSK)':<15} | {'СЧЕТ':<15} | {'ТИКЕР':<6} | {'ТИП':<12} | {'СУММА':<12}")
|
||
print("-" * 70)
|
||
|
||
for acc in accounts:
|
||
# Запрашиваем абсолютно все операции за 14 дней
|
||
ops = client.operations.get_operations(
|
||
account_id=acc.id,
|
||
from_=datetime.now(timezone.utc) - timedelta(days=14),
|
||
to=datetime.now(timezone.utc)
|
||
).operations
|
||
|
||
for o in ops:
|
||
ticker = ticker_map.get(o.instrument_uid) or figi_map.get(o.figi) or ""
|
||
|
||
# Нам интересны только Сбер, Татнефть и движение денег
|
||
is_target = any(t in ticker for t in ["SBER", "TATN"])
|
||
is_money = "PAYMENT" in o.operation_type.name or "DEPOSIT" in o.operation_type.name
|
||
|
||
if is_target or is_money:
|
||
date = o.date.astimezone(timezone(timedelta(hours=3))).strftime("%d.%m %H:%M")
|
||
op_name = o.type
|
||
payment = q_to_f(o.payment)
|
||
|
||
# Если это покупка/продажа, добавим цену для наглядности
|
||
price_str = f" @ {q_to_f(o.price):.2f}" if q_to_f(o.price) > 0 else ""
|
||
|
||
print(f"{date:<15} | {acc.name[:15]:<15} | {ticker:<6} | {op_name[:12]:<12} | {payment:>10.2f} RUB {price_str}")
|