Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_trading_bot-master/trading/get_securities.py
5933 views
1
from tinkoff.invest import Client
2
from config.personal_data import get_token
3
from trading.trade_help import quotation_to_float
4
import Levenshtein
5
6
"""
7
8
Тут собраны все функции, которые позволяют получить FIGI по тикеру бумаги
9
или тикер бумаги по FIGI
10
11
Тинькофф использует для всех операций покупок/продаж FIGI,
12
при этом для человеческого восприятия удобнее использовать название компании.
13
14
Например. Акции ВТБ:
15
FIGI = "BBG004730ZJ9"
16
Ticker = "VTBR"
17
Name = "Банк ВТБ"
18
19
"""
20
21
'''
22
Функция для получения названия бумаги по FIGI
23
'''
24
25
26
def security_name_by_figi(figi, user_id):
27
with Client(get_token(user_id)) as client:
28
29
try:
30
security_name = client.instruments.get_instrument_by(id_type=1, id=figi).instrument.name
31
return security_name
32
except Exception as ex:
33
print(ex)
34
35
return 0
36
37
38
'''
39
Функция для получения минимального шага цены по FIGI
40
'''
41
42
43
def security_incr_by_figi(figi, user_id):
44
with Client(get_token(user_id)) as client:
45
46
try:
47
security_incr = quotation_to_float(
48
client.instruments.get_instrument_by(id_type=1, id=figi).instrument.min_price_increment)
49
return security_incr
50
except Exception as ex:
51
print(ex)
52
53
return 0
54
55
56
"""
57
Позволяет получить список ценных бумаг по названию или FIGI
58
"""
59
60
61
def security_by_figi(user_id, figi):
62
with Client(get_token(user_id)) as client:
63
try:
64
security = client.instruments.get_instrument_by(id_type=1, id=figi).instrument
65
return security
66
except Exception as ex:
67
print(ex)
68
return security
69
70
71
"""
72
Позволяет получить список ценных бумаг по названию или FIGI
73
"""
74
75
76
def get_security_list(user_id, name, security_type="share"):
77
78
with Client(get_token(user_id)) as client:
79
80
examples = []
81
82
try:
83
examples += [client.instruments.get_instrument_by(id_type=1, id=name).instrument]
84
return examples
85
except:
86
87
if security_type == "share":
88
security = client.instruments.shares()
89
elif security_type == "bond":
90
security = client.instruments.bonds()
91
elif security_type == "etf":
92
security = client.instruments.etfs()
93
elif security_type == "future":
94
security = client.instruments.futures()
95
elif security_type == "currency":
96
security = client.instruments.currencies()
97
98
for i in security.instruments:
99
if Levenshtein.distance(i.name.lower().replace(" ", ""), name.lower().replace(" ", "")) < len(
100
i.name) * 0.3:
101
examples += [i]
102
elif Levenshtein.distance(i.name.lower().replace(" ", ""), name.lower().replace(" ", "")) < len(
103
i.name) * 0.7:
104
if name.lower() in i.name.lower():
105
examples += [i]
106
107
return examples
108
109