Adding More Trading Commands - Building a Slackbot (Part 2)

This article is the second installment of a 3-part series, detailing how to add more functionality to the Slackbot I created in the first installment.

Adding More Trading Commands - Building a Slackbot  (Part 2)

This article is the second installment of a 3-part series, detailing how to add more functionality to the Slackbot I created in the first installment. If you haven’t read the first installment, check it out here. It will help you get the environment setup, and allow you to follow along with me throughout this article.

Stock Trading Using a Slackbot - Building a Slackbot #1
Hi, I’m Andrew, an intern at Alpaca from Northwestern! As a college student curious about both tech and financial spaces, I was able to leverage Alpaca’s trading API to build a Slackbot.

In the first installment, I created a simple function handler for making a market order. In addition to the market order, the Alpaca API allows you to make limit, stop, and stop_limit orders. We can extend the functionality of our order function to include these options.

The function is pretty long now, so instead of copying the function to this article, here’s a link to the function on GitHub.

sbp2-orders.py
GitHub Gist: instantly share code, notes, and snippets.

All we are changing here is the sub-function in the order handler. I basically duplicated the original market order, except now it can take extra arguments for a limit and/or stop price, depending on the first argument that the function takes.

The rest of the order function is exactly the same. You may want to update the /order command description to be: “Executes order of specified type, limit/stop price as needed,” and the usage hint to be: “<type> <side> <qty> <symbol> <time_in_force> <(optional) limit_price> <(optional) stop_price>.”

Adding More Commands

Here, I’ll detail the other commands I decided to add to the script. Note you will also have to add these commands to your Slack app for this to work.

Listing Things

This command will list things like positions or open orders in my Alpaca account.

  • Command name: /list
  • Description: Lists things
  • Usage hint: <’positions’/’orders’>
  • Code: Link to function

Clearing Things

I made this command to clear all current positions or orders, in case I wanted wipe any hanging orders/positions.

  • Command name: /clear
  • Description: Clears things
  • Usage hint: <’positions’/’orders’>
  • Code: Link to function

Getting Account Info

This command grabs some basic account info, such as equity and buying power.

  • Command name: /account_info
  • Description: Gets basic account info
  • Usage hint: *no args*
  • Code: Link to function

Getting Prices

This command depends on whether you have a live account with access to Polygon data or not. If you have access, you can get price spreads via Polygon. If you do not have access to Polygon, you can use the Alpaca API to get price data via IEX.

For non-Polygon:

  • Command name: /get_prices
  • Description: Gets the price(s) of the given symbol(s)
  • Usage hint: <[symbol(s)]>

For Polygon:

  • Command name: /get_prices_polygon
  • Description: Gets the price(s) of the given symbol(s)
  • Usage hint: <[symbol(s)]>

For both functions:

In the Polygon handler, you get the bid and ask prices, whereas the IEX price data via the API is less verbose.

Cancelling Orders

I used two commands for cancelling orders: cancel_order and cancel_recent_order.

For cancel_order:

  • Command name: /cancel_order
  • Description: Cancels order by order id
  • Usage hint: <order_id>

For cancel_recent_order:

  • Command name: /cancel_recent_order
  • Description: Cancels most recent order
  • Usage hint: *no_args*

For both commands:

Here’s the link to entire script.

sbp2-all.py
GitHub Gist: instantly share code, notes, and snippets.

Wrapping Up

Once you’ve added all these commands to your Slack app and script, you should be all set to run your Slack app. If you forgot how to correctly set everything up, go back to the first article of this series.

Once you try out the new script, you’ll notice that in addition to the order command, you’ll have all the commands you added to this script. For example, the /account_info command:

Next Steps

This is the second installment of a 3-part series detailing how to create a robust Slack app to connect to your Alpaca account. Follow Alpaca’s social media channels to get an update on the next and final installment, which will show you how to add Websockets streaming capabilities to your Slackbot.


Technology and services are offered by AlpacaDB, Inc. Brokerage services are provided by Alpaca Securities LLC, member FINRA/SIPC. Alpaca Securities LLC is a wholly-owned subsidiary of AlpacaDB, Inc.