Crio Projects - Discord bot for stocks | Crio.Do | Project-Based Learning Platform for Developers

Objective

You will be creating a Discord bot that provides hourly and daily stock updates and will also support certain query commands.


Project Context

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.


Project Stages

The project consists of the following stages:

image alt text

High-Level Approach

  • 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:

image alt text

  • 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.

image alt text


Pre-requisite skills

  • Python

Post Project Skills

  • Pandas

  • Plot.ly

Objective

You will be creating a Discord bot that provides hourly and daily stock updates and will also support certain query commands.


Project Context

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.


Project Stages

The project consists of the following stages:

image alt text

High-Level Approach

  • 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:

image alt text

  • 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.

image alt text


Pre-requisite skills

  • Python

Post Project Skills

  • Pandas

  • Plot.ly

Proof of Concept

First validate the idea by doing a low level implementation (Proof of Concept) of the components involved in the project.


This helps you to:

  1. Get more clarity around the unknowns.

  2. Get a better understanding of the stages involved in the project.


Requirements

  • 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.


Bring it On!

  • Can you configure the bot with some basic commands, like banning or muting a user, clearing chats and googling stuff?

Expected Outcome

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


Set up EOD stock updates in the form of text

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.


Requirements

  • 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:

    • This data relates to Tesla Inc.’s EOD stock details for 20th November 2020.

image alt text

  • Set up the configuration for the bot to provide this update daily End-Of-Day.

Bring it On!

  • Try to add a command to the bot, which fetches the EOD stock details of the last day when queried. The user should be able to query the details for a specific company.

Expected Outcome

Your bot should be able to post EOD stock data similar to the screenshot below:

image alt text


Provide hourly updates of a company’s trading status

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.


Requirements

  • 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.


Bring it On!

  • Try to add a command to your bot that delivers the aforementioned update whenever you query it. The user should be able to query the details of a specific company.

Expected Outcome

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..


image alt text


Set up daily trade updates

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.


Requirements

  • 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.


Bring it On!

  • Try to add the command to get the update at any time of the day. The command should also feature company_code as a query parameter.

Expected Outcome

Your bot should be able to send in an update as depicted in the image given below.

image alt text


Retrieve historical stock data

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.


Requirements

  • 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.


Bring it On!

  • Can you come up with a command which fetches the history of more than one company and plots the relative information on the same chart? Your command should be able to accept company codes as parameters. [This command will result in easy comparison between two or more companies.]

Expected Outcome

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:

image alt text


Spice it up!

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.


Requirements

  • 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.


Bring it On!

  • Try to add the feature such that all your above commands can accept multiple company codes as inputs and provide the necessary plots in a single chart. This will result in a quick and easy tool for comparing stocks.

Expected Outcome

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.

image alt text


Publish to GitHub

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.]


Deploy the Discord bot on Heroku

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.


Requirements

  • 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.


Expected Outcome

Your bot should be deployed on the Heroku platform so that people can add it to their respective Discord servers and use it.