Ever read a news article that made you want to immediately buy or sell a stock? Getting to your portfolio via a web or mobile interface can be cumbersome and time-consuming, but now you can trade stocks within seconds using your voice assistant and the power of IoT!
- Requirements
- Setting up Alpaca
- Building an API to Run a Lambda Function with AWS
- Using IFTTT to Connect a Voice Assistant with the API
- Usage
- Alpaca account (or any other trading platform with an accessible API)
- AWS account (only the free tier is needed)
- IFTTT account
- Google Assistant (Alexa will also work)
-
Sign up for an Alpaca account.
-
If you'd like to trade with real money, click
Individual Account
and fill in all the required information. To do paper trading, no additional information is necessary. Simply clickGo to Paper Account
on the top-left. I'm using a Paper Trading account, but most of the steps should be similar across both. -
Head to the Overview Dashboard page, and on the right side you'll see a section titled
Your API Keys
. ClickView
to expand the section and thenGenerate New Key
. Copy your API Key ID and Secret Key, as you won't be able to see it again unless you generate a new one. These will be used in the next section when creating the Lambda function.
-
Sign up for an AWS free tier account, and then go to the AWS Lambda console page. You should see a
Create function
button. -
Click
Create function
, and give it a name. Set the Runtime toPython 3.8
, and then clickCreate function
. -
You should now be on the homepage for your newly-created Lambda function. Below the function name in the
Designer
section, clickLayers
, and thenAdd a layer
. Lambda functions don't natively support therequests
library for Python, so we're going to add it here. -
Under
Choose a layer
, clickSpecify an ARN
and then enterarn:aws:lambda:us-east-2:770693421928:layer:Klayers-python38-requests:14
. Replaceus-east-2
with whichever AWS region you are currently using (you can find that by clicking the menu on the top-right corner, next to your name). Finally, clickAdd
. -
Scroll down to the
Function code
section, and in the text editor, replace everything with this code. On line 44, replaceID
andSECRET
with the Alpaca API Key ID and Secret Key you copied when making your Alpaca account, keeping the quotes around them. Also comment out lines 48-50, unless you would like to create some kind of notification system every time you make a trade (won't be discussing that here). Finally, on the top-right of theFunction code
section, clickDeploy
. -
On the top-right of the Lambda function page, next to
Throttle
,Qualifiers
, andActions
, click onTest
. Set the event template tohello-world
, and give the event a name. Replace the JSON body with the text in the image below, and then clickCreate
. Finally, click theTest
button again, and you should see a message that saysExecution result: succeeded
. Now if you go to your Alpaca account and checkPaper Orders
on the sidebar, you should see that an order has been placed for 5 shares of AMZN.
-
Now that you have a working Lambda function that trades stocks, the next step is to make it accessible from outside the AWS console. To do this, navigate to the AWS API Gateway homepage. From here, create a new REST API (NOT the private one).
-
Under
Create new API
, selectNew API
, and give the API a name. Make sure the Endpoint Type is set toRegional
, and then clickCreate API
. -
Click
Actions
, and thenCreate Method
. A new dropdown box should appear underResources
. Click on it and selectPOST
, and then click the checkmark. -
Click the green
POST
button underResources
, and then set the Integration type toLambda Function
. In theLambda Function
textbox, enter the name of the Lambda function you created earlier. Finally, clickSave
andOK
. -
Under the
Actions
menu, clickDeploy API
. Set the Deployment stage to[New Stage]
and give the stage a name, such asprod
. Then clickDeploy
. -
You should now see a blue box with an invoke URL. Congrats! Your Lambda function is now accessible via a REST API. (Clicking the link won't do anything though, since it only has a POST method and requires some input.)
-
Create an IFTTT account, and then create a new applet. First we are going to build an applet to buy stocks, and then one to sell them. Next to
If This
, clickAdd
, and search forGoogle Assistant
. After selecting Google Assistant, select the triggerSay a phrase with both a number and a text ingredient
. -
At this point you will have to connect your Google account to IFTTT, so click
Connect
and then give IFTTT the permissions it needs. (If you want to revoke permissions later, you can do that at any time on the Google Account permissions page.) -
Once connected, you should be on the page titled
Complete trigger fields
. Here, underWhat do you want to say?
andWhat's another way to say it? (optional)
, you can add your ideal trigger phrase, using $ where the stock symbol will be said and # where the desired quantity will be said. I am usingBuy # shares of $
andGet # shares of $
. Finally, forWhat do you want the Assistant to say in response?
, you can put something likeOkay, getting # shares of $
. Set the language to English, and then clickCreate trigger
. -
Next, click the
Add
button next toThen That
, and search for and selectWebhooks
. ClickMake a web request
, and clickConnect
. Now set the URL to your invoke URL that you saw earlier when creating the API on AWS, and set the method toPOST
. Under Content Type, selectapplication/json
, and set the Body to{% raw %}{"action":"buy","symbol":"{{TextField}}","quantity":"{{NumberField}}"}{% endraw %}
. Finally, clickCreate action
, and thenContinue
. Give the applet a name, or leave it as is, and then hitFinish
. -
Repeat steps 1-4 to create another applet, but this time set the trigger phrases to something like
Sell # shares of $
, and set the action Body to{% raw %}{"action":"sell","symbol":"{{TextField}}","quantity":"{{NumberField}}"}{% endraw %}
. Congrats! You're now ready to trade stocks on-the-fly with your voice assistant.
Use the phrases you wrote in your IFTTT triggers to make trades, and make sure to spell out the letters of the stock symbol to ensure that the voice assistant processes your input correctly (e.g. Hey Google, get 5 shares of A-M-Z-N
). The trade should then show up in your Orders
page on Alpaca.