Trading on Alpaca using Twitter

Vixo2
Automation Generation
3 min readDec 23, 2019

Overview

With 330 million monthly active users, Twitter is one of the hottest social media platforms around. Put your tweets to use and start using them to trade (and otherwise interact with the stock market).

Disclaimer: The method depicted below allows any Twitter account to send trades to your Alpaca account. It is recommend that you first create an authentication method to ensure that you are the only one who can tweet orders, otherwise your Alpaca trading API account can be compromised.

This tutorial is for education purposes, in the hopes that you’ll use your new skills to build awesome things!

Get Alpaca API details

Before you can begin building your program, you’ll need to grab your Alpaca API details. To do this, log into your Alpaca account and generate a new API key. You’ll need your endpoint URL, your API key ID, and your secret key ID.

Now on to building the bot!

Get Twitter API details

You’ll need a Twitter Developer account. This requires an application process and can take several days (so please plan accordingly). If you don’t have an account and need to apply, click here to begin. You can identify yourself as a “Hobbyist” and/or “making a bot” when filling out the form. Once you’ve been approved, create a Twitter App and receive your API details.

Watch for mentions of your Twitter bot

With authenticated Alpaca and Twitter accounts all set, you’ll need to have your bot scan for mentions (in order to identify when/if to trade). This will allow you to post a tweet tagging your bot with your order information.

Parse the mentioned Tweet

Congratulations! Your bot will be able to detect when it is mentioned. However, you will still need to analyze, or parse, the Tweet to understand the intention. The following code is how to parse the mentioned Tweet.

Call the Alpaca API with the parsed information

With that information, your bot can now detect a mention and analyze the intended trade. Finally, take the information and send it over to the Alpaca API. Use the following code to trigger Alpaca to make your order.

Have fun building and fusing the social power of Twitter with the trading power of Alpaca! Leave a comment below if you build anything cool 🤗

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

You can find us @AlpacaHQ, if you use twitter.

Follow Automation Generation, a Medium’s publication created for developers/makers in trading and fintech.

--

--