mirror of
https://github.com/ada-dmitry/dca_bot_tinv.git
synced 2026-09-24 00:30:23 +00:00
Init Repo
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
# check_balance.py
|
||||
from dotenv import load_dotenv
|
||||
from tinkoff.invest import Client
|
||||
from tinkoff.invest.utils import quotation_to_decimal
|
||||
from decimal import Decimal
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def dec_money(mv) -> Decimal:
|
||||
# Работает и для MoneyValue, и для Quotation
|
||||
return quotation_to_decimal(mv) if mv is not None else Decimal(0)
|
||||
|
||||
|
||||
load_dotenv()
|
||||
token = os.environ.get("TINKOFF_TOKEN")
|
||||
account_id = os.environ.get("TINKOFF_ACCOUNT_ID")
|
||||
|
||||
if not token:
|
||||
print("ERROR: TINKOFF_TOKEN is not set")
|
||||
sys.exit(2)
|
||||
|
||||
with Client(token) as client:
|
||||
# Если account_id не задан, берём первый реальный счёт
|
||||
if not account_id:
|
||||
accs = client.users.get_accounts().accounts
|
||||
if not accs:
|
||||
print(
|
||||
"Нет реальных счетов. Проверь права токена: accounts:read и наличие счёта.")
|
||||
sys.exit(3)
|
||||
account_id = accs[0].id
|
||||
|
||||
# Универсальный способ: get_positions -> money (остатки) и blocked (заморожено)
|
||||
pos = client.operations.get_positions(account_id=account_id)
|
||||
|
||||
rub_money = next(
|
||||
(m for m in pos.money if m.currency.lower() == "rub"), None)
|
||||
rub_blocked = next(
|
||||
(m for m in pos.blocked if m.currency.lower() == "rub"), None)
|
||||
|
||||
rub_free = dec_money(rub_money) if rub_money else Decimal(0)
|
||||
rub_frozen = dec_money(rub_blocked) if rub_blocked else Decimal(0)
|
||||
rub_available = rub_free - rub_frozen
|
||||
|
||||
print("ACCOUNT_ID:", account_id)
|
||||
print("RUB free: ", rub_free)
|
||||
print("RUB blocked: ", rub_frozen)
|
||||
print("RUB available:", rub_available)
|
||||
|
||||
# Дополнительно: оценка портфеля (если нужно)
|
||||
pf = client.operations.get_portfolio(account_id=account_id)
|
||||
total_currencies = dec_money(pf.total_amount_currencies) if hasattr(
|
||||
pf, "total_amount_currencies") else None
|
||||
if total_currencies is not None:
|
||||
print("Portfolio total_amount_currencies:", total_currencies)
|
||||
@@ -0,0 +1,42 @@
|
||||
# check_figi.py
|
||||
from dotenv import load_dotenv
|
||||
from tinkoff.invest import Client, InstrumentIdType
|
||||
from tinkoff.invest.utils import quotation_to_decimal
|
||||
import os
|
||||
import sys
|
||||
|
||||
FIGI = ["BBG00425VG07", "BBG0073DLHS1"
|
||||
# вставь сюда твои FIGI из config.yaml
|
||||
# "BBG004730N88", ...
|
||||
]
|
||||
|
||||
load_dotenv()
|
||||
token = os.environ.get("TINKOFF_TOKEN")
|
||||
if not token:
|
||||
print("ERROR: TINKOFF_TOKEN is not set", file=sys.stderr)
|
||||
sys.exit(2)
|
||||
|
||||
with Client(token) as client:
|
||||
# Проверка карточек
|
||||
for f in FIGI:
|
||||
inst = client.instruments.get_instrument_by(
|
||||
id_type=InstrumentIdType.INSTRUMENT_ID_TYPE_FIGI, id=f
|
||||
).instrument
|
||||
if not inst:
|
||||
print(f"{f}: NOT FOUND")
|
||||
continue
|
||||
print(
|
||||
f"{f}: {inst.ticker} | {inst.name} | lot={
|
||||
inst.lot} | curr={inst.currency}"
|
||||
)
|
||||
|
||||
# Проверка last price
|
||||
lp = client.market_data.get_last_prices(figi=FIGI)
|
||||
seen = set()
|
||||
for p in lp.last_prices:
|
||||
price = quotation_to_decimal(p.price)
|
||||
print(f"PRICE {p.figi}: {price}")
|
||||
seen.add(p.figi)
|
||||
missing = [f for f in FIGI if f not in seen]
|
||||
if missing:
|
||||
print("Нет last price для:", missing)
|
||||
@@ -0,0 +1,50 @@
|
||||
from tinkoff.invest import Client
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
load_dotenv()
|
||||
TOKEN = os.getenv("TINKOFF_TOKEN")
|
||||
|
||||
|
||||
with Client(TOKEN) as client:
|
||||
# Все акции
|
||||
shares = client.instruments.shares()
|
||||
for s in shares.instruments:
|
||||
if s.ticker == "TGLD@": # ищем по тикеру
|
||||
print(s.ticker, s.figi, s.name)
|
||||
|
||||
# Все облигации
|
||||
bonds = client.instruments.bonds()
|
||||
for b in bonds.instruments:
|
||||
if "ОФЗ 26212" in b.name: # ищем все ОФЗ по названию
|
||||
print(b.name, b.figi)
|
||||
|
||||
# Все ETF
|
||||
etfs = client.instruments.etfs()
|
||||
for e in etfs.instruments:
|
||||
if "TPAY" in e.ticker:
|
||||
print(e.ticker, e.figi, e.name)
|
||||
|
||||
# Поиск валют по тикеру/названию и вывод figi
|
||||
wanted = ["USD", "EUR", "GBP", "JPY"]
|
||||
currencies = client.instruments.currencies()
|
||||
for cur in currencies.instruments:
|
||||
for w in wanted:
|
||||
if w in (cur.ticker or "") or w in (cur.name or ""):
|
||||
print(cur.ticker, cur.figi, cur.name)
|
||||
break
|
||||
|
||||
# Поиск золота по тикеру (ищем среди валют, фьючерсов, ETF и акций)
|
||||
gold_tickers = ["TGLD@"]
|
||||
lists_to_search = [
|
||||
client.instruments.currencies(),
|
||||
client.instruments.futures(),
|
||||
client.instruments.etfs(),
|
||||
client.instruments.shares(),
|
||||
]
|
||||
for lst in lists_to_search:
|
||||
for inst in lst.instruments:
|
||||
for gt in gold_tickers:
|
||||
if gt in (inst.ticker or "") or gt in (inst.name or ""):
|
||||
print(inst.ticker, inst.figi, inst.name)
|
||||
break
|
||||
Reference in New Issue
Block a user