Alpaca’s trade API allows me to execute trades using a predetermined strategy. I thought it would be great to share my trading activity through Twitter for a realtime view of all executions. In this brief tutorial I will show you how to:
- Trade with a simple script using Alpaca’s V2 API
- Set up a Twitter developer application
- Link your trading script to your Twitter application
The first step to creating this multi-platform integration was to create a simple script that trades. For this, I wrote a quick program in python that connects with Alpaca’s paper trading so that we can test before live-trading. My code can be seen to the left or can be copied from the gist here . For this test, my script connects to my paper trading account to purchase 1 share of AAPL at market price that is “good ’til canceled.”
After copying the code, make sure to replace the API key variables with your own keys that can be found on the Alpaca Dashboard .
After running the script, you should be able to see your positions and or orders within the paper trading dashboard. Next is setting up the twitter developer application. To accomplish this, navigate here and begin submitting your application. This is a tad tedious, but verification is instantaneous! After being approved for Developer Twitter, make your Twitter application, and allow the application to tweet from your Developer Twitter Account.
After completing that process, navigate to the “Keys and Tokens” tab under your application which can be seen below.
After generating those keys, we can now successfully integrate with the script you built earlier with just a few additional steps. Initialize your Twython object, and use your consumer key, consumer secret key, access token, and secret access token as parameters. Mine looks something like this:
As you can see above, I have added my keys, and parsed my order so I can tweet key information. The update_status function will now tweet with a string that includes the number of shares, the executed price, and the symbol. For a copy of my full script, please look here. Thank you for reading.
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.