1
+ #!/usr/bin/env python
2
+ # coding: utf-8
3
+
4
+ # In[ ]:
5
+
6
+
7
+ #-------------Importing all packages Package---------
8
+ import discord
9
+ from discord .ext import commands
10
+ from pandas_datareader import data as pdr
11
+ import datetime as dt
12
+ import yfinance as yf
13
+ import pandas as pd
14
+ import numpy as np
15
+ import matplotlib .pyplot as plt
16
+ from bs4 import BeautifulSoup
17
+ import bs4
18
+ import ssl
19
+ import urllib .request ,urllib .parse ,urllib .error
20
+ import requests
21
+ get_ipython ().run_line_magic ('matplotlib' , 'inline' )
22
+ import io
23
+ import urllib , base64
24
+ import plotly .graph_objects as go
25
+
26
+ #---------Using it to allow nested event loops---------------
27
+ import nest_asyncio
28
+ nest_asyncio .apply ()
29
+
30
+ #-----activates workaround-------------
31
+ yf .pdr_override ()
32
+
33
+
34
+ # In[ ]:
35
+
36
+
37
+ #------Creating a client or a handle to connect to our bot & a prefix-----
38
+ client = commands .Bot (command_prefix = "." )
39
+
40
+
41
+ #---------------Giving 6 months history---------------------------------
42
+ @client .command (name = 'history' )
43
+ async def history (ctx ,arg ):
44
+ #-----------Today's date--------------
45
+ date = dt .date .today ()
46
+ nowy = date .year
47
+ nowm = date .month
48
+ nowd = date .day
49
+
50
+ #------start date-----------
51
+ syear = nowy + (nowm - 7 )// 12
52
+ smonth = (nowm - 7 )% 12 + 1
53
+ sday = nowd
54
+
55
+ start = dt .datetime (syear ,smonth ,sday )
56
+ now = dt .datetime .now ()
57
+
58
+ df = pdr .get_data_yahoo (arg ,start ,now ,interval = "1mo" )
59
+ df = df .reset_index ()
60
+ df_close = df [['Date' ,'Close' ]]
61
+ df_close .set_index ('Date' ,inplace = True )
62
+ f = plt .figure ()
63
+ plt .plot (df_close ,'b' )
64
+ plt .plot (df_close ,'ro' )
65
+ plt .grid (True )
66
+ plt .title (arg + " " + "Close Price Representation" )
67
+ plt .xlabel ('Trading Months' )
68
+ plt .ylabel ("Close Price" )
69
+
70
+ #-------Setting the bg as white--------------
71
+ rect = f .patch
72
+ rect .set_facecolor ("white" )
73
+ #-----------Generating the html link & setting bg as white--------------
74
+ fig = plt .gcf ()
75
+ buf = io .BytesIO ()
76
+ fig .savefig (buf , format = 'png' ,facecolor = f .get_facecolor ())
77
+ #buf.seek(0)
78
+ #string = base64.b64encode(buf.read())
79
+ #uri = 'data:image/png;base64,' + urllib.parse.quote(string)
80
+ #html='<img src = "%s"/>' % uri
81
+
82
+ #----------Saving the plot------------------
83
+ #plt.plot(df_close)
84
+ plt .savefig ('plot_history_6.png' )
85
+ #plt.show()
86
+
87
+ await ctx .send (file = discord .File ("plot_history_6.png" ))
88
+
89
+
90
+
91
+ #----------------Current OHLC Price of a stock--------------------------
92
+ @client .command (name = 'stock' )
93
+ async def stock (ctx ,arg ):
94
+ #-------Getting channel/server info----------
95
+ guild = ctx .guild
96
+ #---------Getting info of the members--------------
97
+ member = ctx .message .author
98
+
99
+ stck = yf .Ticker (arg )
100
+ curr = stck .history (period = '1d' ,interval = '1m' )
101
+ curr = curr .reset_index ()
102
+ df_curr = curr [['Datetime' ,'Open' ,'High' ,'Low' ,'Close' ]]
103
+ df_curr = df_curr .values .tolist ()
104
+ df_curr = df_curr [- 1 ]
105
+ stk = str (stck )
106
+ embed = discord .Embed (
107
+ title = f"{ arg } OHLC Price" ,
108
+ color = 0xa200ff ,
109
+ timestamp = dt .datetime .utcnow ()
110
+ )
111
+ embed .add_field (name = "Open" ,value = df_curr [1 ],inline = False )
112
+ embed .add_field (name = "High" ,value = df_curr [2 ],inline = False )
113
+ embed .add_field (name = "Low" ,value = df_curr [3 ],inline = False )
114
+ embed .add_field (name = "Close" ,value = df_curr [4 ],inline = False )
115
+ embed .set_author (name = "RS-Updates" ,icon_url = guild .icon_url )
116
+ embed .set_thumbnail (url = member .avatar_url )
117
+ embed .set_footer (text = f"Requested by { member .name } " )
118
+
119
+ await ctx .send (embed = embed )
120
+
121
+ #----------TOP 25 Gainers---------------
122
+ @client .command (name = "gainers" )
123
+ async def gain (ctx ):
124
+ #-------Getting channel/server info----------
125
+ guild = ctx .guild
126
+ #-------------Incase SSL Certificate error occurs-------------
127
+ ct = ssl .create_default_context ()
128
+ ct .check_hostname = False
129
+ ct .verify_made = ssl .CERT_NONE
130
+
131
+ url = ('https://www.moneycontrol.com/stocks/marketstats/nsegainer/index.php' )
132
+ r = requests .get (url )
133
+
134
+
135
+
136
+ soup = BeautifulSoup (r .content ,'html.parser' )
137
+ company = soup .find_all ('a' ,style = "color:#333" )
138
+ high = soup .find_all ('td' ,align = "right" ,width = "75" )
139
+ low = soup .find_all ('td' ,align = "right" ,width = "80" )
140
+ lPrice = soup .find_all ('td' ,align = "right" ,width = "85" )
141
+ PClose = soup .find_all ('td' ,align = "right" ,width = "80" )
142
+
143
+ h = list ()
144
+ l = list ()
145
+ lP = list ()
146
+ PC = list ()
147
+ com = list ()
148
+
149
+ for i in high :
150
+ h .append (i .text )
151
+ for i in low :
152
+ l .append (i .text )
153
+ for i in lPrice :
154
+ lP .append (i .text )
155
+ for i in PClose :
156
+ PC .append (i .text )
157
+ for c in company :
158
+ com .append (c .text )
159
+
160
+ lw = list ()
161
+ for i in range (0 ,len (l ),2 ):
162
+ lw .append (l [i ])
163
+ pc = list ()
164
+ for i in range (1 ,len (PC ),2 ):
165
+ pc .append (PC [i ])
166
+ df = pd .DataFrame (list (zip (com ,h ,lw ,lP ,pc )),columns = ['Company Name' ,'High' ,'Low' ,'Last Price' ,'Previous Price' ])
167
+ embed = discord .Embed (title = "Top 25 Gainers" , color = 0x03f8fc ,timestamp = dt .datetime .utcnow ())
168
+ embed .set_thumbnail (url = guild .icon_url )
169
+ embed .set_footer (text = "Daily Updates" )
170
+ for i in range (len (df )):
171
+ embed .add_field (name = f'**{ df .loc [i , "Company Name" ]} **' ,value = f'> High: { df .loc [i ,"High" ]} \n > Low: { df .loc [i ,"Low" ]} \n > Last Price: { df .loc [i ,"Last Price" ]} ' ,inline = False )
172
+ await ctx .send (embed = embed )
173
+
174
+ #----Connecting to our bot using its token key---------
175
+ client .run ('----------------------------------------------------' )
0 commit comments