/
SimpleSpace
/
AutoInvestDataSync
Обзор
Документация
Войти
/
SimpleSpace
/
AutoInvestDataSync
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
sync/dividends.py
74 строки
3 KB
Alex Just
Start commit for mvp 0.0.5 version
04 май 2026, 22:47
04 май 2026, 22:47
e12863e
Код
Авторство
О чём код?
from __future__ import annotations import logging from datetime import datetime, timedelta from dataclasses import dataclass from sync.client import TInvestClient from sync.portfolio import PositionData from sync.utils import money_to_float logger = logging.getLogger(__name__) @dataclass class UpcomingPayout: figi: str = "" payout_date: str = "" payout_amount: float = 0.0 payout_currency: str = "" payout_type: str = "" def enrich_payouts(tinvest: TInvestClient, positions: list[PositionData]) -> list[UpcomingPayout]: payouts: list[UpcomingPayout] = [] now = datetime.utcnow() future = now + timedelta(days=365) for pos in positions: if not pos.figi: continue itype = pos.instrument_type.upper() if pos.instrument_type else "" if "SHARE" in itype: try: resp = tinvest.get_dividends(pos.figi, from_=now, to=future) for div in resp.dividends: if div.dividend_net: payouts.append(UpcomingPayout( figi=pos.figi, payout_date=div.record_date.strftime("%Y-%m-%d") if div.record_date else "", payout_amount=money_to_float(div.dividend_net) * pos.quantity, payout_currency=div.dividend_net.currency, payout_type="Дивиденд", )) break except Exception as e: logger.debug("Dividends fetch failed for %s: %s", pos.figi, e) elif "BOND" in itype: try: resp = tinvest.get_bond_coupons(pos.figi, from_=now, to=future) for coupon in resp.events: if coupon.coupon_date and coupon.pay_one_bond: payouts.append(UpcomingPayout( figi=pos.figi, payout_date=coupon.coupon_date.strftime("%Y-%m-%d"), payout_amount=money_to_float(coupon.pay_one_bond) * pos.quantity, payout_currency=coupon.pay_one_bond.currency, payout_type="Купон", )) break except Exception as e: logger.debug("Bond coupons fetch failed for %s: %s", pos.figi, e) for pos in positions: matching = [p for p in payouts if p.figi == pos.figi] if matching: nearest = matching[0] pos.next_payout_date = nearest.payout_date pos.next_payout_amount = nearest.payout_amount pos.next_payout_currency = nearest.payout_currency return payouts