Path: blob/master/ invest-robot-contest_trading_bot-master/trading/get_securities.py
5933 views
from tinkoff.invest import Client1from config.personal_data import get_token2from trading.trade_help import quotation_to_float3import Levenshtein45"""67Тут собраны все функции, которые позволяют получить FIGI по тикеру бумаги8или тикер бумаги по FIGI910Тинькофф использует для всех операций покупок/продаж FIGI,11при этом для человеческого восприятия удобнее использовать название компании.1213Например. Акции ВТБ:14FIGI = "BBG004730ZJ9"15Ticker = "VTBR"16Name = "Банк ВТБ"1718"""1920'''21Функция для получения названия бумаги по FIGI22'''232425def security_name_by_figi(figi, user_id):26with Client(get_token(user_id)) as client:2728try:29security_name = client.instruments.get_instrument_by(id_type=1, id=figi).instrument.name30return security_name31except Exception as ex:32print(ex)3334return 0353637'''38Функция для получения минимального шага цены по FIGI39'''404142def security_incr_by_figi(figi, user_id):43with Client(get_token(user_id)) as client:4445try:46security_incr = quotation_to_float(47client.instruments.get_instrument_by(id_type=1, id=figi).instrument.min_price_increment)48return security_incr49except Exception as ex:50print(ex)5152return 0535455"""56Позволяет получить список ценных бумаг по названию или FIGI57"""585960def security_by_figi(user_id, figi):61with Client(get_token(user_id)) as client:62try:63security = client.instruments.get_instrument_by(id_type=1, id=figi).instrument64return security65except Exception as ex:66print(ex)67return security686970"""71Позволяет получить список ценных бумаг по названию или FIGI72"""737475def get_security_list(user_id, name, security_type="share"):7677with Client(get_token(user_id)) as client:7879examples = []8081try:82examples += [client.instruments.get_instrument_by(id_type=1, id=name).instrument]83return examples84except:8586if security_type == "share":87security = client.instruments.shares()88elif security_type == "bond":89security = client.instruments.bonds()90elif security_type == "etf":91security = client.instruments.etfs()92elif security_type == "future":93security = client.instruments.futures()94elif security_type == "currency":95security = client.instruments.currencies()9697for i in security.instruments:98if Levenshtein.distance(i.name.lower().replace(" ", ""), name.lower().replace(" ", "")) < len(99i.name) * 0.3:100examples += [i]101elif Levenshtein.distance(i.name.lower().replace(" ", ""), name.lower().replace(" ", "")) < len(102i.name) * 0.7:103if name.lower() in i.name.lower():104examples += [i]105106return examples107108109