Path: blob/master/ invest-robot-contest_trading_bot-master/trading/check_av.py
5932 views
from config.personal_data import get_account, get_account_type1from tinkoff.invest import Client2from config.personal_data import get_token3from datetime import datetime, timedelta4import pytz567def check_money(user_id, price, quantity, currency, account_id="", account_type=""):8if account_id == "":9account_id = get_account(user_id=user_id)1011if account_type == "":12account_type = get_account_type(user_id=user_id)1314with Client(get_token(user_id)) as client:15if account_type == "sandbox":16positions = client.sandbox.get_sandbox_positions(account_id=account_id)17else:18positions = client.operations.get_positions(account_id=account_id)1920for i in positions.money:21if (i.currency == currency) and ((i.units + (i.nano * 1e-9)) > price * quantity * 1.03):22return True2324return False252627def check_time(user_id, figi):28# Получаем расписание всех площадок за неделю29with Client(get_token(user_id)) as client:30schedules = client.instruments.trading_schedules(31from_=datetime.utcnow(),32to=datetime.utcnow() + timedelta(days=6),33).exchanges3435# Получаем инструмент36try:37security = client.instruments.get_instrument_by(id_type=1, id=figi).instrument38except Exception as ex:39print(ex)40error = "Такой бумаги не существует!"41return False, error4243# Получаем время сейчас в часовом поясе UTC44now = datetime.utcnow().replace(tzinfo=pytz.UTC)4546for i in schedules:47if i.exchange == security.exchange: # Если совпадают торговые площадки48for j in i.days:49if j.is_trading_day and j.start_time < now < j.end_time:50return True, ""51elif j.is_trading_day:52start_time = j.start_time - now53error = (54f"Бумаги <b>{security.name}</b>\n"55f"FIGI: {security.figi}\n"56f"Площадка: {security.exchange}\n"57f"До торгов осталось: <b>{start_time}</b>"58)59return False, error6061return False, "else"626364