-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
132 lines (116 loc) · 4.21 KB
/
main.py
File metadata and controls
132 lines (116 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# coding: utf-8
import requests
import os
from datetime import date
from dotenv import load_dotenv
from telegram import (
InlineKeyboardButton,
InlineKeyboardMarkup,
)
from telegram.ext import (
Updater,
MessageHandler,
Filters,
CommandHandler,
CallbackQueryHandler
)
def ticker_search(update, context) -> list:
"""
This function search ticker, name, price in list everyone tickers
name: here in Cyrillic in application telegram
"""
word = update.message.text
list_tikers = []
url = "https://iss.moex.com/iss/engines/stock/markets/shares/boards/TQBR" \
"/securities.json?iss.meta=off&iss.only=securities&securities" \
".columns=SECID,SECNAME"
all_names = requests.get(url).json().get('securities')['data']
for names in all_names:
if word.upper() in names[1].upper():
list_tikers.append(names)
create_buttom(update, list_tikers)
def create_buttom(update, data):
"""This function created buttom"""
word = update.message.text
keyboard = []
for x in data:
keyboard.append(
[
InlineKeyboardButton(
text=x[1], callback_data=x[0]
)
]
)
reply_markup = InlineKeyboardMarkup(keyboard)
return update.message.reply_text(
f"По слову <b>{word}</b> есть такие эмитенты:", parse_mode='html',
reply_markup=reply_markup
)
def search_ticker_price(tickers):
"""
Получает цены по всем бумагам в заданной секции на дату.
По тикеру ищет его цену закрытия дневной сессии.
Документация на ссылку https://iss.moex.com/iss/reference/825
"""
tikers_name_price = {}
start = 0
stop_while = 0
while stop_while <= 100:
url = (
f'https://iss.moex.com/iss/history/'
f'engines/stock/'
f'markets/shares/'
f'session/TQBR/'
f'boardgroups/57/'
f'securities.json?'
f'iss.meta=off'
f'&data={date.today()}'
f'&security_type_id=3,1'
f'&tradingsession=1'
f'&start={start}'
f'&iss.only=history'
f'&history.columns=SECID,SHORTNAME,LEGALCLOSEPRICE,TRADEDATE'
)
list_data = requests.get(url).json().get('history')['data']
stop_while = len(list_data)
keys = ['SECID', 'SHORTNAME', 'LEGALCLOSEPRICE', 'TRADEDATE']
for name in list_data:
if name[0] == tickers:
tickers = dict(zip(keys, name))
tikers_name_price.update(tickers)
return tikers_name_price
if stop_while < 100:
break
start += 100
def help(update, context):
chat = update.effective_chat
name = update.message.chat.first_name
context.bot.send_message(
chat_id=chat.id,
text=(
f'Привет {name}! \n'
f'бот ищет по названию все совпадения в названиях компаний которые'
f' торгуются на ММВБ в режиме TQBR (T+2) и предоставляет цены '
f'за предыдущую торговую сессию. \n'
f'Введи любое название которое знаешь, например: "Сбер".'
)
)
def push_buttom(update, context):
chat = update.effective_chat
ticker = update.callback_query.data
data = search_ticker_price(ticker)
message = f"На дату {data.get('TRADEDATE')} цена акции " \
f"'{data.get('SHORTNAME')}' =" \
f" {data.get('LEGALCLOSEPRICE')}р."
context.bot.send_message(chat_id=chat.id, text=message)
def main():
load_dotenv()
token = os.getenv('TELEGRAM_TOKEN')
updater = Updater(token=token)
updater.dispatcher.add_handler(CommandHandler('help', help))
updater.dispatcher.add_handler(MessageHandler(Filters.text, ticker_search))
updater.dispatcher.add_handler(CallbackQueryHandler(push_buttom))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()