Step-by-Step: Building a Bash script for the Top Stock Gainers of the Day

Dan Blevins
4 min readApr 2, 2021
Image from: https://miro.medium.com/max/1080/1*v4o2AXLIJaHSZmqYZk26qA.jpeg

With this project I wanted to learn more about Bash, so I decided to do this small project. If this post was helpful for you, let me know on LinkedIn!

Here are the steps to make a bash executable that hits the GroupMe API and the Financial Modeling Prep (FMP) API and returns a message to your GroupMe group chat.

Step 1: Set up your GroupMe and FMP APIs.

GroupMe:

To set up your GroupMe API (and Bot), first create a new bot (and sign in if you haven’t already). For testing, I created a personal GroupMe group named “Bots” and named my bot “test_bot”, so I recommend you do the same. Additionally, we don’t need a Callback URL for this tutorial and feel free to add a fun image (search for cute chat bots) for your bot. See image below for more info.

Once created, take note of your Bot ID (you’ll use it later). You can find it here.

FMP:

To set up your FMP API, sign up and use the Free tier. Once you sign up, visit your dashboard page and find your API Key because we’ll use it shortly (P.S. make sure to always keep this a secret!). The FMP API offers a ton of stuff in many programming languages, so I recommend to read more on their documentation.

To test that you can hit the FMP API, open your MacOS terminal or Windows Command Line and run:

curl https://financialmodelingprep.com/api/v3/profile/AAPL?apikey=YOUR_API_Key

This will print the key stats of Apple in JSON format. The output should be similar to this:

{ symbol : ‘AAPL’, profile : { price : 275.22, beta : ‘1.139593’, volAvg : ‘36724977’, mktCap : ‘1270897184310.00’, lastDiv : ‘2.92’, range : ‘142–233.47’, changes : 0.33, changesPercentage : ‘(+0.12%)’, companyName : ‘Apple Inc.’, exchange : ‘Nasdaq Global Select’, industry : ‘Computer Hardware’, website : ‘http://www.apple.com’, description : ‘Apple Inc is designs, manufactures and markets..’, ceo : ‘Timothy D. Cook’, sector : ‘Technology’, image : ‘https://financialmodelingprep.com/images-New-jpg/AAPL.jpg' } }

Step 2: Create Bash executable.

For this project, we’re going to learn how to use a for loop, iterate through JSON, append items to a list, and lastly print the finalized list to our GroupMe group chat, all using bash!

Now, let’s code! I personally use VSCode for my text editor but I understand IntelliJ and Atom are good too. Or maybe you’d just like to use a .txt file for now. FYI, for simplicities sake, I’ll be saving my bash file to my Desktop.

Below is the full executable. We’ll also walkthrough it together and go line by line.

Line 1: To run this bash executable you should have this in line one. I believe the technical term for it is a shebang.

Line 3 and 4: This is where you’ll add your FMP API key and Bot ID. Be sure to keep the $ and “ “.

Line 6: This is the start of our final output. As you can see, it provides an announcement of today’s top stock gainers.

Line 8: This is a curl command that’s saved as a variable named gainers. This stores all of the top stock gainers for the day.

Line 11 thru Line 22: This is our for loop and is the meat of the project. On Line 11, we specify that we’ll loop through the first (or top) three stock gainers of the day. Lines 13, 14, 15, and 16 pulls specific information from each of the gainers. Respectively, it pulls the Company Name, the Ticker symbol, the Percentage Change (that also removes characters like (, ), and % to make it look prettier), and Price.

Line 17: Line 17 is unique because it’s another curl command. However, this time it pulls the Stock Exchange Symbol for each specific stock. You’ll see how we use this at the end.

Line 18: Just makes the final output look pretty with a numbered list (1., 2., and 3.)

Line 20 and Line 21: Line 20 creates the one sentence that we’ll sending in the message. Line 21 appends that message to a variable named final. That way we can send all three sentences in one message instead of three separate messages.

Line 23: Prints the final output in our terminal just so we can view it.

Line 26: Use our bot to send the final message in our GroupMe group. Your output will look similar to the image below.

And that’s it! You’ve created a bash script that sends the top three stock gainers to your GroupMe group.

As you can see, we used Line 17 as part of the google.com/finance/… url in case our audience wants more information for each stock.

Step 3: Your Turn to Explore further!

--

--