-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
324 lines (300 loc) · 14.4 KB
/
bot.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import argparse
import datetime
import hashlib
import io
import logging
import random
import re
import sys
import toml
import time
from aiogram import Bot, Dispatcher, executor, types
from aiogram.types import InlineQuery, ParseMode, message, \
InputTextMessageContent, InlineQueryResultPhoto
import matplotlib.font_manager
from utils import *
from commands import argparse_kline, argparse_define, argparse_xqimport, argparse_status, argparse_now, argparse_compare
import xueqiu_portfolio
sys.setrecursionlimit(10000) # To support the usage of bs4
_market_emoji = {"SZ": '🇨🇳', "SH": '🇨🇳', "US": '🇺🇸', "NASDAQ": '🇺🇸', "NYSE": '🇺🇸', "AMEX": '🇺🇸', "HK": '🇭🇰'}
# file id for the picture, i.e. placeholder of inline keyboard
_file_id_inline = "AgACAgUAAxkBAAIDymATQjsOZbFGxGqQMKt-Q_MUyUXdAAL1qjEbgr-YVC1IVvhlQFtLfoeybnQAAwEAAwIAA20AA47jAQABHgQ"
config = toml.load("reimu.toml")
# Configure logging
logging.basicConfig(filename="./hakurei_bot.log",
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
# Initialize bot and dispatcher
try:
bot = Bot(token=config["telegram"]["token"], proxy=config["telegram"]["proxy"])
except KeyError:
bot = Bot(token=config["telegram"]["token"])
dp = Dispatcher(bot)
@dp.message_handler(commands=['start'])
async def send_welcome(message):
await message.reply("Busy in stock trading, no time for talks.\n"
"/help if you need, contribute or build your own from https://github.com/wsyxbcl/Reimu")
@dp.message_handler(commands=['help'])
async def send_help(message):
await message.reply("Current functions are: \n"
"/kline, /status, /now /define and /compare, "
"parameters are retrieved using argparse, add -h accordingly for detail.")
@dp.message_handler(commands=['kline'])
async def kline(message, query=None):
logging.info(f'{message.text}')
args = argparse_kline(message.text)
if args.help:
await message.reply(argparse_kline.__doc__)
return 0
try:
stock_list = stock_query(keyword=args.keyword[0], filter_md5=args.md5)
except IndexError:
raise
logging.info(f'query result:{stock_list}')
# Get time_range from user input
if args.days:
time_arg = '-d '+args.days
time_begin, time_end = get_time_range(int(args.days))
macd = (int(args.days) >= 100)
else:
time_arg = None
time_begin, time_end = get_time_range()
macd = True
if len(stock_list) == 1:
stock = stock_list[0]
buf = io.BytesIO()
if type(stock) == Stock_mix:
stock_info = ''
time_mix_created = stock.create_time.strftime("%Y%m%d")
if args.days is None:
time_begin = time_mix_created
macd = False
stock_data, _ = await mix_data_collector_async(stock, time_begin=time_begin, time_end=time_end,
time_ref='oldest')
else:
stock_data, _ = await mix_data_collector_async(stock, time_begin=time_begin, time_end=time_end,
time_ref='created')
plot_kline(stock_data=stock_data, title=f'kline of {stock.code}',
plot_type='line', volume=False, macd=macd, output=buf)
else:
if stock_info := stock.company_info:
stock_info = f"[INFO]({stock_info})"
plot_kline(stock_data=stock.collect_data_daily(time_begin, time_end),
title=f'kline of {stock.code}', plot_type='hollow_candle', macd=macd, output=buf)
buf.seek(0)
kline_caption = (' '.join([stock.code, stock.name, stock_info])).replace('*', '\*') # A-share sucks!
if args.md5:
# Not open to user input, can only result from inline keyboard callback
await query.message.edit_media(types.InputMediaPhoto(media=buf, caption=kline_caption, parse_mode=ParseMode.MARKDOWN))
else:
await message.reply_photo(buf, caption=kline_caption, parse_mode=ParseMode.MARKDOWN)
else:
# get user's selection from inline keyboard
keyboard_markup = types.InlineKeyboardMarkup()
for stock in stock_list:
stock_emoji = _market_emoji[stock.market]
keyboard_markup.row(types.InlineKeyboardButton(' '.join([stock_emoji, stock.code, stock.name]),
callback_data=' '.join(filter(None, ['/kline', time_arg, '-e', stock.md5, args.keyword[0]]))))
# add exit button
keyboard_markup.row(types.InlineKeyboardButton('exit', callback_data='exit'))
await message.reply_photo(_file_id_inline, caption="Found multiple results", reply_markup=keyboard_markup)
@dp.callback_query_handler(lambda cb: '/kline' in cb.data)
@dp.callback_query_handler(text='exit') #TODO need expection on multi-user behavior?
async def inline_kline_answer_callback_handler(query):
logging.info(f'{query.inline_message_id}: {query.data}')
if query.data == 'exit':
await query.message.delete()
return 1
await kline(message.Message(text=query.data), query=query)
@dp.message_handler(commands=['define'])
async def define(message):
try:
args = argparse_define(message.text)
except argparse.ArgumentError:
pass
if args.help:
await message.reply(argparse_define.__doc__)
return 0
logging.info(f'{message.chat.id}: {message.text}')
try:
(code, name) = args.code_and_name
except ValueError:
raise #Wrong command received
stock_list = args.stock_list
if args.weights is None:
holding_ratio = [1 / len(stock_list)] * len(stock_list)
else:
holding_ratio = [float(w) for w in args.weights]
stock_mix = gen_stock_mix(code, name, stock_names=stock_list, holding_ratios=holding_ratio, create_time=datetime.datetime.utcnow())
logging.info(f'creating stock mix:{stock_mix}')
if type(stock_mix) is dict:
candidate_list = stock_mix
await message.reply("Try using code to specify the following stocks:\n"+str(candidate_list).replace('*', '\*'), parse_mode=ParseMode.MARKDOWN)
return 2
buf = io.BytesIO()
stock_mix.draw(output=buf)
buf.seek(0)
await message.reply_photo(buf, caption=stock_mix.code+' '+stock_mix.name+" created")
@dp.message_handler(commands=['xqimport'])
async def xqimport(message):
try:
args = argparse_xqimport(message.text)
except argparse.ArgumentError:
pass
if args.help:
await message.reply(argparse_xqimport.__doc__)
return 0
logging.info(f'{message.chat.id}: {message.text}')
try:
(xqcode, query_code) = args.codes
except ValueError:
raise #Wrong command received
if args.preferred_name is None:
name = xueqiu_portfolio.get_pfdata(xqcode)['name']
else:
name = args.preferred_name
asset_list = xueqiu_portfolio.get_asset_list(xqcode)
stock_list = list(asset_list['Code'])
holding_ratio = [float(weight.strip('%')) for weight in list(asset_list['Weight'])]
stock_mix = gen_stock_mix(query_code, name, stock_names=stock_list, holding_ratios=holding_ratio, create_time=datetime.datetime.utcnow())
buf = io.BytesIO()
stock_mix.draw(output=buf)
buf.seek(0)
await message.reply_photo(buf, caption=stock_mix.code+' '+stock_mix.name+" created")
@dp.message_handler(commands=['status'])
async def status(message):
"""
plot the return rate of given stock mix
"""
logging.info(f'{message.chat.id}: {message.text}')
args = argparse_status(message.text)
if args.help:
await message.reply(argparse_status.__doc__)
return 0
if args.query:
results = [f for f in os.listdir(data_path) if args.stock_mix_code in f]
portfolios = []
for f in results:
with open(os.path.join(data_path, f), 'rb') as fp:
portfolios.append(pickle.load(fp))
await message.reply('\n'.join([portfolio.__repr__() for portfolio in portfolios]))
return 0
stock_list = stock_query(keyword=args.stock_mix_code)
logging.info(f'query result:{stock_list}')
if len(stock_list) == 1 and type(stock_mix := stock_list[0]) is Stock_mix:
buf = io.BytesIO()
if args.ls:
stock_mix.draw(output=buf)
buf.seek(0)
await message.reply_photo(buf, caption=str(stock_mix))
else:
# add buffer time for non-trading days
time_created = stock_mix.create_time.strftime("%Y%m%d")
time_begin = (stock_mix.create_time - datetime.timedelta(days=9)).strftime("%Y%m%d")
time_now = datetime.datetime.utcnow().strftime("%Y%m%d %H:%M:%S")
# stock_data, matrix_close_price = mix_data_collector(stock_mix, time_begin=time_begin)
stock_data, matrix_close_price = await mix_data_collector_async(stock_mix, time_begin=time_begin, time_ref='created')
profit_ratio, stock_profit_ratio = stock_mix.get_profit_ratio(stock_data, matrix_close_price,
date_ref=stock_mix.create_time)
if args.detail:
plot_stock_profit(stock_mix, stock_profit_ratio,
title=f'{stock_mix.name} {time_created}-{time_now} (UTC)',
output=buf)
else:
plot_profitline(stock_data, profit_ratio,
title=f'Return rate of {stock_mix.code}, {time_created}-{time_now} (UTC)',
output=buf)
buf.seek(0)
await message.reply_photo(buf, caption=stock_mix.code+' '+stock_mix.name+\
"\nCurrent return rate: {:.2%}".format(profit_ratio[-1]))
else:
pass
@dp.message_handler(commands=['now'])
async def now(message, query=None):
logging.info(f'{message.text}')
try:
args = argparse_now(message.text)
except argparse.ArgumentError:
pass
if args.help:
await message.reply(argparse_now.__doc__)
return 0
try:
stock_list = stock_query(keyword=args.keyword, filter_md5=args.md5)
except IndexError:
raise
logging.info(f'query result:{stock_list}')
if len(stock_list) == 1:
buf = io.BytesIO()
stock = stock_list[0]
if type(stock_mix := stock_list[0]) is Stock_mix:
try:
time_begin, _ = get_time_range(int(message.text.split()[2]))
except IndexError:
time_begin = stock_mix.create_time.strftime("%Y%m%d")
datetime_ref = (datetime.datetime.utcnow() - datetime.timedelta(days=30)).strftime("%Y%m%d") # refer to last trading day
stock_data, matrix_close_price = await mix_data_collector_async(stock_mix, time_begin=datetime_ref)
time.sleep(random.randint(0,3))
profit_ratio, stock_profit_ratio = stock_mix.get_profit_ratio(stock_data, matrix_close_price,
date_ref='latest')
plot_stock_profit(stock_mix, stock_profit_ratio,
title=f'{stock_mix.name} Latest return rate', #TODO add a timestamp
output=buf)
buf.seek(0)
await message.reply_photo(buf, caption=stock_mix.code+' '+stock_mix.name+\
"\nLatest return rate: {:.2%}".format(profit_ratio[-1]))
else:
if stock_info := stock.company_info:
stock_info = f"[INFO]({stock_info})"
stock_data, stock_preclose = stock.collect_data_live()
latest_price = float(list(stock_data['close'])[-1])
stock_pct = '{:.2%}'.format((latest_price - stock_preclose) / stock_preclose)
plot_kline(stock_data=stock_data, live=True, preclose=stock_preclose,
title=f'Live price of {stock.code} (UTC+8)', plot_type='line', volume=True, macd=False, output=buf)
buf.seek(0)
now_caption = (' '.join([stock.code, stock.name, stock_pct, stock_info])).replace('*', '\*') # A-share sucks!
if args.md5:
# Not open to user input, can only result from inline keyboard callback
await query.message.edit_media(types.InputMediaPhoto(media=buf, caption=now_caption, parse_mode=ParseMode.MARKDOWN))
else:
await message.reply_photo(buf, caption=now_caption, parse_mode=ParseMode.MARKDOWN)
else:
# get user's selection from inline keyboard
keyboard_markup = types.InlineKeyboardMarkup()
for stock in stock_list:
stock_emoji = _market_emoji[stock.market]
keyboard_markup.row(types.InlineKeyboardButton(' '.join([stock_emoji, stock.code, stock.name]),
callback_data=' '.join(filter(None, ['/now', '-e', stock.md5, args.keyword]))))
# add exit button
keyboard_markup.row(types.InlineKeyboardButton('exit', callback_data='exit'))
await message.reply_photo(_file_id_inline, caption="Find multiple results", reply_markup=keyboard_markup)
@dp.callback_query_handler(lambda cb: '/now' in cb.data)
@dp.callback_query_handler(text='exit')
async def inline_now_answer_callback_handler(query):
logging.info(f'{query.inline_message_id}: {query.data}')
if query.data == 'exit':
await query.message.delete()
return 1
await now(message.Message(text=query.data), query=query)
@dp.message_handler(commands=['compare'])
async def compare(message):
try:
args = argparse_compare(message.text)
except argparse.ArgumentError:
pass
if args.help:
await message.reply(argparse_compare.__doc__)
return 0
logging.info(f'{message.chat.id}: {message.text}')
time_begin, _ = get_time_range(int(args.days))
stock_list = [stock_query(keyword)[0] for keyword in args.stocks]
buf = io.BytesIO()
await plot_return_rate_anlys_async(stock_list, date_begin=time_begin, output=buf)
buf.seek(0)
await message.reply_photo(buf, caption='')
# To get photo file_id
# @dp.message_handler(content_types=['photo'])
# async def echo(message):
# await message.answer(str(message.photo[0].file_id))
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True)