You will be creating a Discord bot that provides hourly and daily stock updates and will also support certain query commands.
You will be creating a Discord bot that provides hourly and daily stock updates and will also support certain query commands.
Even though Discord was originally created for the Gamer community, it brought a stir into the traditional internet messaging systems. The lifeline of this application is its marvelous bots! There are Discord bots for various purposes. Whenever anyone has an idea of a bot, she/he will simply need to code one and voila! You have an automated bot that can take care of your work, which you otherwise would have to do manually.
Speaking of bots, in this project, we’ll be creating a bot for the Discord platform that provides us information about stocks of various companies in the form of messages in selected discord channels and direct messages. The stock market is a volatile one and one needs to stay updated about the stock rates to step up one’s trading capabilities.
Our bot will support the following functionalities:
Hourly and daily updates of stock information.
Query commands for information such as:
Stock details of company X for last 6 months,
Details of multiple companies plotted on the same graph for easy comparison,
Stock history of multiple companies and more.
The project consists of the following stages:
Fetch the respective CSV files containing the required stock details of a company.
Sort out the required data and plot it using plot.ly. This will provide us with proper visualization of the data. For example:
Set up a Discord bot on your server and add the necessary code for configuring regular updates in specific channels.
Add various commands to your bot, which will enable the users to get trade data, based on specific queries.
Deploy your bot on the Heroku platform.
Pandas
Plot.ly
You will be creating a Discord bot that provides hourly and daily stock updates and will also support certain query commands.
Even though Discord was originally created for the Gamer community, it brought a stir into the traditional internet messaging systems. The lifeline of this application is its marvelous bots! There are Discord bots for various purposes. Whenever anyone has an idea of a bot, she/he will simply need to code one and voila! You have an automated bot that can take care of your work, which you otherwise would have to do manually.
Speaking of bots, in this project, we’ll be creating a bot for the Discord platform that provides us information about stocks of various companies in the form of messages in selected discord channels and direct messages. The stock market is a volatile one and one needs to stay updated about the stock rates to step up one’s trading capabilities.
Our bot will support the following functionalities:
Hourly and daily updates of stock information.
Query commands for information such as:
Stock details of company X for last 6 months,
Details of multiple companies plotted on the same graph for easy comparison,
Stock history of multiple companies and more.
The project consists of the following stages:
Fetch the respective CSV files containing the required stock details of a company.
Sort out the required data and plot it using plot.ly. This will provide us with proper visualization of the data. For example:
Set up a Discord bot on your server and add the necessary code for configuring regular updates in specific channels.
Add various commands to your bot, which will enable the users to get trade data, based on specific queries.
Deploy your bot on the Heroku platform.
Pandas
Plot.ly
First validate the idea by doing a low level implementation (Proof of Concept) of the components involved in the project.
This helps you to:
Get more clarity around the unknowns.
Get a better understanding of the stages involved in the project.
Import the yfinance
module in a Python script and download a CSV file containing stock details of your preferred company. The stock details should be for a period of one day with intervals of 1 minute.
Plot the closing price against the Datetime using Plot.ly and export the plotting as a PNG file.
Create a Discord bot for your server by going to Discord’s developer portal. Enable the bot in your server(Create a server if you don’t have one).
Write a sample Python script for the bot. The script needs to access the bot using it’s token and send the earlier generated image to a specific channel in your server.
For curious readers: Learn how to create beautiful and insightful charts with Python
How to send message to a specific channel in Discord - discordpy
On completion of this milestone you should be able to understand the following:
How to use yfinance
module to download CSV files based on your need
How to use Pandas to import data from a CSV file
How to plot that data using Plot.ly
How to configure a Discord bot that can send a file to a specific channel on a server
The first thing that our bot needs is a way to give us daily conclusive updates of the stock information of a company. To achieve this, you need to set up a mechanism which provides daily End-Of-Day stock updates, w.r.t. a company’s trading data, in the form of text messages.
Fetch the CSV file containing the stock details of a company for a particular date using the yfinance
module. (Set the period
to 1d
and withdraw the interval
parameter; refer to milestone 1.)
Render out the required data to send it as a message to a dedicated channel in your Discord server. A sample image of the data to be rendered is given below:
Your bot should be able to post EOD stock data similar to the screenshot below:
Now that we have basic EOD updates in place, we should make the bot more useful. That can be done by configuring useful hourly updates about stocks. In this milestone, we’ll configure hourly updates of the trading information of a company in the form of a plotted chart.
Come up with a script which does the following on intervals of every hour:
Fetching stock data of a company in CSV format(as depicted in earlier milestones). [Set the period
to 1d
and interval
to 1m
.]
Filter out the data for last hour.
Plot the closing price against the Datetime using Plot.ly using the filtered data and export the chart as a PNG file.
Send the file to a dedicated channel in your Discord server.
You should be able to fetch the data and filter it based on the required needs and plot the chart of the same and then send it to the server. This action needs to be repeated every hour.
A sample plotted chart is given below. The chart relates to the first trading hour of 20th November, 2020 w.r.t. the trading information of Tesla Inc..
We already have daily EOD updates, in the form of text, in place. But we need some human understandable details of the stock with respect to the changes that occur on the current day. To achieve this, set up a script that fetches the trade information of a company such as details of stock parameters for the day and sends an update in a Discord channel, in the form of a plotted chart.
Fetch stock data of a company in CSV format(as depicted in earlier milestones). [Set the period
to 1d
and interval
to 1m
.]
Plot the following two charts:
Closing price against Datetime.
All the parameters(except Volume) against Datetime.
Send the charts as an update in a Discord channel two times a day.
company_code
as a query parameter.Your bot should be able to send in an update as depicted in the image given below.
It is always a good practice to look up a company's history in the stock market before making any investment. On completion of this milestone, you’ll be able to add a feature to your bot which will help you with the same. You should be able to configure your bot, such that it is able to provide the history of a company’s stock information.
Figure out how to obtain the CSV file which contains all the stock information available on a company.
Plot the Closing Price against the Date using Plot.ly and export the chart as a PNG file.
Configure your bot with a command which fetches the aforementioned plotted chart by taking the company code as input.
You should be able to write a command for the bot which will fetch all the available data on a company’s stock, and plot the closing price against the date on a chart using Plot.ly, then export the chart as a PNG file and send it back to the server as the result of the command.
A sample image of Tesla Inc.’s stock history is given below:
Add a few more commands to make your bot more useful.
This is an Optional Milestone for all those who want to go that extra mile.
Add a command which will take the period
of plotted data required and the company code as inputs and send back the required chart having the closing price plotted against the date. Some valid periods are 1d
, 5d
, 1m
, 3m
, 6m
, 1y
.
Add a command which will take the start date, end date, and the company code as inputs and send back the chart with the closing price plotted against the date.
Add a command which will take a year and the company code as the input fields and provide the plotted chart of closing price against the date.
You should be able to write the commands to perform the aforementioned tasks.
As an example, suppose you have a command which gives you the visualization of 2014’s stock information of Apple and Google in the same chart. This should result in a chart as given below.
Publish your project in a GitHub repository and share it with the world!
[Note: Kindly go through this Byte if you’re unfamiliar with Git.]
Now that you have completed working on the Discord bot, it’s time to deploy it on the Heroku platform, so that people can easily make use of it.
Create an account on Heroku, if you don’t have one already.
Create a new application for your bot.
Setup the required files for deployment to the Heroku platform.
Use Heroku-CLI or sync your GitHub repository with the application on the Heroku platform.
Turn the bot online, either using Heroku-Bash or the resources section of the Heroku dashboard.
Your bot should be deployed on the Heroku platform so that people can add it to their respective Discord servers and use it.