-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
466 lines (403 loc) · 20.1 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# bot.py
from asyncio.windows_events import NULL
from dis import disco
import os
import random
from re import A
from discord import emoji,channel
import yfinance as yf
from discord.ext import commands
from discord.ui import Button, View, Select
from dotenv import load_dotenv
import matplotlib.pyplot as plt
from bs4 import BeautifulSoup
import requests
import discord
import plotly.graph_objects as go
import pandas as pd
import requests, json, random, datetime, asyncio
from database import *
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
intents = discord.Intents().all()
bot = commands.Bot(command_prefix='@!',intents=intents)
announce_channel_id = 0
top_stock_companies = ['AAPL', 'GOOGL', 'TSLA', 'MSFT', 'AMZN', 'FB', 'BRK-B', 'SPY',
'BABA', 'JPM', 'WMT', 'V', 'T', 'UNH', 'PFE', 'INTC', 'VZ', 'ORCL','RELIANCE.NS']
stocks = {"RELIANCE" : "Reliance Industries Ltd(L)",
"ADANIPORTS" : "Adani Ports and Special Economic Zone Ltd(L)",
"ITC" : "ITC Ltd(L)",
"TCS" : "TATA Consultancy Services",
"HDFCBANK" : "HDFC Bank Limited",
"INFY" : "Infosys Ltd",
"ICICIBANK" : "ICICI Bank Ltd",
"HINDUNILVR" : "Hindustan Unilever Ltd",
"SBIN" : "State Bank of India" }
if not os.path.exists("images"):
os.mkdir("images")
# async def schedule_daily_message():
# now = datetime.datetime.now()
# # then = now+datetime.timedelta(days=1)
# then = now.replace(hour=1, minute=32,second=59)
# wait_time = (then-now).total_seconds()
# channel = bot.get_channel(979094994608594994)
# await asyncio.sleep(wait_time)
# await channel.send("IT's TIME")
@bot.event
async def on_ready():
while True:
# channel = bot.get_channel(753269986520465571)
# embed=discord.Embed(title=":bell: MARKET DOWN :bell: ", description="Market down at {}".format(123), color=0xFF5733)
# await channel.send(embed=embed)
# break
# selected_company = ['RELIANCE']
# print(selected_company)
# for i in selected_company:
# print(i)
# company = yf.Ticker("{}.NS".format(i))
# company_price = company.info['regularMarketPrice']
# comp_prevclose = company.info['previousClose']
# comp_per = round((((company_price - comp_prevclose) / (comp_prevclose)) * 100),2)
# print(comp_per)
# if(comp_per < 0):
# user_array = see_user(i)
# for y in user_array:
# user = await bot.fetch_user(y)
# await user.send("🛑 {} is running very low".format(i))
# break
now = datetime.datetime.now()
day = now.strftime("%a")
if(day == "Sat"):
next_mon = now + datetime.timedelta(days = 2)
next_mon_time = next_mon.replace(hour=9,minute=0,second=0)
wait_time = next_mon_time - now
print(wait_time.total_seconds())
await asyncio.sleep(wait_time.total_seconds())
if(day == "Sun"):
next_mon = now + datetime.timedelta(days = 1)
next_mon_time = next_mon.replace(hour=9,minute=0,second=0)
wait_time = next_mon_time - now
print(wait_time.total_seconds())
await asyncio.sleep(wait_time.total_seconds())
start = now.replace(hour=9,minute=0,second=0)
end = now.replace(hour=15,minute=30,second=0)
if(now < start):
wait_time = start - now
print(wait_time.total_seconds())
await asyncio.sleep(wait_time.total_seconds())
if(now > end):
next_day = now + datetime.timedelta(days = 1)
next_day_time = next_day.replace(hour=9,minute=0,second=0)
wait_time = next_day_time - now
print(wait_time.total_seconds())
await asyncio.sleep(wait_time.total_seconds())
nifty50 = yf.Ticker("^NSEI")
marketprice = nifty50.info['regularMarketPrice']
marketprevclose = nifty50.info['previousClose']
market_percent = round((((marketprice - marketprevclose) / (marketprevclose)) * 100),2)
if(market_percent < -1):
channel = bot.get_channel(announce_channel_id)
embed=discord.Embed(title=":bell::bell: MARKET DOWN :bell::bell: ", description="NIFTY 50 is at {}%".format(market_percent), color=0xFF5733)
await channel.send(embed=embed)
selected_company = auto_check_company()
for i in selected_company:
company = yf.Ticker("{}.NS".format(i))
company_price = company.info['regularMarketPrice']
comp_prevclose = company.info['previousClose']
comp_per = round((((company_price - comp_prevclose) / (comp_prevclose)) * 100),2)
if(comp_per < -1):
user_array = see_user(i)
for y in user_array:
user = await bot.fetch_user(y)
await user.send("🛑 {} is running very low".format(i))
await asyncio(10,800)
@bot.command(name = "ac-channel")
async def sending(ctx, channel: discord.TextChannel):
global announce_channel_id
announce_channel_id = channel.id
await ctx.send("Announcements will now be sent to "+channel.mention +" channel")
@bot.command(name = "crypto")
async def sending(ctx,crypto):
name = crypto
crypto = crypto + "USDT"
key = "https://api.binance.com/api/v3/ticker/price?symbol="+str(crypto)
data = requests.get(key)
data = data.json()
price = data['price']
await ctx.send(f"{name} is currently at ${price}")
@bot.command(name="get-list", help="Check list of companies for which stock details can be fetched.")
async def get_list(ctx):
list=""
for key, value in stocks.items():
temp='🔹 {} : {}\n'.format(key, value)
list+=temp
embed=discord.Embed(title="List of Top Companies", description=list, color=0x57FF33)
await ctx.send(embed=embed)
@bot.command(name='dailystats', help='Provides daily stats')
async def get_list(ctx):
d=[':one:',':two:',':three:',':four:',':five:',':six:',':seven:',':eight:',':nine:']
html_text=requests.get('https://www.moneycontrol.com/stocks/marketstats/nsegainer/index.php').text
soup= BeautifulSoup(html_text,'lxml')
top_table=soup.find('div', class_='bsr_table hist_tbl_hm')
comp_table=top_table.find('tbody')
comp_rows=comp_table.find('tr')
embedgain=discord.Embed(title='Top Gainers', color=0xFF5733)
embedlose=discord.Embed(title='Top Losers', color=0xFF5733)
for i in range(5):
namee=comp_rows.td.span.h3.a.text
high=comp_rows.next_element.next_element.next_sibling.next_sibling
low=high.next_sibling.next_element
last_price=low.next_sibling.next_element
prev_close=last_price.next_sibling.next_element
change=prev_close.next_sibling.next_element
gain=change.next_sibling.next_element
embedgain.add_field(name='{} {}- `{}`**%**'.format(d[i],namee,gain.text), value="*High*-{}\n*Low*-{} \n*Last price*-{}\n*Prevclose*-{} *Change*-{} ".format(high.text,low.text,last_price.text,prev_close.text,change.text), inline=False)
comp_rows=comp_rows.next_sibling.next_element
html_text2=requests.get('https://www.moneycontrol.com/stocks/marketstats/nseloser/index.php').text
soup2= BeautifulSoup(html_text2,'lxml')
top_table2=soup2.find('div', class_='bsr_table hist_tbl_hm')
comp_table2=top_table2.find('tbody')
comp_rows2=comp_table2.find('tr')
for i in range(5):
namee=comp_rows2.td.span.h3.a.text
high=comp_rows2.next_element.next_element.next_sibling.next_sibling
low=high.next_sibling.next_element
last_price=low.next_sibling.next_element
prev_close=last_price.next_sibling.next_element
change=prev_close.next_sibling.next_element
gain=change.next_sibling.next_element
embedlose.add_field(name='{} {}- `{}`**%**'.format(d[i],namee,gain.text), value="*High*-{}\n*Low*-{} \n*Last price*-{}\n*Prevclose*-{} *Change*-{} ".format(high.text,low.text,last_price.text,prev_close.text,change.text), inline=False)
comp_rows2=comp_rows2.next_sibling.next_element
button1=Button(label="Top Gainers", style=discord.ButtonStyle.green,emoji="🔺")
button2=Button(label="Top Losers", style=discord.ButtonStyle.green,emoji="🔻")
async def button_callback(interaction):
await interaction.response.edit_message(embed=embedgain, view=view)
button1.callback=button_callback
async def button_callback(interaction):
await interaction.response.edit_message(embed=embedlose, view=view)
button2.callback=button_callback
view=View()
view.add_item(button1)
view.add_item(button2)
await ctx.send(embed=embedgain,view=view)
@bot.command(name='stock', help='Enter the name of the company')
async def stock(ctx,stock_name):
stock_name=stock_name+".NS"
color_embed = 0x000000
msft = yf.Ticker(stock_name)
price = msft.info['regularMarketPrice']
prev_close = msft.info['previousClose']
if(price >prev_close):
embed1=discord.Embed(title=msft.info['longName'], url=msft.info['website'], color=0x008000)
color_embed=0x008000
embed1.add_field(name="🔺{}".format(price),value='\u200b', inline=False)
# embed1.add_field('\u200b', '\u200b')
else:
embed1=discord.Embed(title=msft.info['longName'], url=msft.info['website'], color=0xFF0000)
color_embed = 0xFF0000
embed1.add_field(name="🔻{}".format(price),value='\u200b', inline=False)
# embed1.add_field('\u200b', '\u200b',)
embed1.add_field(name="Previous Close", value=prev_close, inline=True)
embed1.add_field(name="Open", value=msft.info['open'], inline=True)
cap=format(msft.info['marketCap']/1000000000000, ".3f")
embed1.add_field(name="Market Cap", value="{}T".format(cap), inline=True)
embed1.add_field(name="Bid", value=msft.info['bid'], inline=True)
embed1.add_field(name="Beta", value=format(msft.info['beta'],".2f"), inline=True)
embed1.add_field(name="PE Ratio", value=format(msft.info['trailingPE'],".2f"), inline=True)
embed1.add_field(name="EPS", value=format(msft.info['trailingEps'],".2f"), inline=True)
embed1.add_field(name="Ask", value=msft.info['ask'], inline=True)
embed1.add_field(name="Day's Range", value="{} - {}".format(msft.info['dayLow'],msft.info['dayHigh']), inline=False)
embed1.add_field(name="52 week Range", value="{} - {}".format(msft.info['fiftyTwoWeekLow'],msft.info['fiftyTwoWeekHigh']), inline=False)
embed1.add_field(name="Forward Dividend & Yield", value="{}({}%)".format(msft.info['dividendRate'],msft.info['dividendYield']*100), inline=False)
embed1.add_field(name="Volume", value="{:,}".format(msft.info['volume']), inline=True)
embed1.add_field(name="Ex-Dividend Date", value=msft.info['exDividendDate'], inline=True)
embed1.add_field(name="Avg. Volume", value="{:,}".format(msft.info['averageVolume']), inline=True)
embed1.add_field(name="1y Target Est", value=msft.info['targetMeanPrice'], inline=True)
embed3=discord.Embed(title=msft.info['longName'], url=msft.info['website'], color=color_embed)
embed3.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar)
embed3.set_thumbnail(url=msft.info['logo_url'])
embed3.add_field(name="Sector", value=msft.info['sector'], inline=True)
embed3.add_field(name="Industry", value=msft.info['industry'], inline=True)
embed3.set_footer(text="Requested by: {}".format(ctx.author.display_name))
embed2=discord.Embed(title=msft.news[0]['title'], url=msft.news[0]['link'], description="Publisher: {}".format(msft.news[0]['publisher']), color=color_embed)
for i in range (2,6):
embed2.add_field(name=msft.news[i]['title'], value="Publisher: {}".format(msft.news[0]['publisher']), inline=False)
button1=Button(label="Analytics", style=discord.ButtonStyle.blurple,emoji="📊")
button2=Button(label="News", style=discord.ButtonStyle.gray,emoji="📰")
button3=Button(label="About", style=discord.ButtonStyle.danger,emoji="👁🗨")
async def button_callback(interaction):
await interaction.response.edit_message(embed=embed1, view=view)
button1.callback=button_callback
async def button_callback(interaction):
await interaction.response.edit_message(embed=embed2, view=view)
button2.callback=button_callback
async def button_callback(interaction):
await interaction.response.edit_message(embed=embed3, view=view)
button3.callback=button_callback
view=View()
view.add_item(button1)
view.add_item(button2)
view.add_item(button3)
await ctx.send(embed=embed1,view=view)
@bot.command(name='chart-year', help='Enter the name of the company')
async def stock_data(ctx, stock_company):
stock = yf.Ticker(stock_company)
data = stock.history(period="1y")
data.to_csv('yahoo.csv')
df = pd.read_csv('yahoo.csv')
candlestick = go.Candlestick(x=df['Date'], open=df['Open'], high=df['High'], low=df['Low'], close=df['Close'])
fig = go.Figure(data=[candlestick])
fig.write_image("images/yourfile.png")
await ctx.send(file=discord.File('images/yourfile.png'))
#os.remove('images/yourfile.png')
# os.remove('yahoo.csv')
@bot.command(name='select')
async def selection(ctx,*,companies):
author = str(ctx.author)
stock =[]
a = companies.split(',')
for i in a:
b = i.strip()
stock.append(b)
if(check_company(stock)):
user_id=str(ctx.author.id)
add_company(author,stock,user_id)
await ctx.send("Added successfully 💥 !!")
else:
await ctx.send("Wrong input ⛔")
@bot.command(name='delete')
async def selection(ctx,*,companies):
author = str(ctx.author)
stock =[]
a = companies.split(',')
for i in a:
b = i.strip()
stock.append(b)
if(check_company(stock)):
delete_company(author,stock)
await ctx.send("Deleted successfully ✂ !!")
@bot.command(name='check')
async def selection(ctx):
author = str(ctx.author)
list=""
user_companies = see_company(author)
for i in user_companies:
company = yf.Ticker("{}.NS".format(i))
company_price = company.info['regularMarketPrice']
comp_prevclose = company.info['previousClose']
comp_per = round((((company_price - comp_prevclose) / (comp_prevclose)) * 100),2)
if(comp_per > 0):
temp="**{}** 🟢\n".format(i)
else:
temp="**{}** 🔴\n".format(i)
list+=temp
embed=discord.Embed(title="{}'s companies".format(ctx.author.display_name),description = list, color=0xC733FF)
await ctx.send(embed=embed)
@bot.command(name='tanmay')
async def tanmay(ctx):
embed=discord.Embed(title="Title", description="For now just description", color=0xE69527)
embed2=discord.Embed(title="EMBED 2", description="THIS IS EMBED 2", color=0x5733FF)
button=Button(label="Click me!", style=discord.ButtonStyle.green, emoji="😊" )
async def button_callback(interaction):
await interaction.response.edit_message(content="You clicked",embed=embed2, view=None)
button.callback=button_callback
view = View()
view.add_item(button)
await ctx.send("Hi!",embed=embed, view=view)
await ctx.author.send("Yo")
@bot.command(name='hmm')
async def tanmay(ctx):
user = await bot.fetch_user(534491042494283776)
print(type(ctx.author.id))
await user.send("Hello there!")
@bot.command(name='chart')
async def tanmay(ctx,company):
company = company +".NS"
select = Select(placeholder="Choose time period", options=[
discord.SelectOption(label="1 month", value="1mo"),
discord.SelectOption(label="100 days",value="100d"),
discord.SelectOption(label="1 year", value="1y"),
],)
async def my_callback(interaction):
t = select.values[0]
stock = yf.Ticker(company)
#title = stock.info['longName']
data = stock.history(period=t)
data.to_csv('yahoo.csv')
df = pd.read_csv('yahoo.csv')
candlestick = go.Candlestick(x=df['Date'], open=df['Open'], high=df['High'], low=df['Low'], close=df['Close'])
fig = go.Figure(data=[candlestick])
fig.update_layout(title_text="{} - {}".format(company,t),title_x=0.5)
fig.write_image("images/yourfile.png")
await interaction.response.send_message(file=discord.File('images/yourfile.png'))
select.callback=my_callback
view = View()
view.add_item(select)
await ctx.send("Choose",view=view)
@bot.command(name='commands')
async def tanmay(ctx):
embed=discord.Embed(title="Command list", description="use *@!* for command without space", color=0x67bb88)
embed.add_field(name="ac-channel [channel-mention]", value="announcement channel selection", inline=False)
embed.add_field(name="crypto [ticker]", value="cryptocurrency price", inline=False)
embed.add_field(name="get-list", value="top companies list with names and ticker", inline=False)
embed.add_field(name="dailystats", value="Top gainers and loser of a day", inline=False)
embed.add_field(name="stock [ticker]", value="company stats, news, about", inline=False)
embed.add_field(name="chart [ticker]", value="candlestick chart with dropdown for timeperiod", inline=False)
embed.add_field(name="select", value="user selection of his/her company", inline=False)
embed.add_field(name="delete", value="deletion of user company", inline=False)
embed.add_field(name="ipo", value="list of upcoming ipos", inline=False)
view = View()
await ctx.send(embed=embed, view=view)
@bot.command(name='ipo')
async def tanmay(ctx):
list=""
ipohtml_text=requests.get('https://zerodha.com/ipo/').text
iposoup= BeautifulSoup(ipohtml_text,'lxml')
iposection=iposoup.find('section', id='ipo')
upcoming_ipo_talbe = iposection.find('tbody')
n=len(upcoming_ipo_talbe.find_all("tr")) # no.of upcoming IPOs
iporow=upcoming_ipo_talbe.find('tr')
for i in range (n):
stock=iporow.td
date=stock.next_sibling.next_element #IPO date
listing_date=date.next_sibling.next_element #listing date
price_range=listing_date.next_sibling.next_element #price range
min_qnt=price_range.next_sibling.next_element #min quantity
rhp_link=min_qnt.next_sibling.next_element.find('a').get('href')
temp=":dollar: **{}** : {}\n".format(stock.text.lstrip().rstrip(), "[RHP]"+"("+rhp_link+")")
list+=temp
iporow=iporow.next_sibling.next_element
ipoembed=discord.Embed(title="Upcoming IPOs", description=list, color=0xFF5733)
view = View()
await ctx.send(embed=ipoembed, view=view)
@bot.command(name="prev-stock-data", help="Check previous day stock data of a company.")
async def stock_data(ctx, stock_company):
if stock_company in top_stock_companies:
stock_company_df = yf.download(stock_company, period="2d")
msg = create_msg(stock_company, stock_company_df)
stock_company_df = yf.download(
stock_company, period="2d", interval="1m")
stock_company_df[0:390].plot(y='Close', linewidth=0.85)
plt.xlabel('Datetime')
plt.ylabel('Close')
plt.title('Stock prices of {company} for previous day'.format(
company=stock_company))
plt.savefig('images/stock_previous_day.png')
await ctx.send(msg, file=discord.File('images/stock_previous_day.png'))
os.remove('images/stock_previous_day.png')
else:
await ctx.send("Stock data for {stockCompany} doesn't exist!".format(stockCompany=stock_company))
def create_msg(top_stock_company, top_stock_company_df):
date = str(top_stock_company_df.head(1).index[0]).split(' ')[0]
msg = '''\
{company} EOF Data
- Date: {Date}
- Open: {Open}
- High: {High}
- Low: {Low}
- Close: {Close}
- Adj Close: {Adj_Close}
- Volume: {Volume}\
'''.format(company=top_stock_company, Date=date, Open=top_stock_company_df.iat[0, 0], High=top_stock_company_df.iat[0, 1], Low=top_stock_company_df.iat[0, 2], Close=top_stock_company_df.iat[0, 3], Adj_Close=top_stock_company_df.iat[0, 4], Volume=top_stock_company_df.iat[0, 5])
return msg
bot.run(TOKEN)