You've successfully subscribed to Alpaca Learn - Developer API Trading Tutorials, Code Samples
Great! Next, complete checkout for full access to Alpaca Learn - Developer API Trading Tutorials, Code Samples
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.
Search
Use Cases

More Features with Amazon Alexa and Alpaca

Alpaca Team
Alpaca Team

If you want to add additional features and functionality to your Alexa skill, whether it’s to improve the user experience or more, you’re in the right place.

Adding More Intents

There are many more ways to interact with your Alpaca account than just sending a market order. Below are several intents that you can add to your skill (as well as instructions on how to implement them).

Stop and Limit Orders

You can split up orders into three different kinds of intents: market, stop or limit, and stop limit. The function handlers and the intent page look a bit cleaner if and when you choose to do this. You could also choose to mash all four order types into one intent and call it something like “OrderIntent”.

Adding this functionality will be similar to how you added the market order in the “Alexa Skill for Trading” tutorial. For this intent, create a new slot called price, which you can set to the AMAZON.Number type.

  • Intent name: StopOrLimitOrderIntent
  • Sample Utterance: {type} {side} {quantity} {stock} {time_in_force} at {price}
  • Code: Link to function

Stop-Limit Orders

This order type will need two price inputs (stop and limit prices), which differentiates it from the other two intents. Other than that, implementation is essentially the exact same.

  • Intent name: StopLimitOrderIntent
  • Sample Utterance: Are you sure you would like to submit a stop limit order of {side} , {quantity} , {stock} {time_in_force} at a limit price of {price_one} and a stop price of {price_two} ?
  • Code: Link to function

Listing Orders

  • Intent name: OrdersIntent
  • Slots: none
  • Sample Utterances: list my orders, get my orders
  • Code: Link to function

Listing Positions

  • Intent name: PositionsIntent
  • Slots: none
  • Sample Utterances: list my positions, get my positions
  • Code: Link to function

Getting a Single Position

  • Intent name: PositionIntent
  • Slots: stock
  • Sample Utterances: get my position in {stock}, what’s my position in {stock}
  • Code: Link to function

Clearing Orders and Positions

For this one, add a slot called “position_order” which can be only two things: positions and/or orders. This will indicate which thing you are clearing.

  • Intent name: ClearIntent
  • Slots: position_order
  • Sample Utterance: clear all {position_order}, clear my {position_order}
  • Code: Link to function

Getting Account Info

  • Intent name: AccountIntent
  • Slots: none
  • Sample Utterances: get my account info, get my account
  • Code: Link to function

Cancel Most Recent Order

  • Intent name: CancelOrderIntent
  • Sample Utterances: cancel last order, delete last order
  • Code: Link to function

Get Portfolio Performance

  • Intent name: PerformanceIntent
  • Slots: performanceType
  • Sample Utterances: Get my {performanceType} performance
  • Code: Link to function

Here’s a link to the entire script[link].

Be sure to add all of the new intent handlers to the exports.handler declaration at the bottom of the script.

Enhancing the User Experience

Using any voice recognition service can be very frustrating. Luckily, the Alexa Dev Console allows for some extra features that can ease the friction of the experience.

Adding Addition Sample Utterances

One way you can improve the performance of your skill is to include more ways to invoke an intent, so it’s more likely to trigger when it is supposed to.

Adding Confirmation Phrases

When performing important actions like making an order, you’ll likely want to make sure Alexa has all the proper inputs, especially since orders take many arguments. You can check that Alexa got all the arguments right by adding a confirmation phrase. These will trigger after the intent has been triggered.

To add a confirmation phrase, go into an intent and scroll to the bottom. Under “Intent Confirmation”, click the switch to allow for confirmation phrases and add some phrases of your own. For example, for MarketOrderIntent, you can add the phrase:

Are you sure you would like to submit a market order of {side} , {stock} , {time_in_force} ?

When Alexa prompts you with this, you will have to answer yes or no to confirm or deny the intent from triggering that action.

Slot Filling, Confirmation, Validation

Under each intent, each slot will have its own entry on the left bar. If you click on one, you can add a number of features to confirm slot values.

You can, but do not have to, add slot confirmation or validation, nor implement slot filling. With slot filling, if the user does not include a certain slot, Alexa will prompt the user specifically for this value. This can be useful for functions like MarketOrderIntent, where the user can fill each slot one by one.

To implement this, type a prompt for Alexa to say when Alexa wants the particular slot. For the “side” slot, a prompt could be, “What is the side of this order?” You’ll also need to add sample user utterances which Alexa can expect as responses to this prompt.

I did this for all the slots in MarketOrderIntent, PerformanceIntent, PositionIntent, and ClearIntent. Examples:

  • side — Prompt: “What is the side of this order?”; Utterances: buy, sell
  • stock — Prompt: “What is the stock for this order?”; Utterances: alphabet, apple, aapl, bac, bank of america
  • quantity — Prompt: “How many shares for this order?”; Utterances: one, five, seven, ten, twenty
  • performanceType — Prompt: “Would you like to hear overall performance, individual stock performance, or both?”; Utterances: overall, individual, both
  • position_order — Prompt: “Clear all positions or orders?”; Utterances: positions, orders

After adding these slot filling prompts, you can add some sample utterances to intents that doesn’t include all the slots. For example, you could add the sample utterance to MarketOrderIntent: “submit a market order”. Because you now have these slot filling prompts, Alexa will ask for each slot until all the slots are filled, and trigger the MarketOrderIntentHandler to send the order.

Wrapping Up

This ends the tutorial on how to create an Alexa skill to trade stocks and do some other cool things with your Alpaca account. Stay tuned to any Alpaca social media channel to catch other cool projects you can replicate at home!


Follow @AlpacaHQ on Twitter!

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

Use Cases

Alpaca Team

API-first stock brokerage. *Securities are offered through Alpaca Securities LLC* http://alpaca.markets/#disclosures